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
public IActionResult CustomValidateCallback()
    {
        string responseBody = @"{
            ""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
                            }
                        ]
                    }
                ]
            }
        }";

        try
        {
            var callbackResponse = _customCheckoutClient.ValidateCallback("username", "password", "bc842c31a9e54efe320d30d948be61291f3ceee4766e36ab25fa65243cd76e0e", responseBody);
            return Ok(callbackResponse);
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Custom ValidateCallback API Failed");
            return StatusCode(500, new { message = ex.Message });
        }
    }
  • 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
typeCallbackTypeContains type of callback received at the merchant end.
payloadCallbackDataContains all the details related to that event.
Callback Type
Callback TypeContext
PG_ORDER_COMPLETEDOrder Completed for PG
PG_ORDER_FAILEDOrder Failed for PG
PG_REFUND_COMPLETEDRefund Completed for PG
PG_REFUND_ACCEPTEDRefund Accepted by PhonePe and will be initiated
PG_REFUND_FAILEDRefund Failed for PG
  • The CallbackData are explained below:
PropertyTypeDescription
merchantIdStringThe merchant from which request was initiated
stateStringState of the ORDER/REFUND
refundIdStringRefund id generated by PhonePe (Only present in case of refund callback)
merchantRefundIdStringRefund id generated by merchant (Only present in case of refund callback)
orderIdStringOrder id generated by PhonePe (Only present in case of order callbacks)
originalMerchantOrderIdStringOrder id generated by merchant (Only present in case of refund callback)
merchantOrderIdStringOrder id generated by merchant (Only present in case of order callbacks)
expireAtlongExpiry in epoch
errorCodeStringError code. (Only present when state is failed)
detailedErrorCodeStringDetailed error code. (Only present when state is failed)
paymentDetailsList<PaymentDetail>Contain list of details of each transaction attempt made corresponding to this particular order
amountlongAmount in Paisa of the order/refund processed
metaInfoMetaInfoAdditional Information about the 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.
PropertyTypeDescription
transactionIdStringTransaction Id generated by the PhonePe
paymentModeStringMode of Payment. It can be anyone of the following modes: UPI_INTENTUPI_COLLECTUPI_QRCARDTOKENNET_BANKING
timestamplongTimestamp of the attempted transaction in epoch
stateStringAttempted transaction state. It can be any one of the following states:PENDINGCOMPLETEDFAILED
amountlongAmount in Paisa of the order/refund processed
errorCodeStringError code present only when the transaction state is Failed
detailedErrorCodeStringDetailed Error Code present only when transaction state is Failed
railPaymentRailContains processing rail details under which transaction attempt is made.
instrumentPaymentInstrumentV2Contains instrument details of that particular transaction Id
splitInstrumentsList<InstrumentCombo>Contains split instrument details of all the transactions made

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
Is this article helpful?