Search code examples
androidkotlinandroid-billing

Android Kotlin - Billing subscriptions - purpose of consumeAsync?


I've followed a tutorial about implementing subscriptions with Google Billing, but I've left out some methods but it seems to work just fine.

This is one of the methods I left out:

private fun handelPurchase(purchase: Purchase){
    val consumeParams =
        ConsumeParams.newBuilder()
            .setPurchaseToken(purchase.purchaseToken)
            .build()

    val listener = ConsumeResponseListener { billingResult, s ->

        if(purchase.purchaseState == BillingClient.BillingResponseCode.OK){

        }
    }
    billingClient.consumeAsync(consumeParams, listener)
    if(purchase.purchaseState == Purchase.PurchaseState.PURCHASED){

    }
}

What is the purpose of it? Should I still implement it?

This is the last step that I'm using so far:

private fun ackPurchase(purchase: Purchase) {
    if (!purchase.isAcknowledged) {
        val params = AcknowledgePurchaseParams.newBuilder().setPurchaseToken(purchase.purchaseToken).build()
        billingClient.acknowledgePurchase(params) { billingResult ->
            if(billingResult.responseCode == BillingClient.BillingResponseCode.OK){
                premiumActivate()
            }
        }
    }else{
        premiumActivate()
    }
}

so do I need both or only one of those? Google docs are as always incomplete

The only other question that I've found that is similar is:

Android In-app billing, How to determine whether to call consumeAsync or acknowledgePurchase?

but it's not answered


Solution

  • Basically, there are the following types of products:

    • Subscriptions – both auto-renewable and non-renewable (prepaid plans).
    • Non-consumable products – the products that are meant to be purchased only once in a lifetime, e.g. lifetime premium access or ads removal.
    • Consumable products – the ones that are meant to be purchased multiple times, e.g. coins.

    consumeAsync is used only for the last category. This means acknowledging + marking it as "consumed", allowing you to purchase it again. Otherwise, you will encounter an ITEM_ALREADY_OWNED error if you try to purchase it more than once.

    In Google Play, there is no technical difference between consumable and non-consumable products (unlike, for example, App Store). So, it's up to the developer whether to consume or only acknowledge them.