RxBilling
Rx wrapper for Billing Library with connection management
Install / Use
/learn @betterme-dev/RxBillingREADME
RxBilling
RxBilling is a simple wrapper above Google Billing library with connection management
Download
implementation 'com.github.betterme-dev:RxBilling:$latestVersion'
implementation 'com.android.billingclient:billing:$billingClientVer'
How to use
Connection management
BillingConnectionManager
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))
}
}
Retry / Repeat connection
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)
})
Observe Billing updates
override fun onStart() {
super.onStart()
disposable.add(
rxBilling.observeUpdates()
.subscribe({
//handle update here
}, {
//handle error
})
)
}
override fun onStop() {
disposable.clear()
super.onStop()
}
Launch Billing flow
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
}))
}
Load owned products
private fun loadPurchases() {
disposable.add(rxBilling.getPurchases(BillingClient.SkuType.INAPP)
.subscribe({
//handle purchases
}, {
//handle error
}))
}
Load owned purchases
private fun loadPurchases() {
disposable.add(
rxBilling.getPurchases(BillingClient.SkuType.SUBS)
.subscribe({
Timber.d("getPurchases $it")
tvPurchases.text = it.toString()
}, {
Timber.e(it)
}))
}
Load history
private fun loadHistory() {
disposable.add(
rxBilling.getPurchaseHistory(BillingClient.SkuType.SUBS)
.subscribe({
Timber.d("getPurchaseHistory $it")
tvHistory.text = it.toString()
}, {
Timber.e(it)
}))
}
Load sku details
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)
}))
}
Consume product
private fun consume() {
disposable.add(
rxBilling.consumeProduct(
ConsumeParams.newBuilder()
.setPurchaseToken("token")
.build())
.subscribe()
)
}
Acknowledge item
private fun acknowledge() {
disposable.add(
rxBilling.acknowledge(
AcknowledgePurchaseParams.newBuilder()
.setPurchaseToken("token")
.build())
.subscribe({
Timber.d("acknowledge success")
}, {
Timber.e(it)
}))
}
Related Skills
node-connect
351.4kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.4kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.4kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
