Initiate Payment


To initiate a payment, once a payment instrument is selected and Pay is clicked, call the Create Order API to receive the Order 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(
  context = this,
   request = TransactionRequest(
      orderId = "Order_123",
      token = "OrderTokenValue",
      paymentMode = paymentMode
      ),
   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 NameTypeDescription
orderIdStringThe unique Merchant Order ID
tokenStringThe order token received in the backend from Create Order API call.
paymentModePpeIntentPaymentModePass the details of the payment mode selected by the user.
activityResultLauncherActivityResultLauncher<Intent>Pass your activityResultLauncher where you would like to handle the activityResult

When user selects an instrument, pass the type and iD of the selected instrument to getPaymentMode method to get the paymentMode.

PpeIntentPaymentMode(instrumentType: String, instrumentId: String?)
import com.phonepe.intent.sdk.api.models.transaction.paymentMode.PpeIntentPaymentMode

val paymentMode = PpeIntentPaymentMode(instrument.type, instrument.id)
Parameter NameTypeDescription
idStringThe respective Id of the instrument selected by the user on the checkout page.
typeStringThe respective type of the instrument selected by the user on the checkout page

Get Payment completion result in your activityResultLauncher in your activity or fragment.

private val activityResultLauncher: ActivityResultLauncher<Intent> = registerForActivityResult(
    StartActivityForResult()
) { 
    // Todo: call Order Status API
}

Once you get callback from SDK, you need to call the Order Status API, to get the final status of the payment.

In the next section, you will learn how to fetch the current status of an order. This is a step to confirm whether a payment is successful, pending, or failed, ensuring the final transaction status is updated accurately in your application.

Is this article helpful?