Laravel

Paytm payment gateway integration with laravel 7

Hello, everyone today I will tell how to integrate the Paytm payment gateway in your laravel project. It’s so simple and easy to integrate.

Requirments

So, Let’s get started just follow the step and you will reach your destination.

Step – 1 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 by using below command

 composer create-project --prefer-dist laravel/laravel paytm

Step – 3 Install Paytm by using composer

composer require anandsiddharth/laravel-paytm-wallet

Step – 4 You have to register your laravel-paytm-wallet package to do so, you have to add the following lines under config/app.php in your provider Array.

'providers' => [
    // Other service providers...
    Anand\LaravelPaytmWallet\PaytmWalletServiceProvider::class,
],

Step – 5 Also, add the PaytmWallet facade to the aliases array in your app configuration file:

'aliases' => [
    // Other aliases
    'PaytmWallet' => Anand\LaravelPaytmWallet\Facades\PaytmWallet::class,
],

Step – 6 Add the Paytm credentials to the .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

Step – 7 Go to config->services.php and the below code

 '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 – 8 Create controller by using the following command

Php artisan make:controller PaytmController

Step – 9 Create two routes one for payment and one for your call back URL.

Route::get('/payment','PaytmController@pay');
Route::post('/payment/status', 'PaytmController@paymentCallback');

Important: The callback_url must not be CSRF protected

To disable CSRF in laravel for specific route, just to this small thing go to your Middleware/VerifyCsrfToken.php. Under protected $except array add your route like below

 /**
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [
        '/payment/status*',
    ];

Step – 10 Write the given code under your PaytmController

<?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 pay() {

        $payment = PaytmWallet::with('receive');

        $payment->prepare([
          'order' => 23, // your order id taken from cart
          'user' => 'Cust_id_12', // your user id
          'mobile_number' => 7277407765, // your customer mobile no
          'email' => 'hungerforcode@gmail.com', // your user email address
          'amount' => 20.00, // amount will be paid in INR.
          'callback_url' => 'http://localhost/paytm/public/payment/status' // callback URL
        ]);
        
        return $payment->receive();
    }

    /**
     * Obtain the payment information.
     *
     * @return Object
     */
    public function paymentCallback()
    {   
        $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()){

        }else if($transaction->isFailed()){
          //Transaction Failed
        }else if($transaction->isOpen()){
          //Transaction Open/Processing
        }
        $transaction->getResponseMessage(); //Get Response Message If Available
        //get important parameters via public methods
        $transaction->getOrderId(); // Get order id

        $transaction->getTransactionId(); // Get transaction id
    }    
}

Step – 11 Now visit the URL

 http://localhost/paytm/public/payment 

Step – 12 After visiting the URL you will be redirected to the following page

Step – 13 Make payment by entering the following card details.

card no - 4242424242424242
expiry - 12/24
cvv - 123

Your transaction will be shown under your Dashboard -> Transaction section.

paytm-dashboard
Paytm Dashboard

Getting Unusual Error try to run below command

php artisan optimize:clear
composer dump-autoload

I hope the given blog post help’s you regarding Paytm payment gateway integration. Facing while issue while integration, Feel free to comment.

Video tutorial is in Hindi

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button