Razorpay Payment Gateway
June 3, 2026 3 min read

Razorpay payment gateway integration with PHP

HS
HEMANT SINGH
Technical Writer & Educator

1. Download razorpay PHP SDK.

composer require razorpay/razorpay:2.*

2. Add sdk file in the project.

3. Create an order in the server.

use Razorpay\Api\Api;
$api = new Api("[YOUR_KEY_ID]", "[YOUR_KEY_SECRET]");
$orderData = [
    'receipt'         => 'rcptid_11',
    'amount'          => 39900, // 39900 rupees in paise
    'currency'        => 'INR'
];

$razorpayOrder = $api->order->create($orderData);

4. Code to add Pay Button

Handler function checkout code.

<button id="rzp-button1">Pay</button>
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var options = {
    "key": "YOUR_KEY_ID", // Enter the Key ID generated from the Dashboard
    "amount": "50000", // Amount is in currency subunits. Default currency is INR. Hence, 50000 refers to 50000 paise
    "currency": "INR",
    "name": "Acme Corp",
    "description": "Test Transaction",
    "image": "https://example.com/your_logo",
    "order_id": "order_IluGWxBm9U8zJ8", //This is a sample Order ID. Pass the `id` obtained in the response of Step 1
    "handler": function (response){
    // store this data on the server.
        alert(response.razorpay_payment_id);
        alert(response.razorpay_order_id);
        alert(response.razorpay_signature)
    },
    "prefill": {
        "name": "Gaurav Kumar",
        "email": "gaurav.kumar@example.com",
        "contact": "9000090000"
    },
    "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>

5. Verify the payment signature.

Use the SHA256 algorithm, the razorpay_payment_id and the order_id to construct an HMAC hex digest as shown below:

generated_signature = hmac_sha256(order_id + "|" + razorpay_payment_id, secret);

  if (generated_signature == razorpay_signature) {

    payment is successful

  }

6. Generate signature on the server

$api = new Api($key_id, $secret);
$api->utility->verifyPaymentSignature(
array('razorpay_order_id' => $razorpayOrderId, 
'razorpay_payment_id' => $razorpayPaymentId, 
'razorpay_signature' => $razorpaySignature
));

7. Webhooks

Use webhooks to get notified about events related to the Razorpay payment flow such as orders, payments, settlements, disputes and workflow steps related to other Razorpay Products.

Example.

Suppose you have subscribed to the order.paid webhook event, you will receive a notification every time a user pays you for an order.