9

I'm astonished to find very little documentation or examples about obtaining the expiration date for a Play Store subscription. Throughout the official In-app Billing documentation, the following is noted:

When the user successfully purchases a new subscription, your app notifies a backend server, which stores the purchase token, user name, and other information in a secure location.

Since your app cannot know the expiration date, your server can poll Google Play to get the expiration and store it with the purchase token and other data.

Because your server now knows the expiration date, it does not need to poll Google Play again until after the expiration date, at which time it can confirm that the subscription was not cancelled.

In my app I'm able to obtain an Authorization Token via OAuth and a Purchase Token (using convenience methods for in-app billing) and I can send that information to my back end server. However at that point I can find little information on what to do with those tokens once I have them. The only information I've been able to uncover is reference to Google APIs Client Libraries, which provides sample libraries for various languages Client Libraries (I happen to be using PHP). Inspection of the PHP "Documentation" sample (Getting up and running in 60 seconds LOL), shows the following line of code:

$client->setApplicationName('Google+ PHP Starter Application');

What does this have to do with finding a subscription expiration date???? It seems to me that the parameter passed to setApplicationName should have some reference to the Play Store, but there seems to be NO reference to the Play Store in the list of samples (i.e. Google Analytics API, YouTube Data API, etc) Google APIS

So I'm really at a loss on how to move forward.

Any direction would be appreciated.

user1168400
  • 111
  • 3
  • 9

1 Answers1

3

You need to use the Purchase Status API, which has the Purchases:get method which takes a packageName, subscriptionId, and token and returns a Purchases resource, which contains (among other things) the validUntilTimestampMsec field which is the time at which the subscription will expire.

ianhanniballake
  • 191,609
  • 30
  • 470
  • 443
  • I've studied Purchases.get and manually created: https://www.googleapis.com/androidpublisher/v1.1/applications/com.mypackage.name/subscriptions/1111111111111111.222222222222/purchases/?access_token=Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234 – user1168400 Jul 12 '13 at 18:02
  • I've studied Purchases.get and manually created a HTTP request like >https://www.googleapis.com/androidpublisher/v1.1/applications/com.mypackage.name/subscriptions/111.222/purchases/?access_token=Blaa1234 I expected to see a Purchase result in my browser but nothing was returned. – user1168400 Jul 12 '13 at 18:13
  • Your URL does not end in the in-app billing token (which is completely different from the authorization `access_token`). For example, [this URL](https://www.googleapis.com/androidpublisher/v1.1/applications/com.mypackage.name/subscriptions/1111111111111111.222222222222/purchases/Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234Blaa1234) returns a response - going through the [Authorization steps](https://developers.google.com/android-publisher/authorization) should get you a valid response – ianhanniballake Jul 12 '13 at 18:21
  • I'm getting the token from my App which uses: GoogleAuthUtil.getToken, with scope ...googleapis.com/auth/userinfo.profile Maybe this is token exclusive to userinfo???? Is there a different scope for purchase???? – user1168400 Jul 13 '13 at 01:58
  • @user1168400 - as per the [Purchases.get Auth section](https://developers.google.com/android-publisher/v1_1/purchases/get#auth) you need `https://www.googleapis.com/auth/androidpublisher` for your scope get a valid `access_token`. Note you **also** need the `purchaseToken` returned by the in-app purchases API. – ianhanniballake Jul 13 '13 at 02:09
  • I create the following scope: mEmail,"oauth2:" + SCOPE_USER_PROFILE + " " + SCOPE_EMAIL + " " + SCOPE_PUBLISHER where the SCOPE_ are defined. I'm assuming that I can create one GoogleAuthUtil.getToken for all uses. The token returned allows me to get user info but fails when used to get token for androidpublisher. https://www.googleapis.com/androidpublisher/v1.1/applications/com.mypackage.name/subscriptions/my_subscrkption_sku/purchases/value_provided_by_play_store?access_token=valueprovided_by_GoogleAuthUtil.getToken Throws 403 error – user1168400 Jul 15 '13 at 22:58