Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 383
Stafox Avatar asked Nov 29 '16 13:11

Stafox


People also ask

How do I check my subscriptions on Android?

Open the Google Play app. Choose Account -> Subscriptions & Payments -> Subscriptions. Click your test subscription and change the payment method to “Test card, always declines.” After five minutes, payment should be declined.

Do apps automatically charge you after free trial?

Here are three things to know about free trial offers: 1. If you don't cancel on time, you'll be charged. Usually, you have to give your credit card number for a “free trial.” That way, the company can charge you if you don't cancel before the trial period ends.

What are free trials on apps?

A free trial allows users to try out a product or service at no cost for a set period of time.


2 Answers

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

like image 55
Stafox Avatar answered Oct 16 '22 20:10

Stafox


[--------- 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'
like image 38
Pavel Poley Avatar answered Oct 16 '22 20:10

Pavel Poley