Initialisation

The best place to initialise the SDK is in your Application class. If you don't have one, we recommend that you create one.

After initialisation you need to start the SDK. Preferably this is done by adding an observer on behalf of the SDK, to the ProcessLifecycleOwner. This way the SDK will automatically start and stop itself.

You can also handle it manually by calling start() and stop().

1
class MyApplication : Application() {
2
fun onCreate() {
3
// Initialise the SDK with your app credentials
4
val sdk = IZettleSDK.init(this, <Client ID>, <Redirect URL>)
5
// Attach the SDKs lifecycle observer to your lifecycle. It allows the SDK to
6
// manage bluetooth connection in a more graceful way
7
ProcessLifecycleOwner.get().lifecycle.addObserver(SdkLifecycle(IZettleSDK))
8
// Alternatively, start the SDK manually, but remember to also stop it manually.
9
sdk.start()
10
}
11
// ...
12
}

User authorisation

Your application is responsible for user authorisation. The SDK itself doesn't track auth state, but will return NotAuthorized errors if you try to take payments or make refunds without valid user authorisation.

Observe authorisation state

To track if the user is authorised or not you need to create an observer.

1
private val authObserver = object : StateObserver<User.AuthState> {
2
override fun onNext(state: User.AuthState) {
3
when (state) {
4
is User.AuthState.LoggedIn -> // User authorised
5
is User.AuthState.LoggedOut -> // There is no authorised use
6
}
7
}
8
}

Then you need to observe the user state.

1
fun onStart() {
2
super.onStart()
3
IZettleSDK.user.state.addObserver(authObserver)
4
}

Remember to remove the observer when it's not used anymore.

1
override fun onStop() {
2
super.onStop()
3
IZettleSDK.user.state.removeObserver(authObserver)
4
}

You can also use LiveData and Observer from AndroidX to observe the authorisation state.

1
private val authObserver = Observer<User.AuthState> {
2
when (it) {
3
is User.AuthState.LoggedIn -> // User authorised
4
is User.AuthState.LoggedOut -> // There is no authorised use
5
}
6
}
7
override fun onCreate(savedInstanceState: Bundle?) {
8
// ...
9
IZettleSDK.user.state.toLiveData().observe(this, authObserver)
10
//...
11
}

The User.AuthState.LoggedIn instance has an info property with the following fields.

NameTypeDescription
publicNameString?Public company or merchant name.
imageUrlProfileImageUrl?Profile image in small, medium and large size.
userIdString?Unique user ID.
organizationIdString?Unique organisation ID
timeZoneTimeZoneIdMerchant time zone.
countryCountryId?Merchant country.
currencyCurrencyIdCurrency used for all payments and refunds.

Initiate authorisation with provided UI

To authorise a user, call the login method from your Activity, and provide a toolbar color compatible with your color theme.

1
private fun doProvidedUILogin() {
2
IZettleSDK.user.login(this, ResourcesCompat.getColor(resources, R.color.colorAccent, null))
3
}

Initiate authorisation with token

The SDK requires a proof key for code exchange (PKCE) flow for the user to authorise. If you have a PKCE flow setup to get access and refresh tokens, you can call the login method with the refresh token as parameter. For information about setting up a PKCE flow, see OAuth PKCE method.

1
private fun doTokenLogin() {
2
IZettleSDK.user.login("pre-authorized-refresh-token")
3
}

Note: You still need to declare the OAuthActivity in your manifest since it's used when performing refunds.