I’m creating a payment gateway in woocommerce. After sending a request to payment processor server and return Success as a status code. The server will send a GET request to an EndPoint of my own platform with some param indicating that an amount has been deducted from the user and the transaction has been successful .
Based on the (successful param) the user will be redirected to Thank You page.
I managed to create a simple API EndPoint but I’m stuck on how to respond to the Status Code and redirect the user to Thank You Page
add_action( 'rest_api_init', function () {
register_rest_route( 'zaindob/v1', '/reqendpoint/' . 'statuscode=' . '(?P<statuscode>d+)' , array(
'methods' => 'GET',
'callback' => 'respondfun',
) );
} );
function respondfun(){
$order = wc_get_order($order_id);
wc_add_notice('Success = true' , 'Success' );
$order->payment_complete();
$woocommerce->cart->empty_cart();
wp_redirect('https://iotkidsiq.com/thank-you');
}
After responding, the user won’t be redirected. I’m sure my code is not right but i just want to show you what i have created so far
3
Answers
you can add “exit;” after the wp_redirect() function.
it will immediately stop further process for the rest api part.
// API custom endpoints for WP-REST API
// http://localhost/wp-form/wp-json/custom-plugin/add/formdata
Instead of doing redirect with
wp_redirect
and doingexit
you could simply navigate the browser to perform redirect by returning status code302
(or other 3xx code depending on your desired outcome. For more info visit MDN) and specifyLocation
header.So, to do the redirect simply return the following: