PhonePe Payment Gateway Example PHP

  • 03-10-2023
  • 7447
  • PHP
  • Haresh Chauhan

Watch Youtube Video (HINDI) :

PhonePe Payment Gateway Example! Today In this post we will integrate the Phonepe payment gateway into out php application. This tutorial is also useful for the php framework like laravel and many more php frameworks.

Phonepe India's larget payment collection platform accepts payments from customers to merchants. Nowadays Phonepe is also popular for integrating online platform websites like ecommerce, and other paid service-related websites.

Php Phonepe integrate in this comprehensive example I will integrate Phonepe payment in our php application with a live example. I also made a YouTube video on Phonepe integration in the description, you can also watch and follow the video.

Integrating PhonePe payment into your PHP website or application involves using PhonePe's APIs. PhonePe provides APIs that allow you to initiate and process payments. Keep in mind that API endpoints and documentation may have changed since then, so it's essential to refer to the latest documentation provided by PhonePe.

Here are the general steps to integrate PhonePe payment into your PHP application:
  • Create a Business Account with PhonePe
  • Generate API Keys
  • Use Below php code
  • Make API Requests
  • Handle the Payment Response
  • Test the Integration

Step 1. Initialize Payment

Generated payemnt initialize phonepe payment request, Just copy this code and and paste in your php application. I made a data payload array that i will pass to the phonepe platform. I also puted there testing credential. here nothing to do just paste this code snippet to your app.

You can make dynamic user data like mobile and merchant transaction IDs according to your requirement format. In the payload, you need to paste the Redirect URL as well as callback with the specified the method to response method handle.

All the Phonepe core php code descriptions are below given. You can more about phone payments on the phone API documentation portal. This is a quick integration example.

index.php
<?php

$jayParsedAry = [
    "merchantId" => 'MERCHANTUAT', // <THIS IS TESTING MERCHANT ID>
    "merchantTransactionId" => rand(111111,999999),
    "merchantUserId" => 'MUID' . time(),
    "amount" => (1 * 100),
    "redirectUrl" =>  '<YOUR_SITE_REDIRECT_URL>',
    "redirectMode" => "POST" // GET, POST DEFINE REDIRECT RESPONSE METHOD,
    "redirectUrl" =>  '<YOUR_SITE_CALLBACK_URL>',
    "mobileNumber" => "<YOUT MOBILE NUMBER>",
    "paymentInstrument" => [
        "type" => "PAY_PAGE"
    ]
];

$encode = json_encode($jayParsedAry);
$encoded = base64_encode($encode);
$key = '099eb0cd-02cf-4e2a-8aca-3e6c6aff0399'; // KEY
$key_index = 1; // KEY_INDEX
$string = $encoded . "/pg/v1/pay".$key;
$sha256 = hash("sha256", $string);
$final_x_header = $sha256 . '###'.$key_index;

// $url = "https://api.phonepe.com/apis/hermes/pg/v1/pay"; <PRODUCTION URL>

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

$headers = array(
    "Content-Type: application/json",
    "accept: application/json",
    "X-VERIFY: " . $final_x_header,
);

$data = json_encode(['request' => $encoded]);

$curl = curl_init($url);

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);

$resp = curl_exec($curl);

curl_close($curl);

$response = json_decode($resp);

header('Location:' . $response->data->instrumentResponse->redirectInfo->url);

Step 2. Handle Payment Response

We will handle the response after phone response back to your origin site.

We will find the order payment status via merchant id & merchant transaction id.

Here below given status check API example using curl very simple way.

After the response from phone pay, you will redirect the user base on payment "SUCCESS" & "FAILED" response bases. That's it.

response.php
<?php

$key = '099eb0cd-02cf-4e2a-8aca-3e6c6aff0399'; // KEY
$key_index = 1; // KEY_INDEX

$response = $_POST; // FETCH DATA FROM DEFINE METHOD, IN THIS EXAMPLE I AM DEFINING POST WHILE I AM SENDING REQUEST

$final_x_header = hash("sha256", "/pg/v1/status/" . $response['merchantId'] . "/" . $response['transactionId'] . $key_index) . "###" . $key;

$url = "https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/status/".$response['merchantId']."/".$response['transactionId']; // <TESTING URL>

$headers = array(
    "Content-Type: application/json",
    "accept: application/json",
    "X-VERIFY: " . $final_x_header,
    "X-MERCHANT-ID:". $response['merchantId']
);

$curl = curl_init($url);

curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

$resp = curl_exec($curl);

curl_close($curl);

$responsePayment = json_decode($resp, true);
// HANDLE YOUR PHONEPAY RESPONSE

I hope you like this quick Phonepe payment gateway integration tutorial in the PHP application. I explained simply. In this payment code testing credentials are used. After successfully integration your your php application switch your app to live or production credential, collection payment.


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 :