Skip to content

2. Construction and Connection

Mahdi Nouri edited this page Nov 21, 2019 · 2 revisions

In order to work with Poolakey, You need to construct an instance of Payment class:

val securityCheck = SecurityCheck.Enable(rsaPublicKey = "PUBLIC RSA KEY OF YOUR APP")
val paymentConfig = PaymentConfiguration(localSecurityCheck = securityCheck)
val payment = Payment(context = context, config = paymentConfig)

You can also disable local security checks, only if you're using Bazaar's REST API by passing SecurityCheck.Disable object in PaymentConfiguration class.
And after that, you need to connect to the in-app billing service via connect function in Payment class:

payment.connect {
    connectionSucceed {
        ...
    }
    connectionFailed { throwable ->
        ...
    }
    disconnected {
        ...
    }
}

As you can see, There are three callbacks available for you to get notified whenever the connection state changes. It's worth mentioning that the return type of connect function is a Connection interface which has two functions:

  • getState to get the current state of service connection
  • disconnect to disconnect from the in-app billing service

You have to always keep a global reference to this Connection interface.

private lateinit var paymentConnection: Connection

override fun onCreate(bundle: Bundle) {
    super.onCreate(bundle)
    paymentConnection = payment.connect {
        connectionSucceed {
            ...
        }
        connectionFailed { throwable ->
            ...
        }
        disconnected {
            ...
        }
    }
}

Note that

You have to disconnect from the in-app billing service when your view(Activity or Fragment) gets destroyed.

override fun onDestroy() {
    paymentConnection.disconnect()
    super.onDestroy()
}

Reactive way of this

payment.connect()
    .subscribe({ connection ->
        when (connection.getState()) {
            is ConnectionState.Connected -> {
                ...
            }
            is ConnectionState.Disconnected -> {
                ...
            }
        }
    }, { throwable ->
        ...
    })

connect returns an Observable<Connection> which you can subscribe to it and get notified about the connection state changes.