Hello, All in this post i will integrate Razorpay Payment Gateway, I will explain each and every thing Step wise.
- Create Razorpay Account in Razorpay
- Install Razorpay Package
- Add API Key and Secret Key
- Route for payment
- Create Controller
- Create View File
- Finally The Result
Step 1. Create Razorpay Account in Razorpay
You can create account by clicking on link, and i am sure you are here because you already have account.
Step 2. Install Razorpay Package
we need to install razorpay/razorpay package in laravel.
$ 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.
and now put this into your .env file like below image.
Step 4. Route for payment
In this step, we will creates routes in the web.php.
Route::get('razorpay', [RazorpayController::class, 'razorpay']); Route::post('razorpaypayment', [RazorpayController::class, 'payment']);
Step 5. Create Controller
Here, i will create RazorpayController, by using the artisan command, You can directly create, but for the best practice always use command.
$ php artisan make:controller RazorpayController
and put the below code inside your newly created controller.
<?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('payment'); } public function payment(Request $request) { $input = $request->all(); $api = new Api(env('RAZORPAY_APIKEY'), 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', 'Congratulations, Your Payment successful.'); return redirect()->back(); } }
Step 6 : Create View
Create a view file for the above controller , here i named it payment.blade.php
<!DOCTYPE html> <html> <head> <title>How To Integrate Payment Gateway In Laravel 8</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"> <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> </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 {!! Session::forget('error') !!} @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 {!! Session::forget('success') !!} <div class="panel panel-default" style="margin-top: 30px;"> <h3>How To Integrate Payment Gateway In Laravel 8</h3><br> <div class="panel-heading"> <h2>Pay Now</h2> <form action="{!!route('payment')!!}" method="POST" > <script src="https://checkout.razorpay.com/v1/checkout.js" data-key="{{ env('RAZORPAY_APIKEY') }}" data-amount="1000" data-buttontext="Pay Amount" data-name="TestName" data-description="Payment" data-prefill.name="name" data-prefill.email="[email protected]" data-theme.color="#ff7529"> </script> <input type="hidden" name="_token" value="{!!csrf_token()!!}"> </form> </div> </div> </div> </div> </div> </body> </html>
Now run your project and test by using the test card from this link .
Step 6. Finally The Result
here we will completed successful ,Razorpay payment gateway intergation in laravel 8. if you have any confusion please comment below.
Thanks.