如何检查Android订阅中的交易免费试用?

时间:2016-11-29 13:21:25

标签: android in-app-purchase subscription

是否有可能知道订阅是作为免费试用购买的? 目前我无法找到如何在服务器/设备端执行此操作的方法。

有人有建议怎么做吗?

2 个答案:

答案 0 :(得分:2)

2017年6月9日,在页面https://developers.google.com/android-publisher/api-ref/purchases/subscriptions上显示了有关paymentState - 2新值的信息,这意味着"免费试用"。

答案 1 :(得分:1)

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

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

首先,您需要允许Google Play开发者控制台中的API访问权限并创建service accountdocs)。

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

重要:在Google开发者帐户的“用户和权限”部分中添加“查看财务数据”权限。

还要下载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身份验证库

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

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