Android PG SDKLess Integration

Steps to Integrate PG SDKLess UPI Open Intent Flow (ANDROID)

  1. Get the list of UPI Apps installed on the user’s device using the below code snippet.
private ArrayList < String > getInstalledUPIApps() {
    ArrayList < String > upiList = new ArrayList < > ();
    Uri uri = Uri.parse(String.format("%s://%s", "upi", "pay"));
    Intent upiUriIntent = new Intent();
    upiUriIntent.setData(uri);
    PackageManager packageManager = getApplication().getPackageManager();
    List < ResolveInfo > resolveInfoList = packageManager.queryIntentActivities(upiUriIntent, PackageManager.MATCH_DEFAULT_ONLY);
    if (resolveInfoList != null) {
        for (ResolveInfo resolveInfo: resolveInfoList) {
            upiList.add(resolveInfo.activityInfo.packageName);
        }
    }
    return upiList;
}
  1. Add the below line to your AndroidManifest.xml file
    Note: If you declare a element in your app’s manifest file, then the app associated with that package name appears in the results of any query to PackageManager that matches a component from that app.
<queries>
    <package android:name="com.phonepe.app" />								// PhonePe Prod
    <package android:name="com.phonepe.app.preprod" />						// PhonePe UAT
    <package android:name="com.google.android.apps.nbu.paisa.user" />		// GPay
  	<package android:name="net.one97.paytm" />								// Paytm
  	<package android:name="in.org.npci.upiapp" />							// BHIM
		<package android:name="in.amazon.mShop.android.shopping" />			// AmazonPay
</queries>
  1. Display the custom UI containing the list of UPI apps obtained from Step 1. on the checkout page under the UPI section.

Make Sure packageName is accessible when the user selects any of the UPI Applications.

  1. When the user selects any of the UPI apps to initiate the payment, then you must pass the following parameters in the PAY API request payload based on the app selected by the user and the device platform.
  • deviceContext.deviceOS – Possible Values: ANDROID, IOS
  • paymentInstrument.type – “UPI_INTENT
  • paymentInstrument.target app – The package name of the UPI app selected by the user to initiate the payment.
    Example: PhonePe App – “com.phonepe.app“, Gpay App – “com.google.android.apps.nbu.paisa.user“, etc.
{
  "merchantId": "MERCHANTUAT",
  "merchantTransactionId": "MT7850590068188104",
  "merchantUserId": "MU933037302229373",
  "amount": 10000,
  "callbackUrl": "https://webhook.site/callback-url",
  "mobileNumber": "9999999999",
  "deviceContext": {
    "deviceOS": "ANDROID"
  },
  "paymentInstrument": {
    "type": "UPI_INTENT",
    "targetApp": "com.phonepe.app"
  }
}
{
  "success": true,
  "code": "PAYMENT_INITIATED",
  "message": "Payment Initiated",
  "data": {
    "merchantId": "MERCHANTUAT",
    "merchantTransactionId": "OD620471739210623",
        "instrumentResponse": {
            "type": "UPI_INTENT",
            "intentUrl": "upi://pay?pa=MERCHANTUAT@ybl&pn=MerchantUAT&am=3.00&mam=3.00&tr=OD620471739210623&tn=Payment%20for%OD620471739210623&mc=5311&mode=04&purpose=00&utm_campaign=DEBIT&utm_medium=FKRT&utm_source=OD620471739210623"
    }   
    }
}
  1. After calling the PAY API, you will receive the intentUrl in response. You have to invoke the app-specific Intent using the received intentUrl. Use the below code snippet to invoke app-specific Intent.

Update the APP_PACKAGE value with the package name of the UPI app selected by the user on the merchant’s checkout page to launch the payment.

private static int B2B_PG_REQUEST_CODE = 777;

val intent = Intent()
intent.action = Intent.ACTION_VIEW
intent.data = Uri.parse(redirectUrl)	//PhonePe Intent redirectUrl from the response.
intent.setPackage(APP_PACKAGE)      	//APP_PACKAGE will be the package name of the App selected by the user.

// To Initiate Payment.
startActivityForResult(intent,B2B_PG_REQUEST_CODE);
  1. After the payment completion, once you get the UI control back to the merchant app, check for the payment status with your backend server. You can rely on a Server-to-Server callback response. If not received, you must call the Check Status API.

Override onActivityResult to receive the UI Callback after the payment completion.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
 
    if (requestCode == B2B_PG_REQUEST_CODE) {
      
      /*This callback indicates only about completion of UI flow.
            Inform your server to make the transaction
            status call to get the status. Update your app with the
            success/failure status.*/
    
    }   
}
  1. Once the payment status is validated against the order, fulfill the order.