skip to Main Content

I have a custom payment gateway where I need to reload checkout page if payment failed.

Reason:

When Card details submitted the payment gateway generates a card token which I need to process the payment but we can use the card token only once with a request.

What I need:

Currently, I am just showing the error message and a return when payment is failed.

if($payment_status['status']){
    
    $order->update_status( 'on-hold', __( "ABC Payment Donen", 'stackoverflow' ) );
    
    wc_reduce_stock_levels($order_id);

    WC()->cart->empty_cart();

    return array(
        'result'    => 'success',
        'redirect'  => $this->get_return_url( $order )
    );
  
}else{
    
    wc_add_notice( $payment_status['message'] , 'error' );
    return;
}

How can I reload/refresh the page if payment failed? S user can enter the card details again and we can process the payment.
Or any other suggestion?

3

Answers


  1. It depends if you want it sync or async.
    Sync way:
    header("Refresh:0");

    It refreshes your current page. You can specify also the page you want:

    header("Refresh:0; url=another_page.php");

    Async way:
    Generate the content dynamically, clean all changes and tokens.

    Login or Signup to reply.
  2. Fksjii is true.

    However, it might be cleaner to have a specified page that generates token code.

    With an XHR request you can get it, then submit every data with an XHR and wait for a callback.

    However, if you can’t edit JS code you might do

    if ($failed) {
       unset($_REQUEST);
       Header("Refresh: 0");
       exit;
    }
    

    Do not forget to clean $_GET or $_POST data before reloading your page.

    It is always very important to exit even after a redirection sent in the headers. Indeed, headers are sent but code continue to execute and it could create some bugs.

    Login or Signup to reply.
  3. You need return the array for example:

    if ( $payment_status['status'] ) {
        $order->update_status( 'on-hold', __( "ABC Payment Donen", 'stackoverflow' ) );
        wc_reduce_stock_levels($order_id);
        WC()->cart->empty_cart();
        return array(
            'result'    => 'success',
            'redirect'  => $this->get_return_url( $order )
        );
    
    } else {
        $order->update_status( 'failed', __( 'Payment error:', 'woocommerce' ) . $this->get_option( 'error_message' ) );
        wc_add_notice( $payment_status['message'] , 'error' );
        WC()->cart->empty_cart();
        return array(
            'result'   => 'failure',
            'redirect' => WC()->cart->get_checkout_url()
        );
    }
    

    That code will redirect to the checkout page. Hope help you.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search