View as Markdown

Economy

Read more about the Economy system in Hiro here.

Store type configuration #

When making hard currency purchases, set the correct EconomyStoreType based on the player’s platform. The store type determines which platform’s receipt validation endpoint the server uses.

ValueDescription
EconomyStoreType.AppleAppstoreApple App Store
EconomyStoreType.GooglePlayGoogle Play Store
EconomyStoreType.FbinstantFacebook Instant Games
EconomyStoreType.UnspecifiedDefaults to Apple App Store

For more details on store types and purchase validation, see Virtual Store.

Initializing the economy system #

The economy system relies on the Nakama System and an ILogger, both must be passed in as dependencies via the constructor. You must also specify the store type for receipt validation.

1
2
3
var economySystem = new EconomySystem(logger, nakamaSystem,
    UnityPurchasingSystem.GetStoreType(Application.platform));
systems.Add(economySystem);

Subscribing to changes in the economy system #

You can listen for changes in the economy system so that you can respond appropriately, such as updating the UI, by implementing the IObserver pattern, or use the SystemObserver<T> type which handles it for you.

1
2
3
4
5
var disposer = SystemObserver<EconomySystem>.Create(economySystem, system => {
    Instance.Logger.Info($"System updated.");

    // Update UI elements etc as necessary here...
});

Refreshing the economy system #

To ensure the economy system has the latest information from Nakama you can refresh it.

1
await economySystem.RefreshAsync();

Processing IAP purchases #

Hiro’s UnityPurchasingSystem manages the full purchase flow: initiating the platform purchase, collecting the receipt, and submitting it to Nakama for server-side validation. By the time BuyProductAsync or BuyProductByIdAsync resolves, the receipt has been validated and the reward granted.

  • BuyProductAsync(storeItem): initiates a platform purchase for a given economy store item.
  • BuyProductByIdAsync(id): initiates a platform purchase by store item ID.

Receipt validation against Apple App Store or Google Play is handled server-side by Nakama. No manual validation code is required.

For a complete implementation guide including failure handling, localized prices, and testing, see How to implement IAPs in Unity.

Additional information #