Home > Enterprise >  Wordpress / WooCommerce custom payment gateway plugin redirect client and return
Wordpress / WooCommerce custom payment gateway plugin redirect client and return

Time:12-05

I want to make a custom payment gateway plugin for woocommerce with our bank payment gateway API, when the client clicks on the "place order" button on the checkout page, I want the client to be redirected to a custom page on our website where he can continue the procedure to pay, then this custom page should return a response that will be checked and handled if it is successful payment or canceled or error, and then update the order status and stock count.

how to redirect the client to a custom page on my website? and how to get the result (if payment is success or not) from this custom page?

Please can you provide an example or guide? Thank you very much `

<?php 
public function process_payment( $order_id ) {
    $order = wc_get_order( $order_id );
    $order->update_status( 'on-hold', __( 'Awaiting offline payment', 'wc-gateway-test01' ) );
    $valuetopost = [
        'gatename' => $gatename,
        'total' => $order->get_total(),
        'ID' => 'Order ID ' . $order->get_order_number(),
    ];
    $response = wp_remote_post("https://www.mywebsite.com/criptedURL.php", array(
        'method' => 'POST',
        'headers' => array(),
        'body' => http_build_query($valuetopost),
        'timeout' => 90,
    ));
            //the response is the URL that the client should be redirected to
            //$response_body = wp_remote_retrieve_body( $response );
            //wc_add_notice( __('Payment error:', 'woothemes') . $response_body, 'error' );
            //return;
            
            // I want the client to be redirected to a custom page ($response_body) on my website and approve the order
            // this custom page should return a response that I will check and handel       
            
            if ( is_wp_error( $response ) ) {
                $error_message = $response->get_error_message();
                echo "Something went wrong: $error_message";
            } else {
                echo 'Response:<pre>';
                print_r( $response );
                echo '</pre>';
            }
    return array(
        'result'   => 'success',
        'redirect' => $response_body,//$this->get_return_url( $order ),
    );
}

`

I followed some tutorials and I have created the code above, I stuck in client redirection and get return a response if the payment is successful or not.

CodePudding user response:

To redirect the client to a custom page on your website, you can use the wp_redirect function provided by WordPress. This function takes a URL as an argument and redirects the user to that URL.

Here is an example of how you can use this function to redirect the user to your custom page:

wp_redirect( $response_body );
exit;

The exit statement is important because it ensures that no further code is executed after the redirect.

To get the response from your custom page, you can use the wp_remote_post function, which sends a HTTP POST request to the specified URL and returns the response. You can then check the response and handle it accordingly.

Here is an example of how you can use this function to get the response from your custom page:

$response = wp_remote_post( "https://www.mywebsite.com/custom-page.php", array(
    'method' => 'POST',
    'headers' => array(),
    'body' => http_build_query( $valuetopost ),
    'timeout' => 90,
) );

if ( is_wp_error( $response ) ) {
    // there was an error sending the request
    $error_message = $response->get_error_message();
    wc_add_notice( __( 'Payment error:', 'woothemes' ) . $error_message, 'error' );
    return;
} else {
    // the request was successful
    $response_body = wp_remote_retrieve_body( $response );
    if ( $response_body == 'success' ) {
        // the payment was successful
        // update the order status and stock count
    } else {
        // the payment was not successful
        wc_add_notice( __( 'Payment error:', 'woothemes' ) . $response_body, 'error' );
        return;
    }
}

I hope this helps.

  • Related