简体   繁体   English

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

[英]How to check is transaction free trial in android subscription?

Is it possible to know is subscription was purchased as free trial? 是否有可能知道订阅是作为免费试用购买的? For now I can't find a way how to do it on server/device side. 目前我找不到如何在服务器/设备端执行此操作的方法。

Does anybody has suggestions how to do it? 有人有建议怎么做吗?

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

[--------- NOT SECURE ---------] [---------不安全---------]

After spending few days i will share my solution(on client side). 花了几天后,我将分享我的解决方案(在客户端)。

First you need to allow API access in Google play developer console and create service account ( docs ). 首先,您需要在Google Play开发者控制台中允许API访问并创建service accountdocs )。

You will get account id similar to this : your-service@api-621**********846-16504.iam.gserviceaccount.com 您将获得与此类your-service@api-621**********846-16504.iam.gserviceaccount.com帐户ID: your-service@api-621**********846-16504.iam.gserviceaccount.com

IMPORTANT : add "View financial data" permission in Users & permissions section in Google developer account. 重要提示 :在Google开发者帐户的“用户和权限”部分添加“查看财务数据”权限。

Also download p12 key and put it in your assets folder. 还要下载p12键并将其放入资源文件夹中。

Code to get subscription details(you need to know the subscription token): 获取订阅详细信息的代码(您需要知道订阅令牌):

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() is network request so you need to run this code on separate thread(i used RxJava) get.execute()是网络请求所以你需要在单独的线程上运行此代码(我使用RxJava)

You need to use AndroidPublisher and Play Services Auth libraries 您需要使用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'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android应用内购买免费试用订阅 - 如何避免滥用? - Android in-app purchase of a subscription with a free trial - how to avoid abuse? 在 Google Play 上为 Android 订阅启用免费试用 - Enable Free Trial for Android Subscription on Google Play 如何使用免费试用选项覆盖 SKProduct(订阅)以禁止免费试用并立即计费? - How to override a SKProduct (subscription) with a free-trial option to be disallow free-trial and be billed immediately? 如何防止滥用应用免费试用期(非订阅)? - How to prevent abuse of app free trial period (non-subscription)? Android,如何检查应用程序的试用期是否到期? - Android, How to check trial period of application is expired? 如何避免将应用内订阅免费提供给已经消费过的用户? - How to avoid giving free trial of the Application's In-App Subscription to users who already have consumed it? 如何在android中为每个用户提供免费试用期 - How to give each user a free trial period in android 如何在Android应用程序购买中为每个用户提供免费试用期 - How to give each user a free trial period in app purchase in android 谷歌播放订阅和移动应用免费试用自动取消/结束 - Google play subscription and free trial for mobile app automatically cancels/ends Android - 如何检查订阅是否续订? - Android - How to check if subscription is renewed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM