Java – Handling in-app purchases/consumables across sessions/devices?

Handling in-app purchases/consumables across sessions/devices?… here is a solution to the problem.

Handling in-app purchases/consumables across sessions/devices?

My questions revolve around using Google’s In-App Billing API to process in-app consumer purchases. ( https://developer.android.com/google/play/billing/api.html#consumetypes )

Their documentation describes consumables as:

Consumable products

In contrast, you can implement consumption for products that can be made available for purchase multiple times. Typically, these products provide certain temporary effects. For example, the user’s in-game character might gain life points or gain extra gold coins in their inventory. Dispensing the benefits or effects of the purchased product in your application is called provisioning the managed product. You are responsible for controlling and tracking how managed products are provisioned to the users.

Consumables are things that can be purchased multiple times (e.g. in-game currency, in-game elements or upgrades used, etc.), while non-consumables can only be purchased once (no ads, skins/characters, etc.).

In the documentation on consumer purchases ( https://developer.android.com/training/play-billing-library/purchase-iab-products.html ) , it also mentions:

How you use the consumption mechanism in your app is up to you. Typically, you would implement consumption for products with temporary benefits that users may want to purchase multiple times, such as in-game currency or replenishable game tokens. You would typically not want to implement consumption for products that are purchased once and provide a permanent effect, such as a premium upgrade.

It’s your responsibility to control and track how the in-app product is provisioned to the user. For example, if the user purchased in-game currency, you should update the player’s inventory with the amount of currency purchased.

My question is how to track user inventory for consumables? Documentation and various videos seem to quickly obscure this, basically saying that the application must apply the effect of the consumables after obtaining confirmation of a successful purchase. But that’s not really the whole picture. What if a user logs out and signs back in with a different account? Or they got a new phone and they should have installed the product on that phone.

You can’t actually save your purchase history in SharedPreferences or a permanent cache because it’s associated with your phone. If users log in on different phones, then they should enjoy the benefits of all the purchases they make.

For example:

The game starts with 1000 gold coins for the player. Players purchase an additional 500 gold through an in-app purchase and then spend 200 gold. If a player buys a new phone and installs the app on that phone, they should have 1300 coins.

How is this usually done?

Do you need to run private servers independent of Google to track purchases/purchases of such items?

Thanks!!

Solution

I’m implementing in-app purchases myself.

Do you need to run a private server that keeps track of purchases/consumption of such things separate from Google?

Absolutely, as Google said in Security Best Practices As suggested

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?

Bind orderId to an account or device.
In the first case, you can easily manage purchases when users switch devices (another reason to get a private server).
In the second case, you can allow switching accounts on the same device.
So, which one to choose is up to you.

You need to synchronize your on-premises consumption to the server.

This is the process of verifying your 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 process for using 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 the following article:

  • Tutorial: How to implement in-app billing in Android LINK
  • Articles on implementing InApp purchases LINK
  • How to verify Android app purchases on the server (Google Play in App Billing v3)LINK .
  • Another SO answer LINK .
  • Another SO answer LINK .
  • Code project example LINK

Related Problems and Solutions