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 method by providing the required attributes.StandardCheckoutPayRequestBuilder::builder()
Request
Use the Standard Checkout Pay Request builder to create a payment request. Below are the attributes you can set:
| Parameter Name | Data Type | Mandatory (Yes/No) | Description | Constraints |
| String | Yes | Unique order ID assigned by you | Max length: 63 characters, no special characters except “_” and “-” |
| Long | Yes | Order amount in paisa | Minimum value: 100 (in paisa) |
metaInfo | Object | No | Meta information is defined by you to store additional information. The same data will be returned in status and callback response. | |
metaInfo.udf1-5 | String | No | Optional details you can add for more information. | Maximum length = 256 characters |
| String | No | URL 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();
}
?>Response
The pay method returns a StandardCheckoutPayResponse object with the following properties:
| Property | Data Type | Description |
state | String | Current status of the order (e.g., PENDING). |
redirectUrl | String | URL for the PhonePe Payment Gateway Standard Checkout page. This is the URL to which the user should be redirected for payment. |
orderId | String | A unique internal order ID generated by PhonePe PG. |
expireAt | Long | Order expiry timestamp in epoch. |
What’s Next?
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.