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()
.
1class MyApplication : Application() {2fun onCreate() {3// Initialise the SDK with your credentials4val sdk = IZettleSDK.init(this, <Client ID>, <Redirect URL>)5// Attach the SDKs lifecycle observer to your lifecycle. It allows the SDK to6// manage bluetooth connection in a more graceful way7ProcessLifecycleOwner.get().lifecycle.addObserver(SdkLifecycle(IZettleSDK))8// Alternatively, start the SDK manually, but remember to also stop it manually.9sdk.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.
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. |
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.
1private fun doProvidedUILogin() {2IZettleSDK.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.
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.