Paytm payment gateway integration example in Laravel 5

By Hardik Savani | May 16, 2017 | | 6950 Viewer | Category : PHP Laravel Bootstrap


In this tutorial, I am going to share with you How to integrate paytm payment gateway in laravel 5.4 application.

As we know today market, all the things digital and as specially for india. India become digital india. Paytm is a very popular payment and commerce company based india. They provide very easy way to shopping and recharge of mobile, D2H, electricity bill etc. So in this example we will learn how to make paytm payment integration in laravel application. This example is from scratch using "anandsiddharth/laravel-paytm-wallet" package. So let's follow bellow few step and get preview as bellow image:

Preview:

Step 1 : Install Laravel 5.4

In first step, we require to get fresh Laravel 5.4 application using bellow command, So open your terminal OR command prompt and run bellow command:

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

Step 2: Install Package

In this step we have to anandsiddharth/laravel-paytm-wallet package for paytm developer api so one your terminal and fire bellow command:

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,

],

After successfully installed package and providers and aliases, we require to add configuration of api key and id as like bellow file:

config/services.php

<?php


return [


......

'paytm-wallet' => [

'env' => 'local', // values : (local | production)

'merchant_id' => env('YOUR_MERCHANT_ID'),

'merchant_key' => env('YOUR_MERCHANT_KEY'),

'merchant_website' => env('YOUR_WEBSITE'),

'channel' => env('YOUR_CHANNEL'),

'industry_type' => env('YOUR_INDUSTRY_TYPE'),

],


];

Now, we have to add that variable on .env file with following value. Now i set testing paytm details:

.env

YOUR_MERCHANT_ID=DIY12386817555501617

YOUR_MERCHANT_KEY=bKMfNxPPf_QdZppa

YOUR_WEBSITE=DIYtestingweb

YOUR_CHANNEL=WEB

YOUR_INDUSTRY_TYPE=Retail

Step 3: Create Event Registration Table and Model

In this step we have to create migration for event_registration table using Laravel 5.4 php artisan command, so first fire bellow command:

php artisan make:migration create_event_registration_table

After this command you will find one file in following path database/migrations and you have to put bellow code in your migration file for create categories table.

<?php


use Illuminate\Support\Facades\Schema;

use Illuminate\Database\Schema\Blueprint;

use Illuminate\Database\Migrations\Migration;


class CreateEventRegisterTable extends Migration

{

/**

* Run the migrations.

*

* @return void

*/

public function up()

{

Schema::create('event_registration', function (Blueprint $table) {

$table->increments('id');

$table->string('name');

$table->string('mobile_no');

$table->text('address');

$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::drop("event_registration");

}

}

Now run bellow command for migration:

php artisan migrate

After create "categories" table you should create EventRegistration model for categories, so first create file in this path app/EventRegistration.php and put bellow content in item.php file:

app/EventRegistration.php

<?php


namespace App;


use Illuminate\Database\Eloquent\Model;


class EventRegistration extends Model

{

protected $table = 'event_registration';


protected $fillable = ['name','mobile_no','address','status','fee','order_id','transaction_id'];


/*

status : 0 => progress, 1 => Fail, 2 => Successful

*/

}

Step 4: Create Web and API Route

In this step, we will create web and api routes. Laravel provide web.php and api.php file for write web services route. So, let's add new route on that file.

routes/web.php

<?php


/*

|--------------------------------------------------------------------------

| 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::get('event-registration', 'OrderController@register');

Route::post('payment', 'OrderController@order');

routes/api.php

<?php


/*

|--------------------------------------------------------------------------

| API Routes

|--------------------------------------------------------------------------

|

| Here is where you can register API routes for your application. These

| routes are loaded by the RouteServiceProvider within a group which

| is assigned the "api" middleware group. Enjoy building your API!

|

*/


Route::post('payment/status', 'OrderController@paymentCallback');

Step 5: Create Controller

In last step we have to create new controller and their methods, So let's create OrderController and put bellow code:

app/Http/Controllers/OrderController.php

<?php


namespace App\Http\Controllers;


use PaytmWallet;

use Illuminate\Http\Request;

use App\EventRegistration;


class OrderController extends Controller

{


/**

* Redirect the user to the Payment Gateway.

*

* @return Response

*/

public function register()

{

return view('register');

}


/**

* Redirect the user to the Payment Gateway.

*

* @return Response

*/

public function order(Request $request)

{


$this->validate($request, [

'name' => 'required',

'mobile_no' => 'required|numeric|digits:10|unique:event_registration,mobile_no',

'address' => 'required',

]);


$input = $request->all();

$input['order_id'] = $request->mobile_no.rand(1,100);

$input['fee'] = 50;


EventRegistration::create($input);


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

$payment->prepare([

'order' => $input['order_id'],

'user' => 'your paytm user',

'mobile_number' => 'your paytm number',

'email' => 'your paytm email',

'amount' => $input['fee'],

'callback_url' => url('api/payment/status')

]);

return $payment->receive();

}


/**

* Obtain the payment information.

*

* @return Object

*/

public function paymentCallback()

{

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


$response = $transaction->response();

$order_id = $transaction->getOrderId();


if($transaction->isSuccessful()){

EventRegistration::where('order_id',$order_id)->update(['status'=>2, 'transaction_id'=>$transaction->getTransactionId()]);


dd('Payment Successfully Paid.');

}else if($transaction->isFailed()){

EventRegistration::where('order_id',$order_id)->update(['status'=>1, 'transaction_id'=>$transaction->getTransactionId()]);

dd('Payment Failed.');

}

}

}

Step 6: Create View Files

In this step, we will create register blade file and we create form with name, mobile number and address details. So, let's create register.blade.php file and put bellow code:

application/views/register.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 5.4 - Payment gateway using Paytm</title>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>


<div class="container">

<div class="panel panel-primary" style="margin-top:30px;">

<div class="panel-heading"><h2 class="text-center">Laravel 5.4 - Payment gateway using Paytm</h2></div>

<div class="panel-body">

<form action="{{ url('payment') }}" class="form-image-upload" method="POST" enctype="multipart/form-data">


{!! csrf_field() !!}


@if (count($errors) > 0)

<div class="alert alert-danger">

<strong>Whoops!</strong> There were some problems with your input.<br><br>

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif


<div class="row">

<div class="col-md-12">

<strong>Name:</strong>

<input type="text" name="name" class="form-control" placeholder="Name">

</div>

<div class="col-md-12">

<strong>Mobile No:</strong>

<input type="text" name="mobile_no" class="form-control" placeholder="Mobile No.">

</div>

<div class="col-md-12">

<strong>Address:</strong>

<textarea class="form-control" placeholder="Address" name="address"></textarea>

</div>

<div class="col-md-12">

<br/>

<div class="btn btn-info btn-block">

Event Fee : 50 Rs/-

</div>

</div>

<div class="col-md-12">

<br/>

<button type="submit" class="btn btn-success btn-block">Pay to PatTM</button>

</div>

</div>


</form>

</div>

</div>

</div>


</body>

</html>

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

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/event-registration

I hope it can help you



We are Recommending you: