billingClient!!.queryProductDetailsAsync 无法从 Google Play 控制台返回任何产品。

6

我正在我的Android应用程序中使用Google计费实现订阅功能。

我遵循了Google Play Billing的官方文档。

我已经在Play Console中创建了订阅,并将相关功能添加到了我的应用程序中。

问题是,从Play控制台没有任何订阅进入应用程序,而且billingClient!!.queryProductDetailsAsync方法总是返回一个空的productDetailsList

有谁能帮我识别出这里的问题吗?

以下是我的实现方式:

在我的应用级Gradle文件中:

implementation "com.android.billingclient:billing-ktx:5.1.0"

在我的订阅文件中

 private var billingClient: BillingClient? = null

 override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    //Setup billing
    billingSetup()
 }

 private fun billingSetup() {
    billingClient = BillingClient.newBuilder(this)
        .setListener(purchasesUpdatedListener)
        .enablePendingPurchases()
        .build()

    //Connect to Google Play
    connectToGooglePlay()
}

private fun connectToGooglePlay() {
    billingClient!!.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(billingResult: BillingResult) {
            if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                Log.i("payment_flow", "OnBillingSetupFinish connected")
                queryProduct()
            } else {
                Log.i("payment_flow", "OnBillingSetupFinish failed")
            }
        }

        override fun onBillingServiceDisconnected() {
            Log.i("payment_flow", "OnBillingSetupFinish connection lost")

            //Re-connect to Google Play
            connectToGooglePlay()
        }
    })
}

private fun queryProduct() {
    val productList = ImmutableList.of(
        Product.newBuilder()
            .setProductId("monthly_plan")
            .setProductType(BillingClient.ProductType.SUBS)
            .build(),
        Product.newBuilder()
            .setProductId("yearly_plan")
            .setProductType(BillingClient.ProductType.SUBS)
            .build()
    )
    val queryProductDetailsParams = QueryProductDetailsParams.newBuilder()
        .setProductList(productList).build()

    billingClient!!.queryProductDetailsAsync(queryProductDetailsParams) { 
    billingResult: BillingResult, productDetailsList: List<ProductDetails> ->
    Log.d("payment_flow", "onProductDetailsResponse: 1: $billingResult")
        if (productDetailsList.isNotEmpty()) {
            tmpProductDetailsList.addAll(productDetailsList)
            Log.d("payment_flow", "onProductDetailsResponse: " + productDetailsList.size)
        } else {
            Log.i("payment_flow", "onProductDetailsResponse: No products")
        }
    }
}

在Play控制台上的订阅 输入图像描述

打开应用程序中的订阅页面时的日志 输入图像描述

提前致谢


1
你解决了这个问题吗?我现在也遇到了同样的问题。 - DelphiQin
1
我刚刚解决了我的问题,现在我的三个产品都显示出来了。不幸的是,我不懂 Kotlin,我使用 JAVA,但我可以说的是,我的问题是我如何循环遍历 ProductDetails 的结果。 - Robby Lebotha
1
@Waseem 你把应用上传到Google Play了吗?我已经上传并发布了我的应用(内部测试就足够了)。产品列表现在已经显示出来了。 - DelphiQin
@DelphiQin 是的,是通过内部渠道发布的。但没有用处。 - Waseem
@RobbyLebotha 我已经将应用程序发布到内部测试轨道,然后添加了测试人员的电子邮件地址并“接受”了内部邀请。但是,ProductDetails仍然为空。已经过去几个小时(至少3个小时)。您建议再等几个小时吗? - dumbfingers
显示剩余3条评论
3个回答

4
对我来说,问题是我刚在 Google Play 控制台中创建了订阅。有两个基本计划,一个每周和一个每月。它返回的是每周的详细信息而不是每月的。在代码中进行了大量搜索后,我认为可能是新创建的订阅需要时间,结果发现 Google 在您的设备中缓存产品。我在我的设备上选择了 Google Play 应用程序,并在应用程序信息页面中选择了“清除数据”,这样所有缓存的数据都被清除了,现在它完美地返回所有产品。

我为此苦苦挣扎了1.5个小时,谢谢。 - A. Batuhan Özkaya
1
然而,此方法的文档中写道:“执行网络查询以获取您应用程序中可供销售的产品的详细信息。”太棒了。 - undefined
然而,该方法的文档却表示:“执行网络查询以获取您应用程序中可销售的产品的详细信息。” 真是太棒了。 - ravindu1024

1
清除Google Play应用程序数据,然后再试一次。这对我起作用了。

1
谢谢,但我尝试过了,没有帮助。 - Waseem

0

好的,我的问题是我的产品没有在用户界面中显示,所以我将我的循环和产品展示包裹在一个线程中。以下是我的代码,希望能帮到你。除此之外,你的代码看起来和我的一样。所以关键点是runOnUiThread。现在我的产品已经完美地加载并且可以购买了。

billingClient.queryProductDetailsAsync(
            params,
            (billingResult, productDetailsList) -> {
                // Process the result

                Log.d(TAG,"Got list queryProductDetailsAsync "+productDetailsList.size());

                for (ProductDetails productDetails : productDetailsList){
                    Log.d(TAG,"Got productDetails: "+productDetails.getName()+"\nDesc: "+productDetails.getDescription());

                    String productName = productDetails.getName();
                    String productPrice = productDetails.getSubscriptionOfferDetails().get(0)
                            .getPricingPhases().getPricingPhaseList().get(0).getFormattedPrice();
                    String productID = productDetails.getProductId().toString();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (productDetails.getProductId().equals(Subscriptions.MONTHLY)) {
                                Log.d(TAG,"Got product Subscriptions.MONTHLY");
                                cvMonth.setVisibility(View.VISIBLE);
                                tvTitleMonth.setText(productName+"\n"+productPrice);
                                tvDescMonth.setText(productDetails.getDescription());

                                btnBuyMonth.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        launchPurchaseFlow(productDetails);
                                    }
                                });
                            }
                            if (productDetails.getProductId().equals(Subscriptions.BI_ANNUAL)) {
                                Log.d(TAG,"Got product Subscriptions.BI_ANNUAL");
                                cvSixMonth.setVisibility(View.VISIBLE);
                                tvTitleSixMonth.setText(productName+"\n"+productPrice);
                                tvDescSixMonth.setText(productDetails.getDescription()
                                        +"\nYou save 40% compared to Monthly Subscription");
                                btnBuySixMonth.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        launchPurchaseFlow(productDetails);
                                    }
                                });
                            }
                            if (productDetails.getProductId().equals(Subscriptions.ANNUAL)) {
                                Log.d(TAG,productName+"\n"+productPrice);
                                cvAnnual.setVisibility(View.VISIBLE);
                                tvTitleAnnual.setText(productName+"\n"+productPrice);
                                tvDescAnnual.setText(productDetails.getDescription()
                                        +"\nYou save 40% compared to Monthly Subscription. Only " +
                                        "pay once a year.");
                                btnBuyAnnual.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View view) {
                                        launchPurchaseFlow(productDetails);
                                    }
                                });
                            }

                            if(loadingDialog != null){
                                loadingDialog.dismiss();
                            }
                        }
                    });


                }
            }
    );

2
我的问题是,“Got list queryProductDetailsAsync” 总是返回 0。 - Waseem

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接