Handle Webhooks with .NET SDK


Use callback verification to confirm that the callback you received from PhonePe is authentic.

Request Parameters
Parameter NameData TypeMandatory
(Yes/NO)
Description
usernameStringYesYour unique username configured for the callback URL
passwordStringYesYour unique password configured for the callback URL
authorizationStringYesValue of the Authorization header under the callback response.
responseBodyStringYesThe response body received in the callback as a string
Sample Request
string authorizationHeaderData = "ef4c914c591698b268db3c64163eafda7209a630f236ebf0eebf045460df723a" // received in the response headers
string phonepeS2SCallbackResponseBodyString = @"{
                                ""event"": ""pg.order.completed"",
                                ""payload"": {
                                    ""orderId"": ""OMOxx"",
                                    ""merchantId"": ""merchantId"",
                                    ""merchantOrderId"": ""merchantOrderId"",
                                    ""state"": ""EXPIRED"",
                                    ""amount"": 10000,
                                    ""expireAt"": 1291391291,
                                    ""metaInfo"": {
                                        ""udf1"": """",
                                        ""udf2"": """",
                                        ""udf3"": """",
                                        ""udf4"": """",
                                        ""udf5"": """"
                                    },
                                    ""paymentDetails"": [
                                        {
                                            ""paymentMode"": ""UPI_COLLECT"",
                                            ""timestamp"": 12121212,
                                            ""amount"": 10000,
                                            ""transactionId"": ""OM12333"",
                                            ""state"": ""FAILED"",
                                            ""errorCode"": ""AUTHORIZATION_ERROR"",
                                            ""detailedErrorCode"": ""ZM"",
                                            ""splitInstruments"": [
                                                {
                                                    ""rail"": {
                                                        ""type"": ""PPI_EGV""
                                                    },
                                                    ""instrument"": {
                                                        ""type"": ""CREDIT_CARD"",
                                                        ""bankTransactionId"": ""bankTransactionId"",
                                                        ""bankId"": ""bankId"",
                                                        ""brn"": ""brn"",
                                                        ""arn"": ""arn""
                                                    },
                                                    ""amount"": 10000
                                                }
                                            ]
                                        }
                                    ]
                                }
                            }"; // callback body as string
  
var usernameConfigured = "<MERCHANT_USERNAME>"
var passwordConfigured = "<MERCHANT_PASSWORD>" 
 
CallbackResponse callbackResponse = checkoutClient.ValidateCallback(usernameConfigured, passwordConfigured, authorizationHeaderData, phonepeS2SCallbackResponseBodyString);  
 
var orderId = callbackResponse.payload.orderId;
var state = callbackResponse.payload.state;
  • The function returns a CallbackResponse object containing two main parameters: type, which indicates the event type, and payload, which holds all the event-specific details.
Parameter NameData TypeDescription
eventStringContains event of callback received at the merchant end.
payloadCallbackDataContains all the details related to that event
  • The CallbackData are explained below:
PropertyTypeDescription
merchantIdStringThe merchant from which request was initiated
orderIdStringOrder id generated by PhonePe (Only present in case of order callbacks)
merchantOrderIdStringOrder id generated by merchant (Only present in case of order callbacks)
originalMerchantOrderIdStringOrder id generated by merchant (Only present in case of refund callback)
refundIdStringRefund id generated by PhonePe (Only present in case of refund callback)
merchantRefundIdStringRefund id generated by merchant (Only present in case of refund callback)
stateStringState of the ORDER/REFUND
amountLongAmount in Paisa of the order/refund processed
expireAtLongExpiry in epoch
errorCodeStringError code. (Only present when state is failed)
detailedErrorCodeStringDetailed error code. (Only present when state is failed)
metaInfoMetaInfoAdditional Information about the order
paymentDetailsList<PaymentDetail>Contain list of details of each transaction attempt made corresponding to this particular order
  • The paymentDetails property contains a list of payment details for each payment attempt made against an order. The details of each payment are explained in the table below.
AttributeData TypeDescription
transactionIdStringMerchant ID from which the request was initiated.
paymentModeStringOrder ID generated by PhonePe Payment Gateway (only for order callbacks).
timestampLongOrder ID generated by you (only for order callbacks).
stateStringAttempted transaction state. It can be any one of the following states:
COMPLETED
FAILED
PENDING
errorCodeStringError code (only present when the state is failed).
detailedErrorCodeStringA more specific error code (only present when the state is failed).
railPaymentRailContains processing rail details under which transaction attempt is made.
instrumentPaymentInstrumentV2Contains instrument details of that particular transaction ID

Exception handling in the PhonePe SDK is managed through the PhonePeException, which captures errors related to PhonePe APIs. It provides detailed information such as HTTP status code, error code, message, and additional error data to help identify and resolve issues effectively.

PhonePeException

Exception raised for errors related to PhonePe APIs.

AttributeTypeDescription
codeStringThe status code of the http response.
messageStringThe http error message.
httpStatusCodeIntegerThe status code of the http response.
dataMap<String, String>The details of the error that happened while calling PhonePe API.
Sample Request

var merchantOrderID = Guid.NewGuid().ToString();
 
var payRequest = StandardCheckoutPayRequest.Builder()
            .SetMerchantOrderId(merchantOrderID)
            .SetAmount(100)
            .SetRedirectUrl("https://www.merchant.com/redirect")
            .SetExpireAfter(300)
            .SetPaymentModeConfig(paymentModeConfig)
            .SetMessage("message")
            .Build();
 
try
{
    StandardCheckoutPayResponse response = await checkoutClient.Pay(payRequest);
    logger.LogInformation("Pay API Response:\n{Response}", JsonSerializer.Serialize(response, JsonOptions.IndentedWithRelaxedEscaping));
}
catch (Exception ex)
{
    logger.LogError(ex);
}

Response Models for Reference

  • PaymentRail
    • Represents 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
utrString
upiTransactionIdString
vpaString
PG RAIL
PropertyType
typePaymentRailType
transactionIdString
authorizationCodeString
serviceTransactionIdString
PPI_WALLET RAIL
PropertyType
typePaymentRailType
PPI_EGV RAIL
PropertyType
typePaymentRailType
  • PaymentRail
    • Defines the type of rail used to initiate payment.
UPI RAIL
PropertyType
typePaymentRailType
utrString
upi_transaction_idString
vpaString
PG RAIL
PropertyType
typePaymentRailType
transaction_idString
authorization_codeString
service_transaction_idString
  • PaymentInstrumentV2
    • Represents different types of instruments which will be received at the time of getOrderStatus(). It falls under the instrument attribute in PaymentDetail Object
ACCOUNT
PropertyType
typePaymentInstrumentType
ifscString
account_typeString
masked_account_numberString
account_holder_nameString
CREDIT_CARD
PropertyType
typePaymentInstrumentType
bank_transaction_idString
bank_idString
arnString
brnString
DEBIT_CARD
PropertyType
typePaymentInstrumentType
bank_transaction_idString
bank_idString
arnString
brnString
NET_BANKING
PropertyType
typePaymentInstrumentType
bank_transaction_idString
bank_idString
arnString
brnString
EGV
PropertyType
typePaymentInstrumentType
cardNumberString
programIdString
WALLET
PropertyType
typePaymentInstrumentType
walletIdString

Now that you have learned how to verify the payment and what happens when the webhook fails, this concludes your website integration. The next step is to complete UAT testing and understand the process to go live.

Is this article helpful?