Integration

<html-block html=”

Server Side

“>

  1. Save the below assigned apiEndPoint value at your server for the debit request.
String apiEndPoint = "/v4/debit";

2.Collect the phonePeVersionCode from your app.

3.Call /v4/debit API. Refer this link for more details.

4.Select one of the API keys shared with you and note it’s index.
Construct the checksum at your server as follows:

String checksum = sha256(base64Body + apiEndPoint + saltKey) + ### + saltKeyIndex;

5.Initiate the payment process using Accept Payments API from the server and in the response you will receive the redirectURL which needs to be passed to the app side.

<html-block html=”

App Side

“>

1.Check whether the PhonePe app is installed or not on the user’s device.

public boolean doesPhonePeExist(Context context)
{
    PackageInfo packageInfo = null;
    long phonePeVersionCode = -1L;
    try {
        packageInfo = getPackageManager().getPackageInfo(PHONEPE_PACKAGE_NAME,PackageManager.GET_ACTIVITIES);
        phonePeVersionCode = packageInfo.versionCode;
    } catch (PackageManager.NameNotFoundException e) {
        Log.e(TAG, String.format("failed to get package info for package name = {%s}, exception message = {%s}",
                PHONEPE_PACKAGE_NAME, e.getMessage()));
    }

    if (packageInfo == null) {
        return false;
    }

    if (phonePeVersionCode > 94033) {
        return true;
    }
    return false;
}

2.If the PhonePe app is installed on the user’s device, then you need to get the PhonePe app version code and give it to your server.

3.Once the merchant app receives the redirectUrl from the server, the deep linking option would be shown where merchant has to handle both web and intent flows by using the below code:

Web flow:

WebView mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true);
mWebview.loadUrl(redirectUrl);
setContentView(mWebview);

Note:

  • In this case, you will receive the Web url in the “redirectURL” parameter which needs to be passed to the app side and should be launched in the android web view.
  • On loading this url, merchant will be landing on the PhonePe page, where the user can complete the payment using any payment options.
  • After completion of the payment, User interface redirection will happen to merchant redirect url. In server side merchant should be passing the following headers in the /v4/debit API: X-REDIRECT-URL and X-REDIRECT-MODE.
  • Upon payment completion merchant needs to invoke javascript bridge to inform the native Android client.
  • This will be an indication that payment flow is complete and app should check the payment status with server.

Intent flow:

Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(redirectUrl));
i.setPackage(PHONEPE_PACKAGE_NAME);
startActivityForResult(i, PHONEPE_REQUEST);

Note:

  • In this case, you will receive the intent URI in the “redirectURL” parameter in the response of the Accept Payment API response.
  • On loading this url, the merchant will invoke the PhonePe app only and completes the payment.
  • Upon payment completion merchant will receive the UI callback in onActivityResult.
  • Once you receive the UI callback, It indicates that payment flow is completed then you should check the payment status with server.

Note:

  • You will get redirectURL in the response of the Accept Payment API response.
  • For pre prod environment PhonePe package name is “com.phonepe.app.preprod”.

4.Override onActivityResult to listen to debit result:

private static final int PHONEPE_REQUEST = 123;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   super.onActivityResult(requestCode, resultCode, data);
   if (requestCode == PHONEPE_REQUEST) {
     /*This callback indicates only about
          completion of flow. 
          Inform your server to make the transaction 
          status call to get the status. Update your app with the 
          success/failure status.*/
   }
}
  • Inform your server about the completion of the transaction flow from your app.
  • Your server should make the Check Transaction Status API call to PhonePe server to check the transaction status(Even for RESULT_CANCELED).
  • Get the result and notify your app.
  • Based on the result inform the user about success or failure status of the transaction.

Server Side

1. Refer to Check Transaction Status API for the server to server call to get the transaction status.
The payment status can be Success, Failed, Pending or any of codes. For Pending, merchants should retry until the status changes to Success or Failed.

2.Refer to Refund API for the reconciliation.
There could be cases where amount gets deducted but you receive payment in Pending in the Check Transaction Status. For such cases either you can show order failed immediately and refund the amount when pending changes to success using Refund API or show order placed according to your use case.