This section explains how merchant a app can integrate PhonePe Android SDK and initiate payment.
- Add the below code to ‘repositories’ section of your project level build.gradle file
//https://dash.readme.com/project/phonepe-docs/v1/docs/android-pg-sdk-integration 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:3.0.0'
For our SDK :
We have compileSdkVersion: 28, minSdkVersion: 21, targetSdkVersion: 28
- 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, // todo: pass your activity context here
merchantId = "DOC" //todo: change this to your merchant id
)
if(result){
// good to go
}else {
// some error occured in SDK. report it to phonepe integration team
// NOTE: SDK is not in the state to use. no other method should be called
}
- Get payment options from SDK:
create an instance of PhonePeOptionManager
import com.phonepe.intent.sdk.api.ppeOptions.PhonePeOptionManager
val phonePeOptionManager = PhonePeOptionManager(
token = "TODO", // TODO: This is the token fetched in Saved Instruments Init API
flowId = "", // TODO: send a unique alphanumeric random string here
callback = this // TODO: PhonePeOptionsCallback instance
)
call getOptions method of PhonePeOptionManager. You will get the result in the callback methods of PhonePeOptionsCallback
phonePeOptionManager.getOptions()
Callback details:
- fun
showLinkButton()
– You get this callback when phonepe doesn’t have user consent to share phonepe options. Once you get this callback, show your UI with which user can interact with to trigger the consent (preferably a button which says link PhonePe).Once user clicks on the above button, call below method to trigger consent.
phonePeOptionManager.linkPhonePeAccount(
activity = this // send the activity instance here which extends AppCompatActivity
)
- fun
hideLinkButton(reason: LinkButtonHideReason)
: When you get this callback, you can hide your UI for linking phonepe account. You additionaly get the reason in the param reason. You can optionally log it on your side for any issue debugging. - fun
onConsentGiven()
: You can optionally override this. This callback is given when user accepts the consent. - fun
onConsentNotGiven()
: You can optionally override this. This callback is given when user rejects the consent. - fun onOptionsReady()
fun onOptionsReady(resultCode: OptionsResultCode, options: List<PaymentOption>? = null, additionalInfo: String? = null)
You get the callback as a terminal state of this flow. options are returned when all the conditions are met.
If resultCode == OptionsResultCode.SUCCESS, then you can render the options on your UI.
for any error, you get appropriate code in resultCode parameter.
- Once user selects one of the payment option and click on pay, Hit the Create Order API from your backend and get the token and merchant order id.
- Call PhonePeKt.startTransaction to start the transaction flow.
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(
activity = this,
request = TransactionRequest(
orderId = "DOC", // todo: get this from your backend
token = "DOC", // todo: get this from your backend
paymentMode = // more on this below
),
requestCode = 725 // todo: pass your own request code that you would like to handle in onActivityResult method
)
)
} catch(ex: PhonePeInitException){
// Transaction could not be started.
// either re-init the SDK and call startTransaction again
// or use other ways to complete your transaction
}
paymentMode parameter can be of two different Types, you need to pass the correct payment mode based on the instrument selected by the user:
- If UPI Account is selected by user:
import com.phonepe.intent.sdk.api.models.transaction.paymentMode.PpeIntentPaymentMode
import com.phonepe.intent.sdk.api.models.transaction.selectedOption.SelectedOption
paymentMode = PpeIntentPaymentMode(
selectedOption = SelectedOption.Account(accountId = "ACC_ID") //todo: send account id here
)
- If Flash pay is selected by user:
import com.phonepe.intent.sdk.api.models.transaction.paymentMode.PpeIntentPaymentMode
import com.phonepe.intent.sdk.api.models.transaction.selectedOption.SelectedOption
paymentMode = PpeIntentPaymentMode(
selectedOption = SelectedOption.FlashPay
)
Get Payment completion result by overriding onActivityResult in your activity.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when (requestCode) {
725 -> { // todo: the request code that you passed to phonepe SDK
// once you get callback from sdk, hit phonepe transaction status API
}
}
}