WooCommerce Custom Payment Gateway Refund Issue – Page Not Reloading
-
Hello everyone,
I’m developing a custom payment gateway for WooCommerce that integrates with SATIM (payment operator in Algeria). Everything works fine, including processing payments and refunds. However, I encounter an issue with the refund process where the loading icon keeps spinning indefinitely, even though the refund is processed successfully, and I receive the confirmation email, refunded status.
Here are the details of my setup:
The refund is processed correctly, and the status is updated in WooCommerce.
I receive a 200 success response from the SATIM API.
There are no errors in the console or logs.
Code Overview:Here’s the relevant part of my gateway class where I handle the refund:
public function process_refund($order_id, $amount = null, $reason = '') {
$order = wc_get_order($order_id);
if (!$order) {
return false;
}
if ($amount < 50) {
$notice = sprintf(esc_html__('Refund amount is less than 50%s'), $this->currency_symbol);
$this->log($notice);
$order->add_order_note('Refund Failed: ' . $notice);
} else {
$args = [
'body' => [
'userName' => $this->userName,
'password' => $this->password,
'orderId' => $order->get_transaction_id(),
'amount' => $amount * 100
]
];
$response = wp_remote_get(add_query_arg($args['body'], $this->api_refund_url));
$this->log(is_wp_error($response) ? $response : $response['body']);
if (!is_wp_error($response)) {
$response = json_decode($response['body'], true);
$response_code = intval($response['errorCode']);
$refund_message = intval($response['errorMessage']);
if ($response_code == 0) {
$refund_message = sprintf(esc_html__('Refunded %s%s'), $amount, $this->currency_symbol);
$this->log('Success: ' . wp_strip_all_tags($refund_message));
$order->add_order_note( $refund_message );
// Tried various methods to reload the page here
return true;
} else {
$this->log('Refund Failed: ' . $refund_message, 'error');
$order->add_order_note('Refund Failed: ' . $refund_message);
return false;
}
}
}
}What I’ve Tried:
JavaScript Injection:
I attempted to inject JavaScript to reload the page using echo ‘<script type=”text/javascript”>window.location.reload();</script>’;, but it didn’t work.
Redirect with PHP:
Using wp_safe_redirect followed by exit to reload the page, but it seems WooCommerce’s AJAX handling doesn’t allow for PHP redirections directly.
Admin Footer Action:
Added a script in admin_footer to check a flag and reload the page, but this didn’t resolve the issue either.
Question:
How can I properly reload the WooCommerce admin page after a successful refund to stop the loading icon from spinning indefinitely? Has anyone encountered a similar issue or have any suggestions on how to resolve this?
Thank you in advance for your help!
- You must be logged in to reply to this topic.