billingClient.queryPurchases(BillingClient.SkuType.SUBS).getPurchasesList() 返回null。

问题描述 投票:0回答:1

我正在为安卓系统制作一个带有计费服务的应用。我已经添加了 "商店 "方法,所以用户可以订阅。我的应用程序只有一个订阅出售。问题是,当用户打开应用时,我无法知道他是否有subscript。直到现在,我才发现这段代码,即使用户有subscript,也是如此purchasesResult.getPurchasesList() 返回 null:

premium = false

try{
            Purchase.PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS);

        for (Purchase purchase : purchasesResult.getPurchasesList()) {

            acknowledgePurchaseParams =
                    AcknowledgePurchaseParams.newBuilder()
                            .setPurchaseToken(purchase.getPurchaseToken())
                            .build();

            acknowledgePurchaseResponseListener = new AcknowledgePurchaseResponseListener() {
                @Override
                public void onAcknowledgePurchaseResponse(BillingResult billingResult) {
                    premium = true;
                }
            };

            handlePurchase(purchase);
        }}
        catch (Exception e){
            e.printStackTrace();
        }

handlePurchase方法。

 void handlePurchase(Purchase purchase) {
        if (purchase.getPurchaseState() == Purchase.PurchaseState.PURCHASED) {
            if (!purchase.isAcknowledged()) {
                AcknowledgePurchaseParams acknowledgePurchaseParams =
                        AcknowledgePurchaseParams.newBuilder()
                                .setPurchaseToken(purchase.getPurchaseToken())
                                .build();
                billingClient.acknowledgePurchase(acknowledgePurchaseParams, acknowledgePurchaseResponseListener);
            }
            else SplashScreenActivity.premium = true;
        }
    }

我希望检查用户是否被订阅,所以,我可以设置: premium = true. 直到现在,我还在使用一个丑陋的解决方案,那就是再次购买产品,然后检查它是否返回ITEM_ALREADY_OWNED。还在寻找更好的解决方案。

: 我在alpha测试中得到这个结果。

注2: 此链接 可能会有帮助。

注3: 此链接 给其他有同样问题的人看。

android in-app-billing in-app-subscription
1个回答
0
投票

你应该可以在这里找到灵魂之门!

https://stackoverflow.com/questions/59735936/implement-google-play-billing-library-version-2

我也用一个封闭的alpha版本测试了这个问题,以检查订阅是否活跃。当订阅不再活跃时,sku不再在购买列表中。

如果我取消5分钟的测试订阅,那么sku就不会再出现在列表中。因此,这对我来说是工作正常

注意skuname是我之前传递的标识符,比如 "mytestsub.iap1.com"。

我的办法是

private fun setupBillingClient() {
    mBillingClient = BillingClient
            .newBuilder(context!!)
            .enablePendingPurchases() // Useful for physical stores
            .setListener(this)
            .build()


    mBillingClient?.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(billingResult: BillingResult) {
            if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {



                val purchasesResult = mBillingClient?.queryPurchases(BillingClient.SkuType.SUBS) 




                // Init all the purchases to false in the shared preferences (security prevention)
                prefs.purchased = false


                // Retrieve and loop all the purchases done by the user
                // Update all the boolean related to the purchases done in the shared preferences
                if (purchasesResult?.purchasesList != null) {
                    for (purchase in purchasesResult.purchasesList!!) {


                        if (purchase.isAcknowledged) {
                            Log.e(Config.APPTAG, purchase.sku)
                            when (purchase.sku) {
                                skuname ->  {

                                    Log.e(Config.APPTAG, " Product "+purchase.sku+" is subscribed")



                                    // The subscription sku is found and active so then purchases to true in prefs
                                    prefs.purchased = true
                                }





                            }
                        }
                    }





                }
            }
        }

        override fun onBillingServiceDisconnected() {
            // Try to restart the connection on the next request to
            // Google Play by calling the startConnection() method.
            // TODO Note: It's strongly recommended that you implement your own connection retry policy and override the onBillingServiceDisconnected() method. Make sure you maintain the BillingClient connection when executing any methods.


            Log.e(Config.APPTAG, "onBillingServiceDisconnected")
        }
    })
}
© www.soinside.com 2019 - 2024. All rights reserved.