Initiate Payment with Python SDK
The Initiate Payment step allows you to start a payment transaction by creating a payment request with essential details like order ID, amount, and redirect URL. This request prepares the transaction on PhonePe’s platform and generates a redirect URL where users complete their payment securely.
Request
Use to create the payment request. Below are the key attributes you can set:StandardCheckoutPayRequest.build_request()
| 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
from uuid import uuid4
from phonepe.sdk.pg.payments.v2.standard_checkout_client import StandardCheckoutClient
from phonepe.sdk.pg.payments.v2.models.request.standard_checkout_pay_request import StandardCheckoutPayRequest
from phonepe.sdk.pg.common.models.request.meta_info import MetaInfo
from phonepe.sdk.pg.env import Env
client_id = "<YOUR_CLIENT_ID>"
client_secret = "<YOUR_CLIENT_SECRET>"
client_version = <CLIENT_VERSION> # Insert your client version here
env = Env.SANDBOX # Change to Env.PRODUCTION when you go live
should_publish_events = False
client = StandardCheckoutClient.get_instance(client_id=client_id,
client_secret=client_secret,
client_version=client_version,
env=env,
should_publish_events=should_publish_events)
unique_order_id = str(uuid4())
ui_redirect_url = "https://www.merchant.com/redirect"
amount = 100
meta_info = MetaInfo(udf1="udf1", udf2="udf2", udf3="udf3")
standard_pay_request = StandardCheckoutPayRequest.build_request(merchant_order_id=unique_order_id,
amount=amount,
redirect_url=ui_redirect_url,
meta_info=meta_info)
standard_pay_response = client.pay(standard_pay_request)
checkout_page_url = standard_pay_response.redirect_urlResponse
The function returns a StandardCheckoutPayResponse object with the following properties:
| Attribute | Data Type | Description |
state | String | State of the order created, expected value is PENDING. |
redirect_url | String | URL for the PhonePe Payment Gateway Standard Checkout page. This is the URL to which the user should be redirected for payment. |
order_id | String | A unique internal order ID generated by PhonePe PG. |
expire_at | String | 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 create the order SDK.
Proceed to the next section to learn how to Create Order SDK .