How to get expiry date for Subscription with client side in Android?
Asked Answered
A

3

10

We are implementing the subscription using in-app purchase in android, We are getting the purchase timestamp like the below format

'{
   "orderId":"GPA.1234-5678-9012-34567",
   "packageName":"com.example.app",
   "productId":"exampleSku",
   "purchaseTime":1345678900000,
   "purchaseState":0,
   "developerPayload":"bGoa+V7g/yqDXvKRqq+JTFn4uQZbPiQJo4pf9RzJ",
   "purchaseToken":"opaque-token-up-to-1000-characters"
 }'

But We need to display the expiry date in application UI and we wanna get the exact expiry date from play store. We assume that If we manually calculate the expiry date then it might be the conflict with the play store expiry date. Can anyone explain about "How to get the expiry date for Subscription in android?"

Ameba answered 6/8, 2016 at 5:40 Comment(5)
ask expiry date to your admin side they will give you,post this detail to your server side they wii get expiry from google inappPamphleteer
Possible duplicate of how to get subscription expire date in inapp v3 androidPamphleteer
This is not user based app, We wanna implement like as every user can validate their subscription with play store. We want to implement this in client side.Ameba
My question is how to get the expiry date in client side, We are facing authentication problems, So that anyone can explain the step-by-step process for client side. it is Possible in IOSAmeba
Hi, Do you got any solution?Unnerve
S
8

To Get Subscription Expiry date after subscription, need to follow below steps.

Step 1: First Get "mRefreshToken" from following api Google Publisher API

Step 2: Next need to get "access_tokon" using params("mRefreshToken", "client_id" and "client_secret") and below api.

final StringRequest mStringRequest = new StringRequest(Request.Method.POST, "https://accounts.google.com/o/oauth2/token",
    new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            [From response -get access_tokon]
        }
    }, new Response.ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        error.printStackTrace();
   }}) {
@Override
protected Map<String, String> getParams() {

    Map<String, String> params = new HashMap<>();
    params.put("grant_type", "refresh_token");
    params.put("client_id", "your_client_id");
    params.put("client_secret", "your_client_secret");
    params.put("refresh_token", mRefreshToken);

    return params;
}};

Step 3: You have "accessToken" from above api, after that get expiry json using below api and params

String url = "https://www.googleapis.com/androidpublisher/v2/applications/" + AppController.getInstance().getPackageName() + "/purchases/subscriptions/" + mSubscriptionId + "/" + "tokens/" + mPurchaseToken;

 final StringRequest mStringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            [From response -get expiry detail json]
        }
    }, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
    error.printStackTrace();
}}) {

@Override
public Map<String, String> getHeaders() throws AuthFailureError {

    Map<String, String> params = new HashMap<>();
    params.put("Authorization", "Bearer " + accessToken);
    return params;
}};

For more reference: official document url from Google "https://developers.google.com/android-publisher/api-ref/purchases/subscriptions/get" and "https://developers.google.com/android-publisher/authorization"

Sorbian answered 19/6, 2018 at 9:56 Comment(4)
I would not include client id/secret in the Android app because it can be misused.Hauge
How is possible?Sorbian
You can extract the secrets + refresh token from the code and then use it to access all REST functions which have the same authorization level.Hauge
Keep your key with secure like encrypt and decrypt.Sorbian
H
2

There is a two ways to get the subscription expiry date. First way you can implement google Apis for fetching the expiry date Second way- when your transaction is sucuessful then you get the subscription Period and purchase Time for these param you can get the date easily without any error

private String getSubscriptionRenewingDate3(long purchaseTime, 

String setSubscriptionPeriod) {

            Date purchaseDate = new Date(purchaseTime);

            Calendar calendar = Calendar.getInstance();

            calendar.setTime(purchaseDate);

            Date now = new Date();

            while (calendar.getTime().before(now)) {

                switch (setSubscriptionPeriod) {

                    case "P1W":
                        calendar.add(Calendar.HOUR, 7 * 24);
                        break;
                    case "P1M":
                        calendar.add(Calendar.MONTH, 1);
                        break;
                    case "P3M":
                        calendar.add(Calendar.MONTH, 3);
                        break;
                    case "P6M":
                        calendar.add(Calendar.MONTH, 6);
                        break;
                    case "P1Y":
                        calendar.add(Calendar.YEAR, 1);
                        break;
                }
            }

            String name = String.valueOf(calendar.getTime());
            Toast.makeText(requireContext(), "Dateeeeee" + name, Toast.LENGTH_SHORT).show();
            return name;
    //    }

       // return null;
    }
Headmaster answered 28/8, 2021 at 10:54 Comment(0)
F
-1

I tried with this API "https://www.googleapis.com/androidpublisher/v2/applications/" + AppController.getInstance().getPackageName() + "/purchases/subscriptions/" + mSubscriptionId + "/" + "tokens/" + mPurchaseToken;

with proper valid data but I found this error :

**{
    "error": {
        "errors": [
            {
                "domain": "androidpublisher",
                "reason": "permissionDenied",
                "message": "The current user has insufficient permissions to perform the requested operation."
            }
        ],
        "code": 401,
        "message": "The current user has insufficient permissions to perform the requested operation."
    }
}**

Please tell me if any solutions . I follow all steps and get all done with play console and google cloud console account.

Folksy answered 6/2, 2020 at 12:38 Comment(1)
Try to use v3 in the following api "googleapis.com/androidpublisher/v2/applications". v1 and v2 are deprecated.Sorbian

© 2022 - 2024 — McMap. All rights reserved.