Laravel 11 Razorpay Payment Gateway Integration Example

By Hardik Savani April 26, 2024 Category : Laravel

In this tutorial, we will learn how to use the Razorpay payment gateway in laravel 11 application.

Razorpay is an Indian online payment gateway company that offers a range of payment processing solutions for businesses of all sizes. It was founded in 2014 and has quickly become one of the most popular payment gateway providers in India. Razorpay provides businesses with a simple and secure way to accept online payments through various payment channels such as credit cards, debit cards, net banking, UPI, and digital wallets. They also offer easy integration with popular e-commerce platforms such as Shopify, WooCommerce, Magento, and more.

In this example, we will simply install the "razorpay/razorpay" composer package for payment API. Then we will create one form with a "Pay 10 INR" button, and the user will pay by clicking on the button. After payment is successful, the user will see a success message on the page. So, let's follow the below steps:

Preview:

laravel 11 install razorpay

Step for How to use Razorpay Payment Gateway in Laravel 11?

  • Step 1: Install Laravel 11
  • Step 2: Create Razorpay Account
  • Step 3: Install razorpay/razorpay Package
  • Step 4: Create Route
  • Step 5: Create Controller
  • Step 6: Create Blade File
  • Run Laravel App

Step 1: Install Laravel 11

This step is not required; however, if you have not created the Laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Create Razorpay Account

First you need to create account on razorpay. then you can easily get account key id and key secret.

Create Account from here: www.razorpay.com.

After register successfully. you need to go bellow link and get id and secret as bellow screen shot:

Go Here: https://dashboard.razorpay.com/app/keys.

Next you can get account key id and secret and add on .env file as like bellow:

.env

RAZORPAY_KEY=rzp_test_XXXXXXXXX
RAZORPAY_SECRET=XXXXXXXXXXXXXXXX

Step 3: Install razorpay/razorpay Package

In this step, we need to install the Razorpay/Razorpay Composer package to use the Razorpay API. So let's run the below command:

composer require razorpay/razorpay

Step 4: Create Route

Now we will create one route for calling our example, so let's add a new route to the web.php file as below:

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\RazorpayPaymentController;
  
Route::get('razorpay-payment', [RazorpayPaymentController::class, 'index']);
Route::post('razorpay-payment', [RazorpayPaymentController::class, 'store'])->name('razorpay.payment.store');

Step 5: Create Controller

In this step, we will create RazorpayPaymentController and write send SMS logic, so let's add a new route to the web.php file as below:

app/Http/Controllers/RazorpayPaymentController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Razorpay\Api\Api;
use Exception;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
  
class RazorpayPaymentController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(): View
    {        
        return view('razorpay');
    }
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function store(Request $request): RedirectResponse
    {
        $input = $request->all();
  
        $api = new Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));
  
        $payment = $api->payment->fetch($input['razorpay_payment_id']);
  
        if(!empty($input['razorpay_payment_id'])) {

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

        return redirect()->back()
                         ->with('success', 'Payment successful');
    }
}

Step 6: Create Blade File

Now we need to add a Blade file. So let's create a `razorpay.blade.php` file and put the code below:

resources/views/razorpay.blade.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel - Razorpay Payment Gateway Integration</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel="stylesheet" crossorigin="anonymous">
</head>
<body>

<div class="container">

    <div class="card mt-5">
        <h3 class="card-header p-3">Laravel 11 Razorpay Payment Gateway Integration - ItSolutionStuff.com</h3>
        <div class="card-body">

            @session('error')
                <div class="alert alert-danger" role="alert"> 
                    {{ $value }}
                </div>
            @endsession

            @session('success')
                <div class="alert alert-success" role="alert"> 
                    {{ $value }}
                </div>
            @endsession

            <form action="{{ route('razorpay.payment.store') }}" method="POST" class="text-center">
                @csrf
                <script src="https://checkout.razorpay.com/v1/checkout.js"
                        data-key="{{ env('RAZORPAY_KEY') }}"
                        data-amount="1000"
                        data-buttontext="Pay 10 INR"
                        data-name="ItSolutionStuff.com"
                        data-description="Rozerpay"
                        data-image="https://www.itsolutionstuff.com/frontTheme/images/logo.png"
                        data-prefill.name="name"
                        data-prefill.email="email"
                        data-theme.color="#ff7529">
                </script>
            </form>
        </div>
    </div>
</div>
</body>
</html>

Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:

http://localhost:8000/razorpay-payment

Output:

you can get testing card for razorpay from here: Click Here

Now you can run and check.

I hope it can help you...

Shares