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 One Click pay/ 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.OneClickPay
)
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
}
}
}