如何查看 android 订阅中的交易是否免费试用?

How to check is transaction free trial in android subscription?

是否可以知道订阅是作为免费试用购买的? 现在我找不到在 server/device 方面如何做到这一点的方法。

有人对如何操作有建议吗?

2017 年 6 月 9 日,第 https://developers.google.com/android-publisher/api-ref/purchases/subscriptions 页出现了有关 paymentState - 2 新值的信息,这意味着 "Free trial"。

[-------- 不安全 --------]

几天后我将分享我的解决方案(在客户端)。

首先,您需要在 Google 中允许 API 访问开发者控制台并创建 service account(docs).

您将获得与此类似的帐户 ID:your-service@api-621**********846-16504.iam.gserviceaccount.com

重要:在 Google 开发者帐户的用户和权限部分添加 "View financial data" 权限。

同时下载 p12 密钥并将其放入您的资产文件夹中。

获取订阅详情的代码(您需要知道订阅令牌):

HttpTransport httpTransport = new NetHttpTransport();

    JsonFactory jsonFactory = new JacksonFactory();

    List<String> scopes = new ArrayList<String>();
    scopes.add(AndroidPublisherScopes.ANDROIDPUBLISHER);


    HttpRequestInitializer credential = null;

    try {

        KeyStore keystore = KeyStore.getInstance("PKCS12");
        keystore.load(context.getAssets().open("key.p12"), "notasecret".toCharArray());
        PrivateKey pk = (PrivateKey) keystore.getKey("privatekey", "notasecret".toCharArray());

        credential = new GoogleCredential.Builder().setJsonFactory(jsonFactory)
                .setTransport(httpTransport)
                .setServiceAccountId("your-service@api-621**********846-16504.iam.gserviceaccount.com")
                .setServiceAccountPrivateKey(pk)
                .setServiceAccountScopes(scopes)
                .build();

    } catch (GeneralSecurityException e) {

    } catch (IOException e) {

    }


    AndroidPublisher publisher = new AndroidPublisher.Builder(httpTransport, jsonFactory, credential).build();
    AndroidPublisher.Purchases purchases = publisher.purchases();


    try {

        AndroidPublisher.Purchases.Subscriptions.Get get = purchases.subscriptions()
                .get(context.getPackageName(), Constants.SUBSCRIPTION_ID, subscription_token);

        final SubscriptionPurchase purchase = get.execute();

        if (purchase != null && purchase.getPaymentState() != null) {

            int paymentState = purchase.getPaymentState();

            if (paymentState == 0) {
                // Subscriber with payment pending
            } else if (paymentState == 1) {
                // Subscriber in good standing (paid)
            } else if (paymentState == 2) {
                // Subscriber in free trial
            }
        }


    } catch (IOException e) { }

get.execute() 是网络请求所以你需要 运行 这个代码在单独的线程上(我用的是 RxJava)

您需要使用 AndroidPublisher 和 Play Services Auth 库

implementation 'com.google.apis:google-api-services-androidpublisher:v3-rev22-1.25.0'

implementation 'com.google.android.gms:play-services-auth:15.0.1'