Laravel Paytm Payment Gateway Integration Example
Laravel 17-Aug-2020

Laravel Paytm Payment Gateway Integration Example

How to integrate paytm payment gateway in laravel 5.7 application. In this tutorial, we’ll discuss step by step how to integrate paytm payment gateway in our laravel 5.7 App using anandsiddharth/laravel-paytm-wallet package. In this example we will integrate paytm payment gateway in simple and easy way.

And this paytm payment gateway example also work with laravel 5.8 version.

Paytm is a very popular company. Paytm provide a payment gateway in many diffrent languages like PHP, Java, .Net, Node.js, Ruby on Rails, Perl, Python, Express.

Sometimes we found invalid checksum error in paytm payment gateway sdk. We have resolved invalid checksum error in integrate paytm payment gateway.

Content

  • Install Laravel 5.7 Fresh Setup
  • Configuration .env file
  • Install Anandsiddharth paytm package
  • Generate Migration and Create Model
  • Make Route
  • Create Controller
  • Create Blade View file
  • Start Development Server
  • Conclusion

Install Laravel 5.7 Fresh Project

We need to install Laravel 5.7 fresh application using below command, Open your command prompt and run the below command :

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


After successfully install laravel 5.7 Application, Go to your project .env file and set up database credential and move next step.

Configuration in .env

In this step, we will set database credential in .env file. Let’s open .env file.

 DB_CONNECTION=mysql 
 DB_HOST=127.0.0.1 
 DB_PORT=3306 
 DB_DATABASE=your database name here
 DB_USERNAME=database username here
 DB_PASSWORD=database password here

Install Anandsiddharth paytm package

Use the below command and install the package :

composer require anandsiddharth/laravel-paytm-wallet

After successfully install package, we need to register provider and aliase. Go to the app/config/app.php and put the below lines here :

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

We need to set paytm credential like below. Go to the app/config/services.php and set the paytm credential.

'paytm-wallet' => [
     'env' => 'production', // values : (local | production)
     'merchant_id' => 'YOUR_MERCHANT_ID',
     'merchant_key' => 'YOUR_MERCHANT_KEY',
     'merchant_website' => 'YOUR_WEBSITE',
     'channel' => 'YOUR_CHANNEL',
     'industry_type' => 'YOUR_INDUSTRY_TYPE',
 ],

All the credentials mentioned are provided by Paytm after signing up as merchant.

Generate Migration and Create Model

Now we will Create table name notes and it’s migration file. use the below command :

php artisan make:model Event -m

It command will create one model name Event and also create one migration file for events table. After successfully run the command go to database/migrations file and put the below here :

<?php
use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 
    
class CreateEventsTable extends Migration 

    /** 
     * Run the migrations. 
     * 
     * @return void 
     */ 
    public function up() 
    { 
        Schema::create('products', function (Blueprint $table) { 
            $table->increments('id'); 
            $table->string('name');
            $table->string('mobile_number');
            $table->integer('amount');
            $table->string('order_id');
            $table->string('status')->default('pending');
            $table->string('transaction_id')->default(0);
            $table->timestamps(); 
        }); 
    } 
    
    /** 
     * Reverse the migrations. 
     * 
     * @return void 
     */ 
    public function down() 
    { 
        Schema::dropIfExists('events'); 
    } 

Next migrate the table using the below command.

php artisan migrate

Now, add the fillable property inside Event.php file.

<?php
  
namespace App;
  
use Illuminate\Database\Eloquent\Model;
  
class Note extends Model
{
  protected $fillable = ['name','mobile_number',
                         'amount','status',
                       'order_id','transaction_id'];
}

Make Route

<?php 
Route::get('event', 'EventController@bookEvent');
Route::post('payment', 'EventController@eventOrderGen');
Route::post('payment/status', 'EventController@paymentCallback');

Create Contoller

Create the controller name EventController using the below command.

php artisan make:controller EventController

Go to app/http/Controller/EventController and put the below code :

