Skip to Content

How to Integrate the PhonePe Payment Gateway in a PHP Website

26 April 2025 by
How to Integrate the PhonePe Payment Gateway in a PHP Website
Sourabh Sahani
| No comments yet

If you're running an online business or e-commerce website, offering easy and secure payment options is key to enhancing your customer experience. One of the most popular payment gateways in India is PhonePe, which provides seamless and secure payment solutions for merchants and customers. In this blog, we’ll walk you through the steps on how to integrate the PhonePe payment gateway into your PHP website

Why Choose PhonePe for Payment Integration?

PhonePe is a fast-growing payment platform in India, widely used for digital transactions. With over millions of users, integrating PhonePe into your website can offer several benefits:

  • Quick and Easy Transactions: Instant payments directly to your bank account.
  • Security: PhonePe ensures secure encryption and complies with PCI DSS standards.
  • Popular in India: Users are already familiar with the PhonePe platform, making it a trusted choice for payment.

Prerequisites

Before you begin the integration, ensure you have the following:

  1. PhonePe Merchant Account: If you don’t already have one, you need to create a merchant account on the PhonePe portal.
  2. API Credentials: After registering, you’ll receive essential API credentials like your Merchant ID, Merchant Key, and other details required for integration.

Steps to Integrate PhonePe Payment Gateway in PHP Website

Step 1: Set Up the PhonePe SDK or API

PhonePe doesn't provide an official SDK for PHP, but you can easily integrate it using cURL requests. Your PHP server must have cURL enabled to send requests and receive responses from the PhonePe API.

Step 2: Create a Payment Request (Initiate Transaction)

You need to send a request to the PhonePe API with transaction details such as the amount, order ID, and other necessary information. Here’s a simple PHP code snippet to initiate a payment request:

php

CopyEdit

<?php $merchant_id = "your_merchant_id"; $merchant_key = "your_merchant_key"; $transaction_id = "txn_" . uniqid(); // Unique transaction ID $order_id = "order_" . uniqid(); // Unique order ID // Transaction data $data = [ "merchantId" => $merchant_id, "transactionId" => $transaction_id, "amount" => "100.00", // Amount to be paid (in INR) "currency" => "INR", "orderId" => $order_id, "redirectUrl" => "http://yourwebsite.com/payment-success.php", // Success URL "cancelUrl" => "http://yourwebsite.com/payment-fail.php", // Failure URL ]; // Convert data array to JSON $jsonData = json_encode($data); // Initialize cURL $url = "https://payment.phonepe.com/v3/merchant/transaction/initiate"; // API endpoint $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Content-Type: application/json", "Merchant-Id: $merchant_id", "Merchant-Key: $merchant_key" ]); // Execute the cURL request $response = curl_exec($ch); curl_close($ch); // Decode the response $responseData = json_decode($response, true); // Handle response and redirect to the payment page if ($responseData['status'] == 'SUCCESS') { // Redirect user to PhonePe payment page header("Location: " . $responseData['paymentUrl']); exit(); } else { echo "Payment initiation failed. Please try again."; } ?>

Explanation:

  • merchantId: Your unique PhonePe merchant ID.
  • transactionId: A unique transaction ID for each payment.
  • amount: The total amount the user has to pay.
  • currency: Currency used for the transaction (INR for India).
  • redirectUrl: The URL to which users will be redirected after a successful transaction.
  • cancelUrl: The URL users will be redirected to if the payment is unsuccessful.

Once this step is complete, the user will be redirected to PhonePe’s payment page to complete the transaction.

Step 3: Handle the Payment Callback (Success/Failure)

PhonePe will redirect the user to the success or failure URL based on the outcome of the transaction. You'll need to handle these redirects and confirm the transaction status.

Success Callback (payment-success.php):

php

CopyEdit

<?php $transactionId = $_GET['transactionId']; $orderId = $_GET['orderId']; $status = $_GET['status']; // Payment status: SUCCESS or FAILURE if ($status == 'SUCCESS') { // Handle successful payment echo "Payment Successful! Transaction ID: $transactionId"; // Update your database with the transaction details and complete the order } else { // Handle payment failure echo "Payment Failed! Please try again."; } ?>

Failure Callback (payment-fail.php):

php

CopyEdit

<?php echo "Payment failed. Please try again."; ?>

Step 4: Verify the Payment (Optional but Recommended)

It’s essential to verify the payment after receiving the success callback. You can send a verification request to PhonePe’s API to check if the payment was genuinely successful.

php

CopyEdit

<?php // Verify payment using transaction ID $transaction_id = $_GET['transactionId']; // Get transaction ID from success callback $url = "https://payment.phonepe.com/v3/merchant/transaction/status"; // API endpoint $data = [ "merchantId" => "your_merchant_id", "transactionId" => $transaction_id, ]; // Convert data to JSON $jsonData = json_encode($data); // Initialize cURL $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Content-Type: application/json", "Merchant-Id: your_merchant_id", "Merchant-Key: your_merchant_key" ]); $response = curl_exec($ch); curl_close($ch); // Decode the response $responseData = json_decode($response, true); if ($responseData['status'] == 'SUCCESS') { // Confirm payment and update database echo "Payment verified successfully."; } else { echo "Payment verification failed."; } ?>

Step 5: Testing and Going Live

Before going live, it’s important to test the payment integration in a sandbox environment (if available). Once you confirm everything works as expected, switch to the production environment using your live credentials.

Conclusion

Integrating the PhonePe payment gateway into your PHP website is a straightforward process if you follow these steps. With a secure and seamless transaction process, you can provide a better user experience for your customers. Remember to test your integration thoroughly before launching it in production to avoid any issues later on.

By following these steps, you’ll be able to implement PhonePe as a payment option on your PHP website and offer your customers a secure and easy way to pay for products or services online.

If you need further help or customization, feel free to reach out!


Sign in to leave a comment