RxBilling is a simple wrapper above Google Billing library with connection management
implementation 'com.github.betterme-dev:RxBilling:$latestVersion'
implementation 'com.android.billingclient:billing:$billingClientVer'
The entry point to Billing connection management is BillingConnectionManager, that connect and disconnect in onStart() / onStop() callbacks of your LifecycleOwner
Add next lines to your Activity, Fragment or any other lifecycle owner
class MainActivity : AppCompatActivity() {
private lateinit var rxBilling: RxBilling
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rxBilling = RxBillingImpl(BillingClientFactory(applicationContext))
lifecycle.addObserver(BillingConnectionManager(rxBilling))
}
}
The default implementation of retry transformation is RepeatConnectionTransformer().
You can provide your own transformer to BillingClientFactory and BillingServiceFactory
val clientFactory = BillingClientFactory(this, FlowableTransformer { upstream ->
upstream.retry(2)
})
override fun onStart() {
super.onStart()
disposable.add(
rxBilling.observeUpdates()
.subscribe({
//handle update here
}, {
//handle error
})
)
}
override fun onStop() {
disposable.clear()
super.onStop()
}
The result of this operation will be delivered to your updates observer
private fun startFlowWithClient() {
disposable.add(rxBilling.launchFlow(this, BillingFlowParams.newBuilder()
.setSkuDetails(SkuDetails) // see ## Load sku details
.setType(BillingClient.SkuType.SUBS)
.build())
.subscribe({
//flow started
}, {
//handle error
}))
}
private fun loadPurchases() {
disposable.add(rxBilling.getPurchases(BillingClient.SkuType.INAPP)
.subscribe({
//handle purchases
}, {
//handle error
}))
}
private fun loadPurchases() {
disposable.add(
rxBilling.getPurchases(BillingClient.SkuType.SUBS)
.subscribe({
Timber.d("getPurchases $it")
tvPurchases.text = it.toString()
}, {
Timber.e(it)
}))
}
private fun loadHistory() {
disposable.add(
rxBilling.getPurchaseHistory(BillingClient.SkuType.SUBS)
.subscribe({
Timber.d("getPurchaseHistory $it")
tvHistory.text = it.toString()
}, {
Timber.e(it)
}))
}
private fun loadDetails() {
disposable.add(
rxBilling.getSkuDetails(
SkuDetailsParams.newBuilder()
.setSkusList(listOf("your_id1", "your_id2"))
.setType(BillingClient.SkuType.SUBS)
.build())
.subscribe({
Timber.d("loadDetails $it")
tvDetails.text = it.toString()
}, {
Timber.e(it)
}))
}
private fun consume() {
disposable.add(
rxBilling.consumeProduct(
ConsumeParams.newBuilder()
.setPurchaseToken("token")
.build())
.subscribe()
)
}
private fun acknowledge() {
disposable.add(
rxBilling.acknowledge(
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken("token")
.build())
.subscribe({
Timber.d("acknowledge success")
}, {
Timber.e(it)
}))
}