Giter VIP home page Giter VIP logo

rxbilling's Introduction

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)
                    }))
}

rxbilling's People

Contributors

dmytro-ostapovets avatar kchernenko avatar dima-ostapovets avatar mrstif avatar constmikhailovskiy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.