简体   繁体   中英

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

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'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM