Creating an Economy #
For this guide we will allow players to purchase a premium currency, Gems, via IAPs. These Gems can then be spent in-game to purchase Coins which can in turn be used to purchase in-game items.
We will also allow players to become a Premium player using an IAP and later restore that purchase if necessary.
Purchasing premium currency with IAPs #
The following server runtime code example assumes the use of the Unity IAP package to submit a purchase receipt to a custom Nakama RPC.
This RPC first checks the payload to see which app store was used to make the purchase, then validates the purchase with the appropriate app store. If the purchase is valid, it calls a separate function for each validated purchase in the array to check the purchase’s product ID and give the appropriate amount of Gems to the player in their virtual wallet.
Note that for Nakama to validate purchases you must provide configuration variables appropriate to each app store.
Purchasing in-game currency with premium currency #
Now that the player has purchased a premium currency, they can use it to purchase an in-game currency, Coins. The following RPC allows the user to specify how many Gems they would like to spend on Coins. Here the conversion rate (1 Gem = 1000 Coins) is hardcoded.
Purchasing items with in-game currency #
For this example we’re going to store a configuration object inside the Nakama Storage Engine which will map each item in the game to a price in Coins. We will configure this inside our server’s
With our prices stored in the Storage Engine we can write an RPC that will allow the user to buy an item (provided they have enough Coins).
Purchasing non-consumables with IAPs #
As well as being able to purchase a virtual currency through In-App Purchases, you may wish to provide players with the ability to directly purchase non-consumable goods too.
For this example, we’ll revisit our server runtime RPC from earlier that allowed players to purchase Gems. However, we’ll now add the ability to purchase a non-consumable which can be restored on a different device later. The non-consumable in this instance will be the ability to become a Premium status player.
For this, once the purchase has been validated, we will set a flag in the user’s metadata to indicate that they are a Premium player. This can then be used throughout the game to provide various perks/rewards.
Restoring an IAP purchase #
If a user changes their device, they should be able to restore any purchases they had previously made. This does not apply to consumable purchases (e.g. virtual currency) which will have already been “consumed” at the time of purchase, but for things such as unlocking the full game, removing ads, or becoming a Premium member, the user should receive all of the same benefits on any new device where they install your game.
For this, we will provide an RPC that the game client can call to receive a list of all the IAP Product IDs and purchase timestamps that have been verified as successful purchases in Nakama. The client can then use this information to restore the appropriate feature on the new device.