Android In-App Billing Subscription status change callbacks
Asked Answered
P

2

16

I'm writing an app that will support in-app subscriptions. The subscriptions will enable my users to use a number of services and benefits outside the Android world. I know that I can use Google Play Developer API to query the status of a subscription, but due to the nature of the offered services and benefits, it is not very convenient to do that. I was wondering if there is a way to get notified about the status of the subscription when it changes. Something like a web hook for example.

Picasso answered 8/5, 2015 at 9:23 Comment(2)
As far as I know, there aren't webhooks as other payment processors use on the web. How else did you implement this btw?Platinotype
I haven't so far. The only idea I've got is to query the subscription status when my service is about to be used. I wouldn't like to delay my service though by querying Google Play. This is my main concern but I guess I will have to live with it.Picasso
P
11

I wanted to post a comment in response to yours, but apparently this was too long. In addition to that, it is also kind of an answer to your question, so I thought this might be a good thing to just post it as an answer instead. Maybe it even solves your problem.

So, regarding your comment. You don't necessarily have to do it that way. Now, I don't know your system requirements, but I'll just share mine and hope it helps you out.

Save Users and Subscriptions on your server (database tables). Have a check_at column on the Subscriptions table, besides a renew_at one. Suppose the user has a monthly subscription created on the 1st of January. On that first day, you call your server from Android after the purchase is finished, save the subscription details, then set renew_at as February 1st and check_at as February 4th, considering you set 3 days as delay for transaction failure in Google Play Developer Console. This way, the user has time from 1st February to 3rd February to change credit card details if the payment fails on the 1st. Then on the 4th you will check the Google Play API for subscription details. But don't disable premium functionality during 1st-4th February. Disable it on the 4th if the subscription was canceled. This way, premium content is not delayed nor tampered with in any way. If the subscription end date returned by the API is in the future, then it was renewed, so you should keep premium activated. Now, how do you check it monthly on the 4th you may ask. Use some kind of scheduler to schedule tasks on the database.

This way you remove the need to rely on webhooks. Although I agree, webhooks would've been more efficient and probably even easier to implement.

Let me know if you have any questions or there are some flaws in the system I described.

Platinotype answered 13/6, 2015 at 15:43 Comment(4)
Thank you for your answer @Andrew, I had implemented subscriptions in Stripe before, and now with Google in app products I feel like in stone age.Digitoxin
I am in the same boat :) Just done Stripe, then apple and now google.Implausibility
I personally find Google's subscriptions way easier to implement than Apple's thanks to Real Time Notifications (which are like a sad joke in iTunes). Implementing Apple's subscriptions in our app took our team about 5x more time than Google's subscriptions. The end result contained more bugs too.Weighted
Going back to Bogdan's solution. User can upgrade/downgrade a subscription (if you have multiple products available) or terminate it with a refund. So there are situations in which you'd want to take away or replace his/hers premium features. That's why querying Google's API once a month won't be enough. I would recommend implementing Real Time Notifications and still query API regularly as a fallback in case if any RTN requests don't hit your server.Weighted
W
10

Take a look at Real Time Notifications using Google Cloud's PubSub service. It sends notifications of payment/subscription events like: new buy, renewal, cancellation etc. It includes the token which can be used to retrieve additional info from the API like you do now.

You can also configure this PubSub Subscription to call a custom webhook (which still needs to query the PubSub) on each message that you receive.

Weighted answered 8/5, 2018 at 12:41 Comment(2)
I already setup Real Time Notifications and it works. I get the token and I can get transaction info. But it doesn't contain any user information. How do I know that transaction from which user ?Aviatrix
You can user the developerPayload parameter in your initial buy made from within the App to pass your user ID to Google. Next, when you get your PubSub Notification, it will contain the purchaseToken and you'll get your developerPayload value back when querying Google API for subscription details using this purchaseToken.Weighted

© 2022 - 2024 — McMap. All rights reserved.