In this part we will talk about how you can integrate android SDK and initiate the payment.
Quick Links
App Side Implementation
Android PG SDK Setup
- Add the below code to ‘repositories’ section of your project level build.gradle file
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 below line to the ‘dependencies’ section of your app level build.gradle file
implementation 'phonepe.intentsdk.android.release:IntentSDK:5.0.1'
For our SDK :
We have compileSdkVersion: 28, minSdkVersion: 21, targetSdkVersion: 28
SDK Initialization
- Initialize the SDK on launch of your application (Application class) or launch of your checkout activity. This needs to be done before calling any method of Phonepe SDK. This doesn’t block Main Thread
// 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 | Type | Description |
---|---|---|
context | Object | Pass your activity context |
merchantId | String | The merchantId shared by PhonePe |
flowId | String | Pass the merchant user Id or a unique alphanumeric random string |
phonePeEnvironment | Enum | Values Allowed: – PhonePeEnvironment.SANDBOX (For PreProd) – 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 page, then call startCheckoutPage method.
- Pass the PhonePe generated Order ID and Token which was received in response of Create order API from your backend.
// 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.
}
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 Create Order API response. |
activityResultLauncher | ActivityResultLauncher<Intent> | Pass your activityResultLauncher where you would like to handle the activityResult |
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.
Server Side Implementation
Step 1. Fetch Auth Token
Merchants to check whether the valid Auth Token is present already. If not, the Fetch Auth Token API should be called to get the valid Auth Token.
Step 2. Call the Create Order API
Merchants should call Create Order API for Order creation with the valid Auth Token from the merchant Backend side by passing the required details. PhonePe backend will pass the Order Token in response to the merchant Backend.
Step 3. Pass the Order Token and Order ID to frontend
Merchant should pass the Order Token and Order ID received in the Create Order API response to the frontend application which will further passed to the SDK method: PhonePeKt.startCheckoutPage.
Step 4. Check the payment status
Once the payment is completed, merchants should check with the backend server if the Webhook has been received or not.
- If not, merchants should call the Order Status API to fetch the current payment status.
- If the status is terminal status like: COMPLETED or FAILED, the the order status can be updated accordingly.
- If incase, the status is PENDING, then merchants should call the Order Status API at regular intervals like every 15 secs or 30 secs or 1min once till the terminal status is reached.