Guides · SaaS
How to integrate Razorpay in Laravel: orders, checkout, verification and webhooks.
Step-by-step Razorpay integration in Laravel: create an order on the server, open Checkout, verify the payment signature.
Key takeaways
- Always create the Razorpay order on your server, and decide the amount there, never in the browser.
- After Checkout, verify the signature on the server before marking anything paid.
- Use webhooks as a backup, because a customer can close the tab before your page confirms.
- Make payment handling idempotent, so a repeated callback cannot credit an order twice.
The flow in four steps
- Your server creates an order with the amount and currency.
- The browser opens Razorpay Checkout using that order id.
- After payment, Checkout returns a payment id and a signature to your page.
- Your server verifies the signature, then marks the order paid.
A webhook from Razorpay to your server runs in parallel and confirms the same event.
Install the SDK and keys
composer require razorpay/razorpay
Put keys in .env and read them through config:
RAZORPAY_KEY_ID=rzp_test_xxxxxxxx
RAZORPAY_KEY_SECRET=xxxxxxxx
The secret must never reach the browser.
Step 1: create an order on the server
use Razorpay\Api\Api;
$api = new Api(config('services.razorpay.key'), config('services.razorpay.secret'));
$order = $api->order->create([
'receipt' => 'booking_' . $booking->id,
'amount' => $booking->amountInPaise(), // 1 rupee = 100 paise
'currency' => 'INR',
]);
$booking->update(['razorpay_order_id' => $order['id']]);
Amounts are in the smallest unit (paise). Compute the amount from your own database, not from a form field.
Step 2: open Checkout
Return the order id and public key id to the page and open Checkout with Razorpay's JavaScript. Pass order_id, key and a handler that posts the three returned values to your server: razorpay_payment_id, razorpay_order_id and razorpay_signature.
Step 3: verify the signature
try {
$api->utility->verifyPaymentSignature([
'razorpay_order_id' => $request->razorpay_order_id,
'razorpay_payment_id' => $request->razorpay_payment_id,
'razorpay_signature' => $request->razorpay_signature,
]);
} catch (\Razorpay\Api\Errors\SignatureVerificationError $e) {
abort(400, 'Invalid payment signature');
}
Only after this passes should you mark the booking paid. Also check that the order id belongs to the booking of the logged-in user.
Step 4: webhooks as the safety net
Customers close tabs and lose network. A webhook such as payment.captured reaches your server regardless. Verify its signature and handle it with the same code path as step 3. The details are in how to verify Razorpay webhook signatures.
Idempotency: do not credit twice
Both the page callback and the webhook may arrive for one payment. Wrap the update in a database transaction and check the status first:
DB::transaction(function () use ($booking, $paymentId) {
$booking = Booking::lockForUpdate()->find($booking->id);
if ($booking->isPaid()) return;
$booking->markPaid($paymentId);
});
Common mistakes
- Trusting the amount sent by the browser.
- Skipping signature verification because "it works in test".
- Saving only the payment id and not the order id.
- Not handling failed or abandoned payments, leaving bookings stuck.
- Mixing test and live keys. See the test mode checklist.
Frequently asked questions
Do I need webhooks if I verify the signature?
Yes, for reliability. The browser callback can be lost.
Can I use Razorpay without Checkout?
Yes. Payment Links let you send a link without building a payment page. See payment links vs Checkout.
What about refunds?
The API supports refunds against a payment id. Record them in your own database too.
Razorpay is part of several of my booking and SaaS builds.
See the tourism platform