How to check the expiration for subscription items
Asked Answered
R

1

9

QueryInventoryFinishedListener of IabHelper has not returned the expired subscription items.

On the other hand, PurchaseHistoryResponseListener of Google Play Billing Library seems to receive all purchased items, which is including expired items.

On Google Play Billing Library, we have to check the purchased date of PurchaseHistoryResponseListener and each expiration date of items?

Robers answered 19/10, 2017 at 7:48 Comment(1)
Good question. Have you found the answer already? Or do you have any implementation hints?Rafa
R
1

queryPurchases vs queryPurchaseHistoryAsync

Generally, we should use queryPurchases(String skuType), which does not returns expired items. queryPurchaseHistoryAsync returns enabled and disabled items, as you see the documentation like following.

queryPurchases

Get purchases details for all the items bought within your app. This method uses a cache of Google Play Store app without initiating a network request.

queryPurchaseHistoryAsync

Returns the most recent purchase made by the user for each SKU, even if that purchase is expired, canceled, or consumed.

About queryPurchaseHistoryAsync

I could not image the use case for queryPurchaseHistoryAsync. If we need to use queryPurchaseHistoryAsync, we need the implementation to check if it is expired or not.

  private PurchaseHistoryResponseListener listener = new PurchaseHistoryResponseListener() {
    @Override
    public void onPurchaseHistoryResponse(int responseCode, List<Purchase> purchasesList) {
      for (Purchase purchase : purchasesList) {
        if (purchase.getSku().equals("sku_id")) {
          long purchaseTime = purchase.getPurchaseTime();
          // boolean expired = purchaseTime + period < now
        }
      }
    }
  };

Purchase object does not have the information of period, so the above period must be acquired from BillingClient.querySkuDetailsAsync or be hard-coded. The following is sample implementation to use querySkuDetailsAsync.

    List<String> skuList = new ArrayList<>();
    skuList.add("sku_id");
    SkuDetailsParams.Builder params = SkuDetailsParams.newBuilder();
    params.setSkusList(skuList).setType(BillingClient.SkuType.SUBS);
    billingClient.querySkuDetailsAsync(params.build(), new SkuDetailsResponseListener() {
      @Override
      public void onSkuDetailsResponse(int responseCode, List<SkuDetails> skuDetailsList) {
        if (skuDetailsList == null) {
          return;
        }
        for (SkuDetails skuDetail : skuDetailsList) {
          if (skuDetail.getSku().equals("sku_id")) {
            String period = skuDetail.getSubscriptionPeriod();

          }
        }
      }
    });
Robers answered 19/2, 2019 at 12:49 Comment(3)
How does this work with grace period though? isAutoRenewing remains true and according to docs pushes the next billing date further. Does it change purchaseTime? If not, then a situation where you have set the product active for next period because of a false positive from grace period might occur, and while you set it active to say a month, it should've only been a week (the grace period).Otherworld
According to doc String getSubscriptionPeriod () Subscription period, specified in ISO 8601 format. For example, P1W equates to one week, P1M equates to one month, P3M equates to three months, P6M equates to six months, and P1Y equates to one year. So basically you are going to convert a P1M or P3M or other like these into milliseconds but month can be of 28 to 31 days so how exactly are to converting period in milliseconds.Husha
I checked the value returned by purchase.getPurchaseTime() with test subscription, and this value is always the timestamp of the first time the subscription was enabled, it didn't take in consideration subscription renewals. If this is the case with real subscriptions, this is not very helpful.Wilhelmstrasse

© 2022 - 2024 — McMap. All rights reserved.