Integrating Razorpay, a prominent Indian payment gateway, into your Laravel application enables seamless handling of various payment methods, including credit/debit cards, UPI, and net banking. Follow these steps to implement Razorpay in a Laravel 11 project:
1. Install Laravel 11
If you haven't set up a Laravel 11 project yet, create one using Composer:
composer create-project laravel/laravel example-app
2. Create a Razorpay Account
Register for a Razorpay account at Razorpay's official website. After registration, navigate to the API Keys section in your Razorpay dashboard to generate your Key ID
and Key Secret
.
3. Install the Razorpay Package
Add the Razorpay PHP package to your Laravel project via Composer:
composer require razorpay/razorpay
4. Configure Environment Variables
In your .env
file, add the following lines with your Razorpay credentials:
RAZORPAY_KEY=your_key_id
RAZORPAY_SECRET=your_key_secret
5. Define Routes
Set up routes in routes/web.php
to handle the payment form display and payment processing:
use App\Http\Controllers\RazorpayPaymentController;
Route::get('razorpay-payment', [RazorpayPaymentController::class, 'index']);
Route::post('razorpay-payment', [RazorpayPaymentController::class, 'store'])->name('razorpay.payment.store');
6. Create the Controller
Generate a controller named RazorpayPaymentController
:
php artisan make:controller RazorpayPaymentController
In app/Http/Controllers/RazorpayPaymentController.php
, implement the following methods:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Razorpay\Api\Api;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
class RazorpayPaymentController extends Controller
{
/**
* Display the payment form.
*
* @return View
*/
public function index(): View
{
return view('razorpay');
}
/**
* Handle the payment processing.
*
* @param Request $request
* @return RedirectResponse
*/
public function store(Request $request): RedirectResponse
{
$input = $request->all();
$api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));
$payment = $api->payment->fetch($input['razorpay_payment_id']);
if (!empty($input['razorpay_payment_id'])) {
try {
$api->payment->fetch($input['razorpay_payment_id'])->capture(['amount' => $payment['amount']]);
} catch (Exception $e) {
return redirect()->back()->with('error', $e->getMessage());
}
}
return redirect()->back()->with('success', 'Payment successful');
}
}
7. Create the Blade View
In resources/views/razorpay.blade.php
, create the payment form:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel - Razorpay Payment Gateway Integration</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="card mt-5">
<h3 class="card-header p-3">Laravel 11 Razorpay Payment Gateway Integration</h3>
<div class="card-body">
@if(session('error'))
<div class="alert alert-danger" role="alert">
{{ session('error') }}
</div>
@endif
@if(session('success'))
<div class="alert alert-success" role="alert">
{{ session('success') }}
</div>
@endif
<form action="{{ route('razorpay.payment.store') }}" method="POST" class="text-center">
@csrf
<script src="https://checkout.razorpay.com/v1/checkout.js"
data-key="{{ env('RAZORPAY_KEY') }}"
data-amount="1000"
data-buttontext="Pay 10 INR"
data-name="YourAppName"
data-description="Razorpay Payment"
data-image="https://yourapp.com/logo.png"
data-prefill.name="John Doe"
data-prefill.email="john.doe@example.com"
data-theme.color="#ff7529">
</script>
</form>
</div>
</div>
</div>
</body>
</html>
8. Run the Application
Start your Laravel application:
php artisan serve
Visit http://localhost:8000/razorpay-payment
to access the payment form.
By following these steps, you can integrate Razorpay into your Laravel 11 application, enabling secure and efficient payment processing.