5

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 3

4

you can add "exit;" after the wp_redirect() function. it will immediately stop further process for the rest api part.

Sign up to request clarification or add additional context in comments.

Comments

1
You can use below code   


add_action( 'rest_api_init', 'add_form_data_into_custom_database' );

// API custom endpoints for WP-REST API

// http://localhost/wp-form/wp-json/custom-plugin/add/formdata

function add_form_data_into_custom_database() {

register_rest_route('custom-plugin', '/add/formdata', array(
        'methods'  => 'POST',
        'callback' => 'add_formdata',
    )
);


  }

function add_formdata($request) {

$create_id = $_POST['create_id']?$_POST['create_id']:"";
$create_name = $_POST['create_name'] ?$_POST['create_name']:"";
$create_email =$_POST['create_email'] ?$_POST['create_email']:"";

global $wpdb;       
$table_name = $wpdb->prefix . 'userstable';
wp_handle_upload($_FILES["create_file"], array('test_form' => FALSE));

$wpdb->insert(
    $table_name, //table
    array('user_id' => $create_id,'name' => $create_name,'email' => $create_email), //data
    array('%s','%s') //data format          
);

wp_redirect( home_url() );
exit();
}

Comments

1

Instead of doing redirect with wp_redirect and doing exit you could simply navigate the browser to perform redirect by returning status code 302 (or other 3xx code depending on your desired outcome. For more info visit MDN) and specify Location header.

So, to do the redirect simply return the following:

return rest_ensure_response(new WP_REST_Response(
  null,
  302,
  array(
    'Location' => get_home_url() // or set any other URL you wish to redirect to
  )
));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.