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.• For udf1 to udf10, there is no constraint and Maximum length for Udf1-10 = 256 characters
• For udf11 to udf15, alphanumeric + _, -, @, ., + only are allowed and Maximum length for Udf11-15 = 50 characters
• It is mandatory to keep the parameter names udf1udf2, etc., exactly as they are in the metainfo block. Renaming these key values will result in a production error.
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
    ->udf1('udf1')
    ->udf2('udf2')
    ->udf3('udf3')
    ->udf4
('udf4
')
    ->udf5('udf5')
    ->build();
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?