• Resolved livemotion

    (@livemotion)


    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!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @livemotion

    It seems like you’re doing everything correctly. However, the issue you’re experiencing might be related to the way AJAX is handled in WooCommerce.

    When you perform a refund, WooCommerce uses AJAX to process the refund and then updates the page accordingly. If the AJAX request doesn’t receive a proper response, the loading icon could continue to spin indefinitely.

    In your case, you’re returning true when the refund is successful. However, WooCommerce might be expecting a more detailed response. Try returning an array with result and message fields, like this:

    return array(
    'result' => 'success',
    'messages' => sprintf(esc_html__('Refunded %s%s'), $amount, $this->currency_symbol)
    );

    This should tell WooCommerce that the refund was successful and provide a message to display, which might stop the loading icon from spinning.

    If this doesn’t resolve your issue, using WooCommerce’s built-in logging system could be helpful. Logging the entire response you get from the SATIM API might provide more insight into what’s going wrong.

    Please note that writing or providing custom code is not within the scope of our support policy. For development and custom coding questions, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question.

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

    Plugin Support Shameem R. a11n

    (@shameemreza)

    HI @livemotion

    We’ve not heard back from you in a while, so I’m marking this thread as resolved. Hopefully, you were able to find a solution to your problem!

    If you have further questions, please feel free to open a new topic.

    Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.