### Step 1: Android PG SDK setup

Install PhonePe PG SDK using Android Studio. To add the SDK to the android app please follow the steps given below:

  1. Add the below code to 'repositories' section of your project level build.gradle file

  1. Add the below line to the 'dependencies' section of your app build.gradle.

<!-- SNAPSHOT implementation 'phonepe.intentsdk.android.snapshot:IntentSDK:1.6.6-SNAPSHOT2’ RELEASE implementation 'phonepe.intentsdk.android.release:IntentSDK:1.6.6’ For, SDK version 1.6.6, We have compileSdkVersion: 29, minSdkVersion: 24, targetSdkVersion: 29 For, SDK version 1.6.7, We have compileSdkVersion: 28, minSdkVersion: 21, targetSdkVersion: 28implementation 'phonepe.intentsdk.android.snapshot:IntentSDK:[email protected]’ implementation 'phonepe.intentsdk.android.release:IntentSDK:[email protected]’ For, SDK version 1.6.8, We have compileSdkVersion: 28, minSdkVersion: 21, targetSdkVersion: 28| implementation 'phonepe.intentsdk.android.snapshot:IntentSDK:1.7.1-546661-SNAPSHOT’ implementation 'phonepe.intentsdk.android.release:IntentSDK:1.7.1’\-->

For, SDK version 1.8.0

  • We have compileSdkVersion: 28, minSdkVersion: 21, targetSdkVersion: 28

  • Debuggable flag is removed from version 1.8.0 and above

  1. Add meta-data in \<application\> tag of AndroidManifest.xml file.

Post UAT Sign-Off, get the App Id from PhonePe for Production

Share us with the Android signature using the below code in your android application(Production). String string_signature = PhonePe.getPackageSignature()

### Step 2: SDK Initialization

  1. Initialize the PhonePe SDK in an Activity that gets launched before a user selects the PhonePe option on your checkout screen.

**Note**: Don't initialize in the Application class of your project.

  1. Get a list of UPI Apps installed on the user’s device:

**UPIApplicationInfo Model**

  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.

### Step 3: Server-Side Setup

  1. Use the below PhonePe Pay API endPoint value for the debit request.

  1. Construct the request body and encode it using Base64.

  1. Select one of the salt keys and saltIndex. Refer [link](🔗) link for salt & index credentials. Use these values to construct the checksum as follows:

  1. Pass on these values to your Android app in an API.

### Step 4: Payment using the SDK

  1. Use the B2BPGRequestBuilder to construct B2BPGRequest from the data obtained from your server as mentioned in [Step-3](🔗).

  1. Define an intent request code for initiating a PhonePe transaction.

  1. Initiate payment using any UPI app.

  1. Override onActivityResult to receive a callback once the PhonePe app exits.

### Step 5: Handling callback from PhonePe

In order to receive [server to server callback](🔗) from the PhonePe server, we recommend merchants to pass **callbackUrl** in the request body of the PAY API. Before considering this response you need to validate the checksum and amount parameter. This will provide the payment status response as success/failure only.

  1. After the transaction is complete: Once the transaction is complete, you will get a Base64 Encoded JSON response along with the headers with checksum value. Find the below code snippet:

  1. To calculate the checksum in the server to server callback response. Find the code snippet

  1. To validate the amount, the merchant needs to compare the amount parameter received in the [server to server callback response](🔗) with the amount parameter passed in the [Pay API](🔗)

  2. You need to [validate the X-verify](🔗). Once both checksum and the amount parameters are validated, order can be fulfilled based on the response. Note that completion of a transaction does not imply that payment is successful. Payment can be in a successful or failed state which needs to be derived from JSON response. Here is the code snippet:

  1. When the back button is pressed by customer, SDK will return a UI callback to the App.You should take action as mentioned below

UI callbacksServer to Server callbacksCheck Transaction Status APIAction
RESULT_OKSuccess / FailureSuccess / FailureTake the decision based on response.
RESULT_CANCELLEDSuccess / FailureSuccess / FailureTake the decision based on response.
RESULT_CANCELLEDXPayment PendingTemporarily Mark transaction as payment failure. Timeout is 10 minutes. Call Check Transaction Status API after 10 to 15 minutes. If response is failure then mark as failure transaction in the system. If the response is successful then process the Refund.
RESULT_CANCELLEDXInternal Server Error Timed OutCall Check Transaction Status API - Take decisions based on the response received.


### Step 6: Validating the X-verify

Once the response is received, you need to validate the X-verify in order to ensure that the response received is not tampered with. In order to validate the X-verify, please follow the below steps:

  • Generate the checksum by using a SHA256 of the encoded body received in the response, salt key and salt index

  • The salt we would use will be appended with ### to the checksum value in the checksum attribute. Upon receiving this payload, you need to look at the salt_index in the checksum attribute after ### delimiter and use the appropriate salt_index to be able to calculate checksum at their end for the said payload. If it doesn't match, then you need to abort the request.

  • Salts are already pre-shared with you at the time of onboarding

  • If the value of any parameter is null, it will not be included in the calculation of the checksum.