如何检查用户在 Android、Google Play 中是否有有效订阅?

How to check if the user has an active subscription in Android, Google Play?

我有一个在 Google Play 中订阅的应用程序。 当用户启动应用程序时,我需要知道用户是否有有效订阅。这似乎是一件显而易见的事情,但是从搜索和尝试实施它,这似乎是不可能的?

我正在使用 Google 的较新的 2/3 账单,遵循 Google 的教程,

class BillingManager implements PurchasesUpdatedListener
...
public void checkAsync() {
    Log.e(TAG, "checkAsync");
    billingClient.queryPurchaseHistoryAsync(BillingClient.SkuType.SUBS, new PurchaseHistoryResponseListener() {
        @Override
        public void onPurchaseHistoryResponse(BillingResult billingResult, List<PurchaseHistoryRecord> list) {
            Log.e(TAG, "checkCached result: " + list);
            if (list == null) {
                return;
            }

            for (PurchaseHistoryRecord ps : list) {
                //System.out.println("PAYMENT: " + ps.getSku() + " : " + ps.getPurchaseTime());
            }
        }
    });
}

public void checkCached() {
    Log.e(TAG, "checkCached");
    List<Purchase> result = billingClient.queryPurchases(BillingClient.SkuType.SUBS).getPurchasesList();
    Log.e(TAG, "checkCached result: " + result);
    if (result == null) {
        return;
    }
    for (Purchase purchase : result) {
        handlePurchase(purchase);
    }
}

这就是我认为您应该获得用户购买的方式。但它根本不起作用,两个调用 return null 总是。当您重新安装应用程序或清除数据时,它只会 return 正确购买。

那么应用程序究竟应该如何做到这一点?

进入内测后购买即可,通过GooglePlaylink下载即可。 (在此之前订阅根本不起作用)。

*** 已更新 所以进一步澄清:

我使用的是有效的测试用户,订阅工作正常。我的问题是 API queryPurchases() 或 queryPurchaseHistoryAsync() 应该做什么。

我所看到的是,只有这些 return 购买没有被应用程序处理。他们似乎存储了购买是在应用程序数据中处理的。 购买后这些 return null,应用程序重新启动后这些 return null。 如果我清除应用程序数据或重新安装应用程序,那么他们 return 购买(一次),然后在重新启动后再次为空。

据我所知,这些仅用于检测用户何时重新安装您的应用程序,或安装到不同的 phone。它们不能用于确定订阅的状态。

所以我的问题是,

1 - 这是否在内部测试中不起作用,并且在应用程序发布时会神奇地以不同的方式工作?

2 - 是否有不同的 API 您打算用来检查订阅状态?

3 - 您是否想通过在您第一次确认订阅时存储用户 preference/cookie 来在您的应用程序中自行管理订阅,以便您知道订阅​​何时到期?

您需要“有执照的测试人员”。他们将允许您在设备上“旁加载”您的应用程序,即使是调试版本。在这种情况下,我对侧载的解释包括从 Android Studio 构建工具以及 adb install .... 和其他不涉及 Play 商店的方法进行安装。

https://developer.android.com/google/play/billing/test

Ordinarily, the Google Play Billing API is blocked for apps that aren't signed and uploaded to Google Play. License testers can bypass this check, meaning you can sideload apps for testing, even for apps using debug builds with debug signatures without the need to upload to the new version of your app. Note that the package name must match that of the app that is configured for Google Play, and the Google account must be a license tester for the Google Play Console account.

我也不明白你是如何使用 startConnection 的。在成功完成之前,我无法确定您拥有最新数据。如果这让你得到陈旧的价值观,我不会感到惊讶。我会通过查看 onBillingSetupFinishedonBillingServiceDisconnected 仔细检查以确保没有发生静默错误。暂时不要相信 queryPurchases():

https://medium.com/@NandagopalR/integrating-google-play-billing-into-an-android-application-b6eb6af176a7

The queryPurchases() method uses a cache of the Google Play Store app without initiating a network request. If you need to check the most recent purchase made by the user for each product ID, you can use queryPurchaseHistoryAsync(), passing the purchase type and a PurchaseHistoryResponseListener to handle the query result.

顺便问一下 queryPurchaseHistoryAsync 之前的 isReady() 的值是多少,BillingResult::getDebugMessageBillingResult::getResponseCode 的值是多少?

此外,请使用 isFeatureSupported,但您的问题似乎并非出自此处。但我建议在所有活动部件正常工作之前不要使用订阅进行测试:https://developer.android.com/reference/com/android/billingclient/api/BillingClient#isFeatureSupported(java.lang.String)

好吧,明白了,是我的错。

我在主 activity onCreate() 中调用了 queryPurchases(),但是 BillingClient 还没有准备好。

我将其移至 onBillingSetupFinished(),现在 returns 正确购买。 现在一切都按预期工作。当您在应用重启后调用 queryPurchases() 时,您会获得有效订阅。