SDK Integration

Add Dependencies & Android Manifest declarations

  1. Add the CloudRepo Maven Repository in build.gradle:
allprojects {
    repositories {
        google()
        jcenter()
        maven {
            url "https://phonepe.mycloudrepo.io/public/repositories/phonepe-intentsdk-android"
    }
}
  1. Add the below line to the 'dependencies' section of your app build.gradle
implementation 'phonepe.intentsdk.android.release:IntentSDK:1.6.4'
implementation 'com.google.android.gms:play-services-ads-identifier:16.0.0'

( Optional - If you aren’t using Kotlin in your app, add the following Kotlin stdlib dependency

implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.3.50'
  1. Add below line inside application tag of your AndroidManifest.xml file
<!-- Keep it false in production environment--> 
<meta-data
   android:name="com.phonepe.android.sdk.Debuggable"
   android:value="true"/>
<!-- Keep it false in production environment-->
<meta-data
   android:name="com.phonepe.android.sdk.isUAT"
   android:value="true"/> 
<!-- Get the MerchantId & App Id from PhonePe-->
<meta-data
    android:name="com.phonepe.android.sdk.MerchantId"
    android:value="@string/<merchantId-shared-by-phonepe>"/>
<meta-data
    android:name="com.phonepe.android.sdk.AppId"
    android:value="@string/<appId-shared-by-phonepe>" />

Android SDK Integration

  1. Initialize the SDK ( ideally in onCreate of your MainActivity ) using:
PhonePe.init(this);
  1. Get a list of UPI Apps installed on the user’s device:
try {
    List<UPIApplicationInfo> upiApps = PhonePe.getUpiApps();
} catch (PhonePeInitException exception) {
    exception.printStackTrace();
}

UPIApplicationInfo Model

public class UPIApplicationInfo {
    String packageName;
    String applicationName;
    Long version;
}
  1. Show your custom UI containing the list of apps obtained from step 2. Make Sure packageName is accessible when the user selects any of the UPI Application.

  2. Create transactionRequest as follows:

TransactionRequest transactionRequest = new TransactionRequestBuilder()
    .setData(base64Body)
    .setChecksum(checksum)
    .setUrl("/v4/debit")
    .build();

🚧

Note

base64Body and checksum need to be constructed on the server-side.
Please refer to the section Server Side Steps for details.

  1. Start the transaction activity by passing the packageName of the App selected by the user & transactionRequest obtained from step 4.
private static final int PHONEPE_PAYMENT_REQUEST_CODE = 101;

try {
    startActivityForResult(PhonePe.getImplicitIntent(this,transactionRequest,
    packageName),PHONEPE_PAYMENT_REQUEST_CODE);
} catch (PhonePeInitException e) {
    //handle invalid packageName & app not installed cases here.
    e.printStackTrace();
}
  1. Handle onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PHONEPE_PAYMENT_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.*/
    }
}

Server Side Steps

  1. Define the API endPoint and request parameters at your server as follows:
String apiEndPoint = "/v4/debit";
  1. Construct the base64Body request of the defined data as follows:
HashMap<String, Object> data = new HashMap<>();
data.put("merchantId", “UATMERCHANT”); //String. Mandatory
data.put("transactionId", “TX123456789”); //String. Mandatory.
data.put("amount", 100); //Long. Mandatory
data.put("merchantOrderId", “OD1234”); //String. Mandatory
data.put("message", “Payment for order 001234”); //String. Optional
data.put("mobileNumber", “9xxxxxxxxx”); //String. Optional
data.put("paymentScope", "ALL_UPI_APPS"); //Mandatory
data.put("openIntentWithApp",packageName); //Mandatory
// transactionId length should be less than 38 characters.
// merchantOrderId length should be less than 48 characters.
// Put more info as intimated. Nesting is allowed

String base64Body = Base64(new GSON().toJson(data));
  1. Select one of the API keys shared with you and note its index. Construct the checksum at your server, as follows:
String checksum = SHA256(base64Body + apiEndPoint + salt) + ### + saltIndex;

References

  • Use the Check Transaction Status API to check the status of the payment transaction. Refer to this link.

  • S2S Callback, Refer to this link

  • For refund API, Refer to this link.