ReactivePlayBilling

Introduction: An RxJava wrapper for the Google Play Billing Library [work-in-progress]
More: Author   ReportBugs   
Tags:

This project acts us a simple wrapper for the Play Billing Library from Google for Android. This allows you to interact with the library in a reactive manner and use it within your reactive streams.

Functionality

Reactive Play Billing currently supports most of the operations that you will find within the library itself.

Connecting to Play Billing

You can connect to Play Billing by using the connect() method and subscribing to changes in the connection status.

reactiveBilling.connect()
        .subscribe({
                // Play billing connection successful
        }, {
                // Play billing connection failed / disconnected
        })

Observing purchase changes

You can observe purchase change by using the observePurchaseUpdates() method. This will be called when the user purchases and item / subscription to provide you with the status of the change. You must subbscribe to this method if you are carrying our purchases.

reactiveBilling.observePurchaseUpdates()
        .subscribe({
                // Purchase complete, handle result
        }, {
                // Purchase failed, handle result
        })

Querying in-app items for purchase

You can query items that are available for purchase using the queryItemsForPurchase() method. When calling, you need to pass in a list of SKU ids that you wish to retrieve the details for. If successful this will return you a list of SkuDetail instances. Otherwise, a QueryItemsForPurchaseError will be returned.

reactiveBilling.queryItemsForPurchase(skuList)
        .subscribe({
                // Handle items returned
        }, {
                // Handle item retrieval failure
        })

Querying subscriptions for purchase

You can query subscriptions that are available for purchase using the querySubscriptionsForPurchase() method. When calling, you need to pass in a list of SKU ids that you wish to retrieve the details for. If successful this will return you a list of SkuDetail instances. Otherwise, a QueryItemsForSubscriptionError will be returned.

reactiveBilling.querySubscriptionsForPurchase(skuList)
        .subscribe({
                // Handle items returned
        }, {
                // Handle item retrieval failure
        })

Purchasing an in-app Item

You can purchase in-app items by calling the purchaseItem() method. When calling this you need to pass in the SKU of the item which you wish to perform the purchase request on, followed by a reference to the current activity - this is required for activity result events.

A succesfull request will return an instance of the PurchaseResponse class. An unsuccessful request will return an ItemsForPurchaseQueryError.

reactiveBilling.purchaseItem(sku, activity)
        .subscribe({
                // Handle purchase success
        }, {
                // Handle purchase failure
        })

Purchasing a subscription

You can purchase subcsriptions by calling the purchaseSubscription() method. When calling this you need to pass in the SKU of the subscription which you wish to perform the purchase request on, followed by a reference to the current activity - this is required for activity result events.

A succesfull request will return an instance of the SubscriptionResponse class. An unsuccessful request will return an SubscriptionError.

reactiveBilling.purchaseSubscription(sku, activity)
        .subscribe({
                // Handle subscription success
        }, {
                // Handle subscription failure
        })

Querying purchases

You can query previous purchases for the current user by using the queryPurchaseHistory() observable. This will return you a list of Purchase instances upona successful request, but a QueryPurchasesError if the request fails.

reactiveBilling.queryPurchaseHistory()
        .subscribe({
                // Handle purchase history
        }, {
                // Handle failure
        })

Querying subscriptions

You can query subscriptions for the current user by using the querySubscriptionHistory() observable. This will return you a list of Purchase instances upona successful request, but a QuerySubscriptionsError if the request fails.

reactiveBilling.querySubscriptionHistory()
        .subscribe({
                // Handle purchase history
        }, {
                // Handle failure
        })

Consuming a purchase

You can consume an item that has been purchases by using consumeItem(). When calling this you need to pass in a reference to the purchase token for the desired item. A successful call will return a ConsumptionResponse instance and upon a failure a ConsumptionError will be returned.

reactiveBilling.consumePurchase()
        .subscribe({
                // Handle consumption success
        }, {
                // Handle consumption failure
        })

Installing

Until we get this onto maven central (we will do once the library has had some more usage for a v1 release) you will need to use Jitpack. This can be done like so:

allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}
dependencies {
    compile 'com.github.bufferapp:ReactivePlayBilling:-SNAPSHOT'
}
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools