Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

process in thank you page, if has param error=501 #29

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions includes/class-woocommerce-payu.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ function __construct()
add_action('woocommerce_api_wc_gateway_' . $this->id, [$this, 'gateway_ipn']);
// Status change hook
add_action('woocommerce_order_status_changed', [$this, 'change_status_action'], 10, 3);
// process in thank you page, if has param error=501 - that means that order not paid
// using woocommerce_thankyou_order_id filter, because this nearest hook before wc_get_order
add_filter('woocommerce_thankyou_order_id', [$this, 'processThankYouForNonPaidOrder']);

$this->init_OpenPayU();
}
Expand Down Expand Up @@ -631,4 +634,28 @@ private function getShippingMethods()

return $options;
}

/**
* process in thank you page, if has param error=501 - that means that order not paid
* in this case set status 'failed' for order
* @param $orderId
* @return int
*/
public function processThankYouForNonPaidOrder($orderId)
{
// using parse_url, because wordpress unset $_GET['error']
$parseUrl = parse_url($_SERVER['REQUEST_URI']);
if (!empty($parseUrl['query'])) {
parse_str($parseUrl['query'], $queryArray);
if (!empty($queryArray['error']) && $queryArray['error'] == 501) {
$order = wc_get_order($orderId);
if ($order->get_status() === 'on-hold' && $order->get_payment_method_title() === 'PayU') {
$order->update_status('failed', __('Payment has been rejected.', 'payu'));
wc_maybe_increase_stock_levels($orderId);
}
}
}

return $orderId;
}
}