How to Integrate Razorpay Payment Gateway in Laravel 9 Example

  • 31-08-2022
  • 1719
  • Laravel 9
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

Laravel 9 How To Integrate Razorpay Payment Gateway, In this article, we will integrate the Razorpay payment gateway into the laravel 9 application. Razorpay provided business merchants to accept payment and allows you to business payments all over India. The payment gateway provided fasted payment transfer service in India. The is the popular payment gateway in India.

Razorpay payment gateway integration in laravel 9, we will first generate an API key from the Razorpay business account. We will get two keys from there. These two keys will help us to allow payment to our business account. So if you haven't created an account please go to Razorpay account and create one.

Composer require razorpay/razorpay, This is the Razorpay package, This package we will install in our application. I have given all guidelines and all the steps to reach integrate Razorpay into your laravel application.

Preview :
image

So let's start Razorpay payment gateway integration in laravel with composer requiring Razorpay/Razorpay in laravel.

Step 1. Create Razorpay Account

To getting start integrating Razorpay API in the laravel application instantly we must need an API key for accessing our Razorpay merchant account, So first we will generate API from our Razorpay account, click the given link to start to sign up for your Razorpay account Create Razorpay Account.

Step 2. Install Razorpay Composer

Now, we need to install Razorpay composer for getting Razorpay to integrate basic dependency in our application. So just copy the below-provided command and paste it in your application terminal.

composer require razorpay/razorpay

Laravel Tap Payment Gateway Integration Tutorial Example

Step 3. Generate API Key

Select your payment mode, You can in local development select TEST mode or if you want to integrate directly to love you can also select PRODUCTION mode. To get the API pair key click on the setting option. There you will show four options. Click on API Key and generate your API key and paste it into your temporary text file.

image

Now, open your .env file. The file you will find on the root directory of the project. Add these two keys in the .env file and also paste your ket value which you generated from the Razorpay payment account.

.env
RAZOR_KEY=xxxxx
RAZOR_SECRET=xxxxx

Step 4. Define Route

Create a Razorpay controller, use the below-provided command in your terminal.

php artisan make:controller RazorpayController

Define the route as per the below given, The first route is for the view form to get users' details, and The second route is for the send post request to the Razorpay server.

routes/web.php
<?php

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

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

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

Step 5. Razorpay Controller

Given two methods as per define in the route file. This controller will be run by the route defined. The first method razorpay() will show us Razorpay form and the second method for the know payment status.

app/Http/Controllers/RazorpayController.php
<?php

namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Razorpay\Api\Api;
use Session;
    
class RazorpayController extends Controller
{
    public function razorpay()
    {        
        return view('razorpay');
    }

    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'])) 
        {
            try {

                $response = $api->payment->fetch($input['razorpay_payment_id'])->capture(array('amount'=>$payment['amount'])); 

                // YOU CAN ALSO STORE RESPONSE $response IN DATABASE USERS TABLE

            } catch (\Exception $e) {
                
                return redirect()->back()->with('error',$e->getMessage());
            }            
        }
        
        return redirect()->back()->with('success','Payment successful received,');
    }
}

Step 6. Create View File

Create a blade file to view the user's interface form, so goto "resources/views" and create a file razorpay.blade.php

Add you RAZOR_KEY in this blade file asking for details about Razorpay. Add all necessary details.

<!DOCTYPE html>
<html>
<head>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
    <title>Laravel 9 Razorpay Payment Gateway Integrtion - Webappfix</title>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-12">
            @if(Session::has('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>
                    </button>
                    <strong>Error!</strong> {{ Session::get('error') }}
                </div>
            @endif

            @if(Session::has('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>
                    </button>
                    <strong>Success!</strong> {{ Session::get('success') }}
                </div>
            @endif
            
            <div class="panel panel-default" style="margin-top: 30px;">
                <h3 class="text-danger bg-light py-2 text-center">Laravel 9 Razorpay Payment Gateway Integrtion - Webappfix</h3><br>
                <div class="panel-heading">
                    <form action="{!!route('razorpay.payment')!!}" method="POST" >
                        @csrf
                        <script src="https://checkout.razorpay.com/v1/checkout.js"
                                data-key="{{env('RAZOR_KEY')}}"
                                data-amount="1000"
                                data-buttontext="Pay Amount"
                                data-name="webappfix"
                                data-description="Webappfix Payment"
                                data-prefill.name="Webappfix"
                                data-prefill.email="webappfix@gmail.com"
                                data-theme.color="#162D5D">
                        </script>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>    
</body>
</html>

Step 7. Start Development Server

Start your laravel app development server locally, use the below suggested command in your app terminal and run your project.

php artisan serve

Run the below-given URL in your browser and you will see there a payment button use dummy card data and pay.

http://127.0.0.1:8000/razorpay

How to Integrate PayUMoney Payment Gateway in Laravel 9


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :