In the world of online digital payments, providing a seamless payment experience is crucial. One popular payment gateway in India is Phonepe. In this blog, today I will teach you how PhonePe payment gateway integration step by step guide to your website
First, you have to register a business account with Phonepe and after registering with Phonepe you’ve got your business account and those all-important API credentials from the PhonePe account. Now, we are going to Step-by-step PhonePe payment integration in PHP into the website in just 3 easy steps.
Step 1: Register with PhonePe
You need to create an account with a PhonePe business account. This will give you access to the necessary credentials and APIs for integration. Visit the PhonePe Business website to sign up and submit the necessary documents and get PhonePe API credentials.
// Replace these parameter with your PhonePe API credentials
$apiKey = 'YOUR API KEY';
$merchantId = 'YOUR MERCHANT ID';
Step 2: Request payment data
To send a payment request using cURL and process the response data, you can use the following PHP code as an example. In this code, we’ll make a POST request to a payment gateway URL, process the response, and check the status of the payment request.
<?php
// Define your payment data
$post_data = array(
"merchant_id" => "your_merchant_id",
"order_id" => "your_order_id",
"amount" => 1000, // Amount in paisa (10 INR)
"redirectUrl" => "https://webhook.site/redirect_url",
"redirectMode" => "POST",
"callbackUrl" => "https://webhook.site/redirect_url",
"merchantOrderId" => "ORDER123",
"mobileNumber" => "1234567890",
"message" => "Order description",
"email" => "codingtutes@gmail.com",
"paymentInstrument" => array(
"type" => "PAY_PAGE",
)
);
// Encode the data as base64
$encode = base64_encode(json_encode($post_data));
// Define your salt and salt index
$saltKey = '099eb0cd-02cf-4e2a-8aca-3e6c6aff0399';
$saltIndex = 1;
// Create the string for the X-VERIFY header
$string = $encode . '/pg/v1/pay' . $saltKey;
$sha256 = hash('sha256', $string);
$finalXHeader = $sha256 . '###' . $saltIndex;
// Define the API endpoint
$url = "https://api-preprod.phonepe.com/apis/pg-sandbox/pg/v1/pay";
// Set up cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['request' => $encode]));
// Set cURL headers
$headers = array(
"Content-Type: application/json",
"X-VERIFY: " . $finalXHeader,
"accept: application/json"
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// Execute the cURL request
$response = curl_exec($ch);
//handle the response
Step 3: Handle the response
To handle the response from PhonePe, you can parse the JSON response and then take appropriate actions based on the data returned. Here’s how you can modify the code to handle the response
// Check for cURL errors
if (curl_errno($ch)) {
echo 'cURL error: ' . curl_error($ch);
}else{
// Handle the response from PhonePe
$res = json_decode($response);
if(isset($res->success) && $res->success=='1'){
$paymentCode = $res->code;
$paymentMsg = $res->message;
$payUrl = $res->data->instrumentResponse->redirectInfo->url;
header('Location:'.$payUrl);
}
}
curl_close($ch);
var_dump(json_decode($response)); // You can change this handling as needed
Thank you for taking the time to read this post. we value your feedback, so please don’t hesitate to leave a comment with your suggestions. If you encounter any issues with the provided code, please reach out to us. We’re here to assist you! Keep following us for more updates and information.
Leave a Reply