Search code examples
androidin-app-billingin-app-subscription

billingClient.queryPurchases(BillingClient.SkuType.SUBS).getPurchasesList() returning null


I'm making an app for android with billing services. I've already added the "shop" method, so the user can subscript. My app has only one subscriptionfor sale. The problem is that when the user open the app I can't get if he has subscript or not. Until now, I've come to this code, even when the user is subscriptpurchasesResult.getPurchasesList() returns 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 method:

 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;
        }
    }

I'm looking forward to check if the user is subscribed so, I can set premium = true. Until now, I'm using an ugly solution that is buy the product again and check if it returns ITEM_ALREADY_OWNED. Still looking for a better solution.

NOTE: I'm getting this result on alpha test.

NOTE 2: This link might help.

NOTE 3: This link show others with the same issue.


Solution

  • you should find the soulution here !

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

    I have tested this too with a closed alpha release to check if the subscription is active. When the subscription is not active anymore then the sku is not in the list of purchases anymore :

    If i cancel the 5 minutes test-subscription then the sku is not anymore in the list. So this is working fine for me

    Note skuname is the identifier i have passed before like "mytestsub.iap1.com" for example

    my solution is :

    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")
            }
        })
    }