Table of Contents
PhonePe is a widely used payment gateway in India that supports UPI transactions. Integrating PhonePe into your A PHP application can streamline the payment process for your users. This guide will walk you through the process step-by-step.
Merchant ID
and Salt Key
).If you do not have a merchant account, register Register Account Here.
Merchant ID
and Salt Key
.$saltKey = 'YOUR_API_KEY'; // Replace with your key $merchantId = 'MERCHANT_ID'; // Replace with your merchant id $base_url = 'http://example.com/'; // Replace with your url $saltIndex = '1'; // Replace your salt index
$payLoad = array( 'merchantId' => $merchantId, // Replace with your merchant ID 'merchantTransactionId' => "UniqueTransactionID", // Give Unique Transaction ID 'merchantUserId' => "M-" . uniqid(), // Replace your Unique User ID 'amount' => 10 * 100, // Amount in paisa 'redirectUrl' => $base_url . "redirect-url", // Replace Your Redirect Url 'redirectMode' => "POST", // Request in Post 'callbackUrl' => $base_url . "redirect-url", // Replace Your Redirect Url 'mobileNumber' => "9999999999", // Replace Your Mobile Number 'message' => "Sample Message", // Give A Msg or Descripation 'email' => "example@gmail.com", // Replace Your Email 'shortName' => "Your Name", // // Replace Your Name 'paymentInstrument' => array( 'type' => "PAY_PAGE", // Always PAY_PAGE ), );
$jsonencode = json_encode($payLoad); $payloadbase64 = base64_encode($jsonencode);
$payloadData = $payloadbase64 . "/pg/v1/pay" . $saltKey; $sha256 = hash("sha256", $payloadData); // Convert sha256 $checksum = $sha256 . '###' . $saltIndex; $request = json_encode(array('request' => $payloadbase64));
https://api.phonepe.com/apis/hermes/pg/v1/pay
https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay
$curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => "check-status.php", // Replace with Production or Sandbox URL CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0, CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POSTFIELDS => $request, CURLOPT_HTTPHEADER => [ "Content-Type: application/json", "X-VERIFY: " . $checksum, "accept: application/json", ], ]); $response = curl_exec($curl); $err = curl_error($curl); curl_close($curl); if ($err) { echo "cURL Error #:" . $err; } else { $res = json_decode($response); if (isset($res->success) && $res->success == '1') { $payUrl = $res->data->instrumentResponse->redirectInfo->url; header('Location:' . $payUrl); } }
$saltKey = "Give Here Your Salt Key"; $saltIndex = "Give Here Your Salt Index"; if (isset($_POST['merchantId']) && isset($_POST['transactionId'])) { $merchantId = $_POST["merchantId"]; $transcationId = $_POST["transactionId"]; } $st = "/pg/v1/status/" . $merchantId . "/" . $transcationId . $saltKey; // $dataSha256 = hash("sha256", $st); $checksum = $dataSha256 . "###" . $saltIndex;
$headers = array( "Content-Type: application/json", "accept: application/json", "X-VERIFY: " . $checksum, "X-MERCHANT-ID:" . $merchantId, ); $url = "" . $merchantId . "/" . $transactionId; $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); // here url curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET'); // GET Request curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($curl); curl_close($curl); $responsePayment = json_decode($response, true); print_r($responsePayment); // Print the response, then send the response data in our success URL
Techthaastu Support Team 1
Typically replies within an hour
Techthaastu Support Team 2
Typically replies within an hour