How to Set Up Razorpay Payment Gateway in Laravel

Published on January 13, 2025 by Aman K Sahu

Razorpay is a popular payment gateway in India that provides a seamless way to handle online payments. In this tutorial, we’ll walk through the steps to integrate Razorpay in a Laravel application.

1. Install Razorpay Package

The first step is to install the Razorpay PHP SDK using Composer. Open your terminal and run:

composer require razorpay/razorpay
                        

2. Configure Razorpay

After installing the package, configure your Razorpay credentials in the Laravel application.

# Add these to your .env file
RAZORPAY_KEY=your_razorpay_key
RAZORPAY_SECRET=your_razorpay_secret
                        

3. Create Payment Controller

Next, create a controller to handle the payment logic. Run the following artisan command:

php artisan make:controller PaymentController
                        

Add the following code to the `PaymentController` to create a Razorpay order:

use Razorpay\Api\Api;

public function createOrder()
{
    $api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));

    $order = $api->order->create([
        'receipt' => 'order_rcptid_11',
        'amount' => 500 * 100, // Amount in paise
        'currency' => 'INR',
    ]);

    return view('payment', ['order' => $order]);
}
                        

4. Add Payment View

Create a `payment.blade.php` view with the Razorpay checkout form:

5. Handle Payment Success

Create a route to handle payment success:

Route::post('/payment-success', [PaymentController::class, 'handleSuccess']);

public function handleSuccess(Request $request)
{
    // Verify payment signature
    // Update your database for successful payment
    return redirect()->back()->with('success', 'Payment successful!');
}
                        

More Blogs