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.

Use StandardCheckoutPayRequest.build_request() to create the payment request. Below are the key 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 
paymentModeConfigObjectNoUsed to dynamically enable only specific instruments on the checkout page.PaymentModeConstraint
redirectUrlStringNoURL to which the user will be redirected after the payment (success or failure)
messageStringNoPayment message shown in APP for collect requests.
expireAfterLongNoSet a orders expiry time in seconds
disablePaymentRetryBooleanNoSetting this field to true will disable retries on standard checkout payment page.
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,
    orderid = str(uuid.uuid4())
    enabled_modes_data = [
        UpiIntentPaymentModeConstraint(PgV2InstrumentType.UPI_INTENT),
        NetBankingPaymentModeConstraint(PgV2InstrumentType.NET_BANKING),
        CardPaymentModeConstraint(card_types=[CardType.DEBIT_CARD, CardType.CREDIT_CARD]),
    ]
    meta_info = MetaInfo(udf1="udf1", udf2="udf2", udf3="udf3")
    payment_mode_config = PaymentModeConfig(enabled_payment_modes=enabled_modes_data)
    pay_page_build = StandardCheckoutPayRequest.build_request(
        merchant_order_id=orderid,
        amount=100,
        meta_info=meta_info,
        payment_mode_config=payment_mode_config,
        redirect_url="https://www.merchant.com/redirect",
        message="Message that will be shown for UPI collect transaction",
        expire_after=3600,
        disable_payment_retry=True
    )
standard_pay_response = client.pay(standard_pay_request)
checkout_page_url = standard_pay_response.redirect_url

The function returns a StandardCheckoutPayResponse object with the following properties:

AttributeData TypeDescription
stateStringState of the order created, expected value is PENDING.
redirect_urlStringURL for the PhonePe Payment Gateway Standard Checkout page. This is the URL to which the user should be redirected for payment.
order_idStringA unique internal order ID generated by PhonePe PG.
expire_atStringOrder 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 create the order SDK.

Proceed to the next section to learn how to Create Order SDK .

Is this article helpful?