Handling Payment Result

Every returning intent will return a serializable PaymentResult object. It can be retrieved from the Intent data by using the PaymentResultInfo.ResultKeys.Transaction key.

import com.yoco.payment_ui_sdk.data.result.PaymentResult
val transactionResult: PaymentResult? = data?.getSerializableExtra(PaymentResultInfo.ResultKeys.Transaction) as PaymentResult?

Parameters

  1. clientTransactionId: String - a unique identifier of this payment.
  2. amountInCents: Long - The amount charged initially for the payment (the bill amount)
  3. tipInCents: Long - The tip amount offered by the cardholder
  4. currency: SupportedCurrency - currency that was used
  5. finalAmountInCents: Long - The total amount charged from the cardholder (bill amount + tip amount)
  6. paymentType: PaymentType - type of payment that was done
  7. staffMember: YocoStaff? - Optional: staff member performing the transaction
  8. userInfo: Map<String, Any>? - (Optional) user info map containing additional user info that is returned based on what was passed in if any
  9. errorMessage: String? - (Optional) (Optional) error message when transaction fails/cancels/aborts

Response Result Codes

Every returning intent will pass back a result code which will provide specific info on the result of the transaction.

Result CodeValue
PaymentResultInfo.ResultCode.SUCCESSFUL1
PaymentResultInfo.ResultCode.IN_PROGRESS2
PaymentResultInfo.ResultCode.ERROR_TRANSACTION_FAILED3
PaymentResultInfo.ResultCode.ERROR_CANCELLED4
PaymentResultInfo.ResultCode.ERROR_PERMISSION_DENIED5
PaymentResultInfo.ResultCode.ERROR_NO_CONNECTIVITY6
PaymentResultInfo.ResultCode.ERROR_INVALID_TOKEN7
PaymentResultInfo.ResultCode.ERROR_BLUETOOTH_DISABLED8
PaymentResultInfo.ResultCode.ERROR_REFUND_FAILED9
PaymentResultInfo.ResultCode.ERROR_TRANSACTION_LOOKUP_FAILED10
PaymentResultInfo.ResultCode.ERROR_TRANSACTION_NOT_FOUND11
PaymentResultInfo.ResultCode.ERROR_PRINT_FAILED12

Usage

After a successful or failed payment from YocoSDK.charge() flow an Intent result will be returned, containing info related to the transaction.

The standard way of handling an intent result:

import com.yoco.payment_ui_sdk.YocoSDK
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
// Check for the result of the charge request
if (requestCode == PaymentResultInfo.RequestCode.CHARGE_REQUEST) {
// Handle charge result here
}
}

Alternative Usage

So there is a new way to handle intent results, by using the Activity Results API with androidx.

Required Dependencies:

implementation 'androidx.activity:activity-ktx:1.5.0' //Activity
implementation 'androidx.fragment:fragment-ktx:1.5.0' //Fragment

Instantiate the result contract, which may be passed to the YocoSDK as the result. There is no need to use the request code, as we tie the contract to the charge() implementation.

import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
private val chargeResult: ActivityResultLauncher<Intent> =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
// Handle charge result here
}
}
// Access SDK
YocoSDK.charge(
...
result = chargeResult
)
// OR
private val refundResult: ActivityResultLauncher<Intent> =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
System.err.println("Result Code: ${result.resultCode}") // PaymentSDK.Response.ResultCode
if (result.resultCode == Activity.RESULT_OK) {
// Handle refund result here
}
}
YocoSDK.refund(
...
result = refundResult
)