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.
1. App Side Implementation
- 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 = uri("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. |
- Payment: Standard Checkout
- To initiate a transaction using PhonePe’s standard checkout flow, call the
startCheckoutPage()method. - Make sure to pass the Order ID and Token generated by PhonePe, which your backend received in the response of the Create Order API.
- To initiate a transaction using PhonePe’s standard checkout flow, call the
// kotlin
import com.phonepe.intent.sdk.api.PhonePeKt
import com.phonepe.intent.sdk.api.PhonePeInitException
try {
PhonePeKt.startCheckoutPage(
context = this,
token = "DOC",
orderId = "DOC",
activityResultLauncher = activityResultLauncher
)
} catch(ex: Exception) {
// Transaction could not be started.
// Either re-init the SDK and call startCheckoutPage again or use other ways to complete your transaction.
}private val activityResultLauncher: ActivityResultLauncher<Intent> = registerForActivityResult(
StartActivityForResult()
) {
// Todo: Call Order Status API to fetch the payment status
}Note: After you get the callback from the SDK, call the Order Status API to find out the final payment status. This tells if the payment was successful, failed, or still pending.
2. Server Side Implementation
This section covers the backend steps you need to follow to integrate with the PhonePe Payment Gateway. From generating tokens to verifying payment status, follow these steps to ensure a smooth and secure transaction flow.
- Fetch Auth Token
- Check if a valid Auth Token is already available. If not, call the Fetch Auth Token API.
- Call the Create Order API
- With the valid Auth Token, call the Create Order API by passing the required order details. PhonePe Payment Gateway will return an Order Token in the response.
- Pass the Order Token and Order ID to your frontend
- Send the SDK Order Token and SDK Order ID to your frontend application.
- These values will be passed to the SDK method:
PhonePeKt.startCheckoutPage.
- Check the payment status
- After the payment is completed, check the backend server if the Webhook has been received or not.
- If the callback was received, update the order status accordingly.
- If not, call the Order Status API to get the latest status.
- If the status is COMPLETED or FAILED, update the order status.
- If the status is PENDING, continue polling the Order Status API at intervals (e.g., every 15s, 30s, or 1 minute) until a terminal status is returned.
What’s Next?
You have completed setting up the Android SDK in your app. Next, let’s learn how to use the APIs, starting with generating the Authorization token.
Head over to the next section to learn how to Generate Authorization Token.