如何检查订阅项目的到期时间
How to check the expiration for subscription items
IabHelper 的QueryInventoryFinishedListener 没有返回过期的订阅项。
另一方面,Google Play Billing Library 的 PurchaseHistoryResponseListener 似乎收到了所有已购买的商品,其中包括过期商品。
在 Google Play Billing Library 上,我们必须检查 PurchaseHistoryResponseListener 的购买日期和每个项目的到期日期?
queryPurchases 与 queryPurchaseHistoryAsync
一般我们应该使用queryPurchases(String skuType)
,不会returns过期的物品。 queryPurchaseHistoryAsync returns 启用和禁用的项目,如您所见的文档如下。
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.
Returns the most recent purchase made by the user for each SKU, even if that purchase is expired, canceled, or consumed.
关于 queryPurchaseHistoryAsync
我无法想象 queryPurchaseHistoryAsync 的用例。如果我们需要使用 queryPurchaseHistoryAsync,我们需要实现来检查它是否过期。
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对象没有period的信息,所以上面的period必须从BillingClient.querySkuDetailsAsync获取或者硬编码。以下是使用 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();
}
}
}
});
IabHelper 的QueryInventoryFinishedListener 没有返回过期的订阅项。
另一方面,Google Play Billing Library 的 PurchaseHistoryResponseListener 似乎收到了所有已购买的商品,其中包括过期商品。
在 Google Play Billing Library 上,我们必须检查 PurchaseHistoryResponseListener 的购买日期和每个项目的到期日期?
queryPurchases 与 queryPurchaseHistoryAsync
一般我们应该使用queryPurchases(String skuType)
,不会returns过期的物品。 queryPurchaseHistoryAsync returns 启用和禁用的项目,如您所见的文档如下。
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.
Returns the most recent purchase made by the user for each SKU, even if that purchase is expired, canceled, or consumed.
关于 queryPurchaseHistoryAsync
我无法想象 queryPurchaseHistoryAsync 的用例。如果我们需要使用 queryPurchaseHistoryAsync,我们需要实现来检查它是否过期。
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对象没有period的信息,所以上面的period必须从BillingClient.querySkuDetailsAsync获取或者硬编码。以下是使用 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();
}
}
}
});