Python SDK
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==1.1.0
Quick start
Create an instance of the PhonePePaymentClient class.
from phonepe.sdk.pg.payments.v1.payment_client import PhonePePaymentClient
from phonepe.sdk.pg.env import Env
merchant_id = "YOUR_MERCHANT_ID"
salt_key = "YOUR_SALT_KEY"
salt_index = 1
should_publish_events = false
env = Env.UAT # Change to Env.PROD when you go live
phonepe_client = PhonePePaymentClient(merchant_id=merchant_id, salt_key=salt_key, salt_index=salt_index, env=env, should_publish_events=should_publish_events)
For Python SDK Version <= 0.0.3, the imports should be:
from phonepe.sdk.pg.payments
For Python SDK Version > 0.0.3, the imports should be:
from phonepe.sdk.pg.payments.v1
Initiate transaction using Pay Page
Each pay request is used by building it using PgPayRequest. To use the pay page, we make a request object using pay_page_pay_request_builder. You will initiate the transaction using the pay function:
import uuid
from phonepe.sdk.pg.payments.v1.models.request.pg_pay_request import PgPayRequest
unique_transaction_id = str(uuid.uuid4())[:-2]
ui_redirect_url = "https://www.merchant.com/redirectPage"
s2s_callback_url = "https://www.merchant.com/callback"
amount = 100
id_assigned_to_user_by_merchant = 'YOUR_USER_ID'
pay_page_request = PgPayRequest.pay_page_pay_request_builder(merchant_transaction_id=unique_transaction_id,
amount=amount,
merchant_user_id=id_assigned_to_user_by_merchant,
callback_url=s2s_callback_url,
redirect_url=ui_redirect_url)
pay_page_response = phonepe_client.pay(pay_page_request)
pay_page_url = pay_page_response.data.instrument_response.redirect_info.url
You will get a PhonePeResponse object. The data will be in a PgPayResponse object. The pay_page_url you get can be handled by redirecting the user to that URL on the front end.
=Check Status of transaction
View the state for the transaction we just initiated.
unique_transaction_id = "INSERT_YOUR_UNIQUE_TRANSACTION_ID"
transaction_status_response = phonepe_client.check_status(merchant_transaction_id=unique_transaction_id)
transaction_state = transaction_status_response.data.state
You will get the data PgTransactionStatusResponse object.
Check the validity of callback
You will receive a callback at the URL you gave (https://www.merchant.com/callback)
It is important to check the validity of the callback received from PhonePe using the verify_response function.
x_verify_header_data = "a005532637c6a6e4a4b08ebc6f1144384353305a9cd253d995067964427cd0bb###1"
phonepe_s2s_callback_response_body_string = '{"response": "eyJzdWNjZXNzIjpmYWxzZSwiY29kZSI6IlBBWU1FTlRfRVJST1IiLCJtZXNzYWdlIjoiUGF5bWVudCBGYWlsZWQiLCJkYXRhIjp7Im1lcmNoYW50SWQiOiJtZXJjaGFudElkIiwibWVyY2hhbnRUcmFuc2FjdGlvbklkIjoibWVyY2hhbnRUcmFuc2FjdGlvbklkIiwidHJhbnNhY3Rpb25JZCI6IkZUWDIzMDYwMTE1NDMxOTU3MTYzMjM5IiwiYW1vdW50IjoxMDAsInN0YXRlIjoiRkFJTEVEIiwicmVzcG9uc2VDb2RlIjoiUkVRVUVTVF9ERUNMSU5FX0JZX1JFUVVFU1RFRSIsInBheW1lbnRJbnN0cnVtZW50IjpudWxsfX0="}'
is_valid = phonepe_client.verify_response(x_verify=x_verify_header_data,
response=phonepe_s2s_callback_response_body_string)
If the output is True the callback was valid.
Refund the transaction
If you want to refund the transaction that was just processed using the refund function.
import uuid
unique_transcation_id = str(uuid.uuid4())[:-2]
original_transaction_id = "MERCHANT_TRANSACTION_YOU_WANT_TO_REFUND"
amount = 100 //refund amount <= pay amount
s2s_callback_url = "https://www.merchant.com/callback"
refund_response = phonepe_client.refund(merchant_transaction_id=unique_transcation_id,
original_transaction_id=original_transaction_id,
amount=amount,
callback_url=s2s_callback_url)
response_code = refund_response.data.response_code