PHP code example of phonepe / phonepe-pg-php-sdk

1. Go to this page and download the library: Download phonepe/phonepe-pg-php-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phonepe / phonepe-pg-php-sdk example snippets


use PhonePe\payments\v2\standardCheckout\StandardCheckoutClient;
use PhonePe\Env;

$clientId = "YOUR_CLIENT_ID";
$clientSecret = "YOUR_CLIENT_SECRET";
$clientVersion = "YOUR_CLIENT_VERSION";
$env = Env::UAT; // Or Env::PRODUCTION

$phonepeClient = StandardCheckoutClient::getInstance($clientId, $clientVersion, $clientSecret, $env);

use PhonePe\payments\v2\models\request\builders\StandardCheckoutPayRequestBuilder;

$merchantOrderId = "ORDER-" . uniqid();
$amount = 1000; // Amount in paise (e.g., 1000 for ₹10.00)
$redirectUrl = "https://your-website.com/payment-redirect";
$message = "Payment for order " . $merchantOrderId;


$payRequest = (new StandardCheckoutPayRequestBuilder())
    ->merchantOrderId($merchantOrderId)
    ->amount($amount)
    ->redirectUrl($redirectUrl)
    ->message($message)
    ->build();

try {
    $payResponse = $phonepeClient->pay($payRequest);
    $redirectUrl = $payResponse->getRedirectUrl();
    // Redirect the user to the $redirectUrl to complete the payment
    header("Location: " . $redirectUrl);
    exit();
} catch (PhonePe\common\exceptions\PhonePeException $e) {
    // Handle exceptions
    echo $e->getMessage();
}

try {
    $statusResponse = $phonepeClient->getOrderStatus($merchantOrderId);
    // Process the status response
    echo "Payment state: " . $statusResponse->getState();
} catch (PhonePe\common\exceptions\PhonePeException $e) {
    // Handle exceptions
    echo $e->getMessage();
}

// Get headers and body from the callback request
$headers = [];
foreach ($_SERVER as $key => $value) {
    if (strpos($key, 'HTTP_') === 0) {
        $headerKey = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($key, 5)))));
        $headers[$headerKey] = $value;
    }
}
$requestBody = file_get_contents('php://input');
$username = "YOUR_CALLBACK_USERNAME";
$password = "YOUR_CALLBACK_PASSWORD";


try {
    $callbackResponse = $phonepeClient->verifyCallbackResponse($headers, $requestBody, $username, $password);
    // Process the callback response
    echo "Callback state: " . $callbackResponse->getState();
} catch (PhonePe\common\exceptions\PhonePeException $e) {
    // Handle exceptions
    echo $e->getMessage();
}

use PhonePe\payments\v2\models\request\builders\StandardCheckoutRefundRequestBuilder;

$merchantRefundId = "REFUND-" . uniqid();
$originalMerchantOrderId = "ORDER-12345"; // The original order ID
$amount = 500; // Amount to refund in paise

$refundRequest = (new StandardCheckoutRefundRequestBuilder())
    ->merchantRefundId($merchantRefundId)
    ->originalMerchantOrderId($originalMerchantOrderId)
    ->amount($amount)
    ->build();

try {
    $refundResponse = $phonepeClient->refund($refundRequest);
    // Process the refund response
    echo "Refund state: " . $refundResponse->getState();
} catch (PhonePe\common\exceptions\PhonePeException $e) {
    // Handle exceptions
    echo $e->getMessage();
}

try {
    $refundStatusResponse = $phonepeClient->getRefundStatus($merchantRefundId);
    // Process the refund status response
    echo "Refund status: " . $refundStatusResponse->getState();
} catch (PhonePe\common\exceptions\PhonePeException $e) {
    // Handle exceptions
    echo $e->getMessage();
}