How to Integrate PhonePe Payment Gateway in PHP
Sonu Yadav
Tech Lead - Fullstack Engineer | JavaScript, React.js, Redux, NextJs, Express.js,Nest,TypeScript, Node.js, AWS, Lambda, DynamoDB, PHP, Codeigniter | Full Stack (JavaScript) Ex-RealMoney Game
Adding a new payment gateway in your website or web application. In this article, I will explain how we can integrate the phonePe payment gateway using PHP
Assuming you possess a merchant account and API credentials for your PhonePe gateway, here's a simplified example to connect it to your PHP website:
Before starting first, create your merchant account with PhonePe https://www.phonepe.com/business-solutions/payment-gateway/register/
Merchant Account Setup: Ensure you have a merchant account registered with PhonePe for processing payments.
API Credential Retrieval: Obtain the necessary API credentials, such as the Merchant ID and Secret Key, required for authenticating requests to the PhonePe API.
Integration Method Selection: Choose the appropriate integration method provided by PhonePe, which could include options like Web SDK, Custom UI Integration, or API Integration.
Replace these with your real PhonePe API credentials
$apiKey = 'YOUR_API_KEY';
$merchantId = 'YOUR_MERCHANT_ID';
$orderId = rand(1111111111,99999999999);
? ? Now you need to payment Payment request data in payload
领英推荐
$payLoad = array(
'merchantId' => $merchantid,
'merchantTransactionId' => $orderId, // test transactionID
"merchantUserId" => "M-" . uniqid(),
'amount' => $amount * 100, // phone pe works on paise
'redirectUrl' => 'www.sonnu.in/redirect-url',
'redirectMode' => "POST",
'callbackUrl' => 'www.sonnu.in/redirect-url',
"mobileNumber" => CUSTMER_MOBILE_NUMBER,
"shortName"=>"CUSTMER_NAME",
"paymentInstrument" => array(
"type" => "PAY_PAGE",
)
);
Convert the JSON Payload to Base64 Encoded Payload
? $jsonencode = json_encode($payLoad);
$payloadbase64 = base64_encode($jsonencode);
Step 4: Calculate X-Verify/Checksum header
$salt_index = 1; //key index 1
$payload = $payloadMain . "/pg/v1/pay" . $apiKey;
$sha256 = hash("sha256", $payload);
$final_x_header = $sha256 . '###' . $salt_index;
$request = json_encode(array('request'=>$payloadMain));
define("UATURLPAY", "https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay");
define("LIVEURLPAY", "https://api.phonepe.com/apis/hermes/pg/v1/pay");
define("STATUSCHECKURL", "https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/status/");
define("LIVESTATUSCHECKURL", "https://api.phonepe.com/apis/hermes/pg/v1/status/");?
?Remember to replace ‘YOUR_API_KEY’, ‘YOUR_MERCHANT_ID’, ‘YOUR_ORDER_ID’, ‘YOUR_REDIRECT_URL’, and other placeholders with your actual credentials and data.
You can find the documentation on official website: https://developer.phonepe.com/v1/reference/pay-api-1/
Do you want to get implementation help, or modify or extend the functionality of this script? contact me: +91918536154
--
3 个月In PhonePe, after 10 minutes, I want the link and QR code to expire. Is this possible? If yes, please explain how
--
6 个月Phone pay employee chore ha ??
Sr. Content Writer at Cognizant
8 个月Hi Sonu, Can You Integrate phonepe payment gateway in a blogger website?