How to check the expiration for subscription items

强颜欢笑 提交于 2019-12-10 13:32:58

问题


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?


回答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();

          }
        }
      }
    });


来源:https://stackoverflow.com/questions/46825023/how-to-check-the-expiration-for-subscription-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!