Dear friends, greetings. In this tutorial, we will learn How to Integrate Razorpay Payment Gateway in Laravel 9 Application. To acheive this, we will be using Razorpay PHP package. Follow the below mentioned step-by-step guidance.
Now, we will go through the above given steps one-by-one to and get it done.
First of all, lets install a fresh laravel 9 application. Open your terminal and run the following command:
composer create-project laravel/laravel laravel9app
Next, lets update database configuration in .env file as follows:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel9db
DB_USERNAME=dbusername
DB_PASSWORD=dbpassword
Next, install razorpay/razorpay php package by running below given command in terminal:
composer require razorpay/razorpay:2.*
Next, go to https://dashboard.razorpay.com/ website and login to your account. If you do not have your account created, you can create a new account. You can get your API_KEY and API_SECRET from your razorpay dashboard and configure it in .env file as follows:
RAZORPAY_API_KEY={your_razorpay_api_key}
RAZORPAY_API_SECRET={your_razorpay_api_secret}
Next, open app/config/services.php file and copy the below given code in it:
'razorpay' => [
'api_key' => env('RAZORPAY_API_KEY'),
'api_secret' => env('RAZORPAY_API_SECRET'),
],
Next, we need to create model and migration files to capture and store payment details in it. Run below command in therminal:
php artisan make:model RazorpayPayment -m
Above command will create a model file named RazorpayPayment and a migration file named databse/migrations/*_create_razorpay_payments_table.php. Open the migration file and copy below code in it:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('razorpay_payments', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email');
$table->string('mobile');
$table->string('amount');
$table->string('purpose');
$table->string('payment_request_id');
$table->string('payment_link');
$table->string('payment_status')->nullable();
$table->string('payment_id')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('razorpay_payments');
}
};
Next, run the below command to migrate the table:
php artisan migrate
Next, open app/Http/Controllers/RazorpayPaymentController.php file and copy below given code in it:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Validator;
use App\Models\RazorpayPayment;
class RazorpayPaymentController extends Controller
{
protected $rpp = 10;
public function index(Request $request)
{
$request_data = $request->all();
$view_data = [];
if (!$request->ajax()){
$view_data['title'] = 'Payment List';
return view('razorpay.payments.index')->with($view_data);
} else {
$page = $request_data['page'];
$view_data['page'] = $page;
$records = RazorpayPayment::orderBy('created_at', 'desc')->paginate($this->rpp);
$record_starts = $this->rpp * ($page - 1) + 1;
$record_ends = $this->rpp * ($page - 1) + count($records);
$view_data['records'] = $records;
$view_data['record_starts'] = $record_starts;
$view_data['record_ends'] = $record_ends;
return view('razorpay.payments.list')->with($view_data);
}
}
public function create(Request $request)
{
$view_data['title'] = 'Create New Payment';
return view('razorpay.payments.create')->with($view_data);
}
public function store(Request $request)
{
$posted_data = $request->all();
$validator = Validator::make($posted_data, array(
'name' => 'required|min:3',
'email' => 'required',
'mobile' => 'required',
'amount' => 'required'
), array(
'name.required' => 'Enter your name.',
'name.min' => 'Name must be min of 3 characters.',
'email.required' => 'Enter your email address.',
'mobile.required' => 'Enter your mobile number.',
'amount.required' => 'Enter your amount.'
));
if ($validator->fails()) {
return redirect()->back()->withInput($request->only('name', 'email', 'mobile', 'amount'))->withErrors($validator->errors());
}
try {
$razorpay = config('services.razorpay');
$api = new Api($razorpay['api_key'], $razorpay['api_secret']);
$name = trim($request->name);
$email = trim($request->email);
$mobile = trim($request->mobile);
$amount = intval($request->amount) * 100;
$purpose = 'Razorpay Invoice Payment';
$payload = array(
"type" => "link",
"view_less" => 1,
"amount" => $amount,
"currency" => "INR",
"description" => $purpose,
"receipt" => "RPORDER" . Str::random(9),
"sms_notify" => 1,
"email_notify" => 1,
"expire_by" => strtotime("+1 month"),
"callback_url" => url('/razorpay/payments/success'),
"callback_method" => "get",
"customer" => array(
"name" => $name,
"email" => $email,
"contact" => $mobile,
),
);
$invoice = $api->invoice->create($payload);
$invoice = $invoice->toArray();
if ($invoice['status'] == 'issued') {
RazorpayPayment::insert([
'name' => $name,
'email' => $email,
'mobile' => $mobile,
'amount' => $amount,
'purpose' => $purpose,
'payment_request_id' => $invoice['id'],
'payment_link' => $invoice['short_url'],
'payment_status' => $invoice['status'],
'created_at' => now(),
'updated_at' => now()
]);
header('Location: ' . $invoice['short_url']);
exit();
} else {
echo "<pre>";
print_r($invoice);
exit;
}
}catch (Exception $e) {
echo "<pre>";
print('Error: ' . $e->getMessage());
exit;
}
}
public function success(Request $request)
{
$request_data = $request->all();
$invoice_id = $request_data['razorpay_invoice_id'];
$invoice_status = $request_data['razorpay_invoice_status'];
$payment_id = $request_data['razorpay_payment_id'];
$rp_payment = RazorpayPayment::where('payment_request_id', $invoice_id)->first();
if (strtolower(trim($invoice_status)) == 'paid') {
$rp_payment->payment_status = $invoice_status;
$rp_payment->payment_id = $payment_id;
$rp_payment->save();
dd('Payment Successful');
} else {
dd('Payment Failed!');
}
dd($request_data);
}
}
Next, open routes/web.php file and copy the below code in it:
<?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::group(['prefix' => 'razorpay'], function () {
Route::group(['prefix' => 'payments'], function () {
Route::get('/', [RazorpayPaymentController::class, 'index']);
Route::get('/create', [RazorpayPaymentController::class, 'create']);
Route::post('/', [RazorpayPaymentController::class, 'store']);
Route::any('/success', [RazorpayPaymentController::class, 'success']);
});
});
Next, create the below given blade files under resources/views/razorpay/payments directory:
resources/views/razorpay/payments/index.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Laravel 9 Razorpay Payment Gateway Integration Tutorial</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
</head>
<body>
<div class="container mt-3">
<div class="row text-center"><h1>Laravel 9 Razorpay Payment Gateway Integration Tutorial</h1></div>
<div class="row" id="result-div"><strong style="color: #0cf;text-align: center;">Fetching records...</strong></div>
</div>
<script>
var pageNo = 1;
$(function(){
$.ajax({
url: "{{url('razorpay/payments')}}",
type: "GET",
dataType: "HTML",
data: {
'page': pageNo
}
}).done(function(data){
$("#result-div").empty().html(data);
}).fail(function(jqXHR, ajaxOptions, thrownError){
console.log('No response from server');
}).always(function(){
});
});
</script>
</body>
</html>
resources/views/razorpay/payments/list.blade.php
<div class="col-12 mb-3">
<div class="card text-white bg-success mb-3">
<div class="card-header">
<h3 class="card-title" style="display: inline-block;">Payments List</h3>
<a href="{{url('/razorpay/payments/create')}}" class="btn btn-primary float-end">Create New Payment</a>
</div>
<div class="card-body table-responsive">
<table class="table table-striped text-white">
<thead>
<tr style="text-transform: uppercase;">
<th>#</th>
<th>Name</th>
<th>EMail</th>
<th>Mobile</th>
<th>Amount</th>
<th>Purpose</th>
<th>Status</th>
<th>PaymentId</th>
<th>CreatedAt</th>
</tr>
</thead>
<tbody>
@if($records->count() > 0)
@foreach($records as $rec)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$rec->name}}</td>
<td>{{$rec->email}}</td>
<td>{{$rec->mobile}}</td>
<td>{{$rec->amount}}</td>
<td>{{$rec->purpose}}</td>
<td><button class="btn btn-info">{{$rec->payment_status}}</button></td>
<td>{{$rec->payment_id}}</td>
<td>{{$rec->created_at}}</td>
</tr>
@endforeach
@else
<tr class="text-center"><td colspan="8">Record not found!!</td></tr>
@endif
</tbody>
</table>
{!! $records->links() !!}
</div>
</div>
</div>
resources/views/razorpay/payments/create.blade.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Laravel 9 Razorpay Payment Gateway Integration Tutorial</title>
<meta name="csrf-token" content="{{ csrf_token() }}">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
</head>
<body>
<div class="container mt-3">
<div class="row justify-content-center">
<div class="col-12 col-md-6 mb-3">
<div class="card text-dark bg-success mb-3">
<div class="card-header">
<h3 class="card-title text-white" style="display: inline-block;">Create New Payment</h3>
<a href="{{url('/razorpay/payments')}}" class="btn btn-primary float-end">Payments List</a>
</div>
<div class="card-body">
<form action="{{ url('razorpay/payments') }}" method="POST" name="laravel9-razorpay-integration">
{{ csrf_field() }}
<div class="form-floating">
<input type="text" class="form-control" name="name" id="name" value="{{ old('name') }}" placeholder="name">
<label for="name">Full Name</label>
</div>
<div class="form-floating">
<input type="email" class="form-control" name="email" id="email" value="{{ old('email') }}" placeholder="email">
<label for="email">Email Address</label>
</div>
<div class="form-floating">
<input type="text" class="form-control" name="mobile" id="mobile" value="{{ old('mobile') }}" placeholder="mobile">
<label for="mobile">Mobile Number</label>
</div>
<div class="form-floating">
<input type="text" class="form-control" name="amount" id="amount" value="{{ old('amount') }}" placeholder="amount">
<label for="amount">Amount</label>
</div>
<button class="w-100 btn btn-lg btn-primary" type="submit">Place Order</button>
</form>
@if ($errors->any())
<div class="alert alert-danger text-start" role="alert">
<strong>Opps!</strong> Something went wrong<br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
</div>
</div>
</div>
</div>
</div>
</body>
</html>
Great, we are all set. Lets start the application now by using below command in terminal:
php artisan serve
Once development server is started, in your browser, open below URL:
http://127.0.0.1:8000/razorpay/payments
HAPPY LEARNING:)
Note: You can use below given Test Card Credentials to test your application:
===Mastercard
5267 3181 8797 5449
Random CVV
Any future date
===Visa
4111 1111 1111 1111
Random CVV
Any future date
For more information of test cards, please visit the below url:
https://razorpay.com/docs/payments/payments/test-card-upi-details/