Called when we know the product is owned, in particular when it's successfully bought.
If the product is not consumable (which means that it can be owned only once, and it's owned forever once bought): Note that this method may be called multiple times, because there are various situations in which we may "gain knowledge" that user owns this item (e.g. each RefreshPurchases call). Write your code to react gracefullly to this, such that calling this method on an already-owned item is handled correctly.
E.g. if you increase some stat (e.g. "gold owned") when user buys a "chest of gold", and "chest of gold" is non-consumable (you can only own it once, and then you just own it forever), then store the fact that you "already increased gold because of the chest ownership" in the user persistent data (see https://castle-engine.io/manual_user_prefs.php). Do not just increase the "gold owned" at every call of this method.
If the product is a consumable, which means it has a one-time use (and should disappear afterwards, until user will buy it again), then:
Call the Consume method once you know the item is owned. You can call Consume directly from the overridden implementation of Owns , this is often the simplest approach.
Actually perform the consumption (bump the player gold, grant extra life and so on) only when the item is successfully consumed. You are notified about this by the SuccessfullyConsumed call (you can override it), or you can watch if the TInAppProduct.SuccessfullyConsumed flag is set.
Do not give any one-time gain as a response to the Owns call. Always wait for SuccessfullyConsumed call.
This protects you from the scenario when you're notified that you own the item multiple times (which may happen, since purchases may be resumed asynchronously while other code is executing), and you call Consume twice. The SuccessfullyConsumed will only fire once, if user bought item once.
|