In this tutorial you will learn about the Razorpay Payment Gateway Integration in Laravel 8 Tutorial and its application with practical example.
In this Razorpay Payment Gateway Integration in Laravel 8 Tutorial, I’ll show you how to integrate Razorpay in laravel 8 to collect payment. In this tutorial you will learn to integrate Razorpay in laravel 8. In this article I will share example to integrate Razorpay Payment Gateway in laravel 8 application.
Razorpay Payment Gateway
Razorpay is one of the popular payment gateway, that allows us to accept payment from your customer. Razorpay is very simple, hassle free and easy to integrate payment gateway. Integrating Razorpay payment gateway in laravel 8 is a breeze. Before starting with this tutorial you would require a Razorpay developer account, from there you will get the api key and secret key require for the integration.
Razorpay Payment Gateway Integration in Laravel 8 Tutorial
In this tutorial, you will learn to integrate Razorpay payment gateway in your laravel 8 project. Follow this step by step tutorial to learn Razorpay payment gateway integration in laravel 8.
Step 1: Install Laravel
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Step 2: Create Razorpay Account
In this step you need to create account on razorpay website and get account key id and secret. Then you need to add on .env file as following:
1 2 |
RAZORPAY_KEY=rzp_test_XXXXXXXXX RAZORPAY_SECRET=XXXXXXXXXXXXXXXX |
Step 3: Install razorpay/razorpay Package
In this step, we will install razorpay Package via the composer dependency manager. Use the following command to install razorpay Package.
1 |
composer require razorpay/razorpay |
Step 4: Create Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?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'); |
Step 5: Create Controller
Now, lets create a controller for Razorpay Payments. Create a controller named RazorpayPaymentController and put the following code in it:
app/Http/Controllers/RazorpayPaymentController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
<?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(); } } |
Step 6: Create Blade File
In this step, we will create view/blade file to accept razorpay payment. Lets create a blad file “razorpayView.blade.php” in “resources/views/” directory and put the following code in it respectively.
resources/views/razorpayView.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
<!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>Laravel - Razorpay Payment Gateway Integration</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 </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="yoursite.com" data-description="Rozerpay" data-image="https://www.yoursite.com/frontTheme/images/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> |
Run Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
localhost:8000/razorpay-payment |