Please review the section "Verify purchases before granting entitlements".
https://developer.android.com/google/play/billing/security#verify
You should unlock content only after verifying via your backend server that the purchase was legitimate.
You implement onPurchasesUpdated
, to get notifications for purchases updates initiated both within or outside your app.
If you don't acknowledge a purchase, the purchase will be automatically refunded. You implement onAcknowledgePurchaseResponse
, to receive a notification that the acknowledgement of the purchase operation is complete.
But to know if it is a legitimate purchase, you must verify you the purchase is legitimate before granting entitlements.
A special case of sensitive data and logic that should be handled in
the backend is purchase verification. After a user has made a
purchase, you should do the following:
- Send the corresponding purchaseToken to your backend. This means that you should maintain a record of all purchaseToken values for all
purchases.
- Verify that the purchaseToken value for the current purchase does not match any previous purchaseToken values. purchaseToken is globally
unique, so you can safely use this value as a primary key in your
database.
- Use the Purchases.products:get or Purchases.subscriptions:get endpoints in the Google Play Developer API to verify with Google that
the purchase is legitimate.
- If the purchase is legitimate and has not been used in the past, you can then safely grant entitlement to the in-app item or subscription.
- For subscriptions, when linkedPurchaseToken is set in Purchases.subscriptions:get, you should also remove the
linkedPurchaseToken from your database and revoke the entitlement that
is granted to the linkedPurchaseToken to ensure that multiple users
are not entitled for the same purchase.
hence you should unlock content only after all three are completed.
- onPurchaseUpdated, for initially knowing that a purchase is complete.
- onAcknowledgePurchaseResponse, so you know the acknowledgement is done, and the purchase will not be automatically refunded
- you need to verify via your backend server that the purchase was legitimate.
When you have done all three, it is safe to unlock your purchase. If you do not do all three, there is a risk of unlocking content that for a purchase that has either been refunded or is illegitimate.