8

I am New to Integrate Payment Gateway.

How to Redirect a URL after Success or Failure Payments in Razorpay. I want Js Code. I thing handler function can use that redirect. How to Use them

var options = {
    "key": "rzp_test_aIfnpuWEudWnID",
    "amount": "35000", // 2000 paise = INR 20
    "name": "Vanakkam Chennai",
    "description": "I Run.. Becasue .. I Care",
    "image": "http://vanakkamchennai.com/images/about/chennai_2017.jpg",
    "callback_url": 'https://www.google.com',
    "handler": function (response){

            alert(response.razorpay_payment_id);


    },
    "prefill": {
        "name": "Harshil Mathur",
        "email": "harshil@razorpay.com"
    },
    "notes": {
        "address": "Hello World"
    },
    "theme": {
        "color": "#F37254"
    }
};
Karthik
  • 5,589
  • 18
  • 46
  • 78

1 Answers1

12

The way to redirect a user is to alter the value of location.href. Remove alert(response.razorpay_payment_id); and redirect the user based on if the payment id is defined or not:

// alert(response.razorpay_payment_id);
if (typeof response.razorpay_payment_id == 'undefined' ||  response.razorpay_payment_id < 1) {
  redirect_url = '/you-owe-money.html';
} else {
  redirect_url = '/thnx-you-paid.html';
}
location.href = redirect_url;

My example urls are humorous; replace them with your actual urls. Also, you can read more detailed information on location.href and location redirection here.

Community
  • 1
  • 1
WEBjuju
  • 5,797
  • 4
  • 27
  • 36
  • can we have any action over else part ? – Godwin Dec 05 '18 at 07:50
  • @godwin yes! you can have _any_ action in the `else` block! let your creative juices flow! – WEBjuju Dec 05 '18 at 13:43
  • 1
    but other application parts out of the razorpay option variable is not accessible in the `else` part , it allows only redirection , so i achieved my need only after redirection – Godwin Dec 06 '18 at 05:54
  • i really don't have any suggestions for you; but, if it were me, i would `console.log(response)` and go from there. i am glad that you achieved your need, even if only after redirection. – WEBjuju Dec 07 '18 at 02:12
  • How to do this in PHP laravel please suggest something – Ankita Sep 12 '20 at 09:27
  • @anaya this is client side code...so, php laravel being server side code would mean that this would work for you. perhaps you should open a question with details. – WEBjuju Sep 14 '20 at 14:11
  • if the transaction is failure , the handler is not called..how do i call the handler in case of failure ? – Sathya Narayanan GVK Oct 21 '20 at 11:28
  • @sathyanarayanangvk i think you can open a new question with an appropriate snippet of code; i would also encourage you to reference this SO. imho, _comments_ are a poor place to receive assistance that invariably will be more intricate than your everyday tweet. – WEBjuju Oct 21 '20 at 16:18
  • 1
    understood..will do the same @WEBjuju – Sathya Narayanan GVK Oct 22 '20 at 07:40