Android SDK Setup
Follow the steps in this section to integrate PhonePe SDK into your app. This includes adding the required dependencies, initializing the SDK, and configuring the necessary settings to start accepting payments.
Add the SDK Repository
- Update your project-level build.gradle file to include the PhonePe SDK repository.
- Here’s how to set up it in Kotlin.
Top-level build file where you can add configuration options common to all sub-projects/modules.
allprojects {
repositories {
google()
maven {
url "https://phonepe.mycloudrepo.io/public/repositories/phonepe-intentsdk-android"
}
}
}- Add the SDK Dependency
- Add the following line to your app-level build.gradle file.
implementation 'phonepe.intentsdk.android.release:IntentSDK:5.2.0'- Ensure SDK Compatibility
- Make sure your project uses the following SDK versions:
- compileSdkVersion: 28
- minSdkVersion: 21
- targetSdkVersion: 28
- These settings help ensure the SDK works smoothly across different Android devices.
- Make sure your project uses the following SDK versions:
- Initialize the SDK
- Before you make any payments, you need to set up the PhonePe SDK. This makes sure everything is ready to work properly.
- You can set up the SDK in your Application class or when you open the checkout screen.
- Here’s how to set up it in Kotlin.
Code Reference
// kotlin
import com.phonepe.intent.sdk.api.PhonePeKt
val result = PhonePeKt.init(
context = this,
merchantId = "MID",
flowId = "FLOW_ID",
phonePeEnvironment = PhonePeEnvironment.SANDBOX,
enableLogging = false,
appId = null
)
if(result){
// Good to go
}
else {
// Some error occurred in SDK. Report it to PhonePe Integration team.
// NOTE: SDK is not in the state to use. Hence, no other method should be called.
}SDK Initialization Parameters
| Parameter Name | Data Type | Description |
context | Object | Pass your activity context. |
merchantId | String | The Merchant ID shared by PhonePe |
flowId | String | Pass the merchant user ID or a unique alphanumeric random string. |
phonePeEnvironment | Enum | Values Allowed: • PhonePeEnvironment.SANDBOX (For Sandbox) • PhonePeEnvironment.RELEASE (For Prod) |
enableLogging | Boolean | [Optional Parameter] • True (To enable the SDK logs) • False (To disable the SDK logs) Note: In Prod, make sure to set as False. |
appId | String | [Optional Parameter] Can be passed as null or “” also. |
- Get UPI Apps
Get the list of UPI Apps installed on the user’s device using SDK method:
try {
List<UPIApplicationInfo> upiApps = PhonePeKt.getUpiApps();
} catch (PhonePeInitException exception) {
exception.printStackTrace();
}UPIApplicationInfo Model
public class UPIApplicationInfo {
String packageName;
String applicationName;
Long version;
}Flow Id
- Recommended but not Mandatory.
- It acts as a common ID between the merchant app user journey and PhonePe SDK.
- It can be user id, mobile number or any random id which merchant app logs so that there is a track of user between merchant app and PhonePe SDK.
It should be alphanumeric string without any special character.
Payment: Custom Checkout
To initiate a transaction for the custom checkout page for different payment modes (like Intent/Collect/Netbanking), then call startTransaction method.
- Pass the Order ID which is generated and received from the backend.
- Pass the Token which was received in response of Create order API from your backend.
- Pass the paymentMode object with the required values corresponding to the type of payment instrument selected by the user.
Call PhonePeKt.startTransaction to start the transaction flow.
// kotlin
import com.phonepe.intent.sdk.api.PhonePeInitException
import com.phonepe.intent.sdk.api.PhonePeKt
import com.phonepe.intent.sdk.api.models.transaction.TransactionRequest
try{
PhonePeKt.startTransaction(
context = this,
request = TransactionRequest(
orderId = "Order_123",
token = "OrderTokenValue",
paymentMode = paymentMode
),
showLoaderFlag = true,
activityResultLauncher = activityResultLauncher
)
}
catch(ex: PhonePeInitException){
// Transaction could not be started.
// Either re-init the SDK and call startTransaction again or use other instruments to complete your transaction.
}| Parameter Name | Type | Description |
|---|---|---|
orderId | String | The PhonePe Order ID received in the Create Order API response. |
token | String | The order token received in the backend from Create Order API call. |
paymentMode | PpeIntentPaymentMode | Pass the details of the payment mode selected by the user. |
| ActivityResultLauncher<Intent> | Pass your activityResultLauncher where you would like to handle the activityResult |
| Boolean | [Optional] Use for hiding the loader visibility in PhonePe app during intent launch |
Payment Mode Details (For Custom Only)
UpiIntentPaymentMode
import com.phonepe.intent.sdk.api.models.transaction.paymentMode.UpiIntentPaymentMode
paymentMode = UpiIntentPaymentMode(
targetApp = "com.phonepe.app"
)| Parameter Name | Type | Description |
|---|---|---|
targetApp | String | Pass the package name of the UPI app selected by the user |
UpiCollectPaymentMode
// using UpiCollectPaymentMode with VPA
import com.phonepe.intent.sdk.api.models.transaction.paymentMode.UpiCollectDetails
import com.phonepe.intent.sdk.api.models.transaction.paymentMode.UpiCollectPaymentMode
paymentMode = UpiCollectPaymentMode(
details = UpiCollectDetails.VPA(
vpa = "abc@ybl"
),
message = "message"
)
//using UpiCollectPaymentMode with phone number
import com.phonepe.intent.sdk.api.models.transaction.paymentMode.UpiCollectDetails
import com.phonepe.intent.sdk.api.models.transaction.paymentMode.UpiCollectPaymentMode
paymentMode = UpiCollectPaymentMode(
details = UpiCollectDetails.Phone(
phoneNumber = "9999999999"
),
message = "message"
)| Parameter Name | Type | Description |
|---|---|---|
vpa | String | [With VPA] Pass the receiver VPA |
phoneNumber | String | [With Phone Number] Pass the receiver phone number |
message | String | [Optional Parameter] The message for the transaction |
NetBankingPaymentMode
import com.phonepe.intent.sdk.api.models.transaction.paymentMode.NetBankingPaymentMode
paymentMode = NetBankingPaymentMode(
bankId = "DOC"
)| Parameter Name | Type | Description |
|---|---|---|
| String | Bank Id of the bank selected by the user. |
PpeIntentPaymentMode
import com.phonepe.intent.sdk.api.models.transaction.paymentMode.PpeIntentPaymentMode
paymentMode = PpeIntentPaymentMode()Get Payment completion result in your activityResultLauncher in your activity or fragment.
private val activityResultLauncher: ActivityResultLauncher<Intent> = registerForActivityResult(
StartActivityForResult()
) {
// Todo: Call Order Status API to fetch the payment status
}Once you get callback from SDK, you need to call the Order Status API, to get the final status of the payment.