Nov 17, 2022

Integrate Payment Gateway - RazorPay with Laravel

A guide on integrating RazorPay in Laravel Application

Author

Aanchal GoyalAanchal GoyalSenior Software Engineer - II
Integrate Payment Gateway - RazorPay with Laravel

Introduction

In this tutorial, you will learn how to integrate RazorPay in your Laravel 8 application. Read on to know the essential steps for the integration.

Steps to Integrate RazorPay with Laravel

Let’s get started. Follow the steps below, and you are good to go!

Step 1 - Install the Laravel 8 Application

Go to the directory you want to install your project in the terminal and run the below command to install new Laravel project 

composer create-project laravel/laravel RazorpayIntegrationLaravel

Step 2 - Connect Database to an Application

In this step, open your project folder in vs. code and open .env and add your database name, user, and password like this.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_razorpay
DB_USERNAME=root
DB_PASSWORD=

Step 3 - Add RazorPay Credentials

If you already have Razorpay account, login into the Razorpay dashboard or create a new Razorpay account. Then go to settings from the left sidebar, and you will see the API KEYS tab.

Open that tab and generate a new key. Copy those keys, and paste them into your .env

RAZORPAY_KEY=rzp_test_XXXXXXX
RAZORPAY_SECRET=XXXXXXXXXXXXXX

Step 4 - Install the Composer Package of RazorPay

Now, could you install the composer package of Razorpay? To install that, get the root directory of your project in the terminal and run the below command.

composer requires razorpay/razorpay

Step 5 - Create Route

Open web.php and create a new route. 

Route::get('product',[RazorpayController::class,'index']);
Route::post('razorpay-payment',[RazorpayController::class,'store'])->name('razorpay.payment.store');

Step 6 - Create Migration and Model

Now, you need to create migration for new table payments to store the response of razorpay API. Also, create a model Payment for the same. Run this below command 

php artisan make:model Payment —migration

Copy this code in the migration file and run php artisan migrate

Schema::create('payments',function(Blueprint $table) {
    $table->increments('id');
    $table->string('r_payment_id');
    $table->string('method');
    $table->string('currency');
    $table->string('user_email');
    $table->string('amount');
    $table->longText('json_response');
    $table->timestamps();
  });
<?php 

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; 

class Payment extends Model {
    use HasFactory;
    protected $table = 'payments';
    protected $guarded = ['id'];
}

Step 7 - Create Controller

Now, create a controller using this command and write the below code. In the controller, we will write the logic for Razorpay API call and store the response in our database.

public function store(Request $request) {
    $input = $request->all();
    $api = new Api (env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));
    $payment = $api->payment->fetch($input['razorpay_payment_id']);
    if(count($input) && !empty($input['razorpay_payment_id'])) {
        try {
            $response = $api->payment->fetch($input['razorpay_payment_id'])->capture(array('amount' => $payment['amount']));
            $payment = Payment::create([
                'r_payment_id' => $response['id'],
                'method' => $response['method'],
                'currency' => $response['currency'],
                'user_email' => $response['email'],
                'amount' => $response['amount']/100,
                'json_response' => json_encode((array)$response)
            ]);
        } catch(Exceptio $e) {
            return $e->getMessage();
            Session::put('error',$e->getMessage());
            return redirect()->back();
        }
    }
    Session::put('success',('Payment Successful');
    return redirect()->back();
}

Step 8 - Create View File

Create Laravel blade file and add the below code in that file and call that file with the help of the view function from the controller. 

<div class="card card-default">
    <div class="card-header">
        Laravel - Razorpay Payment Gateway Integration
    </div>
    <div class="card-body text-center">
        <form action="{{ route('razorpay.payment.store') }}" method="POST" >
            @csrf 
            <script src="https://checkout.razorpay.com/v1/checkout.js"
                    data-key="{{ env('RAZORPAY_KEY') }}"
                    data-amount="10000"
                    data-buttontext="Pay 100 INR"
                    data-name="GeekyAnts official"
                    data-description="Razorpay payment"
                    data-image="/images/logo-icon.png"
                    data-prefill.name="ABC"
                    data-prefill.email="abc@gmail.com"
                    data-theme.color="#ff7529">
            </script>
        </form>
    </div>
</div>

Now run this below command and open this URL http://127.0.0.1:8000/product

PHP artisan serve

Conclusion

This was a tutorial on integrating Razorpay into your Laravel 8 application. You can try it out and let us know how it went!

Subscribe to Our Newsletter

RELATED ARTICLES

More from the engineering frontline.

Dive deep into our research and insights on design, development, and the impact of various trends to businesses.
The Bug That Doesn't Show Up in Code Review: Why Your Flutter Web App Reloads on Safari
The Bug That Doesn't Show Up in Code Review: Why Your Flutter Web App Reloads on Safari
A real-world look at how oversized images can trigger Safari reloads and iOS crashes in Flutter apps and how smarter image decoding prevents them.
From Prompting to Process: What Changed When Flutter Shipped Agent Skills
From Prompting to Process: What Changed When Flutter Shipped Agent Skills
This blog explores how Flutter Agent Skills improve AI-assisted development by combining official framework workflows with project-specific guidance for more consistent development.
Why Everything Your AI Builds Looks the Same
Why Everything Your AI Builds Looks the Same
This blog explores why AI-generated interfaces often look alike and explains how design systems, product context, and reusable engineering practices help teams build distinctive, scalable
How We Built the Missing Bridge from Code to Figma
Technology

Jul 10, 2026

How We Built the Missing Bridge from Code to Figma
This blog explores how AI-generated React apps get turned into fully editable, designer-ready Figma files by reading React Fiber instead of the DOM.
Building a Resilient Hybrid-Cloud Network with WireGuard HA, Route-Based Failover, and Deep Observability
Technology

Jun 27, 2026

Building a Resilient Hybrid-Cloud Network with WireGuard HA, Route-Based Failover, and Deep Observability
A practical breakdown of building resilient AWS-to-on-premises connectivity with WireGuard HA, active-standby failover, and deep packet-forwarding observability.
We Built a 114-Second AWS-to-Azure Failover. Here’s What We Learned
Technology

Jun 19, 2026

We Built a 114-Second AWS-to-Azure Failover. Here’s What We Learned
A practical guide to building a 114-second multi-cloud disaster recovery failover between AWS and Azure — what we built, what broke, and what we learned.
Cloud-Native and Cloud-Agnostic Are Not Ideologies; They Are Business-Stage Decisions
Technology

Jun 12, 2026

Cloud-Native and Cloud-Agnostic Are Not Ideologies; They Are Business-Stage Decisions
This blog explains how organizations can balance speed, scalability, and operational flexibility as they grow from startup to enterprise scale.

The Right Conversation Can Save You Six Months.

Whether you’re navigating AI adoption, modernizing legacy systems, or scaling a product - we start by listening. No pitch deck. No template. A real conversation.