How do you implement subscriptions with Google Play Billing correctly?
AndroidSenior
In shortLoad product details and eligible offers, launch the billing flow with your account id, grant access only for PURCHASED purchases after your server verifies the token with the Play Developer API, acknowledge within three days, restore with queryPurchasesAsync on every start, and follow the lifecycle with Real-time Developer Notifications.
Model answer
Client:
BillingClientwithenablePendingPurchases, reconnection handling.queryProductDetailsfor base plans and offers (only eligible offers are returned); showformattedPrice.launchBillingFlowwith the chosenofferTokenandsetObfuscatedAccountId(your user, hashed).- In
PurchasesUpdatedListenerand inqueryPurchasesAsyncon every start and resume: ignorePENDING, sendPURCHASEDtokens to the server.
Server:
- Verify each token with
purchases.subscriptionsv2.get: package, product, state, expiry, and thatobfuscatedExternalAccountIdmatches the requesting user; bind token → user uniquely so shared tokens can’t unlock other accounts. - Acknowledge within three days (or Google refunds and revokes the purchase).
- Subscribe to Real-time Developer Notifications via Cloud Pub/Sub; for each, re-fetch the subscription and recompute the entitlement idempotently.
States to access: active, in grace period (with a fix-payment prompt) and canceled-until-expiry grant access; on hold, paused, expired and revoked don’t. One-time refunds come from the Voided Purchases API.
Plan changes: upgrades and downgrades use replacement modes; the new purchase’s linkedPurchaseToken points to the old one, whose entitlement must move.
What they're looking for
The client flow including pending purchases and restore, acknowledgment, server verification and account binding, RTDN with re-fetching, lifecycle states to access, and plan changes.
Common mistakes
- Not acknowledging purchases, leading to automatic refunds.
- Granting access for PENDING purchases or from client data alone.
- Trusting the RTDN notification type without re-fetching state.
More android questions
- How do you make sure background work in a mobile app stops when the user leaves the screen?Mid-level
- What's the difference between a configuration change and process death on Android, and what survives each?Mid-level
- How would you modularize a large Android app, and how do you keep builds fast?Senior
- How do you store an auth token securely on Android?Mid-level