<?php
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use App\Event;
use PaytmWallet;
 
 
class EventController extends Controller
{
 
 
    /**
     * Redirect the user to the Payment Gateway.
     *
     * @return Response
     */
    public function bookEvent()
    {
        return view('book_event');
    }
 
 
    /**
     * Redirect the user to the Payment Gateway.
     *
     * @return Response
     */
    public function eventOrderGen(Request $request)
    {
     $this->validate($request, [
          'name' => 'required',
          'mobile_no' =>'required|numeric|digits:10|unique:events,mobile_number',
        ]);
 
        $input = $request->all();
        $input['order_id'] = rand(1111,9999);
        $input['amount'] = 50;
 
        Event::insert($input);
 
        $payment = PaytmWallet::with('receive');
        $payment->prepare([
          'order' => $input['order_id'],
          'user' => 'user id',
          'mobile_number' => $request->mobile_number,
          'email' => $request->email,
          'amount' => $input['amount'],
          'callback_url' => url('payment/status')
        ]);
        return $payment->receive();
    }
 
    /**
     * Obtain the payment information.
     *
     * @return Object
     */
    public function paymentCallback()
    {
        $transaction = PaytmWallet::with('receive');
 
        $response = $transaction->response();
 
        if($transaction->isSuccessful()){
          Event::where('order_id',$response['ORDERID'])->update(['status'=>'success', 'payment_id'=>$response['TXNID']]);
 
          dd('Payment Successfully Credited.');
 
        }else if($transaction->isFailed()){
          Event::where('order_id',$order_id)->update(['status'=>'failed', 'payment_id'=>$response['TXNID']]);
          dd('Payment Failed. Try again lator');
        }
    }    
}

Create Blade View file

We need to create blade views file, Go to app/resources/views/ and create one file name event.blade.php :

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>Paytm Payment Gateway Integrate - Tutsmake.com</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
    <style>
        .mt40{
            margin-top: 40px;
        }
    </style>
</head>
<body>
   
<div class="container">
 
<div class="row">
    <div class="col-lg-12 mt40">
        <div class="card-header" style="background: #0275D8;">
            <h2>Register for Event</h2>
        </div>
    </div>
</div>
    
@if ($errors->any())
    <div class="alert alert-danger">
        <strong>Whoops!</strong> Something went wrong<br>
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif
    
<form action="{{ route('notes.store') }}" method="POST" name="add_note">
    {{ csrf_field() }}
   
     <div class="row">
        <div class="col-md-12">
            <div class="form-group">
                <strong>Name</strong>
                <input type="text" name="name" class="form-control" placeholder="Enter Name">
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-group">
                <strong>Mobile Number</strong>
                <input type="text" name="mobile_number" class="form-control" placeholder="Enter Mobile Number">
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-group">
                <strong>Email Id</strong>
                <input type="text" name="email" class="form-control" placeholder="Enter Email id">
            </div>
        </div>
        <div class="col-md-12">
            <div class="form-group">
                <strong>Event Fees</strong>
                <input type="text" name="amount" class="form-control" placeholder="" value="100" readonly="">
            </div>
        </div>
        <div class="col-md-12">
                <button type="submit" class="btn btn-primary">Submit</button>
        </div>
    </div>
    
</form>
</div>
    
</body>
</html>

 

Start Development Server

In this step, we will use the php artisan serve command . It will start your server locally

php artisan serve

If you want to run the project diffrent port so use this below command 

php artisan serve --port=8080  

Now we are ready to run our example so run bellow command to quick run.

http://localhost:8000/event

Or direct hit in your browser

http://localhost/LaravelPaytm /public/event

If you want to remove public or public/index.php from URL In laravel, Click Me

Testing Card Credential
 Card No : 4242424242424242
 Month : any future month
 Year : any future Year
 CVV : 123
 Password : 123123

Conclusion

In this tutorial, We have successfully integrate paytm payment gateway in laravel 5.7 Application. Our examples run quickly.