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()
.
1class MyApplication : Application() {2fun onCreate() {3if(!IZettleSDK.isInitialized) {4// Initialise the SDK with your app credentials if not already initialized5IZettleSDK.init(this, <Client ID>, <Redirect URL>)6}7// Attach the SDKs lifecycle observer to your lifecycle. It allows the SDK to8// manage bluetooth connection in a more graceful way9ProcessLifecycleOwner.get().lifecycle.addObserver(SdkLifecycle(IZettleSDK))10// Alternatively, start the SDK manually, but remember to also stop it manually through IZettleSDK.stop()11IZettleSDK.start()12}13// ...14}
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.
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.
1when(IZettleSdk.isLoggedIn) {2true -> // user is logged in3false -> // need to authenticate a user4}
- The second approach is to create an observer, this has the benefit of
1private val authObserver = object : StateObserver<User.AuthState> {2override fun onNext(state: User.AuthState) {3when (state) {4is User.AuthState.LoggedIn -> // User authorised5is User.AuthState.LoggedOut -> // There is no authorised use6}7}8}
Then you need to observe the user state.
1fun onStart() {2super.onStart()3IZettleSDK.user.state.addObserver(authObserver)4}
Remember to remove the observer when it's not used anymore.
1override fun onStop() {2super.onStop()3IZettleSDK.user.state.removeObserver(authObserver)4}
You can also use LiveData
and Observer
from AndroidX to observe the authorisation state.
1private val authObserver = Observer<User.AuthState> {2when (it) {3is User.AuthState.LoggedIn -> // User authorised4is User.AuthState.LoggedOut -> // There is no authorised use5}6}7override fun onCreate(savedInstanceState: Bundle?) {8// ...9IZettleSDK.user.state.toLiveData().observe(this, authObserver)10//...11}
The User.AuthState.LoggedIn
instance has an info
property with the following fields.
Name | Type | Description |
---|---|---|
publicName | String? | Public company or merchant name. |
imageUrl | ProfileImageUrl? | Profile image in small, medium and large size. |
userId | String? | Unique user ID. |
organizationId | String? | Unique organisation ID |
timeZone | TimeZoneId | Merchant time zone. |
country | CountryId? | Merchant country. |
currency | CurrencyId | Currency used for all payments and refunds. |
To authorise a user, call the login
method from your Activity, and provide a toolbar color compatible with your color theme.
1private fun doProvidedUILogin() {2IZettleSDK.user.login(this, ResourcesCompat.getColor(resources, R.color.colorAccent, null))3}
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.
1private fun doTokenLogin() {2IZettleSDK.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.