Search code examples
javascriptnode.jsreactjspayment-gatewayrazorpay

How to Redirect back to my website after transaction ends on Razorpay . Note: if "redirect=true" is passed as an option


Firstly I am using React and NodeJs for this integration.

I know it's possible to redirect back to the website by using location.href in the handler function.

But what if we are using redirect=true as an options.

It's also mentioned that we can pass the callback_url to the options object. This will make razorpay to redirect to this URL after a successful transaction but this is going to be a POST request. So, we cannot give the URL of our website in here.

example:

const options = {
  key: "razorpay_key",
  currency: data.currency,
  amount: data.amount.toString(),
  order_id: data.id,
  handler: function (response) {
    alert(response.razorpay_payment_id);
    alert(response.razorpay_order_id);
    alert(response.razorpay_signature);
  },
  prefill: {
    name,
    email: "[email protected]",
    contact: "9999999999",
    method: "netbanking",
  },
  redirect: true, // this redirects to the bank page from my website without opening a new window
  callback_url: "not sure what to do with it",
};

Is there any way I can get back to the cart/website page with these parameters in params (razorpay_payment_id, razorpay_order_id, razorpay_signature) after completion of the transaction.

here you can check all the parameters for checkout options: https://razorpay.com/docs/payments/server-integration/nodejs/payment-gateway/build-integration/#checkout-options

Razorpay web checkout docs link: https://razorpay.com/docs/payments/payment-gateway/web-integration/standard/


Solution

  • Eventually, I found a way to do the same. As I am using SSR, I created a POST route on my server and simply redirected from there to my webpage.

    like,

    app.post('/verification', asyncHandler((req, res) => {res.redirect(301, `webpage_url?query_param=${req.body.razorpay_payment_id}`}))
    

    so, checkout options object looks like below

    const options = {
        ... // existing options
        redirect: true,
        callback_url: "server_address/verification",
    };
    

    Hope it helps someone out facing the same issue :)