Sechno
Software Development

How to Integrate Razorpay Payment Gateway with Laravel 11

Learn how to connect Razorpay to Laravel 11 so that handling payments is easy and safe. This guide shows you how to set up Razorpay API keys, configure routes and controllers, and make a payment interface that is easy for users to use so that your Laravel application can handle transactions quickly.

SSechno Team 2 min read 353 views
How to Integrate Razorpay Payment Gateway with Laravel 11

Razorpay is a well-known payment gateway in India that lets web and mobile apps securely and easily handle payments. With this guide, you'll be able to connect Razorpay to Laravel 11 easily and safely, allowing your users to make payments without any problems.

Prerequisites

  1. Laravel 11 application setup.
  2. Razorpay account. Sign up here.
  3. Razorpay API keys (you’ll get these from your Razorpay dashboard).

Steps to Integrate Razorpay with Laravel 11

Step 1: Install Razorpay PHP SDK

Install the Razorpay SDK in your Laravel project using Composer. Open your terminal and run:

composer require razorpay/razorpay

Step 2: Configure Environment Variables

Add your Razorpay keys to the .env file. You’ll find these keys in the API Keys section of your Razorpay dashboard.

RAZORPAY_KEY=your_key_id
RAZORPAY_SECRET=your_secret

Step 3: Create Razorpay Controller

Generate a Razorpay controller to handle the payment process.

php artisan make:controller RazorpayController

In RazorpayController, add methods to initiate and confirm payments.

Step 4: Set Up Payment Routes

Define routes to initiate and verify payments in routes/web.php.

Route::get('pay-with-razorpay', [RazorpayController::class, 'initiatePayment'])->name('pay');
Route::post('payment/callback', [RazorpayController::class, 'handlePayment'])->name('payment.callback');

Step 5: Implement Payment Logic in Controller

In RazorpayController, use the Razorpay API to create an order and handle the payment callback.

use Razorpay\Api\Api;
 
public function initiatePayment()
{
    $api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));
 
    $order = $api->order->create([
        'amount' => 1000 * 100, // Amount in paise (₹10)
        'currency' => 'INR',
        'payment_capture' => 1
    ]);
 
    return view('payment.razorpay', ['orderId' => $order['id']]);
}
Payment Callback

Handle the payment callback and verify the payment status.

public function handlePayment(Request $request)
{
    $api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));
 
    $payment = $api->payment->fetch($request->input('razorpay_payment_id'));
    if ($payment->status == 'captured') {
        // Payment was successful
        return redirect()->route('success.page');
    } else {
        // Payment failed
        return redirect()->route('failure.page');
    }
}

Step 6: Create Razorpay Payment View

Create a Blade template resources/views/payment/razorpay.blade.php for the payment UI.

<form action="{{ route('payment.callback') }}" method="POST">
    @csrf
    <script src="https://checkout.razorpay.com/v1/checkout.js"
        data-key="{{ env('RAZORPAY_KEY') }}"
        data-amount="1000"
        data-currency="INR"
        data-order_id="{{ $orderId }}"
        data-buttontext="Pay with Razorpay"
        data-name="Your Company Name"
        data-description="Purchase Description">
    </script>
</form>

Step 7: Test the Payment Integration

Go to the page for payments and start a payment to test the connection. You can use Razorpay’s test mode to simulate different payment statuses.

Conclusion

Integrating Razorpay with Laravel 11 allows your application to handle payments securely. Following the above instructions, you can quickly set up Razorpay and begin taking payments on your Laravel 11 application.

Was this helpful?

Share this post

Comments (0)

Want to join the conversation?

Log in or sign up to leave a comment and share your thoughts.

Log in to Comment