My question is centered around handling in-app purchases for consumables with Google\'s In-App Billing API. (https://developer.android.com/google/play/billing/api.html#consu
I am implementing in-app purchase myself.
Do you need to run a private server that keeps track of purchases/consumption of such things separate from Google?
Yes of course as Google suggests in Security Best Practices
It's highly recommended to validate purchase details on a server that you trust. If you cannot use a server, however, it's still possible to validate these details within your app on a device.
Your second question
What if a user signs out and signs back in with a different account?
Tie the orderId
to account or device.
In the first case, you can easily manage the purchase when the user switches the devices(another reason to get a private server).
While in the second case you can allow switching accounts on the same device.
So it's up to you which one to select.
You need to Synchronize local consumption to the server.
This is the flow for Verifying the purchase:
- User clicks “BUY” button.
- Makes payment with google.
- App receives “receipt” from google and store it locally
- Send this “RECEIPT” to the Server.
- The Server sends the “purchaseToken” to Google Play Developer API for validation
- The Google Play Developer API sends response with status code.
- Store the RECEIPT in the server database (If we you to keep history of purchases by users).
This is the flow for Consuming the product:
- The user opens the app.
- App assigns values to the Resources by reading from local storage.
- App tries to synchronize with the Server.(checks last updated timestamp)
Different scenarios:
Synchronization Successful: Assigns Resource values from the server. Set newly retrieved values in the local storage.
Synchronization Failed: Keep Resource values and try again.
- User consumes the Resource.
- App updates local values in Resource and sync with the server.(checks last updated timestamp)
I used Following articles: