Skip to main content

How to Integrate Razorpay with Laravel

How to Integrate Razorpay with Laravel

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
Tip: Always use Razorpay test keys while developing.

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
Pro Tip: Always verify the Razorpay payment signature on the server before marking payments as successful.

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

Popular posts from this blog

How to Use L5-Swagger for API Documentation in Laravel

Integrating Swagger in Laravel: Annotations, JSON, and YAML What is Swagger? Swagger (OpenAPI) is a powerful tool for generating interactive API documentation. It helps developers understand and test your API easily. ✅ Step-by-Step Guide to Setup L5-Swagger 1. Install L5-Swagger Package composer require "darkaonline/l5-swagger" 2. Publish Config & View Files This command publishes the config file to config/l5-swagger.php : php artisan vendor:publish --provider "L5Swagger\L5SwaggerServiceProvider" 3. Configure Swagger (Optional) Edit the file config/l5-swagger.php to update: API Title Base Path Directories to scan for annotations 4. Add Swagger Annotations Add these before your controller class: /** * @OA\Info( * version="1.0.0", * title="Your API Title", * description=...

How to Send Emails in Node.js using Nodemailer and Ethereal

How to Send Email in Node.js using Nodemailer Email functionality is essential in modern web applications. Whether you're sending confirmation emails, password resets, or notifications, Node.js with Nodemailer makes this simple. In this blog, we'll walk through setting up email sending using Node.js , Express , and Ethereal Email for testing. ๐Ÿงพ Prerequisites Node.js installed Basic knowledge of Express.js Internet connection ๐Ÿ“ Project Structure project-folder/ ├── index.js ├── .env ├── package.json └── app/ └── controller/ └── emailSendController.js ๐Ÿ“ฆ Step 1: Install Dependencies npm init -y npm install express nodemailer dotenv npm install --save-dev nodemon ๐Ÿ” Configure nodemon (Optional but Recommended) Update your package.json with a custom start script: "scripts": { "start": "nodemon index.js" } ๐Ÿ” Step 2: Create a .env File Create a .env file a...

How to Integrate RevenueCat Webhook in Laravel

How to Integrate RevenueCat Webhook in Laravel If you're using RevenueCat to manage in-app subscriptions for your mobile app (iOS, Android, etc.), it’s important to connect it with your backend. Laravel makes it easy to handle incoming webhooks for real-time events like purchases, renewals, cancellations, and more. ๐Ÿ“Œ What is RevenueCat? RevenueCat is a powerful subscription management platform for mobile apps. It simplifies in-app purchases across iOS, Android, and Stripe. With webhooks, it notifies your server in real-time when important events happen, such as a new purchase or a cancellation. ๐Ÿ”— Webhook Events from RevenueCat Common webhook events include: INITIAL_PURCHASE RENEWAL CANCELLATION BILLING_ISSUE EXPIRATION SUBSCRIBER_ALIAS ๐Ÿ”— For a full list of event payloads, visit: RevenueCat Sample Events ✅ Step-by-Step Guide to Integration 1. Create the Route // routes/ap...