Virtual Store

Listing store items #

You can list available store items.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
foreach (var storeItem in economySystem.StoreItems)
{
    Debug.Log($"{storeItem.Name} - {storeItem.Description}). Cost: ");

    if (!string.IsNullOrEmpty(storeItem.Cost.Sku))
    {
        Debug.Log($"{storeItem.Cost.Sku}");
    }
    else
    {
        foreach (var currencyKvp in storeItem.Cost.Currencies)
        {
            Debug.Log($"{currencyKvp.Key}: {currencyKvp.Value}");
        }
    }
}

Refreshing the store #

You can refresh the economy system’s store data.

1
await economySystem.RefreshStoreAsync();

Making a purchase intent #

You can make a purchase intent for a user, which is useful to differentiate product purchases that use the same SKU code.

1
await economySystem.PurchaseIntentAsync(storeItem);

Alternatively you can specify a store item id and SKU code.

1
await economySystem.PurchaseIntentAsync("<itemId1>", "<sku>");

Purchasing a store item #

You can purchase a store item for a user.

1
await economySystem.PurchaseStoreItemAsync("<itemId1>", "<receipt>");

Getting the active store type #

You can get the active store type.

1
Debug.Log($"Active store type is {economySystem.ActiveStoreType}");

Purchasing store items #

The purchase flow depends on whether the store item is configured for game currency purchase (no sku) or in-app purchase (has sku). See Virtual Store purchases for details on how SKU configuration affects purchases.

This example shows how to handle both purchase types based on the store item’s configuration:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public async Task BuyStoreItem(EconomyListStoreItem storeItem)
{
    if (string.IsNullOrEmpty(storeItem.Cost.Sku))
    {
        // Soft currency purchase - server deducts currency and grants reward
        IEconomyPurchaseAck result = await _economySystem.PurchaseStoreItemAsync(storeItem.Id);

        // Show reward animation UI.
        ShowRewardAnimationUI(result.Reward); // Your custom function.
    }
    else
    {
        // Hard currency purchase - requires platform IAP flow
        try
        {
            var result = await _unityPurchasingSystem.BuyProductByIdAsync(storeItem.Id);
            AnalyticsSendReceipt(result.receipt); // Your custom function.

            // Show reward animation UI.
            ShowRewardAnimationUI(storeItem.Reward); // Your custom function.
        }
        catch (PurchaseFailureException e)
        {
            switch (e.Reason)
            {
                case UnityEngine.Purchasing.PurchaseFailureReason.UserCancelled:
                    // Popup some UI.
                    break;
                case UnityEngine.Purchasing.PurchaseFailureReason.PaymentDeclined:
                    // Popup some UI.
                    break;
                case UnityEngine.Purchasing.PurchaseFailureReason.ExistingPurchasePending:
                    // Popup some UI.
                    break;
                case UnityEngine.Purchasing.PurchaseFailureReason.PurchasingUnavailable:
                case UnityEngine.Purchasing.PurchaseFailureReason.ProductUnavailable:
                case UnityEngine.Purchasing.PurchaseFailureReason.SignatureInvalid:
                case UnityEngine.Purchasing.PurchaseFailureReason.DuplicateTransaction:
                case UnityEngine.Purchasing.PurchaseFailureReason.Unknown:
                    break;
            }
        }
    }
}
Soft currency purchases will fail if the store item has a sku but you haven’t configured IAP validation on your server. Remove the sku from items that should only be purchasable with soft currency.