How To Integrate Razorpay Payment Gateway In Laravel 8? |
To integrate the Razorpay payment gateway in your Laravel 8 project use the following few steps:
Steps:
1. Open your Laravel App in command prompt
2. Run the command
composer require razorpay/razorpay:2.*
3. Add the following two line of code to your Laravel .env file
RAZORPAY_KEY=your_razorpay_key
RAZORPAY_SECRET=your_razorpay_secret
4. Create a controller, like RazorpayPaymentController.php, use the following command prompt
php artisan make:controller RazorpayPaymentController
5. Add the following few lines of code in your RazorpayPaymentController.php controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Razorpay\Api\Api;
use Session;
use Exception;
class RazorpayPaymentController extends Controller
{
public function index()
{
return view('razorpayView');
}
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. '. $response->id .' | RRN -->'. $response->acquirer_data->rrn);
return redirect()->back();
}
}
6. Creat the blade file in resources/views/razorpayView.blade.php folder of Laravel and add the following lines of code.
<!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>Razorpay Payment Gateway Integration In Laravel 8</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">
Razorpay Payment Gateway Integration
</div>
<div class="card-body text-center">
@php
$rs = 3213;
$api = new \Razorpay\Api\Api(env('RAZORPAY_KEY'), env('RAZORPAY_SECRET'));
$orderId = $api->order->create(array('receipt' => '123', 'amount' => 3213, 'currency' => 'INR', 'notes'=> array('key1'=> 'value3','key2'=> 'value2')));
@endphp
<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="{{ $rs }}"
data-buttontext="Pay ₹{{ $rs / 100 }}"
data-name="Razorpay Payment"
data-description="Razorpay"
data-image="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEht5j5_UWPAFRgeLgY7rT7gixpv-Ip36FlV49JsEgpy56WYW6d2eifl1g3m5-JZGSTBVVJHHcbqL2W5DuYe04lH9s5gtSujoSsyQPsGCjAbROjdj_N4rR4_WtM4WCpXbHIiEJOqIoD-aBxU/s1600/icon.png"
data-prefill.contact="7414741474"
data-prefill.name="Abc Kumar"
data-prefill.email="testing@email.com"
data-theme.color="#ff7529"
data-notes.shopping_order_id="123"
data-order_id="{{$orderId->id}}">
</script>
</form>
</div>
</div>
</div>
</div>
</div>
</main>
</div>
</body>
</html>
7. At last create the new routes in routes/web.php file
Route::get('razorpay-payment', [\App\Http\Controllers\RazorpayPaymentController::class, 'index']);
Route::post('razorpay-payment', [\App\Http\Controllers\RazorpayPaymentController::class, 'store'])->name('razorpay.payment.store');
8. Now, use the following line of command to run your Laravel 8 project
php artisan serve
For more details refer to the Razorpay docs https://razorpay.com/docs/payment-gateway/server-integration/php/
Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8 | Integrate Razorpay Payment Gateway In Laravel 8
No comments:
Post a Comment