Webhook Handling

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

The validateCallback() method is used to validate webhook or callback responses. You can use this method by passing all the necessary parameters.

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
import com.phonepe.sdk.pg.Env;
import com.phonepe.sdk.pg.payments.v2.CustomCheckoutClient;
import com.phonepe.sdk.pg.common.models.response.CallbackResponse;
 
String clientId = "<clientId>";
String clientSecret = "<clientSecret>";
Integer clientVersion = clientVersion;  //insert your client version here
Env env = Env.SANDBOX;      //change to Env.PRODUCTION when you go live
 
CustomCheckoutClient customCheckoutClient = CustomCheckoutClient.getInstance(clientId, clientSecret,
        clientVersion, env);
 
String username = "<username>";
String password = "<password>";
String authorization = "<authorization>";
String responseBody = "<responseBody>";
 
CallbackResponse callbackResponse = customCheckoutClient.validateCallback(username, password, authorization,
        responseBody);
String callbackType = callbackResponse.getType();
String orderId = callbackResponse.getPayload()
        .getOrderId();
String state = callbackResponse.getPayload()
        .getState();
  • 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
typeCallbackTypeTells you what type of event happened (e.g., order completed, refund failed, etc.)
payloadCallbackDataContains all the details related to that event
  • The event type are explained below:
Event TypeDescription
PG_ORDER_COMPLETEDThe payment was successfully completed
PG_ORDER_FAILEDThe payment failed
PG_REFUND_COMPLETEDA refund was successfully processed
PG_REFUND_FAILEDA refund request failed
  • The payload details are explained below:
Parameter NameData TypeDescription
merchantIdStringMerchant ID from which the request was initiated
orderIdStringOrder ID generated by PhonePe Payment Gateway (only for order callbacks)
originalMerchantOrderIdStringOrder ID generated by you (only for order callbacks)
refundIdStringRefund ID generated by PhonePe PG (only for refund callbacks)
merchantRefundIdStringRefund ID generated by you (only for refund callbacks)
stateStringThe current state of the order or refund.
amountLongThe amount processed in paisa.
expireAtLongThe expiry timestamp in epoch format
errorCodeStringThe error code (only for failed transactions)
detailedErrorCodeStringA more detailed error code (only for failures)
metaInfoMetaInfoMetadata passed during order initialization
paymentDetailsList<PaymentDetail>The Payment details of the transaction
  • The PaymentRefundDetail 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)

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 :

import com.phonepe.sdk.pg.Env;

import com.phonepe.sdk.pg.payments.v2.CustomCheckoutClient;

import com.phonepe.sdk.pg.payments.v2.models.request.CustomCheckoutPayRequest;

import com.phonepe.sdk.pg.payments.v2.models.response.CustomCheckoutPayResponse;
 
String clientId = "<clientId>";
String clientSecret = "<clientSecret>";
Integer clientVersion = 1;      //insert your client version here
Env env = Env.SANDBOX;          //change to Env.PRODUCTION when you go live
 
CustomCheckoutClient customCheckoutClient = CustomCheckoutClient.getInstance(clientId, clientSecret, clientVersion, env);
 
String merchantOrderId = "<duplicateId>";  //will throw exception
long amount = 100;
String redirectUrl = "https://merchant.com/redirectUrl";
 
CustomCheckoutPayRequest request = CustomCheckoutPayRequest.UpiQrRequestBuilder()
        .merchantOrderId(merchantOrderId)
        .amount(amount)
        .redirectUrl(redirectUrl)
        .build();
 

try{
    StandardCheckoutPayResponse standardCheckoutPayResponse = standardCheckoutClient.pay(standardCheckoutPayRequest);
}

catch(PhonePeException phonePeException)
{
    Integer httpStatusCode = phonePeException.getHttpStatusCode();
    String message = phonePeException.getMessage();
    Map<String, Object> data = phonePeException.getData()
    String code = phonePeException.getCode()
}

Use the following response model structure to access payment related details after calling getOrderStatus():

Payment Rail

The rail attribute within the PaymentDetail object indicates the type of payment rail used for the transaction. This value helps you identify the payment method such as UPI, Card, or Wallet through which the transaction was processed.

UPI RAIL
PropertyType
typePaymentRailType
utrString
upiTransactionIdString
vpaString
PG RAIL
PropertyType
typePaymentRailType
transctionIdString
authorizationCodeString
serviceTransactionIdString
PPI WALLET RAIL
PropertyType
typePaymentRailType
PPI EGV RAIL
PropertyType
typePaymentRailType
  • PaymentInstrumentV2
    • Represents the instrument used to initiate a payment. Various instrument types are listed below:
ACCOUNT
PropertyType
typePaymentInstrumentType
ifscString
accountTypeString
maskedAccountNumberString
accountHolderNameString
CREDIT_CARD
PropertyType
typePaymentInstrumentType
bankTransactionIdString
bankIdString
arnString
brnString
DEBIT_CARD
PropertyType
typePaymentInstrumentType
bankTransactionIdString
bankIdString
arnString
brnString
NET_BANKING
PropertyType
typePaymentInstrumentType
bankTransactionIdString
bankIdString
arnString
brnString
EGV
PropertyType
typePaymentInstrumentType
cardNumberString
programIdString
WALLET
PropertyType
typePaymentInstrumentType
walletIdString
  • Instrument Constraints
ACCOUNT
PropertyType
typeAccount
accountNumberString
ifscString
Is this article helpful?