Paytm payment gateway integration in Laravel 8
In this blog I will guide you how we can integrate Paytm Payment gateway integration in Laravel 8. Before moving forward let’s know what is payment gateway
What is a payment gateway?
A payment gateway is a merchant service provided by an e-commerce application service provider that authorizes credit card or direct payments processing for e-businesses, online retailers, bricks and clicks, or traditional brick and mortar.Examples of payment gateway are Stripe, Mollie, Authorize.net, Razorpay, Paypal.
We will follow the below steps to integrate Paytm Payment Gateway in Laravel
- Create account on paytm and get developer key
- Install Laravel 8
- Install Paytm Package
- Set the ENV variables
- Create the Controller
- Add the routes
- Add the Controller Code
- Add the view file
- Test the integration
Step :1 Create account on Paytm and get developer key
To create account as developer please click here.
After creating the Paytm account you need to take your API Credentials from Paytm, So after the login in the sidebar under developers you can see API keys. You have to click on that link to acquire your API credentials.
Step : 2 Install Laravel 8
Run the below command to install laravel
composer create-project --prefer-dist laravel/laravel paytm
Step : 3 Install Paytm Package
Now we will install anandsiddharth/laravel-paytm-wallet package, run the below command to install the package
composer require anandsiddharth/laravel-paytm-wallet
Now we will, add the PaytmWallet
facade to the aliases
array in your app
configuration file:
'aliases' => [ // Other aliases 'PaytmWallet' => Anand\LaravelPaytmWallet\Facades\PaytmWallet::class, ],
Step : 4 Set the ENV variables
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_HERE
PAYTM_MERCHANT_KEY=YOUR_SECRET_KEY_HERE
PAYTM_MERCHANT_WEBSITE=YOUR_MERCHANT_WEBSITE
PAYTM_CHANNEL=YOUR_CHANNEL_HERE
PAYTM_INDUSTRY_TYPE=YOUR_INDUSTRY_TYPE_HERE
Now go to config/services.php
add the following configuration
'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'),
],
Laravel 8 Changes
Our package is comptible with Laravel 7 but same_site setting is changed in default Laravel installation, make sure you change same_site
to null
in config/session.php
or callback won’t include cookies and you will be logged out when a payment is completed
add the below code to your config/session.php file
<?php
use Illuminate\Support\Str;
return [
/... 'same_site' => null,
];
Step – 5: 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
Step – 6: Add Routes
Now we will add routes for our Paytm payment gateway add the below routes in routes/web.php file
First add the namespace on the top
use App\Http\Controllers\PaytmController;
then add the below routes
//Paytm Payment
Route::post('paytm-payment',[PaytmController::Class, 'paytmPayment'])->name('paytm.payment');
Route::post('paytm-callback',[PaytmController::Class, 'paytmCallback'])->name('paytm.callback');
Route::get('paytm-purchase',[PaytmController::Class, 'paytmPurchase'])->name('paytm.purchase');
Now go to app/Http/Middleware and look for VerifyCsrfToken.php middleware add the below URL to your protected $except Array.
protected $except = [
'/paytm-callback*',
];
Step – 7 Add controller Code
Now go to app/http/controller and add the below code in your PaytmController.php file
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use PaytmWallet;
class PaytmController extends Controller
{
/**
* Redirect the user to the Payment Gateway.
*
* @return Response
*/
public function paytmPayment(Request $request)
{
$payment = PaytmWallet::with('receive');
$payment->prepare([
'order' => rand(),
'user' => rand(10,1000),
'mobile_number' => '123456789',
'email' => 'paytmtest@gmail.com',
'amount' => $request->amount,
'callback_url' => route('paytm.callback'),
]);
return $payment->receive();
}
/**
* Obtain the payment information.
*
* @return Object
*/
public function paytmCallback()
{
$transaction = PaytmWallet::with('receive');
$response = $transaction->response(); // To get raw response as array
//Check out response parameters sent by paytm here -> http://paywithpaytm.com/developer/paytm_api_doc?target=interpreting-response-sent-by-paytm
if($transaction->isSuccessful()){
//Transaction Successful
return view('paytm.paytm-success-page');
}else if($transaction->isFailed()){
//Transaction Failed
return view('paytm.paytm-fail');
}else if($transaction->isOpen()){
//Transaction Open/Processing
return view('paytm.paytm-fail');
}
$transaction->getResponseMessage(); //Get Response Message If Available
//get important parameters via public methods
$transaction->getOrderId(); // Get order id
$transaction->getTransactionId(); // Get transaction id
}
/**
* Paytm Payment Page
*
* @return Object
*/
public function paytmPurchase()
{
return view('paytm.payment-page');
}
}
Step – 8 Add the View File
create one folder paytm inside your resources/views folder now under the paytm folder we will create three blade file.
1. payment-page.blade.php
Now add the below code in that 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>Laravel 8 - Paytm Payment Gateway Integration</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</head>
<body>
<div id="app">
<style>
.mt2{
margin-top: 2%;
}
</style>
<main class="py-4">
<div class="container">
<div class="row">
<div class="col-md-6 offset-3 col-md-offset-6">
<div class="card card-default">
<div class="card-body text-center">
<div class="card-header">
Please enter the amount for which you want to make payment
</div>
<label class="form-control">Enter Amount for Paytm Payment Demo</label>'
<form method="post" action="{{route('paytm.payment')}}">
@csrf
<input type="text" name="amount" placeholder="RS 10" class="form-control"/>
<button type="submit" class="btn btn-primary mt2">Pay</button>
</form>
</div>
</div>
</div>
</div>
</div>
</main>
</div>
</body>
</html>
2. paytm-success-page.blade.php
<div class="alert alert-success alert-dismissible fade" role="alert">
<strong>Payment Has been Successfully Received</strong>
</div>
<a href="{{route('paytm.purchase')}}">Check the demo again</a>
3. paytm-fail.blade.php
<div class="alert alert-success alert-dismissible fade" role="alert">
<strong>Your Payment Has been failed</strong>
</div>
<a href="{{route('paytm.purchase')}}">Check the demo again</a>
Step : 9 Test the integration
Now go to your browser and visit http://localhost/laravel-demo/public/paytm-purchase. then you have something like the below image
Enter the amount and Proceed then you have something like the below image.
Enter the below card details and click on pay
card number - 4242 4242 4242 4242
card expiry - 12/24
cvv - 123
then you have something like the below image now click on the successful button.
on the successful payment, you have the message like the below one
Now login to you developer account and check payment is received or not.
Read Also: Stripe Payment Gateway in Laravel 8
Read Also: Razorpay Payment Gateway In Laravel 8
If you like the above blog please give me 5 star so that I can keep posting such a blog.
Now go to app/Http/Middleware and add the below URL to your protected $except Array.
please add proper middleware you missed the middle file name then how can user predict in which middleware file add the code
Thanks, we have updated our blog now.