How to Integrate Razorpay Payment Gateway in Laravel

Razorpay payment integration in Laravel

Razorpay payment integration in Laravel

Hello Guyz, In this youtube tutorial, I am showing you how to integrate the Razorpay payment gateway in laravel 9. But first, let me introduce you to Razorpay Payment Gateway, Razorpay is the only payment solution provider in India which allows businesses and individuals to accept, process, and disburse payments. 

Razorpay payment gateway accepts all payment modes with domestic and international credit & debit cards, EMIs ( Credit/Debit Cards & Cardless), PayLater Options Available, Netbanking from 58 banks, and UPI (Unified Payments Interface) and also 8 types of mobile wallets, Razorpay provides the most extensive set of payment methods available in India today.

Razorpay Payment Gateway is very easy as compared to any other payment gateways, The developer docs in very user friendly and easy to understand. so here i am demonstrating the Razorpay Payment Gateway integration in Laravel 9 here.

So, let’s see Razorpay payment gateway integration in laravel 9 and laravel 8 and laravel 7, how to integrate Razorpay payment gateway in laravel 9, Razorpay payment gateway integration in laravel 9

Step 1 : Create Razorpay Account in Razorpay Payment Gateway Website
Step 2 : Install Razorpay Package in Laravel 9
Step 3 : Add API Key and Secret Key 
Step 4 : Create Route
Step 5 : Create Controller
Step 6 : Create View File

Step 1 : Create Razorpay Account in Razorpay

First, we need to signup/signin into the razorpay and verify our account verfication process is very simple.

Step 2 : Install Razorpay Package in Laravel 9

Now, we need to install razorpay/razorpay package in laravel. & copy the below command and run it in your terminal.

composer require razorpay/razorpay

Step 3 : Add Key And Secret Key

Now, we need to add key and secret key in the .env file for razorpay API integration, you can find these key from the setting menu of the razorpay dashboard. go to setting -> API Key and generate API Keys it will generate key secret id and secret key

Go Here:

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



Step 4 : Create Route

 In this step, we are creating two routes in the web.php file.

Route::get('/', [App\Http\Controllers\RazorpayController::class, 'razorpay'])->name('razorpay');
Route::post('razorpaypayment', [App\Http\Controllers\RazorpayController::class, 'payment'])->name('payment');

Step 5 : Create Controller

In this step, we are creating RazorpayController in this location app/Http/Controllers. through artisan

php artisan make:controller RazorpayController

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Razorpay\Api\Api;
use Session;
use Redirect;

class RazorpayController extends Controller
    public function razorpay()
        return view('welcome');

    public function payment(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'])) 
                $response = $api->payment->fetch($input['razorpay_payment_id'])->capture(array('amount'=>$payment['amount'])); 

            catch (\Exception $e) 
                return  $e->getMessage();
                return redirect()->back();
        \Session::put('success', 'Payment successful, your order will be despatched in the next 48 hours.');
        return redirect()->back();

Step 6 : Create View

create a view from where you will process your payment process

<!DOCTYPE html>
    <title>Integrate Payment Gateway in Laravel - Appfinz Technologies</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <script src="" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<div class="container">
    <div class="row">
        <div class="col-md-12">
            @if($message = Session::get('error'))
                <div class="alert alert-danger alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    <strong>Error!</strong> {{ $message }}

            @if($message = Session::get('success'))
                <div class="alert alert-info alert-dismissible fade in" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    <strong>Success!</strong> {{ $message }}

            <div class="panel panel-default" style="margin-top: 30px;">
                <div class="panel-heading">
              <h1> How to integrate payment gateway in laravel </h1>
                    <h2>Pay With Razorpay</h2>

                    <form action="{{ route('payment') }}" method="POST" >
                        <script src=""
                                data-key="{{ env('RAZORPAY_KEY') }}"
                                data-buttontext="Pay 10 INR"
                      "Kishan Kumar"

And after successful completion of payment, you can see transaction details in the Razorpay dashboard.

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

Now you can run and check.

I hope it can help you…

About Post Author