In the onCreate() method of your Activity class, invoke tapToPaySdk.init() and tapToPaySdk.registerTransactionResultHandler().
fun init(
activity: ComponentActivity,
onInitResult: (InitResult) -> Unit
)activityComponentActivityrequired
The Android Activity
onInitResult(InitResult) -> Unitrequired
The callback for the initialisation result
data class InitResult(
val status: InitStatus,
val errorMessage: String? = null
)statusInitStatusrequired
The initialisation status.
Enum"INIT_SUCCESS""INIT_CONNECTION_ERROR""INIT_AUTH_ERROR""INIT_NFC_UNAVAILABLE_ERROR""INIT_REQUEST_PERMISSIONS_ERROR""INIT_ATTESTATION_FAILED_ERROR""INIT_ATTESTATION_ERROR""INIT_SDK_ERROR""INIT_ERROR"
errorMessageString
The error message if returned.
Register the transaction result handler to be notified of the transaction results.
fun registerTransactionResultHandler(
activity: ComponentActivity,
onTransactionResult: (TransactionResult) -> Unit,
)activityComponentActivityrequired
The Android Activity
onTransactionResult(TransactionResult) -> Unitrequired
The callback for the transaction
...
class MainActivity : ComponentActivity() {
private val viewModel: SdkDemoViewModel by viewModels { SdkDemoViewModel.Factory }
private val tapToPaySdk: TapToPaySdk
get() = TapToPaySdk.instance
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TapToPaySdkDemoApp(viewModel)
}
// register a handler for transaction results
tapToPaySdk.registerTransactionResultHandler(this) { transactionResult ->
viewModel.onTransactionResult(transactionResult)
}
// initialise the Tyro Embedded Payments SDK
// this could take some time, so show a progress spinner
viewModel.showLoadingScreen()
tapToPaySdk.init(this) { initResult ->
viewModel.onInitResult(initResult)
}
}
...
class SdkDemoViewModel(private val tapToPaySdk: TapToPaySdk) : ViewModel() {
...
fun onInitResult(initResult: InitResult) {
when (initResult.status) {
InitStatus.INIT_SUCCESS ->
_state.update { it.copy(screen = HOME) }
else ->
_state.update {
it.copy(
screen = INIT_ERROR,
errorMessage = initResult.errorMessage(),
)
}
}
}
...
fun onTransactionResult(result: TransactionResult) {
when (result.status) {
TXN_CANCELLED ->
_state.update { it.copy(screen = AMOUNT) }
TXN_SUCCESS ->
_state.update { it.copy(screen = SUCCESS) }
else ->
_state.update {
it.copy(
screen = TRANSACTION_ERROR,
errorMessage = result.errorMessage(),
)
}
}
}
...
}