Python SDK – Introduction

A python library for integrating with PhonePe APIs.

Installation

Requires python 3.9 or later

pip install --index-url https://phonepe.mycloudrepo.io/public/repositories/phonepe-pg-sdk-python  --extra-index-url https://pypi.org/simple phonepe_sdk

Quick Start

To get your client ID and secret, refer to the PhonePe business dashboard.

Create an instance of the CustomCheckoutClient class:

from phonepe.sdk.pg.payments.v2.custom_checkout_client import CustomCheckoutClient
from phonepe.sdk.pg.env import Env
 
client_id = "<YOUR_CLIENT_ID>"
client_secret = "<YOUR_CLIENT_SECRET>"
client_version = 1  # Insert your client version here
env = Env.SANDBOX  # Change to Env.PRODUCTION when you go live
 
custom_checkout_client = CustomCheckoutClient.get_instance(client_id=client_id,
                                                           client_secret=client_secret,
                                                           client_version=client_version,
                                                           env=env)

UPI Intent

To init a pay request, we make a request object using builder functions in PgPaymentRequest. Depending on the instrument we call different instrument.

Here for example we will call PgPaymentRequest.build_intent_pay_request to init intent request.

Example :

from uuid import uuid4
from phonepe.sdk.pg.common.models.request.pg_payment_request import PgPaymentRequest
 
unique_order_id = str(uuid4())
ui_redirect_url = "https://www.merchant.com/redirect"
amount = 100
intent_pay_request = PgPaymentRequest.build_upi_intent_pay_request(merchant_order_id=unique_order_id,
                                                                           amount=amount,
                                                                           target_app="PHONEPE")  # GPAY, PAYTM
intent_response = custom_checkout_client.pay(intent_pay_request)
phonepe_intent_url = intent_response.intent_url

The data will be in a PgPaymentResponse object.
The phonepe_intent_url you get can be handled by redirecting the user to that url on the front end.

UPI Collect

To init a pay request, we make a request object using builder functions in PgPaymentRequest. Depending on the instrument we call different instrument.

Here for example we will call PgPaymentRequest.build_upi_collect_pay_via_vpa_request to raise collect request.

Example :

from uuid import uuid4
from phonepe.sdk.pg.common.models.request.pg_payment_request import PgPaymentRequest
 
merchant_order_id = str(uuid4())
amount = 100
vpa = "<REPLACE_WITH_REQUIRED_VPA>"
collect_message = "<MESSAGE_DISPLAYED_TO_USER>"
custom_checkout_pay_request = PgPaymentRequest.build_upi_collect_pay_via_vpa_request(vpa=vpa,
                                                                                               amount=amount,
                                                                                               merchant_order_id=merchant_order_id,
                                                                                               message=collect_message)
 
custom_checkout_pay_response = custom_checkout_client.pay(custom_checkout_pay_request)

A collect request will be raised to the user with the given VPA.

Check status of order

View the state for the order we just initiated.

unique_order_id = "INSERT_YOUR_UNIQUE_ORDER_ID" 
order_status_response = custom_checkout_client.get_order_status(merchant_order_id=unique_order_id) 
order_state = order_status_response.state

You will get the data OrderStatusResponse object.

Order callback handling

You will receive a callback on the URL that you have configured on dashboard link

It is important to check the validity of the callback received from PhonePe using the validate_callback function.

authorization_header_data = "ef4c914c591698b268db3c64163eafda7209a630f236ebf0eebf045460df723a"
phonepe_s2s_callback_response_body_string = """{"type": "PG_REFUND_COMPLETED","payload": {}}"""  # callback body as string
 
username_configured = "MERCHANT_USERNAME"
password_configured = "MERCHANT_PASSWORD"
 
callback_response = custom_checkout_client.validate_callback(username=username_configured,
                                                             password=password_configured,
                                                             callback_header_data=authorization_header_data,
                                                             callback_response_data=phonepe_s2s_callback_response_body_string)
order_id = callback_response.order_id
state = callback_response.state

validate_callback will throw PhonePeException if the callback is invalid.

Possible Callback Response: 

  • CHECKOUT_ORDER_COMPLETED,
  • CHECKOUT_ORDER_FAILED, 
  • CHECKOUT_TRANSACTION_ATTEMPT_FAILED