How to Create SkuDetail in Play Billing v3.0

296 views Asked by At

I am migrating from v1.0 to v3.0 of play billing library.

The code below demonstrates how to launch a purchase flow in v1.0

val billingParams = BillingFlowParams.newBuilder().setSku(skuId).setType(skuType).build()
val billingResult  = billingClient.launchBillingFlow(activity, billingParams)

In version 3.0

it is meant to be done like this:

val billingParams = BillingFlowParams.newBuilder().setSkuDetails(skuDetails).build()
val billingResult  = billingClient.launchBillingFlow(activity, billingParams)

how do I create skuDetail with skuId and skuType?

1

There are 1 answers

2
Manohar On

Here is the method I use , you pass list of package names and it returns list of SkuDetails

  suspend fun queryInAppPurchaseSkuDetails(packagesList: List<String>): List<SkuDetails>? {

        val params = SkuDetailsParams.newBuilder()
        params.setSkusList(packagesList).setType(BillingClient.SkuType.INAPP)
        val skuDetailsResult = withContext(Dispatchers.IO) {
            billingClient?.querySkuDetails(params.build())
        }
        // Process the result.

        if (skuDetailsResult?.billingResult?.responseCode == BillingClient.BillingResponseCode.OK && packagesList.isNotNullOrEmpty()) {
            val skuDetailsList = skuDetailsResult.skuDetailsList
            return skuDetailsList
        }

        return null
    }