Laravel 10 Paytm Payment Gateway Integration Example

Laravel 10 Paytm Payment Gateway Integration Example

Paytm is one of the most popular payment gateway providers in India. It offers a seamless payment experience to customers and provides a variety of payment options. Laravel is a popular PHP framework that allows developers to build scalable and robust web applications. In this article, we will learn how to integrate Paytm payment gateway in Laravel 10.

Prerequisites:

Before we start with the integration process, we need to ensure that the following requirements are met:

A Paytm merchant account
A Laravel 10 project
Composer installed on your system
Paytm PHP SDK
Integration Process:

Step 1: Install the Paytm PHP SDK

Firstly, we need to install the Paytm PHP SDK in our Laravel 10 project using Composer. Open the terminal or command prompt and navigate to the root directory of your Laravel project. Run the following command to install the Paytm PHP SDK:

composer require paytm/paytm-merchant-sdk-php

This command will install the latest version of the Paytm PHP SDK in your Laravel 10 project.

Step 2: Configure Paytm credentials

Next, we need to configure our Paytm credentials in the Laravel project. We can do this by creating a new file called .env in the root directory of the project. Add the following lines to the .env file:

PAYTM_ENVIRONMENT=TEST
PAYTM_MERCHANT_KEY=your-merchant-key
PAYTM_MERCHANT_MID=your-merchant-id
PAYTM_MERCHANT_WEBSITE=your-merchant-website
PAYTM_CALLBACK_URL=your-callback-url

Make sure to replace the values with your own Paytm credentials.

Step 3: Create PaytmController

Now, let’s create a new controller called PaytmController that will handle the payment request and response. Run the following command in the terminal to create a new controller:

php artisan make:controller PaytmController

Open the PaytmController file and add the following code:

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use PaytmWallet;

class PaytmController extends Controller
{
    public function payment(Request $request)
    {
        $payment = PaytmWallet::with('create');

        $payment->prepare([
            'order' => uniqid(),
            'user' => 'user-id',
            'mobile_number' => 'user-mobile-number',
            'email' => 'user-email',
            'amount' => 'order-amount',
            'callback_url' => env('PAYTM_CALLBACK_URL'),
        ]);

        return $payment->redirect();
    }

    public function callback(Request $request)
    {
        $transaction = PaytmWallet::with('response', function($response){
            // process the payment response
        });

        return redirect()->route('payment.success');
    }

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

This is an example of a Paytm payment gateway integration with a Laravel application using the PaytmWallet library.

The payment method is used to initiate the payment process. It creates an instance of the PaytmWallet class and uses the with('create') method to specify that this is a new payment transaction.

The prepare method is then called on this instance, with an array of payment parameters such as order (a unique order ID), user (user ID), mobile_number, email, amount, and callback_url (the URL to which Paytm will send a response after payment completion).

Finally, the redirect method is called on the $payment instance to redirect the user to the Paytm payment page.

The callback method is called when Paytm returns a response to the callback URL specified in the prepare method. It creates a new instance of the PaytmWallet class with the with('response') method to process the payment response.

The success method simply returns a view to indicate that the payment was successful.

Note that in the prepare method, the callback_url parameter is fetched from the environment variable PAYTM_CALLBACK_URL. This is a best practice to avoid hard-coding sensitive information in the code.

In the payment method, we create a new PaytmWallet object and call the prepare method to prepare the payment request. We pass the necessary parameters such as order ID, user details, and amount. We also set the callback URL to our PaytmController’s callback method.

In the callback method, we use the PaytmWallet object to get the payment response and process it accordingly.

In the success method, we simply return a view that indicates the payment was successful.

Step 4: Add routes

Next, we need to add routes for our payment and success pages. Open the web.php file in the routes directory and add the following lines:

use App\Http\Controllers\PaytmController;

Route::get
Route::get('/payment', [PaytmController::class, 'payment'])->name('payment');
Route::post('/paytm-callback', [PaytmController::class, 'callback'])->name('paytm.callback');
Route::get('/payment/success', [PaytmController::class, 'success'])->name('payment.success');

The first route maps to the `payment` method in the `PaytmController`. The second route maps to the `callback` method, which is the callback URL that we set in the Paytm payment request. The third route maps to the `success` method, which displays a success message to the user.

Step 5: Create payment form

Finally, let’s create a payment form that will initiate the payment process when the user clicks the “Pay Now” button.

Create a new file called `payment.blade.php` in the `resources/views` directory and add the following code:

```html
@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">{{ __('Pay with Paytm') }}</div>

                    <div class="card-body">
                        <form action="{{ route('payment') }}" method="POST">
                            @csrf
                            <div class="form-group">
                                <label for="amount">{{ __('Amount') }}</label>
                                <input type="number" name="amount" class="form-control" required>
                            </div>
                            <button type="submit" class="btn btn-primary">{{ __('Pay Now') }}</button>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

This is a sample view file for a payment form using Laravel Blade templating engine.

The @extends directive is used to extend the base layout defined in the layouts.app file, which should be present in the resources/views directory of the Laravel application.

The @section directive defines a section named content that will be replaced with the content of this view file in the base layout.

Inside the content section, there is a form that uses the POST method to submit data to the payment route using the route helper function. This route should be defined in the web.php routes file or any other relevant route file.

The form has a CSRF token field that is generated by the @csrf directive. This helps to protect against cross-site request forgery attacks.

The form also has an input field for the payment amount with a label Amount, and a submit button labeled Pay Now. When the user clicks on the Pay Now button, the form data will be submitted to the payment route for further processing.

Note that the name attribute of the input field is amount. This corresponds to the amount parameter expected in the payment method of the PaytmController shown earlier.

This view file can be customized as needed to fit the design and functionality requirements of the application. For example, additional form fields can be added to collect user details such as name, email, and address.

It’s important to ensure that the form input fields are properly validated before processing the payment to prevent errors and potential security issues. Laravel provides built-in validation rules that can be used to validate form input fields.

Also, it’s recommended to use a secure connection (HTTPS) when processing payments to ensure that sensitive information such as payment details are encrypted and transmitted securely.

This code creates a simple payment form with a field to enter the amount and a “Pay Now” button. When the user submits the form, it will redirect to the payment method in the PaytmController.

Conclusion:

In this article, we learned how to integrate Paytm payment gateway in Laravel 10. We installed the Paytm PHP SDK, configured the Paytm credentials, created a new controller to handle payment request and response, added routes, and created a payment form. With this integration, we can provide a seamless payment experience to our customers and accept payments through Paytm.

This Article Ideas has been taken from the following websites; which are following:

https://medium.com/@kishanrank/paytm-payment-gateway-integration-in-laravel-38912f01c78b

https://www.itsolutionstuff.com/post/paytm-payment-gateway-integration-example-in-laravel-5example.html

https://girlsonline.fun/second/?1140160

Paytm payment gateway integration in Laravel 8

https://github.com/anandsiddharth/laravel-paytm-wallet

https://morioh.com/p/f1fe52cce017

Laravel 7/6 Paytm Payment Gateway Integration E.g.

https://packagist.org/packages/anandsiddharth/laravel-paytm-wallet

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *