如何获得点击 Razorpay 支付失败按钮的失败响应?

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

我正在使用 Razorpay 测试模式将我正在使用的代码集成到我的网站中 我可以在单击成功按钮时控制数据,但我也想在失败时控制数据,因为我需要更新数据库中的状态列。那么我该怎么做呢?下面是我到目前为止尝试过的方法?

$.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){}
});

因此,当我获得成功时,一切正常,而且我也获得了 razorpay_payment_id。我想 运行 另一个 ajax 当出现故障或客户单击失败按钮时,以便我可以更新到我的数据库,但问题是我没有得到 razorpay_payment_id或者 console.log 没有显示任何内容。

只需将您的 ajax 更改为这样的东西即可捕获错误

$.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); 
    } 
});

你可以试试这个,我想你是在问这个,或者你可以关注这个 link 了解更多 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();
}