Skip to content

Commit

Permalink
Deal with race conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberwombat committed Nov 21, 2017
1 parent 4ea76be commit 7361b6f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 15 deletions.
55 changes: 42 additions & 13 deletions includes/class-wc-gateway-fastspring-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public function ajax_get_receipt() {
if(! $allowed )
wp_send_json_error('Access denied');

$order_id = absint(WC()->session->get('order_awaiting_payment'));
$order_id = absint(WC()->session->get('current_order'));

$order = wc_get_order($order_id);
$data = ['order_id' => $order->get_id()];

// Check for double calls
$order_status = $order->get_status();

// Popup closed with payment
if ($order && $payload->reference) {
Expand All @@ -61,14 +63,15 @@ public function ajax_get_receipt() {
WC()->cart->empty_cart();

$order->set_transaction_id($payload->reference);
$order->update_status('pending', __('Order pending payment approval.', 'woocommerce'));
// We could habe a race condition where FS already called wenhook so lets not assume its pending
if($order_status != 'completed')
$order->update_status('pending', __('Order pending payment approval.', 'woocommerce'));
$data = ["redirect_url" => WC_Gateway_FastSpring_Handler::get_return_url($order), 'order_id' => $order_id];

wp_send_json($data);
} else {
wp_send_json_error('Order not found');
}

wp_send_json_error('Order not found - Order ID was');
}

}

Expand All @@ -79,17 +82,20 @@ public function ajax_get_receipt() {
* @return string Receipt URL
*/
static public function get_return_url($order = null) {

if ($order) {
$return_url = $order->get_checkout_order_received_url();
} else {
$return_url = wc_get_endpoint_url('order-received', '', wc_get_page_permalink('checkout'));
}

if (is_ssl() || get_option('woocommerce_force_ssl_checkout') == 'yes') {
$return_url = str_replace('http:', 'https:', $return_url);
}

return apply_filters('woocommerce_get_return_url', $return_url, $order);
$filtered = apply_filters('woocommerce_get_return_url', $return_url, $order);

return $filtered;
}

/**
Expand Down Expand Up @@ -302,11 +308,34 @@ public function listen_webhook_request() {
* @return WC_Order WooCommerce order
*/
public function find_order_by_fastspring_id($id) {
$orders = $this->search_orders(["search_key" => "_transaction_id", "search_value" => $id]);

if (sizeof($orders) === 1) {

// $orders = $this->search_orders(["search_key" => "_transaction_id", "search_value" => $id]);

// if (sizeof($orders) === 1) {

// $this->log(sprintf('Order found with transaction ID %s', $id));
// return wc_get_order($orders[0]->ID);
// }

// // let's try a fallback
// $orders = wc_get_orders(array(
// 'transaction_id' => $id
// ));

// if (sizeof($orders) > 0) {
// $this->log(sprintf('Order found on second try with transaction ID %s', $id));
// return $orders[0];
// }

// let's try a fallback
$orders = wc_get_orders(array(
'_transaction_id' => $id
));

if (sizeof($orders) > 0) {
$this->log(sprintf('Order found with transaction ID %s', $id));
return wc_get_order($orders[0]->ID);
return $orders[0];
}

$this->log(sprintf('No order found with transaction ID %s', $id));
Expand Down Expand Up @@ -416,9 +445,9 @@ public function search_orders($search_args, $args = array(), $return = "") {
if (isset($args) && !is_array($args)) {
$args = array();
}
if (isset($query_args) && !is_array($query_args)) {
$query_args = array();
}
// if (isset($query_args) && !is_array($query_args)) {
// $query_args = array();
// }
$query_args = array();
if (isset($search_args['search_key']) && ($search_args['search_key'] == "_order_id")) {
if (isset($search_args['search_value']) && !empty($search_args['search_value'])) {
Expand Down
13 changes: 13 additions & 0 deletions includes/class-wc-gateway-fastspring.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,17 @@ public function __construct() {
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
add_action('woocommerce_receipt_' . $this->id, array($this, 'payment_page'));
add_action('woocommerce_api_wc_gateway_fastspring_commerce', array($this, 'return_handler'));
//add_filter( 'woocommerce_payment_complete_order_status', array($this, 'filter_woocommerce_payment_complete_order_status'), 10, 1 );

}

/**
* Mark complete after payment
*/
public function filter_woocommerce_payment_complete_order_status( $order_id ) {
return 'completed';
}

/**
* Check if this gateway is enabled
*/
Expand Down Expand Up @@ -244,6 +253,10 @@ public function get_cart_customer_details() {

$order_id = absint(WC()->session->get('order_awaiting_payment'));

// Set another session var that wont get erased - we need that for receipt
// We sometimes get a race condition
WC()->session->set( 'current_order', $order_id);

$order = wc_get_order($order_id);

return [
Expand Down
4 changes: 2 additions & 2 deletions woocommerce-gateway-fastspring.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public function init() {
load_plugin_textdomain('woocommerce-gateway-fastspring', false, plugin_basename(dirname(__FILE__)) . '/languages');

add_filter('woocommerce_checkout_fields', array($this, 'override_checkout_fields'), 20, 1);
add_filter( 'woocommerce_endpoint_order-pay_title', array($this, 'title_order_pending'), 10, 2);
add_filter('woocommerce_endpoint_order-pay_title', array($this, 'title_order_pending'), 10, 2);
add_filter('woocommerce_payment_gateways', array($this, 'add_gateways'));
add_filter('script_loader_tag', array($this, 'modify_loading_scripts'), 20, 2);
add_filter('plugin_action_links_' . plugin_basename(__FILE__), array($this, 'plugin_action_links'));
Expand Down Expand Up @@ -202,7 +202,7 @@ public function override_checkout_fields($fields) {
* @return string
*/
function title_order_pending($title, $endpoint) {
return"Your order is almost complete";
return __("Your order is almost complete", 'woocommerce-gateway-fastspring');
}

/**
Expand Down

0 comments on commit 7361b6f

Please sign in to comment.