Share this:

Hello dev, Today we are going to learn Laravel 9 integrate Razorpay Payment Gateway. This tutorial will cover on how to integrate razorpay payment gateway in laravel 9 application.

Today, I explain laravel 9 integrating razorpay payment gateway. we will learn how to integrating a razorpay payment gateway in laravel 9 application. I will share with you how to integrating Razorpay payment gateway in Laravel 9 application with example. I will share with you how to integrating Razorpay payment gateway in Laravel 9 application with example. In this Laravel 9 Razorpay Payment Gateway Integration Tutorial with Example tutorial.

Razorpay is very simple, hassle free and easy to integrate payment gateway. Integrating Razorpay payment gateway in laravel 9 is a breeze. Razorpay is one of the popular payment gateway, that allows us to accept payment from your customer.

In this tutorial you will learn to integrate Razorpay in laravel 9. In this step by step tutorial I’ll share laravel 9 Razorpay integration example.

Steps for Laravel 9 integrate Razorpay Payment Gateway:

  • Step 1: Installing fresh new Laravel 9 Application
  • Step 2: Create Razorpay Account
  • Step 3: Install razorpay/razorpay Package
  • Step 4: Create Route
  • Step 5: Create Controller
  • Step 6: Create Blade File
  • Step 7: Testing
  • Step 8: Conclusion

Also Read: How to Use Charts.JS in Laravel 9

Step 1: Installing fresh new Laravel 9 Application

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

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.

Create Razorpay Account and Getting Key and Secret
Create Razorpay Account and Getting Key and Secret

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

.env

RAZORPAY_KEY=rzp_test_XXXXXXXXX
RAZORPAY_SECRET=XXXXXXXXXXXXXXXX

Also Read: Laravel 9 JetStream Livewire CRUD Operations Tutorial

Step 3: Install razorpay/razorpay Package

In this step, we need to install razorpay/razorpay composer package to use razorpay api. so let’s run below command:

composer require razorpay/razorpay

Step 4: Create Route

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

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\RazorpayPaymentController;
  
/*
|--------------------------------------------------------------------------
| 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-payment', [RazorpayPaymentController::class, 'index']);
Route::post('razorpay-payment', [RazorpayPaymentController::class, 'store'])->name('razorpay.payment.store');

Also Read: How to Autocomplete Search using Typeahead Js in Laravel 9

Step 5: Create Controller

In this step, we will create RazorpayPaymentController and write send sms logic, so let’s add new route to web.php file as below.

app/Http/Controllers/RazorpayPaymentController.php

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use Razorpay\Api\Api;
use Session;
use Exception;
  
class RazorpayPaymentController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index()
    {        
        return view('razorpayView');
    }
  
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function store(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'])) {
            try {
                $response = $api->payment->fetch($input['razorpay_payment_id'])->capture(array('amount'=>$payment['amount'])); 
  
            } catch (Exception $e) {
                return  $e->getMessage();
                Session::put('error',$e->getMessage());
                return redirect()->back();
            }
        }
          
        Session::put('success', 'Payment successful');
        return redirect()->back();
    }
}

Also Read: How to Create PDF File in Laravel 9?

Step 6: Create Blade File

Now we need to add blade file. so let’s create razorpayView.blade.php file and put below code.

resources/views/razorpayView.blade.php

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>How to Integrate Razorpay Payment Gateway in Laravel 9 - LaravelTuts.com</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
    <div id="app">
        <main class="py-4">
            <div class="container">
                <div class="row">
                    <div class="col-md-6 offset-3 col-md-offset-6">
  
                        @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>
                                </button>
                                <strong>Error!</strong> {{ $message }}
                            </div>
                        @endif
  
                        @if($message = Session::get('success'))
                            <div class="alert alert-success alert-dismissible fade {{ Session::has('success') ? 'show' : 'in' }}" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                </button>
                                <strong>Success!</strong> {{ $message }}
                            </div>
                        @endif
  
                        <div class="card card-default">
                            <div class="card-header">
                                Laravel - Razorpay Payment Gateway Integration - LaravelTuts.com
                            </div>
  
                            <div class="card-body text-center">
                                <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 INR"
                                            data-name="LaravelTuts.com"
                                            data-description="Rozerpay"
                                            data-image="https://laraveltuts.com/wp-content/uploads/2022/08/laraveltuts-rounde-logo.png"
                                            data-prefill.name="name"
                                            data-prefill.email="email"
                                            data-theme.color="#ff7529">
                                    </script>
                                </form>
                            </div>
                        </div>
  
                    </div>
                </div>
            </div>
        </main>
    </div>
</body>
</html>

Also Read: Laravel 9 Import Export Excel & CSV File to Database Example

Step 7: Testing

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

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:

http://127.0.0.1:8000/razorpay-payment

Previews:

Laravel 9 integrate Razorpay Payment Gateway
Laravel 9 integrate Razorpay Payment Gateway

Step 8: Conclusion

Today, We had learn Laravel 9 integrate Razorpay Payment Gateway. Hope this tutorial helped you with learning Laravel 9. If you have any question you can ask us at comment section below. If you like the tutorial please subscribe our YouTube Channel and follow us on social network Facebook and Instagram.

Also Read: How to Upload Image in Laravel 9?

Share this:

Categorized in: