Initialisation

The best place to initialise the SDK is in your Application class. If you don't have one, you can create one by extending the android.app.Application class. If you need to check if the SDK is initialised for the app business logic, check that the flag IZettleSDK.isInitialized is set to true. To make sure the SDK is initialised, you can call IZettleSDK.init more than once. If the SDK is already initialised, the call will be ignored.

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
if(!IZettleSDK.isInitialized) {
4
// Initialise the SDK with your app credentials if not already initialized
5
IZettleSDK.init(this, <Client ID>, <Redirect URL>)
6
}
7
// Attach the SDKs lifecycle observer to your lifecycle. It allows the SDK to
8
// manage bluetooth connection in a more graceful way
9
ProcessLifecycleOwner.get().lifecycle.addObserver(SdkLifecycle(IZettleSDK))
10
// Alternatively, start the SDK manually, but remember to also stop it manually through IZettleSDK.stop()
11
IZettleSDK.start()
12
}
13
// ...
14
}

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 see if the user is authorised or not, you can do it in two different ways.

  • The first is a simple flag indicating weather or not a user is logged in.
1
when(IZettleSdk.isLoggedIn) {
2
true -> // user is logged in
3
false -> // need to authenticate a user
4
}
  • The second approach is to create an observer, this has the benefit of
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.