Skip to content
Last updated

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

activityComponentActivityrequired

The Android Activity

onInitResult(InitResult) -> Unitrequired

The callback for the initialisation result

InitResult

data class InitResult(
    val status: InitStatus,
    val errorMessage: String? = null
)

Parameters

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.

tapToPaySdk.registerTransactionResultHandler(activity, onTransactionResult)

Register the transaction result handler to be notified of the transaction results.

fun registerTransactionResultHandler(
       activity: ComponentActivity,
       onTransactionResult: (TransactionResult) -> Unit,
   )

Parameters

activityComponentActivityrequired

The Android Activity

onTransactionResult(TransactionResult) -> Unitrequired

The callback for the transaction

Example Implementation

...
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(),
                    )
                }
        }
    }

...
}