How To Integrate Razorpay Payment Gateway In Laravel 8


In this tutorial, you will learn how to integrate Razorpay payment gateway in laravel 8. We learn this tutorial Razorpay Laravel 8 integration. We will show Razorpay payment gateway integration in laravel 8 with an example. we will use the javascript lib of the Razorpay payment gateway integrated.

I will receive payments using the Razorpay payment gateway in laravel. Today We will implement Razorpay payment gateway in larvel 8 application. In this example, I will give an easy and simple way to integrate Razorpay payment gateway in laravel application.

Here, I am going to show you a full example of laravel 8 Razorpay payment gateway integration. So let's see the below example step by step.

Before starting the tutorial, you need to create a Razorpay account and get a secret app key from the Razorpay app. So, you can visit https://razorpay.com/ and create a Razorpay account.

Razorpay Payment Gateway Integration Laravel 8 Example [Simple Coding]
Razorpay Payment Gateway Integration Laravel 8 Example [Simple Coding]

Razorpay payment gateway integration in laravel 8 with example

  • Step 1: Install Laravel 8 App
  • Step 2: Setup Database Configuration
  • Step 3: Install Razorpay
  • Step 4: Add New Route
  • Step 5: Create Controller
  • Step 6: Create Blade File
Step 1 : Install Laravel App

First of all, We need to get a fresh laravel version application using the below command. So Let's open the terminal and run the below command.

composer create-project --prefer-dist laravel/laravel laravel_razorpay
Step 2 : Setup Database Configuration

After successfully install laravel app then after configuring database setup. We will open the ".env" file and change the database name, username, and password in the env file.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name
DB_USERNAME=Enter_Your_Database_Username
DB_PASSWORD=Enter_Your_Database_Password
Step 3 : Install Razorpay

In this step, we need to install the Razorpay laravel package in our Razorpay Laravel 8 integration. so let's open the terminal and run the below command:

composer require razorpay/razorpay


Successfully install Razorpay package then after you can add razorkey and razorsecret in .env file.

.env

RAZOR_KEY=your_razorpay_key
RAZOR_SECRET=your_razorpay_secret
Step 4: Add New Route

In this step, we will add two routes, one for display Razorpay payment gateway integration and another for store payment. So you have to simply add two new routes in your laravel application.

/routes/web.php

Route::get('payment-razorpay', 'PaymentController@create')->name('paywithrazorpay');
Route::post('payment', 'PaymentController@payment')->name('payment');
Step 5: Create Controller

In this step, we will create a new PaymentController file to handle the request to created two new route. In this Controller we define two method, create() and payment() in Razorpay payment gateway integration in PHP code. Both method will handle route request. So let's create a new controller and put code:

php artisan make:controller PaymentController


After successfully run the above command. So, let's copy the below code and put on the PaymentController.php file.

app/http/controller/PaymentController.php

<?php

namespace App\Http\Controllers;

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

class PaymentController extends Controller
{    
    public function create()
    {        
        return view('payWithRazorpay');
    }

    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'])); 

            } catch (\Exception $e) {
                return  $e->getMessage();
                \Session::put('error',$e->getMessage());
                return redirect()->back();
            }
        }
        
        \Session::put('success', 'Payment successful');
        return redirect()->back();
    }
}
Step 6: Create Blade File

In the last step, we will create a payWithRazorpay.blade.php file and write code to display the Razorpay Payment Gateway Integration form.

resources/views/payWithRazorpay.blade.php

<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 8 - phpcodingstuff.com</title>
    <!-- Scripts -->
    <script src="{{ asset('js/app.js') }}" defer></script>
    <!-- Fonts -->
    <link rel="dns-prefetch" href="//fonts.gstatic.com">
    <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet">
    <!-- Styles -->
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
</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">
                                How To Integrate Razorpay Payment Gateway In Laravel 8 - phpcodingstuff.com
                            </div>

                            <div class="card-body text-center">
                                <form action="{{ route('payment') }}" method="POST" >
                                    @csrf
                                    <script src="https://checkout.razorpay.com/v1/checkout.js"
                                            data-key="{{ env('RAZOR_KEY') }}"
                                            data-amount="1000"
                                            data-buttontext="product_price"
                                            data-name="Php Coding Stuff"
                                            data-description="Rozerpay"
                                            data-image="{{ asset('/image/phpcodingstuff.png') }}"
                                            data-prefill.name="name"
                                            data-prefill.email="email"
                                            data-theme.color="#ff752950">
                                    </script>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </main>
    </div>
</body>
</html>

Now we are ready to run our example so run bellow command for a quick run:

php artisan serve


Yours learn Laravel 8 Razorpay payment gateway integration tutorial, You have learned how to integrated Razorpay payment gateway in laravel 8 app using Razorpay javascript library.


I hope it can help you...

Leave a Reply

Your privacy will not be published. Required fields are marked *

We'll share your Website Only Trusted.!!