How to store razorpay success response to database

Viewed 724

I am using Razor pay as a payment gatway all the thing is working fine but i want to store the success response to the database which i am getting after every successfull transaction.

    <script>
    var options = {
        "key": "xxxxxxx", // Enter the Key ID generated from the Dashboard
        "amount": "<?php echo $amountsum;?>",
        "currency": "INR",
        "name": "Acme Corp",
        "description": "Test Transaction ",
        "image": "https://example.com/your_logo",
        "order_id": "<?php echo $orderId ?>",
        "handler": function (response){
            alert(" Your Payment ID: "+response.razorpay_payment_id);
            alert(" Your Order ID: "+response.razorpay_order_id);
            alert(" Your Signature ID: "+response.razorpay_signature)
        },
        "prefill": {
            "name": "<?php echo $name?>",
            "email": "<?php echo $useremail?>",
            "contact": "<?php echo $number?>"
        },
        "notes": {
            "address": "Razorpay Corporate Office"
        },
        "theme": {
            "color": "#3399cc"
        }
    };
    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('rzp-button1').onclick = function(e){
        rzp1.open();
        e.preventDefault();
    }
</script>

1 Answers

There are multiple ways to do that, I would suggest two of them :

  1. You will need to write code to save it to the database, in the handler functions.
 "handler": function (response){
         // write some logic to save the response. maybe call and api or 
         // or redirect to other url with data as url params
        },
  1. use redirect flow - When you use redirect flow, Razorpay payment response will be passed as url params . link

  2. Use webhooks. - Create a rest listener on your server, and configure its url as webhook in Razorpay dashboard.- https://razorpay.com/docs/webhooks/

You can choose specific events like order.paid or payment.success

Related