androidkotlinandroid-billing

Android Kotlin - Billing - get productId from In-App purchase


I'm trying to get the productId from a purchase:

private fun queryPurchases(purchases: List<Purchase>){

    for (purchase in purchases) {

        Log.d("pikaboo", purchase.toString())

        when (purchase.purchaseState) {
            Purchase.PurchaseState.PURCHASED -> {

                val purchaseTime = (purchase.purchaseTime / 1000).toInt()

                when (purchase.productId) {
                    "no_ads_in_app_month" -> {

                    }
                    "no_ads_in_app_3_months" -> {

                    }
                }
            }
        }
    }
} 

This is the log:

Purchase. Json: {"orderId":"GPA.333.333.333","packageName":"de.bla.foo","productId":"no_ads_in_app_3_months","purchaseTime":1694810217354,"purchaseState":0,"purchaseToken":"xxxxxxxxxxxx","quantity":1,"acknowledged":true}

The productId in purchase.productId is Unresolved reference

everything else like purchase.purchaseTime purchase.purchaseState purchase.packageName works.

Why not purchase.productId? Is this a bug? How to solve this?


Solution

  • As written in Android Developers documentation on Purchase class, you should be able to get product ids from getProducts() method. Its' return type is List<String>.

    I am not very familiar with the billing library so I assume purchase can be represented by multiple products. You should probably iterate over the list and place your when operator in some kind of loop.