How to Integrate Razorpay Payment Gateway in Laravel

WebSolutionStuff | Jan-06-2021 | Categories : Laravel PHP

Hello Friends,

In this post i will share most important and interesting toping about how to integrate razorpay payment gateway in laravel.

As you all know if you are developing e-commerce website then you must need payment system for buy and sell your product in your website and at that time we require any type of payment integration require in website. Razorpay payment gateway integration is very easy to use like other payment gateway.

So, here i will integrate razorpay integration in laravel using razorpay api integration, So,let's start razorpay payment gateway integration in laravel 8.

Step 1 : Create Account in Razorpay

Step 2 : Install Razorpay Package

Step 3 : Add Key and Secret Key

Step 4 : Create Route

Step 5 : Create Controller

Step 6 : Create View


Step 1 : Create Account in Razorpay

First of all we need to create Razorpay account for integrate razorpay in laravel using below link and create test account for testing purpose.


Step 2 : Install Razorpay Package

Now, we need to install razorpay/razorpay package in laravel for razorpay integration in laravel. So, copy below command and run in your terminal.

composer require razorpay/razorpay



Step 3 : Add Key And Secret Key

Now, we need to add key and secret key in .env file for razorpay api integration, you can find these key from setting menu of razor pay dashboard.





Step 4 : Create Route

 Now, we are creating two route for payment and action add below code in web.php

Route::get('razorpay', [RazorpayController::class, 'razorpay'])->name('razorpay');
Route::post('razorpaypayment', [RazorpayController::class, 'payment'])->name('payment');


Step 5 : Create Controller

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


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('index');

    public function payment(Request $request)
        $input = $request->all();        
        $api = new Api(env('RAZOR_KEY'), env('RAZOR_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 view file for form and output , we have save as index.blade.php file

<!DOCTYPE html>
    <title>How To Integrate Razorpay Payment Gateway In Laravel -</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href="">
    <script type="text/javascript" src=""></script>
	<script src=""></script>    
<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 }}
            {!! Session::forget('error') !!}
            @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 }}
            {!! Session::forget('success') !!}
            <div class="panel panel-default" style="margin-top: 30px;">
                <h3>How To Integrate Razorpay Payment Gateway In Laravel - </h3><br>
                <div class="panel-heading">
                    <h2>Pay With Razorpay</h2>

                <!-- <div class="panel-body text-center"> -->
                    <form action="{!!route('payment')!!}" method="POST" >                        
                        <script src=""
                                data-key="{{ env('RAZOR_KEY') }}"
                                data-buttontext="Pay 10 INR"
                        <input type="hidden" name="_token" value="{!!csrf_token()!!}">


And Finally You will get below screen for payment.



After proceed you will get send page as below.


Add Below Details for Trial : 

Visa Card No. : 4111111111111111

Mobile No. : 1231231231

OTP No. : *****


And after successfully complition of payment you can see transaction details in razorpay dashboard.


Hope, you will understand this example and get output as you expect.


Clone Project from Github Also : Razorpay Payment Gateway Integration In Laravel


Recommended Post
Featured Post

Follow us
facebooklogo github instagram twitter