Initiate Payment with PHP SDK


The pay method is used to initiate a payment via the PhonePe PG. You can create a payment request using the StandardCheckoutPayRequestBuilder::builder() method by providing the required attributes.

Use the Standard Checkout Pay Request builder to create a payment request. Below are the attributes you can set:

Parameter NameData TypeMandatory
(Yes/No)
DescriptionConstraints
merchantOrderIdStringYesUnique order ID assigned by youMax length: 63 characters, no special characters except “_” and “-”
amountLongYesOrder amount in paisaMinimum value: 100 (in paisa)
metaInfoObjectNoMeta information is defined by you to store additional information. The same data will be returned in status and callback response.
metaInfo.udf1-5StringNoOptional details you can add for more information.Maximum length = 256 characters 
redirectUrlStringNoURL to which the user will be redirected after the payment (success or failure)
Sample Request
<?php

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

$merchantOrderId = "ORDER_101"; // Unique order ID
$amount = 1000; // Amount in paisa (e.g., 1000 = ₹10.00)
$redirectUrl = "https://your-website.com/redirect"; // URL to which PhonePe will redirect after payment
$message = "Your order details";

$payRequest = StandardCheckoutPayRequestBuilder::builder()
    ->merchantOrderId($merchantOrderId)
    ->amount($amount)
    ->redirectUrl($redirectUrl)
    ->message($message)  //Optional Message
    ->build();
?>
Call the pay() Method:
<?php
try {
    $payResponse = $client->pay($payRequest);

    // Handle the response
    if ($payResponse->getState() === "PENDING") {
        // Redirect the user to the PhonePe payment page
        header("Location: " . $payResponse->getRedirectUrl());
        exit();
    } else {
        // Handle the error (e.g., display an error message)
        echo "Payment initiation failed: " . $payResponse->getState();
    }
} catch (\PhonePe\common\exceptions\PhonePeException $e) {
    // Handle exceptions (e.g., log the error)
    echo "Error initiating payment: " . $e->getMessage();
}
?>

The pay method returns a StandardCheckoutPayResponse object with the following properties:

PropertyData TypeDescription
stateStringCurrent status of the order (e.g., PENDING).
redirectUrlStringURL for the PhonePe Payment Gateway Standard Checkout page. This is the URL to which the user should be redirected for payment.
orderIdStringA unique internal order ID generated by PhonePe PG.
expireAtLongOrder expiry timestamp in epoch.

After using the pay method to initiate a payment via the PhonePe PG, you can create a payment request and start the payment process. The next step is to check the order status.

Proceed to the next section to learn how to verify the status of the order.

Is this article helpful?