How to Integrate Razorpay Payment Gateway in Laravel
Razorpay payment integration in Laravel
Hello Guyz, In this youtube tutorial, I am showing you how to integrate the Razorpay payment gateway in laravel 9.ย But first, let me introduce you to Razorpay Payment Gateway, Razorpayย is the onlyย paymentย solution provider in India which allows businesses and individuals to accept, process, and disburseย payments.ย
Razorpay payment gateway accepts all payment modes with domestic and international credit & debit cards, EMIs ( Credit/Debit Cards & Cardless), PayLater Options Available, Netbanking from 58 banks, and UPI (Unified Payments Interface) and also 8 types of mobile wallets, Razorpay provides the most extensive set of payment methods available in India today.
Razorpay Payment Gateway is very easy as compared to any other payment gateways, The developer docs in very user friendly and easy to understand. so here i am demonstrating the Razorpay Payment Gateway integration in Laravel 9 here.
So, let’s see Razorpay payment gateway integration in laravel 9 and laravel 8 and laravel 7, how to integrate Razorpay payment gateway in laravel 9, Razorpay payment gateway integration in laravel 9
Step 1 : Create Razorpay Account in Razorpay Payment Gateway Website Step 2 : Install Razorpay Package in Laravel 9 Step 3 : Add API Key and Secret Key Step 4 : Create Route Step 5 : Create Controller Step 6 : Create View File
Step 1 : Create Razorpay Account in Razorpay
First, we need to signup/signin into the razorpay and verify our account verfication process is very simple.
Step 2 : Install Razorpay Package in Laravel 9
Now, we need to install razorpay/razorpay package in laravel. & copy the below command and run it in your terminal.
composer require razorpay/razorpay
Step 3 : Add Key And Secret Key
Now, we need to add key and secret key in the .env file for razorpay API integration, you can find these key from the setting menu of the razorpay dashboard. go to setting -> API Key and generate API Keys it will generate key secret id and secret key
Go Here: https://dashboard.razorpay.com/app/keys.
Next you can get account key id and secret and add on .env file as like bellow:
.env
RAZORPAY_KEY=rzp_test_XXXXXXXXX RAZORPAY_SECRET=XXXXXXXXXXXXXXXX
Step 4 : Create Route
In this step, we are creating two routes in the web.php file.
Route::get('/', [App\Http\Controllers\RazorpayController::class, 'razorpay'])->name('razorpay'); Route::post('razorpaypayment', [App\Http\Controllers\RazorpayController::class, 'payment'])->name('payment');
Step 5 : Create Controller
In this step, we are creating RazorpayController in this location app/Http/Controllers. through artisan
php artisan make:controller RazorpayController
<?php 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('welcome'); } public function payment(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, your order will be despatched in the next 48 hours.'); return redirect()->back(); } }
Step 6 : Create View
create a view from where you will process your payment process
<!DOCTYPE html> <html> <head> <title>Integrate Payment Gateway in Laravel - Appfinz Technologies</title> <meta name="csrf-token" content="{{ csrf_token() }}"> <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 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> </button> <strong>Error!</strong> {{ $message }} </div> @endif @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> </button> <strong>Success!</strong> {{ $message }} </div> @endif <div class="panel panel-default" style="margin-top: 30px;"> <div class="panel-heading"> <h1> How to integrate payment gateway in laravel </h1> <h2>Pay With Razorpay</h2> <form action="{{ route('payment') }}" 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="Appfinz" data-description="Payment" data-prefill.name="Kishan Kumar" data-prefill.email="kkmishra459@gmail.com" data-theme.color="#ff7529"> </script> </form> </div> </div> </div> </div> </div> </body> </html>
And after successful completion of payment, you can see transaction details in the Razorpay dashboard.
you can get a testing card for razorpay from here: Click Here
Now you can run and check.
I hope it can help you…