In this tutorial, we will tell you how to integrate the Paytm payment gateway using the Laravel Framework (Laravel 6 Paytm payment gateway integration example).

The Paytm payment gateway is a popular payment gateway method and it is easily used for the project. so many developers prefer that payment gateway method.

Paytm is a secure payment method and it’s work-based using anandsiddharth/laravel-paytm-wallet package. so let’s start In this article using how to integrate the Paytm payment gateway in a simple and easy way.

Overview

Step 1: Install Laravel 6

Step 2: Setting Database Configuration

Step 3: Create Table using migration

Step 4: Install Package

Step 5: Add providers and aliases

Step 6: Configuration of API Key

Step 7: Create Route

Step 8: Create a Model and Controller

Step 9: Create Blade File

Laravel Paytm Payment Gateway Integration Example

Step 1: Install Laravel 6

We are going to install laravel 6, so first open the command prompt or terminal and go to go to xampp htdocs folder directory using the command prompt. after then run the below command.

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

Step 2: Setting Database Configuration

After the complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name(laravel6_paytm)
DB_USERNAME=Enter_Your_Database_Username(root)
DB_PASSWORD=Enter_Your_Database_Password(root)

Step 3: Create Table using migration

Now, We need to create a migration. so we will below command using create the products table migration.

php artisan make:migration create_products_table --create=events

After complete migration. we need below changes in the database/migrations/create_events_table file.

<?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'); 
		} 
	} 
?>

Run the below command. after the changes above file.

php artisan migrate

Step 4: Install Package

Now, we are going to install the Paytm package using the below command.

composer require anandsiddharth/laravel-paytm-wallet

Step 5: Add providers and aliases

We will add below providers and aliases in the “config/app.php” file

'providers' => [
 
	....
 
	Anand\LaravelPaytmWallet\PaytmWalletServiceProvider::class,
 
],
 
'aliases' => [
 
	....
 
	'PaytmWallet' => Anand\LaravelPaytmWallet\Facades\PaytmWallet::class,
 
]

Step 6: Configuration of API Key

Now, we are going to the configuration of the API key in the app/config/services.php file.

'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',
 ],

Step 7: Create Route

Add the following route code in the “routes/web.php” file.

<?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| 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('/', function () {
    return view('welcome');
});

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

Step 8: Create a Model and Controller

Here below command help to create the controller and model.

php artisan make:controller EventController --resource --model=Event

Event.php

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

EventController.php

<?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');
        }
    }    
}
?>

Step 9: Create Blade File

Finally, We will create event.blade.php file in the “resources/views/” folder directory and paste below code.

event.blade.php

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
  <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="add_note">
			{{ csrf_field() }}
			<div class="row">
				<div class="col-md-12">
					<div class="form-group">
						<strong>Name</strong>
						<input class="form-control" name="name" type="text" placeholder="Enter Name" />
					</div>
				</div>
				<div class="col-md-12">
					<div class="form-group">
						<strong>Mobile Number</strong>
						<input class="form-control" name="mobile_number" type="text" placeholder="Enter Mobile Number" />
					</div>
				</div>
				<div class="col-md-12">
					<div class="form-group">
						<strong>Email Id</strong>
						<input class="form-control" name="email" type="text" placeholder="Enter Email id" />
					</div>
				</div>
				<div class="col-md-12">
					<div class="form-group">
						<strong>Event Fees</strong>
						<input class="form-control" name="amount" readonly="readonly" type="text" value="100" placeholder="" /></div>
					</div>
				<div class="col-md-12">
					<button class="btn btn-primary" type="submit">Submit</button></div>
				</div>
			</div>
		</form>
	</div>
</body>
</html>

We can start the server and run this example using the below command.

php artisan serve

Now we will run our example using the below Url in the browser.

http://127.0.0.1:8000/event

Testing Card Credential

Card No : 4242424242424242

Month : any future month

Year : any future Year

CVV : 123

Password : 123123