How to Integrate Razorpay Payment Gateway in Laravel Application? ?
To integarte the Razorpay payment gateway in a Laravel application, you could observe those steps:
Step 1: Create a Razorpay Account and Obtain API Credentials
- Visit the Razorpay website ( https://razorpay.com/) and create an account.
VIDEO
- Once you have created an account, navigate to the Dashboard and generate your API Key and Secret Key from the API section.
Step 2: Install the Razorpay Package
- Open your terminal and navigate to the basis listing of your Laravel application.
- Run the subsequent command to put in the Razorpay bundle through Composer:
composer require razorpay/razorpay
Step 3: Configure the API Credentials
- Open the `.env` report for your Laravel application.
- Add the Razorpay API Key and Secret Key to the `.env` report:
RAZOR_KEY = your api key
RAZOR_SECRET =your secrete key
Step 4: Create a Route and Controller Method
public function pay()
{
return view('index' );
}
public function payment(Request $request)
{
$api = new Api(env('RAZOR_KEY' ), env('RAZOR_SECRET' ));
$input = $request->all();
// $api = new Api(env('RAZOR_KEY'), env('RAZOR_SECRET'));
$payment = $api->payment->fetch($request->razorpay_payment_id);
if (count($input) && !empty($input['razorpay_payment_id' ])) {
try {
$payment->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 response()->json(['success' => 'Payment successful' ]);
}
Create your Route below :
Route::get('pay' , [RazorpayController1::class ,'pay' ]);
Route::post('payment' , [RazorpayController1::class ,'payment' ])->name('payment' );
This is full code with index . blade file
<! 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 > RazerPay Example</ title >
<!-- Scripts -->
< script src ="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity ="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin ="anonymous" ></ script >
< script src ="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/js/bootstrap.min.js" integrity ="sha512-M5KW3ztuIICmVIhjSqXe01oV2bpe248gOxqmlcYrEzAvws7Pw3z6BK0iGbrwvdrUQUhi3eXgtxp5I8PDo9YfjQ==" crossorigin ="anonymous" ></ script >
<!-- Fonts -->
< link rel ="dns-prefetch" href ="//fonts.gstatic.com" >
< link href ="https://fonts.googleapis.com/css?family=Nunito" rel ="stylesheet" >
<!-- Styles -->
< link rel ="stylesheet" href ="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.2/css/bootstrap.min.css" integrity ="sha512-MoRNloxbStBcD8z3M/2BmnT+rg4IsMxPkXaGh2zD6LGNNFE80W3onsAhRcMAMrSoyWL9xD7Ert0men7vR8LUZg==" 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
< div class ="alert alert-success success-alert alert-dismissible fade show" role ="alert" style =" display: none;" >
< button type ="button" class ="close" data-dismiss ="alert" aria-label ="Close" >
< span aria-hidden ="true" > ×</ span >
</ button >
< strong > Success!</ strong > < span class ="success-message" ></ span >
</ div >
{{ Session::forget('success') }}
< div class ="card card-default" >
< div class ="card-header" >
Razorpay Example
</ div >
< form action ="" method ="post" >
@csrf
< div class ="card-body text-center" >
< div class ="form-group mt-1 mb-1" >
< input type ="text" name ="amount" class ="form-control amount" placeholder ="Enter Amount" >
</ div >
< button id ="rzp-button1" class ="btn btn-success btn-lg" > Pay</ button >
</ div >
</ form >
</ div >
</ div >
</ div >
</ div >
</ main >
</ div >
< script src ="https://checkout.razorpay.com/v1/checkout.js" ></ script >
< script >
$('body' ).on('click' ,'#rzp-button1' ,function (e){
e.preventDefault();
var amount = $('.amount' ).val();
var total_amount = amount * 100 ;
var options = {
"key" : "{{ env('RAZOR_KEY') }}" , // Enter the Key ID generated from the Dashboard
"amount" : total_amount, // Amount is in currency subunits. Default currency is INR. Hence, 10 refers to 1000 paise
"currency" : "INR" ,
"name" : "Infysky" ,
"description" : "Test infysky" ,
"image" : "" ,
"order_id" : "" ,
// "event_id": orderId, //This is a sample Order ID. Pass the `id` obtained in the response of Step 1
"prefill" : {
"name" : "Mehul Bagda" ,
"email" : "mehul.bagda@example.com" ,
"contact" : ""
},
"notes" : {
"address" : "test test"
},
"theme" : {
"color" : "#F37254"
}
};
var rzp1 = new Razorpay ( options ) ;
document. getElementById ( 'rzp-button1' ) . onclick = function ( e ) {
rzp1 . open ( ) ;
e . preventDefault ( ) ;
}
});
</ script >
</ body >
</ html >
Present the payment Structure :
- In your installment structure, make a point to incorporate the Razorpay SDK script and the vital structure fields (e.g., sum, name, email, and so forth.).
- At the point when the structure is submitted, it will send a solicitation to the 'installment' strategy in the 'PaymentController'.
- In the 'installment' technique, a request will be made utilizing the Razorpay Programming interface, and the request subtleties will be passed to the 'index.blade.php' view.
Confirm the payment :
- After the installment is handled on the Razorpay side, you can confirm the paymnet utilizing the Razorpay Programming interface and handle the achievement/disappointment reaction in like manner.
These means give an essential outline of coordinating the Razorpay payment door into your Laravel application. If it's not too much trouble
Thanks info@infysky.com
Post a Comment