Handling in-app purchases / consumable products across sessions/devices?

余生长醉 提交于 2019-11-29 15:31:01

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:

  1. User clicks “BUY” button.
  2. Makes payment with google.
  3. App receives “receipt” from google and store it locally
  4. Send this “RECEIPT” to the Server.
  5. The Server sends the “purchaseToken” to Google Play Developer API for validation
  6. The Google Play Developer API sends response with status code.
  7. 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:

  1. The user opens the app.
  2. App assigns values to the Resources by reading from local storage.
  3. 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.

  1. User consumes the Resource.
  2. App updates local values in Resource and sync with the server.(checks last updated timestamp)

I used Following articles:

  • Tutorial: How to Implement In-app Billing in Android LINK
  • Article on implementing InApp purchase LINK.
  • How to verify purchase for android app in server side (google play in app billing v3) LINK.
  • Another SO answer LINK.
  • Another SO answer LINK.
  • Code Project Sample LINK.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!