Skip to content

1. Construction and Connection

Aref Bahreini edited this page Apr 29, 2020 · 1 revision

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:

  • 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.

Clone this wiki locally