Razorpay is a popular payment gateway in India that supports cards, UPI, net banking, and wallets. Laravel makes Razorpay integration clean and secure using Blade views and controllers. In this tutorial, you’ll learn how to integrate Razorpay with Laravel step by step.
Prerequisites
- A Laravel application
- A Razorpay account
- Razorpay Key ID and Key Secret
Step 1: Install Razorpay PHP SDK
Install the official Razorpay SDK using Composer:
composer require razorpay/razorpay
Step 2: Configure Razorpay Credentials
Add Razorpay keys to your .env file:
RAZORPAY_KEY=your_key_id RAZORPAY_SECRET=your_key_secret
Step 3: Add Razorpay Configuration
Open config/services.php and add the Razorpay configuration:
'razorpay' => [
'key' => env('RAZORPAY_KEY'),
'secret' => env('RAZORPAY_SECRET'),
],
Step 4: Define Routes
Add the following routes in routes/web.php:
use App\Http\Controllers\RazorpayController;
Route::get('/razorpay', [RazorpayController::class, 'index']);
Route::post('/razorpay/payment', [RazorpayController::class, 'payment'])
->name('razorpay.payment');
Step 5: Create Razorpay Controller
Create a RazorpayController to handle order creation and payment flow:
use Razorpay\Api\Api;
use Illuminate\Http\Request;
public function index()
{
return view('razorpay');
}
public function payment(Request $request)
{
$api = new Api(
config('services.razorpay.key'),
config('services.razorpay.secret')
);
$order = $api->order->create([
'receipt' => uniqid(),
'amount' => $request->amount * 100,
'currency' => 'INR'
]);
return view('razorpay-checkout', compact('order'));
}
Step 6: Create Blade File (razorpay.blade.php)
Create the file resources/views/razorpay.blade.php for the payment form:
<form action="{{ route('razorpay.payment') }}" method="POST">
@csrf
<input type="number" name="amount" placeholder="Enter Amount" required>
<button type="submit">Pay with Razorpay</button>
</form>
Step 7: Create Blade File (razorpay-checkout.blade.php)
Create the file resources/views/razorpay-checkout.blade.php to open Razorpay Checkout:
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var options = {
"key": "{{ config('services.razorpay.key') }}",
"amount": "{{ $order['amount'] }}",
"currency": "INR",
"name": "Laravel Razorpay Payment",
"order_id": "{{ $order['id'] }}",
"handler": function (response) {
alert('Payment Successful');
}
};
var rzp1 = new Razorpay(options);
rzp1.open();
</script>
Common Issues
- Invalid Razorpay API keys
- Amount not multiplied by 100
- Incorrect Blade file names
Conclusion
Using Razorpay with Laravel and Blade templates allows you to build a secure and scalable payment system. By separating logic into controllers and views, your integration remains clean, maintainable, and production-ready.
Comments
Post a Comment