Initialise SDK

tapToPaySdk.init(activity, onInitResult)

In the onCreate() method of your Activity class, invoke tapToPaySdk.init() and tapToPaySdk.registerTransactionResultHandler().

Copy
Copied
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

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

Parameters

status
required
InitStatus

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"
errorMessage
String

The error message if returned.

tapToPaySdk.registerTransactionResultHandler(activity, onTransactionResult)

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

Copy
Copied
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

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

...
}
Copyright © Tyro Payments 2019-2024. All right reserved.