Python SDK – Callback Verification

This is used to verify whether the callback received is valid or not

You need to pass 4 parameters to the validate_Callback() function

Parameter NameData TypeMandatoryDescription
usernamestryesUnique username configured for the callback url.
passwordstryesUnique password configured for the callback url.
authorizationstryesValue of the Authorization header under the callback response.
responseBodystryesCallback response body as string.

Example usage :

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)
 
authorization_header_data = "ef4c914c591698b268db3c64163eafda7209a630f236ebf0eebf045460df723a"  # header value under `Authorization` key
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)
callback_type = callback_response.callback_type
merchant_refund_id = callback_response.callback_data.merchant_refund_id
state = callback_response.callback_data.state

Returns :

The function returns a CallbackResponse if the callback is valid, otherwise throws a PhonePeException.

Callback Response:

PropertyData TypeDescription
callback_typeenumContains type of callback received at the merchant end.
callback_payloadObjectContains callback details.

Callback Types

Callback TypeContext
PG_ORDER_COMPLETEDOrder request is successfully completed
PG_ORDER_FAILEDOrder request failed
PG_REFUND_COMPLETEDRefund Completed for PG
PG_REFUND_ACCEPTEDRefund Accepted by PhonePe and will be initiated
PG_REFUND_FAILEDRefund Failed for PG

CallbackData Properties :

PropertyData TypeDescription
merchant_idstrThe merchant from which request was initiated.
order_idstrOrder id generated by PhonePe. (Only present in case of order callbacks)
merchant_order_idstrOrder id generated by merchant. (Only present in case of order callbacks)
original_merchant_order_idstrInternal transaction id for given payment attempt. (Only present in case of refund callback)
refund_idstrRefund id generated by PhonePe. (Only present in case of refund callback)
merchant_refund_idstrRefund id generated by merchant. (Only present in case of refund callback)
statestrState of the order/refund.
amountintAmount of the order/refund processed.
expire_atintExpiry in epoch.
error_codestrError code. (Only present when state is failed)
detailed_error_codestrDetailed error code. (Only present when state is failed)
meta_infoMetaInfoMetaInfo passed during the init of order.
payment_detailsList<PaymentDetail>Payment details.

This document outlines the exception handling for the exceptions that might incur.

PhonePeException

Exception raised for errors related to PhonePe APIs.

AttributeTypeDescription
codeStringThe status code of the response.
messageStringThe error message.
http_status_codeIntegerThe http status received from the API
dataMap<String, String>The details of the error that happened while calling phonepe.

Example Usage :

from phonepe.sdk.pg.common.exceptions 
import PhonePeException
 

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)

try:
    callback_valid = custom_checkout_client.validate_callback(username="username_configured",
                                                               password="password_configured",
                                                               callback_header_data="ef4c914c591698b268db3c64163eafda7209a630f236ebf0eebf045460df723a",
                                                               callback_response_data="phonepe_s2s_callback_response_body_string")

except PhonePeException 
as exception:
    print(exception.code)
    print(exception.message)

Below data can be used when you need to access the response details :

PaymentRail :

Different types of rail which will be received at the time of getOrderStatus(). It falls under the rail attribute in PaymentDetail Object.

UPI RAIL

PropertyType
typePaymentRailType
utrstr
upi_transaction_idstr
vpastr

PG RAIL

Property
Type
type
PaymentRailType
transaction_id
str
authorization_code
str
service_transaction_id
str

PPI WALLET RAIL

PropertyType
typePaymentRailType

PPI EGV RAIL

PropertyType
typePaymentRailType

Payment Instrument V2

ACCOUNT

PropertyType
typePaymentInstrumentType
ifscstr
account_typestr
masked_account_numberstr
account_holder_namestr

CREDIT_CARD

PropertyType
typePaymentInstrumentType
bank_transaction_idstr
bank_idstr
arnstr
brnstr

DEBIT_CARD

PropertyType
typePaymentInstrumentType
bank_transaction_idstr
bank_idstr
arnstr
brnstr

NET_BANKING

PropertyType
typePaymentInstrumentType
bank_transaction_idstr
bank_idstr
arnstr
brnstr

EGV

PropertyType
typePaymentInstrumentType
cardNumberstr
programIdstr

WALLET

PropertyType
typePaymentInstrumentType
walletIdstr

Instrument Constraints

ACCOUNT

PropertyType
typeAccount
accountNumberstr
ifscstr
Is this article helpful?