Android SDK Setup
Follow the steps in this section to integrate PhonePe SDK (v5.3.0) 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.
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.3.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.
// 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.
}| 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
try {
List<UPIApplicationInfo> upiApps = PhonePeKt.getUpiApps();
} catch (PhonePeInitException exception) {
exception.printStackTrace();
}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
Use the particular instrument that you need for your request.
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()NewCardPaymentMode
⚠️ Compliance for SDK Payments!
To accept card payments via the SDK, you must be either PCI or SAQ-A-EP compliant.
import com.phonepe.intent.sdk.api.models.transaction.paymentMode.NewCardPaymentMode
paymentMode = NewCardPaymentMode(
cardNumber = "5172626166161",
cardHolderName = "cardHolderName",
cvv = "123",
expiryMonth = "08",
expiryYear = "29",
merchantUserId = "merchantUserId"
)Next you need to call the validateCardDetails method, to check the card details before calling the startTransaction:
PhonePeKt.validateCardDetails(paymentMode,
object : ValidationListener {
override fun onValidationSuccess() {
Log.i(TAG, "Card details validated successfully")
// can make startTransaction() call
}
override fun onValidationFailure(errorMessage: String) {
Log.i(TAG, "Card details validation failed")
// error handling
}
},
lifecycleScope
)| Parameter | DataType | Description |
cardNumber | String | Card number entered by user. |
cardHolderName | String | Card holder name entered by user. |
cvv | String | Card’s CVV or security code. |
expiryMonth | String | Card’s expiration month entered in mm format. |
expiryYear | String | Card’s expiration year entered in yyyy format. |
merchantUserId | String | Your unique user ID or a random string you’ve generated for this transaction. |
Before you can start a payment, you’ll need to create a NewCardPaymentMode. This is the object you’ll use when you call the startTransaction method.
Here’s how the payment flow works:
- Validation check: If the card number, CVV, expiry date, or cardholder name is invalid, we will let you know right away through an onValidationFailure callback.
- Share the message: You should use this message to tell your customer what is wrong so they can fix the details and try again.
- Ready to go: Once the card details are correct, we will send you an onValidationSuccess callback. This is your green light to call the startTransaction method and begin the payment process.
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.