Initialise SDK
tapToPaySdk.init(activity, onInitResult)
In the onCreate()
method of your Activity
class, invoke tapToPaySdk.init()
and tapToPaySdk.registerTransactionResultHandler()
.
fun init(
activity: ComponentActivity,
onInitResult: (InitResult) -> Unit
)
Parameters
activity required | ComponentActivity The Android Activity |
onInitResult required | (InitResult) -> Unit The callback for the initialisation result |
InitResult
data class InitResult(
val status: InitStatus,
val errorMessage: String? = null
)
Parameters
status required | InitStatus The initialisation status. |
errorMessage | String The error message if returned. |
tapToPaySdk.registerTransactionResultHandler(activity, onTransactionResult)
Register the transaction result handler to be notified of the transaction results.
fun registerTransactionResultHandler(
activity: ComponentActivity,
onTransactionResult: (TransactionResult) -> Unit,
)
Parameters
activity required | ComponentActivity The Android Activity |
onTransactionResult required | (TransactionResult) -> Unit The callback for the transaction |
See Transaction Result
Example Implementation
...
class MainActivity : ComponentActivity() {
private val viewModel: SdkDemoViewModel by viewModels { SdkDemoViewModel.Factory }
private val tapToPaySdk: TapToPaySdk
get() = (application as SdkDemoApplication).tapToPaySDK
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(),
)
}
}
}
...
}