Rx wrapper for Billing Library with connection management
implementation 'com.betterme:rxbilling:$latestVersion'
implementation 'com.android.billingclient:billing:$billingClientVer'
RxBilling is a simple wrapper above Google Billing library. Using RxBilling is preferable to RxBillingFlow in most cases (if you don't care about fine-grained events, and you don't know about this issue https://github.com/googlesamples/android-play-billing/issues/83).
RxBillingFlow is a wrapper above InAppBillingService that allows to launch billing flow and handle result of onActivityResult() callback
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
private lateinit var rxBillingFlow: RxBillingFlow
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
rxBilling = RxBillingImpl(BillingClientFactory(applicationContext))
rxBillingFlow = RxBillingFlow(applicationContext, BillingServiceFactory(this))
lifecycle.addObserver(BillingConnectionManager(rxBilling))
lifecycle.addObserver(BillingConnectionManager(rxBillingFlow))
}
}
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()
.setSku("you_id")
.setType(BillingClient.SkuType.SUBS)
.build())
.subscribe({
//flow started
}, {
//handle error
}))
}
The result of this operation will be delivered to onActivityResult() of your Activity or Fragment, updates observer will not be triggered
private fun startFlowWithClient() {
disposable.add(rxBilling.launchFlow(this, BillingFlowParams.newBuilder()
.setSku("you_id")
.setType(BillingClient.SkuType.SUBS)
.build())
.subscribe({
//flow started
}, {
//handle error
}))
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
disposable.add(rxBillingFlow.handleActivityResult(resultCode, data)
.subscribe({
//handle purchase
}, {
//handle error
}))
}
private fun loadPurchases() {
disposable.add(rxBilling.getPurchases()
.subscribe({
//handle purchases
}, {
//handle error
}))
}
private fun loadSubscriptions() {
disposable.add(rxBilling.getSubscriptions()
.subscribe({
//handle purchases
}, {
//handle error
}))
}
private fun loadPurchasesHistory() {
disposable.add(rxBilling.getPurchaseHistory()
.subscribe({
//handle purchases
}, {
//handle error
}))
}
private fun loadPurchasesHistory() {
disposable.add(rxBilling.getSubscriptionHistory()
.subscribe({
//handle purchases
}, {
//handle error
}))
}
private fun loadPurchasesHistory() {
disposable.add(rxBilling.getPurchaseSkuDetails(listOf("your_id1", "your_id2"))
.subscribe({
//handle details
}, {
//handle details
}))
}
private fun loadDetails() {
disposable.add(rxBilling.getSubscriptionSkuDetails(listOf("your_id1", "your_id2"))
.subscribe({
//handle details
}, {
//handle details
}))
}
private fun consume() {
disposable.add(rxBilling.consumeProduct("purchase_token")
.subscribe({
//completed
}, {
//handle error
}))
}
If you are going to migrate on AndroidX, please use
com.gen.rxbilling.lifecycle.androidx.BillingConnectionManager
and
com.gen.rxbilling.flow.delegate.androidx.FragmentFlowDelegate