javascript - How to get the failure response on clicking Razorpay payment failure button? - Stack Overflow

I am using Razorpay test mode to integrate into my website with the codes that I am using I can console

I am using Razorpay test mode to integrate into my website with the codes that I am using I can console the data when I am clicking on the success button but I want to console the data when there is a failure too because I need to update my status column in the database. So how can I do that below here is what I have tried so far?

$.ajax({
    url:'details-action.php?form-product-test',
    type:'post',
    data:new FormData(this),
    contentType:false,
    processData:false,
    success:function(result_data){
        if(result_data == 'success'){
            var options = {
                "key": "key", 
                "amount": amt1*100, 
                "currency": "INR",
                "name": "Test",
                "description": "Order Details",
                "image": "",
                "handler": function (response){
                    var payment_id = response.razorpay_payment_id;
                    console.log(response);
                    $.ajax({
                        type:'post',
                        url:'form-action.php?paymentsucc',
                        data:{payment_id:payment_id},
                        success:function(result){
                            console.log(result);
                            alert('Your order is successfully done');
                        }
                    });
                }
            };
            var rzp1 = new Razorpay(options);
            rzp1.open();
        } else {
            alert(result_data);
        }
    },
    error:function(result_data){}
});

So when I am getting success everything is working fine and also I am getting the razorpay_payment_id. I want to run another ajax when there is a failure or the customer is clicking on the failure button so that I can update into my database but the problem is I am not getting the razorpay_payment_id or console.log is not showing anything.

I am using Razorpay test mode to integrate into my website with the codes that I am using I can console the data when I am clicking on the success button but I want to console the data when there is a failure too because I need to update my status column in the database. So how can I do that below here is what I have tried so far?

$.ajax({
    url:'details-action.php?form-product-test',
    type:'post',
    data:new FormData(this),
    contentType:false,
    processData:false,
    success:function(result_data){
        if(result_data == 'success'){
            var options = {
                "key": "key", 
                "amount": amt1*100, 
                "currency": "INR",
                "name": "Test",
                "description": "Order Details",
                "image": "",
                "handler": function (response){
                    var payment_id = response.razorpay_payment_id;
                    console.log(response);
                    $.ajax({
                        type:'post',
                        url:'form-action.php?paymentsucc',
                        data:{payment_id:payment_id},
                        success:function(result){
                            console.log(result);
                            alert('Your order is successfully done');
                        }
                    });
                }
            };
            var rzp1 = new Razorpay(options);
            rzp1.open();
        } else {
            alert(result_data);
        }
    },
    error:function(result_data){}
});

So when I am getting success everything is working fine and also I am getting the razorpay_payment_id. I want to run another ajax when there is a failure or the customer is clicking on the failure button so that I can update into my database but the problem is I am not getting the razorpay_payment_id or console.log is not showing anything.

Share Improve this question edited Feb 7, 2021 at 15:55 azibom 1,9541 gold badge11 silver badges24 bronze badges asked Feb 7, 2021 at 13:07 Baby BabaiBaby Babai 1972 silver badges13 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

You can try this i think you are asking for this or you may follow this link to know more https://razorpay./docs/payment-gateway/web-integration/standard/

var rzp1 = new Razorpay(options);
rzp1.on('payment.failed', function (response){
        alert(response.error.code);
        alert(response.error.description);
        alert(response.error.source);
        alert(response.error.step);
        alert(response.error.reason);
        alert(response.error.metadata.order_id);
        alert(response.error.metadata.payment_id);
});
document.getElementById('form-submit').onclick = function(e){
    rzp1.open();
    e.preventDefault();
}

Just change your ajax to sth like this to catch the error

$.ajax({
    type:'post',
    url:'form-action.php?paymentsucc',
    data:{payment_id:payment_id},
    success: function(result) {
    console.log(result);
        alert('Your order is successfully done');
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
        alert("Status: " + textStatus);
        alert("Error: " + errorThrown); 
    } 
});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744994533a4605108.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信