Skip to content
Commitwise
Menu

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:

  • BillingClient with enablePendingPurchases, reconnection handling.
  • queryProductDetails for base plans and offers (only eligible offers are returned); show formattedPrice.
  • launchBillingFlow with the chosen offerToken and setObfuscatedAccountId (your user, hashed).
  • In PurchasesUpdatedListener and in queryPurchasesAsync on every start and resume: ignore PENDING, send PURCHASED tokens to the server.

Server:

  • Verify each token with purchases.subscriptionsv2.get: package, product, state, expiry, and that obfuscatedExternalAccountId matches 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

Esc

Type to search the whole site.