# Economy

**URL:** https://heroiclabs.com/docs/hiro/unity/economy/
**Keywords:** economy, hiro
**Categories:** hiro, unity, economy

---


# Economy

Read more about the Economy system in Hiro [here](../../concepts/economy/).

## 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.

| Value                            | Description                 |
| -------------------------------- | --------------------------- |
| `EconomyStoreType.AppleAppstore` | Apple App Store             |
| `EconomyStoreType.GooglePlay`    | Google Play Store           |
| `EconomyStoreType.Fbinstant`     | Facebook Instant Games      |
| `EconomyStoreType.Unspecified`   | Defaults to Apple App Store |

For more details on store types and purchase validation, see [Virtual Store](/hiro/concepts/economy/virtual-store).

## Initializing the economy system

The economy system relies on the [Nakama System](../getting-started/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.

```csharp
EconomyStoreType storeType;

#if UNITY_ANDROID
    storeType = EconomyStoreType.GooglePlay;
#elif UNITY_IOS
    storeType = EconomyStoreType.AppleAppstore;
#else
    storeType = EconomyStoreType.Unspecified;
#endif

var economySystem = new EconomySystem(logger, nakamaSystem, storeType);
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.

```csharp
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.

```csharp
await economySystem.RefreshAsync();
```

## Processing IAP purchases

To validate a hard currency purchase, call `PurchaseStoreItemAsync` with the store item ID and
the receipt from Unity IAP.

```csharp
  var ack = await economySystem.PurchaseStoreItemAsync(
  product.definition.storeSpecificId,
  product.receipt);
```

Where product comes from Unity's ProcessPurchase callback (PurchaseEventArgs.purchasedProduct).

The method also accepts underlying platform receipts, by passing the raw receipt string in place of `product.receipt`. The SDK determines the format automatically:

- **Unity IAP receipt:** a JSON string with a Payload key. The SDK extracts the payload and forwards
  it to the server.
- **Underlying platform receipt:** any other non-empty string. The SDK forwards it to the server
  as-is.

The server handles final validation and rejects invalid receipts. An empty receipt string always
fails.

## Additional information

- [Hiro Store sample project](../../../../sample-projects/unity/hiro-store)
- [Mage Mayhem sample project](../../../../sample-projects/games/mage-mayhem)
