In this post, I am going to explain “How to Integrate Paytm Payment Gateway in Web”.
Paytm is the most popular mobile wallet and payment method used in India. As we know today market, all the things digital and as specially for India. India become digital india.Paytm is the most popular E commerce and payment tool in India.
There is the official documentation about integrating Paytm but that is very confusing if you are newbie.
Here I will show you step by step explanation “How to Integrate Paytm Payment Gateway”.
Step 1:- Install laravel using below command
1 2 3 | composer create-project --prefer-dist laravel/laravel PaytmIntegration_Laravel |
Step 2:- In this step we need to install required packages anandsiddharth/laravel-paytm-wallet package for paytm developer api so open your terminal and fire bellow command:
1 2 3 | composer require anandsiddharth/laravel-paytm-wallet |
After install package open config/app.php file and add service provider.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 'providers' => [ .... Anand\LaravelPaytmWallet\PaytmWalletServiceProvider::class, ], 'aliases' => [ .... 'PaytmWallet' => Anand\LaravelPaytmWallet\Facades\PaytmWallet::class, ], |
After that add configuration of API keys in service.php file
1 2 3 4 5 6 7 8 9 10 | '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'), ], |
Step 3:- In this step we need to add the variable in .env file, So let’s open .env file and add the variable.
1 2 3 4 5 6 7 | YOUR_MERCHANT_ID="enter your merchant id" YOUR_MERCHANT_KEY="enter your merchant key" YOUR_WEBSITE="your website" YOUR_CHANNEL=WEB YOUR_INDUSTRY_TYPE=Retail |
Step 4:- Create event registration table and model
1 2 3 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | <?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('desc'); $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 the below migration command
1 2 3 | php artisan migrate |
Step 5:- Now create the event registration model file, So let’s create new file in app/EventRegistration.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php namespace App; use Illuminate\Database\Eloquent\Model; class EventRegistration extends Model { protected $table = 'event_registration'; protected $fillable = ['name','mobile_no','desc','status','fee','order_id','transaction_id']; /* status : 0 => progress, 1 => Fail, 2 => Successful */ } |
Step 6:- Now create the web and API routes in web.php and api.php
web.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <?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('eventRegistration', 'OrderController@register'); Route::post('paytmPayment', 'OrderController@order'); |
api.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <?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 7:- Now we need to create the controller file, So let’s create the controller file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | <?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('payment_form'); } /** * 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', 'desc' => '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 8:- Now create the view file with payment form payment_form.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <!DOCTYPE html> <html> <head> <title>Laravel 5.8 - 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.8 - Payment gateway using Paytm</h2></div> <div class="panel-body"> <form action="{{ url('paytmPay') }}" 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>Message:</strong> <textarea class="form-control" placeholder="Message" name="desc"></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 by Paytm</button> </div> </div> </form> </div> </div> </div> </body> </html> |
Now we are ready to run example, So just run below command
1 2 3 | php artisan serve |
I hope you might have understood it properly, keep reading our other blogs posts for more coding tricks.
Thanks!!
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.
Leave a Reply