In this tutorial you will learn about the Laravel 7/6 Paytm Payment Gateway Integration and its application with practical example.
In this Laravel 7/6 Paytm Payment Gateway Integration Example tutorial, I’ll show you how to integrate Paytm payment gateway in laravel 7/6. In this tutorial you will learn to integrate Paytm in laravel 7/6. In this step by step tutorial I’ll share laravel 7/6 Paytm integration example.
Paytm Payment Gateway
Paytm is one of the popular payment gateway, that allows us to accept payment from your customer. Paytm is very simple, hassle free and easy to integrate payment gateway. Integrating Paytm payment gateway in laravel 7/6 is a breeze.
Laravel 7/6 Paytm Payment Gateway Integration
In this tutorial, you will learn to integrate Paytm payment gateway in your laravel 7/6 project. Follow this step by step tutorial to learn Paytm payment gateway integration in laravel 7/6.
- Install laravel 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 Fresh Project
First of all we need to create a fresh laravel project, download and install Laravel using the below command
1 |
composer create-project --prefer-dist laravel/laravel blog |
Configuration in .env
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
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
Now, we need to install Anandsiddharth paytm package in our project. Open your terminal and switch to project directory and use the following composer command to install Anandsiddharth paytm package.
1 |
composer require anandsiddharth/laravel-paytm-wallet |
After installing the package, we need to register the provider and alias. Go to the app/config/app.php and put the below lines here :
1 2 3 4 5 6 7 8 |
'providers' => [ // Other service providers… Anand\LaravelPaytmWallet\PaytmWalletServiceProvider::class, ], 'aliases' => [ // Other aliases 'PaytmWallet' => Anand\LaravelPaytmWallet\Facades\PaytmWallet::class, ], |
Now we need to set a paytm credential as following. Go to the app/config/services.php and set the paytm credential
1 2 3 4 5 6 7 8 |
'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', ], |
Generate Migration and Create Model
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Event -m |
The above command will create a model name Event and also create a migration file for the events table. After that go to database/migrations file and put the below here :
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 |
<?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'); } } |
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan migrate |
Now, add the fillable property inside the Event.php file.
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 |
<?php Route::get('event', 'EventController@bookEvent'); Route::post('payment', 'EventController@eventOrderGen'); Route::post('payment/status', 'EventController@paymentCallback'); |
Create Controller
Now, lets create a controller named EventController using command given below –
1 |
php artisan make:controller EventController |
Go to app/HTTP/Controller/EventController and put the below code :
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 |
<?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
Now, we need to create blade views file, Go to app/resources/views/ and create one file name event.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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
<!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</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="{{ url('payment') }}" method="POST" name="paytm"> {{ 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
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
http://localhost:8000/event
1 2 3 |
Or direct hit in your browser http://localhost/blog/public/event |
Testing Card Credential
1 2 3 4 5 |
Card No : 4242424242424242 Month : any future month Year : any future Year CVV : 123 Password : 123123 |