javascriptphpjqueryrazorpay

How to get the failure response on clicking Razorpay payment failure button?


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.


Solution

  • You can try this i think you are asking for this or you may follow this link to know more https://razorpay.com/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();
    }