- 
                Notifications
    You must be signed in to change notification settings 
- Fork 0
1. Construction and Connection
In order to work with MarketPay, 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 by passing SecurityCheck.Disable object in PaymentConfiguration class.
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:
- 
getStateto get the current state of service connection
- 
disconnectto 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 {
            ...
        }
    }
}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()
}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.