Make IAP Purchases In-Game

Set up in-app purchases through Apple App Store and redeem them with LootLocker to grant rewards to players.

Prerequisites

Implementing In-App Purchases

To implement purchases in your game, you need to:

  1. Initialize Unity's IAP system for Apple App Store

  2. Register your products with the Product IDs from App Store Connect

  3. Handle the purchase flow when users buy items

  4. Redeem purchases with LootLocker to grant rewards

Setting Up the IAP Manager

Create an IAPManager script that inherits from IDetailedStoreListener to handle all purchase callbacks.

chevron-rightIAPManager Scripthashtag
using Unity.Services.Core;
using Unity.Services.Core.Environments;
using UnityEngine;
using UnityEngine.Purchasing;
using UnityEngine.Purchasing.Extension;

public class IAPManager : MonoBehaviour, IDetailedStoreListener
{
    public IStoreController controller;
    private IExtensionProvider extensions;

    public async void Awake()
    {
        try
        {
            var options = new InitializationOptions()
                .SetEnvironmentName("production");
            await UnityServices.InitializeAsync(options);
        }
        catch (Exception exception)
        {
            Debug.Log(exception);
        }

        // Create IAP configuration for Apple App Store
        var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance(AppStore.AppleAppStore));

        // Register your products from App Store Connect
        builder.AddProduct("com.company.game.coins100", ProductType.Consumable);
        builder.AddProduct("com.company.game.coins500", ProductType.Consumable);
        builder.AddProduct("com.company.game.premium_pass", ProductType.NonConsumable);

        // Initialize the purchasing system
        UnityPurchasing.Initialize(this, builder);
    }

    /// <summary>
    /// Called when a user clicks the purchase button
    /// </summary>
    public void OnPurchaseClicked(string productId)
    {
        Purchase(productId);
    }

    /// <summary>
    /// Initiates a purchase for the given product ID
    /// </summary>
    public void Purchase(string productId)
    {
        var product = controller.products.WithID(productId);

        if (product is { availableToPurchase: true })
        {
            controller.InitiatePurchase(product);
        }
        else
        {
            Debug.Log($"Product {productId} is not available for purchase");
        }
    }

    /// <summary>
    /// Called when Unity IAP is ready to make purchases
    /// </summary>
    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
        this.controller = controller;
        this.extensions = extensions;
    }

    /// <summary>
    /// Called when Unity IAP encounters an initialization error
    /// </summary>
    public void OnInitializeFailed(InitializationFailureReason error)
    {
        Debug.Log($"IAP initialization failed: {error}");
    }

    /// <summary>
    /// Called when a purchase completes successfully
    /// </summary>
    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
    {
        string purchaseToken = e.purchasedProduct.transactionID;

        // Redeem with LootLocker (see section below for details)
        RedeemPurchaseForPlayer(purchaseToken);

        return PurchaseProcessingResult.Complete;
    }

    /// <summary>
    /// Called when a purchase fails
    /// </summary>
    public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription)
    {
        Debug.Log($"Purchase failed: {failureDescription.reason}");
    }

    public void OnInitializeFailed(InitializationFailureReason error, string message)
    {
        Debug.Log("IAP Error: " + message);
    }

    private void RedeemPurchaseForPlayer(string purchaseToken)
    {
        LootLockerSDKManager.RedeemAppleAppStorePurchaseForPlayer(purchaseToken, (result) =>
        {
            if (!result.success)
            {
                Debug.Log("Redeem Purchase unsuccessful!");
                return;
            }
        });
    }
}

Redeeming Purchases for the Player

After a purchase completes, you need to redeem it with LootLocker to validate the purchase and grant rewards to the player.

When redeeming, provide the transaction ID from the completed purchase. LootLocker will validate it with Apple's servers and grant the configured rewards.

Redeeming Purchases for a Class

You can also redeem purchases to reward a specific class that the player owns. This is useful for class-specific cosmetics or items.

When redeeming for a class, provide both the transaction ID and the class ID of the target class.

Conclusion

You now have a complete Apple in-app purchase flow: setting up products in App Store Connect, implementing the IAP system in Unity, handling purchases, and redeeming them through LootLocker to grant rewards.

With this setup, your game can securely support Apple in-app purchases while maintaining centralized reward logic through LootLocker.

Last updated

Was this helpful?