You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何判断用户是否取消了Google In-app Billing v3实现的Android订阅?

How to Accurately Determine Subscription Status for UI View Switching with Google In-app Billing v3

Hey there! Let's walk through the exact logic you need to switch between your "Avail A Week Free" and "UpGrade" views correctly—even when dealing with canceled subscriptions (in or out of validity) and expired subscriptions where getPurchase() returns null.

Core Logic Breakdown

The key is to check two critical pieces of data: current active subscriptions and past purchase history. Here's a step-by-step breakdown:

1. Fetch Current Active Subscriptions

First, use queryPurchases to pull the user's currently active (or grace-period) subscriptions:

PurchasesResult purchasesResult = billingClient.queryPurchases(BillingClient.SkuType.SUBS);
List<Purchase> activeSubscriptions = purchasesResult.getPurchasesList();

Case A: Active Subscriptions Exist

If activeSubscriptions isn't empty:

  • Check the autoRenewing flag of the subscription:
    • If autoRenewing == true: The user is actively subscribed with auto-renewal enabled. You’ll likely want to show a "Manage Subscription" view instead of your two target options, as they’re already in a valid subscription state.
    • If autoRenewing == false: The user canceled their subscription but still has access until the current period ends. Per your requirement, show the "UpGrade" view here—since they’ve subscribed before and canceled, they need to re-subscribe to continue access.

Case B: No Active Subscriptions Found

When activeSubscriptions is empty, you need to verify if the user has ever subscribed before using purchase history. Use queryPurchaseHistoryAsync to retrieve past purchases (including expired ones):

billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS, new PurchaseHistoryResponseListener() {
    @Override
    public void onPurchaseHistoryResponse(BillingResult billingResult, List<PurchaseHistoryRecord> purchaseHistoryList) {
        if (billingResult.getResponseCode() == BillingClient.BillingResponseCode.OK && purchaseHistoryList != null) {
            boolean hasEverSubscribed = false;
            for (PurchaseHistoryRecord record : purchaseHistoryList) {
                if (record.getSku().equals(YOUR_SUBSCRIPTION_SKU)) {
                    hasEverSubscribed = true;
                    break;
                }
            }
            // Update UI based on the result
            if (hasEverSubscribed) {
                // User had a subscription that's now expired (or canceled and expired)
                showUpgradeView();
            } else {
                // User has never subscribed before
                showAvailWeekFreeView();
            }
        } else {
            // Handle network/API errors—default to "Avail A Week Free" if history can't be fetched
            showAvailWeekFreeView();
        }
    }
});

2. Edge Cases to Handle

  • Trial Period Cancellations: If a user signs up for the free trial and cancels immediately, autoRenewing will be false but the trial is still active. Per your rule, this counts as "subscribed then canceled"—show the "UpGrade" view.
  • Network Errors: If queryPurchases or queryPurchaseHistoryAsync fails due to connectivity issues, default to "Avail A Week Free" temporarily, but retry the calls once connectivity is restored to get the accurate state.
  • Grace Periods: Google Play may grant a grace period if auto-renewal fails (e.g., payment issue). During this time, autoRenewing might still be true—treat this as an active subscription and avoid showing your target views.

Pro Tip

For the most reliable status checks, always validate purchases on your backend using Google's Play Developer API. Client-side checks are convenient but can be tampered with—backend verification ensures you get the full picture of expiration dates, cancellation reasons, and more.

内容的提问来源于stack exchange,提问作者ganesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:34:01