πŸš€ DevOps & SRE Certification Program πŸ“… Starting: 1st of Every Month 🀝 +91 8409492687 πŸ” Contact@DevOpsSchool.com

Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Full Working Example β€” Paytm Integration in Laravel

I’ll give you a full working Paytm Payment Gateway integration example in Laravel β€” step-by-step, including payment page, initiate payment, callback handling, and success/failure handling.


πŸš€ Full Working Example β€” Paytm Integration in Laravel (Latest)

1. Install Laravel Paytm Package

First, install a Paytm SDK/library.
I recommend using "anandsiddharth/laravel-paytm-wallet" (easy and popular):

composer require anandsiddharth/laravel-paytm-wallet

2. Configure Paytm Keys in .env

Add your Paytm credentials in your .env file:

PAYTM_MERCHANT_ID=YourMerchantID
PAYTM_MERCHANT_KEY=YourMerchantKey
PAYTM_ENVIRONMENT=local  # use 'production' for live
PAYTM_MERCHANT_WEBSITE=WEBSTAGING
PAYTM_CHANNEL=WEB
PAYTM_INDUSTRY_TYPE=Retail

3. Create Paytm Configuration File

Publish the config if needed:

php artisan vendor:publish --provider="Anand\LaravelPaytmWallet\PaytmWalletServiceProvider"

It will create a config/paytm-wallet.php file.

Check it and ensure environment variables are correctly mapped.


4. Create Paytm Controller

Now, create a controller to handle payment:

php artisan make:controller PaytmPaymentController

Now edit app/Http/Controllers/PaytmPaymentController.php:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Anand\LaravelPaytmWallet\Facades\PaytmWallet;

class PaytmPaymentController extends Controller
{
    public function initiatePayment()
    {
        return view('paytm-payment');
    }

    public function pay(Request $request)
    {
        $payment = PaytmWallet::with('receive');

        $payment->prepare([
            'order' => 'ORDER_' . uniqid(),
            'user' => auth()->id() ?? 1, // or your user id
            'mobile_number' => '7777777777', // dummy or real
            'email' => 'test@example.com', // dummy or real
            'amount' => $request->amount, // dynamic
            'callback_url' => route('paytm.callback')
        ]);

        return $payment->receive(); // Redirect to Paytm payment page
    }

    public function callback()
    {
        $transaction = PaytmWallet::with('receive');
        $response = $transaction->response(); // Response from Paytm

        if ($transaction->isSuccessful()) {
            // Payment Successful
            return redirect()->route('payment.success')->with('message', 'Payment Successful');
        } elseif ($transaction->isFailed()) {
            // Payment Failed
            return redirect()->route('payment.failed')->with('message', 'Payment Failed');
        }
    }

    public function success()
    {
        return view('payment-success');
    }

    public function failed()
    {
        return view('payment-failed');
    }
}

5. Create Routes in web.php

Add these routes:

use App\Http\Controllers\PaytmPaymentController;

Route::get('/paytm-payment', [PaytmPaymentController::class, 'initiatePayment'])->name('paytm.initiate');
Route::post('/paytm-payment', [PaytmPaymentController::class, 'pay'])->name('paytm.pay');
Route::post('/paytm-callback', [PaytmPaymentController::class, 'callback'])->name('paytm.callback');

Route::get('/payment-success', [PaytmPaymentController::class, 'success'])->name('payment.success');
Route::get('/payment-failed', [PaytmPaymentController::class, 'failed'])->name('payment.failed');

6. Exclude Callback from CSRF (Important!)

In app/Http/Middleware/VerifyCsrfToken.php, add:

protected $except = [
    '/paytm-callback',
];

βœ… This fixes the 419 error you were facing.


7. Create Blade View for Payment Page

Create a blade file: resources/views/paytm-payment.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Paytm Payment Gateway</title>
</head>
<body>

<h2>Paytm Payment</h2>

<form method="POST" action="{{ route('paytm.pay') }}">
    @csrf
    <label>Amount:</label>
    <input type="text" name="amount" value="100" required>
    <br><br>
    <button type="submit">Pay Now</button>
</form>

</body>
</html>

8. Create Success and Failure Pages

resources/views/payment-success.blade.php

<h1>Payment Successful!</h1>
<p>{{ session('message') }}</p>

resources/views/payment-failed.blade.php

<h1>Payment Failed!</h1>
<p>{{ session('message') }}</p>

πŸ“‹ Full Flow

StepDescription
/paytm-paymentUser visits payment page and enters amount.
/paytm-payment (POST)User submits, redirected to Paytm payment page.
Paytm Payment PageUser makes payment (success/failure).
/paytm-callback (POST)Paytm server sends callback after payment.
Success or Failure pageUser redirected accordingly.

🎯 Summary

ItemStatus
Paytm credentials loaded from .env βœ…
Laravel Paytm package installed βœ…
CSRF excluded for callback βœ…
Payment form, controller, routes done βœ…
User sees success/failure after payment βœ…

πŸ“Œ Bonus Tips

  • Always log the Paytm callback response for debugging.
  • Validate Paytm signature (CHECKSUMHASH) if needed (package does internally).
  • Use unique order IDs every time.
  • Move Paytm keys into a secure vault if in production.

🎯 Quick Commands for Copying

composer require anandsiddharth/laravel-paytm-wallet
php artisan make:controller PaytmPaymentController

Subscribe
Notify of
guest


0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x