简体   繁体   中英

Android: How to check if user have active subscription or not

I'm using Google Billing and trying to understand if subscription is expired or not with two solutions:

  1. billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS) { billingResult, purchasesList -> }

  2. val purchasesList = billingClient.queryPurchases(BillingClient.SkuType.SUBS).purchasesList

And they returns something like that:

{
  "productId": "weekly_with_trial",
  "purchaseToken": "someToken",
  "purchaseTime": 1563305764597,
  "developerPayload": null
}
{
  "orderId": "GPA.3380-7704-5235-01361",
  "packageName": "com.myapp",
  "productId": "weekly_trial_trial",
  "purchaseTime": 1563305764597,
  "purchaseState": 0,
  "purchaseToken": "someToken",
  "autoRenewing": false,
  "acknowledged": false
}

Both of them return me a list of purchases without any info about if it expired or not. How to check it ?

As the documentation of Google play billing clearly suggest's that there are two function to get Purchases List

val purchasesResult: PurchasesResult =
        billingClient.queryPurchases(SkuType.INAPP)

When you do a query purchase only active subscriptions appear on this list. As long as the in-app product is on this list, the user should have access to it .

This means that if the purchase is expired then you will not get that purchase item in the response for queryPurchase. However you have to keep in mind that it returns the local cached result of google play store.

So in order to get the purchase request from network call you need to use an asynchronous method, which is queryPurchaseHistoryAsync()

billingClient.queryPurchaseHistoryAsync(SkuType.INAPP, { billingResult, purchasesList ->
   if (billingResult.responseCode == BillingResponse.OK && purchasesList != null) {
       for (purchase in purchasesList) {
           // Process the result.
       }
   }
})

However there is a catch in object that contains info about the most recent purchase made by the user for each product ID, even if that purchase is expired, cancelled, or consumed.

So you can use queryPurchase() to get all active purchases.

Here is the code that I use to check for active subscription :

private void isUserHasSubscription(Context context) {
        billingClient = BillingClient.newBuilder(context).enablePendingPurchases().setListener(this).build();
        billingClient.startConnection(new BillingClientStateListener() {
            @Override
            public void onBillingSetupFinished(BillingResult billingResult) {
                Purchase.PurchasesResult purchasesResult=billingClient.queryPurchases(BillingClient.SkuType.SUBS);

                billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS,(billingResult1, list) -> {
                    
                    Log.d("billingprocess","purchasesResult.getPurchasesList():"+purchasesResult.getPurchasesList());
                    
                    if (billingResult1.getResponseCode() == BillingClient.BillingResponseCode.OK &&
                            !Objects.requireNonNull(purchasesResult.getPurchasesList()).isEmpty()) {

                            //here you can pass the user to use the app because he has an active subscription  
                            Intent myIntent=new Intent(context,MainActivity.class);
                            startActivity(myIntent);
                    }
                });
            }
            @Override
            public void onBillingServiceDisconnected() {
                // Try to restart the connection on the next request to
                // Google Play by calling the startConnection() method.
                Log.d("billingprocess","onBillingServiceDisconnected");
            }
        });
    }

Please follow the steps below to check if your subscription auto renewal is canceled or not.

Go to phone Settings.

Click iTunes & App Store.

Click Apple ID at top of screen.

Click View Apple ID.

Click Subscriptions.

Click desired app.

View subscription status.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM