Introduction:
In this article, I’ll walk you through integrating the Paytm payment gateway into a Laravel 10 application.
In terms of the market nowadays, particularly in India, everything is digital. India has become a digital country. One well-known e-commerce and payment company in India is Paytm. They offer a really simple way to purchase, recharge your phone, get D2H, pay your electricity bill, and more. With this example, we will thus learn how to incorporate Paytm payments into a Laravel application.
Step 1: Install Laravel 10
Using the bellow command, we must first obtain a new Laravel 8 application. Thus, launch the command below using your terminal or command prompt:
composer create-project --prefer-dist laravel/laravel paytm
Step 2: Install Package
To get started, open your terminal and enter the following command to start the PayTM developer API package for the anandsiddharth/laravel-paytm-wallet:
composer require anandsiddharth/laravel-paytm-wallet
After successfully install package, openĀ config/app.phpĀ file and add service provider.
config/app.php
'providers' => [
Anand\LaravelPaytmWallet\PaytmWalletServiceProvider::class,
],
'aliases' => [
'PaytmWallet' => Anand\LaravelPaytmWallet\Facades\PaytmWallet::class,
],
Step 3 : Login into Paytm developer website and generate API key and Secret
Now set the env variable with your ones, add the below code in your .env file.
PAYTM_ENVIRONMENT=local
PAYTM_MERCHANT_ID=YOUR_MERCHANT_ID
PAYTM_MERCHANT_KEY=YOUR_MERCHANT_KEY
PAYTM_MERCHANT_WEBSITE=YOUR_MERCHANT_WEBSITE
PAYTM_CHANNEL=YOUR_MERCHANT_CHANNEL
PAYTM_INDUSTRY_TYPE=Retail
Also add this configuration to config/services.php file like below.
'paytm-wallet' => [
'env' => env('PAYTM_ENVIRONMENT'), // values : (local | production)
'merchant_id' => env('PAYTM_MERCHANT_ID'),
'merchant_key' => env('PAYTM_MERCHANT_KEY'),
'merchant_website' => env('PAYTM_MERCHANT_WEBSITE'),
'channel' => env('PAYTM_CHANNEL'),
'industry_type' => env('PAYTM_INDUSTRY_TYPE'),
],
Step 4 : Create Model and Migration
To generate Model execute the following command.
php artisan make:model Paytm
Put below code in your in Model class of Paytm.
App/Models/Paytm.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Paytm extends Model
{
protected $table= 'paytms';
protected $fillable = [
'name','mobile','email','status','fee','order_id','transaction_id'
];
}
Now make a migration file for paytms table via your terminal.
php artisan make:migration create_paytms_table
database/migrations/2024_01_03_234224_create_paytms_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePaytmsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('paytms', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->bigInteger('mobile');
$table->string('email');
$table->tinyInteger('status')->default(0);
$table->integer('fee');
$table->string('order_id');
$table->string('transaction_id')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('paytms');
}
}
Firstly create a database in phpmyadmin name that Ā “paytm_laravel”Ā and update inĀ .envĀ file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=paytm_laravel
DB_USERNAME=root
DB_PASSWORD=
Now run below command for migration.
php artisan migrate
Step 5: Create Web Routes
In this step we will create 3 routes for our application as per below :Ā
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
//Paytm Payment
Route::get('/initiate','App\Http\Controllers\PaytmController@initiate')->name('initiate.payment');
Route::post('/payment','App\Http\Controllers\PaytmController@pay')->name('make.payment');
Route::post('/payment/status','App\Http\Controllers\PaytmController@paymentCallback')->name('status');
Step 6 : Create the controller
Now we will create a controller for our Paytm Payment Gateway, enter the below command to create PaytmController.
php artisan make:controller PaytmController
after command PaytmController is Created then write the below code in that file
app/Https/Controllers/PaytmController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Anand\LaravelPaytmWallet\Facades\PaytmWallet;
use App\Models\Paytm;
class PaytmController extends Controller
{
public function initiate()
{
return view('paytm');
}
public function pay(Request $request)
{
$amount = 1; //Amount to be paid
$userData = [
'name' => $request->name,
'mobile' => $request->mobile,
'email' => $request->email,
'fee' => $amount,
'order_id' => $request->mobile."_".rand(1,1000)
];
$paytmuser = Paytm::create($userData);
$payment = PaytmWallet::with('receive');
$payment->prepare([
'order' => $userData['order_id'],
'user' => $paytmuser->id,
'mobile_number' => $userData['mobile'],
'email' => $userData['email'],
'amount' => $amount,
'callback_url' => route('status'),
]);
return $payment->receive();
}
public function paymentCallback(){
$transaction = PaytmWallet::with('receive');
$response = $transaction->response();
$order_id = $transaction->getOrderId();
$trans_id = $transaction->getTransactionId();
if($transaction->isSuccessful()){
Paytm::where('order_id', $order_id)->update(['status'=>1, 'transaction_id'=>$trans_id]);
return redirect(route('initiate.payment'))->with('message',"Your Payment is Successful");
}elseif($transaction->isFailed()){
Paytm::where('order_id', $order_id)->update(['status'=>0, 'transaction_id'=>$trans_id]);
return redirect(route('initiate.payment'))->with('message',"Your Payment is Failed");
}elseif($transaction->isOpen()){
Paytm::where('order_id', $order_id)->update(['status'=>2, 'transaction_id'=>$trans_id]);
return redirect(route('initiate.payment'))->with('message',"Your Payment is Processing");
}
$transaction->getResponseMessage();
}
}
STEP 7 : Create View File
In this step, we will create paytm blade file and we create from with name, mobile number and email. So, let’s create paytm.blade.php file and put below code :
resources/view/paytm.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Payment Gateway using Paytm by dharmu</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
</head>
<body>
<div class="container" width="50px">
<div class="panel panel-primary" style="margin-top:110px;">
<div class="panel-heading">
<h3 class="text-center">Payment Gateway test by dharmu</h3>
</div>
<div class="panel-body">
<form action="{{ route('make.payment') }}" method="post" enctype="multipart/form-data">
{!! csrf_field() !!}
@if($message = Session::get('message'))
<p>{!! $message !!}</p>
<?php Session::forget('success'); ?>
@endif
<div class="row">
<div class="col-md-12">
<strong>Name:</strong>
<input type="text" name="name" class="form-control" placeholder="Name" required>
</div>
<div class="col-md-12">
<strong>Mobile No.:</strong>
<input type="text" name="mobile" class="form-control" maxlength="10" placeholder="Mobile No." required>
</div>
<div class="col-md-12">
<strong>Email:</strong>
<input type="email" name="email" class="form-control" placeholder="Email" required>
</div>
<div class="col-md-12">
<br>
<div class="btn btn-info">
Claim charge : 1 Rs/-
</div>
</div>
<div class="col-md-12">
<br>
<button type="submit" class="btn btn-success">PROCEED</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
STEP 8 : middleware/verifyCsrfToken.php and put this code
<?php
namespace App\Http\Middleware;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
class VerifyCsrfToken extends Middleware
{
/**
* The URIs that should be excluded from CSRF verification.
*
* @var array<int, string>
*/
protected $except = [
'/payment/status'
];
}
STEP 9 : config/session.php
rename same site variable in session.php
//'same_site' => 'lax',
'same_site' => null,
Now we are ready to run out project so run below command to start server.
php artisan serve
http://127.0.0.1:8000/initiate
Thanks for Reading.