如何判断用户是否取消了Google In-app Billing v3实现的Android订阅?
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
autoRenewingflag 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.
- If
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,
autoRenewingwill befalsebut the trial is still active. Per your rule, this counts as "subscribed then canceled"—show the "UpGrade" view. - Network Errors: If
queryPurchasesorqueryPurchaseHistoryAsyncfails 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,
autoRenewingmight still betrue—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

