Phonepay payment-geteway

How to Integrate PhonePe Payment Gateway in PHP

02 Aug, 25 | Aakash Chavhan | 250 | 0 |  2 |  0

Integrating PhonePe with your PHP-based website allows you to offer a seamless payment experience. This guide walks you through creating a payment order, redirecting the user to PhonePe, and handling the callback to confirm the transaction.

🔄 How to Integrate PhonePe Payment Gateway in PHP

Integrating PhonePe with your PHP-based website allows you to offer a seamless payment experience. This guide walks you through creating a payment order, redirecting the user to PhonePe, and handling the callback to confirm the transaction.


🛒 Step 1: Capture the Order and Redirect to Confirmation

After the customer places an order, show a confirmation page summarizing their order. Include a Submit & Pay button that, when clicked, redirects the user to checksum.php.


🧾 Step 2: Construct the Payment Payload in checksum.php

Fetch the order details and generate a unique transaction ID. Then, create the payload:

$amount = $orderAmount * 100; // Convert to paise $eventPayload = [ 'merchantId' => 'YOUR_MERCHANT_ID', 'merchantTransactionId' => uniqid('TXN'), 'merchantUserId' => 'USER123', 'amount' => $amount, 'redirectUrl' => 'https://yourdomain.com/callback.php', 'redirectMode' => 'POST', 'callbackUrl' => 'https://yourdomain.com/callback.php', 'mobileNumber' => '9999999999', 'paymentInstrument' => [ 'type' => 'PAY_PAGE', ], ]; $encodedPayload = base64_encode(json_encode($eventPayload));

🔐 Step 3: Generate X-VERIFY Header

$saltKey = 'YOUR_SALT_KEY'; $saltIndex = 1; $stringToHash = $encodedPayload . '/pg/v1/pay' . $saltKey; $checksum = hash('sha256', $stringToHash); $finalXHeader = $checksum . '###' . $saltIndex;

📡 Step 4: Send the Payment Request to PhonePe API

$headers = [ 'Content-Type: application/json', 'X-VERIFY: ' . $finalXHeader, ]; $phonePeUrl = 'https://api.phonepe.com/apis/hermes/pg/v1/pay'; // Production endpoint $data = ['request' => $encodedPayload]; $options = [ 'http' => [ 'method' => 'POST', 'content' => json_encode($data), 'header' => implode("rn", $headers), ], ]; $context = stream_context_create($options); $response = file_get_contents($phonePeUrl, false, $context); $result = json_decode($response, true); $redirectUrl = $result['data']['instrumentResponse']['redirectInfo']['url'];

💳 Step 5: Redirect to PhonePe Checkout

Embed the checkout script and initiate the transaction:

<script src="https://mercury.phonepe.com/web/bundle/checkout.js"></script> <script> var tokenUrl = '<?php echo $redirectUrl; ?>'; window.PhonePeCheckout.transact({ tokenUrl }); </script>

🔔 Step 6: Handle the Payment Callback

After payment completion, PhonePe will send a POST request to your callback.php with transaction details.

1️⃣ Receive the Callback Data

$transactionId = $_POST['transactionId'];

2️⃣ Construct the X-VERIFY Header for Payment Status Check

$gateway = (object) [ 'token' => 'YOUR_MERCHANT_ID', 'secret_key' => 'YOUR_SALT_KEY', ]; $orderId = $transactionId; $checksumString = '/pg/v1/status/' . $gateway->token . '/' . $orderId . $gateway->secret_key; $encodeIn256 = hash('sha256', $checksumString) . '###1';

3️⃣ Call the PhonePe Status API

$headers = [ 'Content-Type: application/json', 'X-MERCHANT-ID: ' . $gateway->token, 'X-VERIFY: ' . $encodeIn256, 'Accept: application/json', ]; $phonePeStatusUrl = 'https://api.phonepe.com/apis/hermes/pg/v1/status/' . $gateway->token . '/' . $orderId; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $phonePeStatusUrl); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($ch); curl_close($ch); $apiResponse = json_decode($response);

4️⃣ Process the Payment Status

Store the callback and status response for logging or debugging:

$serializedPostData = serialize($_POST); $serializedApiResponse = serialize($apiResponse);

Then, check the payment status:

if ($apiResponse->code == "PAYMENT_SUCCESS") { // ✅ Payment successful: Update order status, notify user, fulfill order. } else { // ❌ Payment failed or pending: Handle accordingly. }

Contact Us
COMMENTS

No comments