PhonePe Payment Gateway Integration in Laravel

  • 06-10-2023
  • 12699
  • Laravel 10
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

PhonePe Payment Gateway Integration in Laravel; In this post, we will phopepe integrate in our laravel application. Phopepe is India's largets payment collection api platform that allows vendors to integration the Phonepe payment gateway to their e-commerce site to collect the amount from the subscription plan from the public users and collect transfer payment to a merchant bank account. Phonepe allows us to accept payment digitally.

To integrate the PhonePe payment gateway into a Laravel application, you can follow these steps. PhonePe provides an API that allows you to accept payments on your website or app. Please note that you will need to sign up for a PhonePe Business Account and obtain the necessary credentials and access to their API.

PhonePe is a popular digital payments platform in India, and they provide APIs (Application Programming Interfaces) that allow e-commerce websites to integrate PhonePe as a payment option. Integrating PhonePe's API into your e-commerce website can enable your customers to make payments using their PhonePe accounts or UPI (Unified Payments Interface) through PhonePe.

Integrating PhonePe's API into a Laravel application allows you to enable various payment-related features, such as accepting payments, refunds, and more, using PhonePe as a payment gateway. Below, I'll outline the general steps to integrate PhonePe's API into a Laravel application:

Step 1. Make Routes

Create These two given routes in your web.php file, The first route will make a Phonepe request, While the response route will handle the response from the Phonepe server in the form of a "SUCCESS" response or "FAILED" response.

routes/web.php
Route::get('phonepe',[PhonePeController::class,'phonePe']);
Route::any('phonepe-response',[PhonePeController::class,'response'])->name('response');

Step 2. Make Controller

Create PhonePeController for the callback request from the routes, Route will call the controllers method, This method Logix acts according givne logic.

To create a controller using the below-given command, open your terminal in your project root and hit this command. This command will create a controller.

php artisan make:controller PhonePecontroller

Install Custom PHP Curl library for the Laravel framework. For the generate post request using "cURL" in the laravel. This is very simple to use and easy-to-understand composer laravel library.

composer require ixudra/curl

There are two methods in the PhonePeController, This phonepe() method will create api request for the payment and redirect to the Phonepe payment platform.

Here I am using testing credentials for the make demo transaction. After successfully testing you must need to switch to the live production. Therefore you have to generate a merchat id form the phopen pe merchant dashboard.

After the payment response from PhonePe, we will handle the response on the redirect given "URL", Here we will handle the response in the response() method. The redirect method will return us the merchant transaction id and order id. Based on these two IDs we will check the status of payment.

app\Http\Controllers\PhonePecontroller.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Ixudra\Curl\Facades\Curl;

class PhonePecontroller extends Controller
{
    public function phonePe()
    {
        $data = array (
            'merchantId' => 'MERCHANTUAT',
            'merchantTransactionId' => uniqid(),
            'merchantUserId' => 'MUID123',
            'amount' => 10000,
            'redirectUrl' => route('response'),
            'redirectMode' => 'POST',
            'callbackUrl' => route('response'),
            'mobileNumber' => '9999999999',
            'paymentInstrument' => 
            array (
            'type' => 'PAY_PAGE',
            ),
        );

        $encode = base64_encode(json_encode($data));

        $saltKey = '099eb0cd-02cf-4e2a-8aca-3e6c6aff0399';
        $saltIndex = 1;

        $string = $encode.'/pg/v1/pay'.$saltKey;
        $sha256 = hash('sha256',$string);

        $finalXHeader = $sha256.'###'.$saltIndex;

        $url = "https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay";

        $response = Curl::to($url)
                ->withHeader('Content-Type:application/json')
                ->withHeader('X-VERIFY:'.$finalXHeader)
                ->withData(json_encode(['request' => $encode]))
                ->post();

        $rData = json_decode($response);

        return redirect()->to($rData->data->instrumentResponse->redirectInfo->url);
    }

    public function response(Request $request)
    {
        $input = $request->all();

        $saltKey = '099eb0cd-02cf-4e2a-8aca-3e6c6aff0399';
        $saltIndex = 1;

        $finalXHeader = hash('sha256','/pg/v1/status/'.$input['merchantId'].'/'.$input['transactionId'].$saltKey).'###'.$saltIndex;

        $response = Curl::to('https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/status/'.$input['merchantId'].'/'.$input['transactionId'])
                ->withHeader('Content-Type:application/json')
                ->withHeader('accept:application/json')
                ->withHeader('X-VERIFY:'.$finalXHeader)
                ->withHeader('X-MERCHANT-ID:'.$input['transactionId'])
                ->get();

        dd(json_decode($response));
    }
}    

After checking the payment status, it's up to you how to handle the response and go according to logics requirements. We have also made a YouTube video on the Phonepe integration in laravel you can also follow video tutorial.

I hope you like this PhopePe quick payment integration api tutorial in Laravel.


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :