How to check is transaction free trial in android subscription?

落花浮王杯 提交于 2020-06-27 07:38:45

问题


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?


回答1:


On 9th of June, 2017 at page https://developers.google.com/android-publisher/api-ref/purchases/subscriptions appeared info about new value for paymentState - 2 which means "Free trial".




回答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).

You will get account id similar to this : your-service@api-621**********846-16504.iam.gserviceaccount.com

IMPORTANT: add "View financial data" permission in Users & permissions section in Google developer account.

Also download p12 key and put it in your assets folder.

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)

You need to use AndroidPublisher and Play Services Auth libraries

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

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


来源:https://stackoverflow.com/questions/40866990/how-to-check-is-transaction-free-trial-in-android-subscription

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!