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 Name | Data Type | Mandatory | Description |
|---|---|---|---|
username | str | yes | Unique username configured for the callback url. |
password | str | yes | Unique password configured for the callback url. |
authorization | str | yes | Value of the Authorization header under the callback response. |
responseBody | str | yes | Callback 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.stateReturns :
The function returns a CallbackResponse if the callback is valid, otherwise throws a PhonePeException.
Callback Response:
| Property | Data Type | Description |
|---|---|---|
callback_type | enum | Contains type of callback received at the merchant end. |
callback_payload | Object | Contains callback details. |
Callback Types
| Callback Type | Context |
|---|---|
PG_ORDER_COMPLETED | Order request is successfully completed |
PG_ORDER_FAILED | Order request failed |
PG_REFUND_COMPLETED | Refund Completed for PG |
PG_REFUND_ACCEPTED | Refund Accepted by PhonePe and will be initiated |
PG_REFUND_FAILED | Refund Failed for PG |
CallbackData Properties :
| Property | Data Type | Description |
|---|---|---|
merchant_id | str | The merchant from which request was initiated. |
order_id | str | Order id generated by PhonePe. (Only present in case of order callbacks) |
merchant_order_id | str | Order id generated by merchant. (Only present in case of order callbacks) |
original_merchant_order_id | str | Internal transaction id for given payment attempt. (Only present in case of refund callback) |
refund_id | str | Refund id generated by PhonePe. (Only present in case of refund callback) |
merchant_refund_id | str | Refund id generated by merchant. (Only present in case of refund callback) |
state | str | State of the order/refund. |
amount | int | Amount of the order/refund processed. |
expire_at | int | Expiry in epoch. |
error_code | str | Error code. (Only present when state is failed) |
detailed_error_code | str | Detailed error code. (Only present when state is failed) |
meta_info | MetaInfo | MetaInfo passed during the init of order. |
payment_details | List<PaymentDetail> | Payment details. |
Python SDK – Exception Handling
This document outlines the exception handling for the exceptions that might incur.
PhonePeException
Exception raised for errors related to PhonePe APIs.
| Attribute | Type | Description |
|---|---|---|
code | String | The status code of the response. |
message | String | The error message. |
http_status_code | Integer | The http status received from the API |
data | Map<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)Python SDK – Response Models
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
| Property | Type |
|---|---|
type | PaymentRailType |
utr | str |
upi_transaction_id | str |
vpa | str |
PG RAIL
Property
Type
type
PaymentRailType
transaction_id
str
authorization_code
str
service_transaction_id
strPPI WALLET RAIL
| Property | Type |
|---|---|
type | PaymentRailType |
PPI EGV RAIL
| Property | Type |
|---|---|
type | PaymentRailType |
Payment Instrument V2
ACCOUNT
| Property | Type |
|---|---|
type | PaymentInstrumentType |
ifsc | str |
account_type | str |
masked_account_number | str |
account_holder_name | str |
CREDIT_CARD
| Property | Type |
|---|---|
type | PaymentInstrumentType |
bank_transaction_id | str |
bank_id | str |
arn | str |
brn | str |
DEBIT_CARD
| Property | Type |
|---|---|
type | PaymentInstrumentType |
bank_transaction_id | str |
bank_id | str |
arn | str |
brn | str |
NET_BANKING
| Property | Type |
|---|---|
type | PaymentInstrumentType |
bank_transaction_id | str |
bank_id | str |
arn | str |
brn | str |
EGV
| Property | Type |
|---|---|
type | PaymentInstrumentType |
cardNumber | str |
| programId | str |
WALLET
| Property | Type |
|---|---|
type | PaymentInstrumentType |
| walletId | str |
Instrument Constraints
ACCOUNT
| Property | Type |
|---|---|
type | Account |
| accountNumber | str |
ifsc | str |