messageCross Icon
Cross Icon
Web Application Development

How to Integrate Razorpay in Laravel (Without Losing Your Sanity)

How to Integrate Razorpay in Laravel (Without Losing Your Sanity)
How to Integrate Razorpay in Laravel (Without Losing Your Sanity)

Introduction: My First Encounter with Online Payments

I still remember the first time I had to integrate a payment gateway into a Laravel project. The client’s words still ring in my ears: “It should be simple, right? Just add Razorpay.” If only it were that easy!

The reality? I spent hours digging through documentation, dealing with vague error messages, and wondering whether I was building a payment system or accidentally writing my own banking software.

If you’ve ever felt that way, you’re not alone. Online payments can feel intimidating, especially if it’s your first time working with APIs like Razorpay. But here’s the good news: integrating Razorpay in Laravel is actually much simpler than it looks once you know the steps.

This post is my attempt to save you from the hair-pulling frustration I once experienced. By the end, you’ll have a clear roadmap for setting up Razorpay in your Laravel app, complete with tips, insights, and a few personal reflections along the way.

Why Razorpay + Laravel is a Winning Combo

Laravel, with its elegant syntax and developer-friendly ecosystem, has become the go-to framework for PHP developers. Razorpay, on the other hand, has earned its place as one of the most developer-friendly payment
gateways in India, offering smooth APIs, great documentation, and a wide range of payment options (cards, UPI, wallets, you name it).

So when you combine Laravel’s structure with Razorpay’s flexibility, you get a robust and scalable payment solution for any kind of project, from small e-commerce stores to large SaaS platforms.

But enough talk. Let’s roll up our sleeves and dive in.

Insightful Analysis: Demystifying Razorpay in Laravel

Let’s address the elephant in the room first: why does payment integration feel so intimidating?

Part of it is because payments involve real money. If your “Hello World” app crashes, nobody bats an eye. But if your payment gateway messes up, it can mean lost revenue, unhappy customers, or worse, compliance issues.

Another reason? Documentation often assumes a level of prior knowledge. You’ll see terms like webhooks, authentication headers, signature verification, and if you’re new, it sounds like a different language.

But here’s the truth: at its core, Razorpay integration is just about three things:

  1. Creating an order (server-side in Laravel).
  2. Collecting payment (client-side with Razorpay’s checkout).
  3. Verifying payment (server-side again, for security).

That’s it. Three simple steps. Once you understand this flow, the rest is just Laravel code and Razorpay API calls.

I like to think of it as a relay race: Laravel starts the race by creating the order, Razorpay takes the baton during checkout, and Laravel finishes the race by confirming everything went smoothly.

Practical Tips: Step-by-Step Razorpay Integration in Laravel

Let’s get into the nitty-gritty. Here’s a step-by-step guide you can follow:

1. Install Laravel and Set Up Your Project

If you don’t already have a Laravel project, spin one up:

Code

composer create-project laravel/laravel razorpay-integration

2. Create a Razorpay Account

First, you need to create a Razorpay Developer account. 

Create an Account from here: https://razorpay.com

  1. Click on “Log in to Dashboard”. If you don’t have an account with Razorpay, then create a new one.
  2. Navigate to your  "Dashboard".
  3. On the dashboard, find the "API Keys" section. Here, you will find your Key ID and Key Secret.
  4. Next, copy the client ID and secret.

3. Install Razorpay Package

Run the following command to install Razorpay’s official PHP package:

Code

composer require razorpay/razorpay

4. Set Up Razorpay Credentials

Add your Razorpay key and secret in the .env file:

Code

RAZORPAY_KEY=your_key_here
RAZORPAY_SECRET=your_secret_here

5. Create Routes

In routes/web.php:

Code

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\RazorpayPaymentController;

Route::controller('RazorpayPaymentController')->prefix('razorpay')->group(function () {
   Route::get('payment', 'index');
   Route::post('payment', 'store')->name('payment.store');
});
      

6. Create a Controller

Now, let’s create a controller named RazorpayPaymentController:

Code

php artisan make:controller RazorpayPaymentController

In app/Http/Controllers/RazorpayPaymentController.php:

Code

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Razorpay\Api\Api;
use Exception;

class RazorpayPaymentController extends Controller
{
   public function index()
   {
       return view('razorpay');
   }

   public function store(Request $request)
   {
       $input = $request->all();

       $api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));

       try {
           $payment = $api->payment->fetch($input['razorpay_payment_id']);
           $payment->capture(['amount' => $payment['amount']]);
       } catch (Exception $e) {
           return back()->with('error', $e->getMessage());
       }

       return back()->with('success', 'Payment successful!');
   }
}
      
Hire Now!

Hire Laravel Developers Today!

Ready to bring your web design ideas to life? Start your project with Zignuts expert Laravel developers.

**Hire now**Hire Now**Hire Now**Hire now**Hire now

7. Create Blade File

Now, create a new Blade view file resources/views/razorpay.blade.php and add this code:

Code

<!DOCTYPE html>
<html>
<head>
   <title>Laravel Razorpay Integration</title>
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
   <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-5">
   <div class="card">
       <h3 class="card-header">Razorpay Payment Integration</h3>
       <div class="card-body">
           @if(session('error'))
               <div class="alert alert-danger">{{ session('error') }}</div>
           @endif
           @if(session('success'))
               <div class="alert alert-success">{{ session('success') }}</div>
           @endif
           <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="1000"
                       data-buttontext="Pay ₹10"
                       data-name="Laravel Demo"
                       data-description="Test Payment"
                       data-theme.color="#528FF0">
               </script>
           </form>
       </div>
   </div>
</div>
</body>
</html>
      

8. Run Laravel App

Finally, start your Laravel app with:

Code

php artisan serve

Now open your browser and go to:

http://127.0.0.1:8000/razorpay/payment

You should see a payment button. Try it with Razorpay’s test card details.

Before diving into the conclusion, it's worth highlighting that our team at Zignuts Technolab brings deep expertise in full stack web development, combining the latest technologies with agile methodologies to deliver scalable and high-performance web applications. Whether you need custom front-end interfaces, robust back-end systems, or seamless third-party API integrations, our comprehensive full-stack development services ensure your project’s success. Contact us for a Free Consultation

Conclusion: From Frustration to Flow

When I first started with Razorpay, I thought it was going to be a nightmare. But once I broke it down into its core steps order creation, checkout, verification it all started to click.

If you’re feeling overwhelmed, remember: you don’t need to master every payment concept in one go. Just take it step by step. And before long, you’ll be handling online payments like a pro.

After all, isn’t that the beauty of being a developer? We take complex problems, break them into smaller parts, and solve them with elegance.

card user img
Twitter iconLinked icon

A passionate problem solver driven by the quest to build seamless, innovative web experiences that inspire and empower users.

card user img
Twitter iconLinked icon

Passionate developer with expertise in building scalable web applications and solving complex problems. Loves exploring new technologies and sharing coding insights.

Book a FREE Consultation

No strings attached, just valuable insights for your project

Valid number
Please complete the reCAPTCHA verification.
Claim My Spot!
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
download ready
Thank You
Your submission has been received.
We will be in touch and contact you soon!
View All Blogs