【问题标题】:google billing queryPurchasesAsync & queryProductDetailsAsync doesn't return any resultgoogle billing queryPurchasesAsync & queryProductDetailsAsync 不返回任何结果
【发布时间】:2022-10-07 08:33:10
【问题描述】:

在我的项目中,我正在尝试集成新版本(5.0)的谷歌计费库,我正在关注谷歌示例

https://codelabs.developers.google.com/play-billing-codelab#3

例如,有两个功能:

fun queryPurchases() {
   if (!billingClient.isReady) {
       Log.e(TAG, \"queryPurchases: BillingClient is not ready\")
   }
   // Query for existing subscription products that have been purchased.
   billingClient.queryPurchasesAsync(
       QueryPurchasesParams.newBuilder().setProductType(BillingClient.ProductType.SUBS).build()
   ) { billingResult, purchaseList ->
       if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
           if (!purchaseList.isNullOrEmpty()) {
               _purchases.value = purchaseList
           } else {
               _purchases.value = emptyList()
           }

       } else {
           Log.e(TAG, billingResult.debugMessage)
       }
   }
}

应该返回purchases that the user has previously made,另一个函数是

fun queryProductDetails() {
   val params = QueryProductDetailsParams.newBuilder()
   val productList = mutableListOf<QueryProductDetailsParams.Product>()
   for (product in LIST_OF_PRODUCTS) {

       productList.add(
           QueryProductDetailsParams.Product.newBuilder()
               .setProductId(product)
               .setProductType(BillingClient.ProductType.SUBS)
               .build()
       )

       params.setProductList(productList).let { productDetailsParams ->
           Log.i(TAG, \"queryProductDetailsAsync\")
           billingClient.queryProductDetailsAsync(productDetailsParams.build(), this)
       }
   }
}

结果,我希望得到available products,但是,这两个函数返回空列表。

我知道这些产品和之前的新 lib 版本一样存在,我使用了前一个 4.x.x 并且它有效。

我在这里想念什么?任何建议表示赞赏。

  • 对于queryPurchasesAsync,您确定有订阅产品吗?您将 BillingClient.ProductType.SUBS 作为产品类型传入。也许这应该是BillingClient.ProductType.INAPP

标签: android


【解决方案1】:

我刚刚迁移到 V5,一切都在为我工作。

对于queryPurchasesAsync,您确定有订阅产品吗?您将 BillingClient.ProductType.SUBS 作为产品类型传入。也许这应该是BillingClient.ProductType.INAPP。否则代码看起来没问题。

对于queryProductDetailsAsync,一旦填充了productList,您将在循环中调用它而不是一次。 let 也是不必要的,没有理由创建新的范围。使用fold,您可以将代码简化为:

val products = inAppPurchaseProductIds.fold(
    ArrayList<QueryProductDetailsParams.Product>()
) { list, productId ->
    val productParams = QueryProductDetailsParams.Product.newBuilder()
        .setProductId(productId)
        .setProductType(BillingClient.ProductType.INAPP)
        .build()
    list.apply { add(productParams) }
}
val params = QueryProductDetailsParams.newBuilder().setProductList(products).build()
billingClient.queryProductDetailsAsync(params, this)

注意我使用了BillingClient.ProductType.INAPP。如果您有订阅,则需要将其更改为 BillingClient.ProductType.SUBS

【讨论】:

    猜你喜欢
    • 2015-04-21
    • 2015-09-14
    • 1970-01-01
    • 2017-12-28
    • 2017-12-09
    • 2020-10-03
    • 2017-03-19
    • 2017-05-20
    • 2012-08-08
    相关资源
    最近更新 更多