Apple Store Purchasing

You can redeem Purchases to specifically the Player and even a Class that the Player owns!

Create In App Purchases on Apple

Navigate to the App Store Connect page, and find Monetization, Underneath you'll find In-App Purchases which is where you will create the Products and find the necessary information.

As you create a new In App Purchase, you need to pick a Type, a Reference Name and finally a Product ID. Create the Product and save the Product ID to create IAP with LootLocker.

Creating Purchases In Engine

To create Purchases in Unity, you must inherit from IDetailedStoreListener and import the required methods that follows.

This is how you add products to your IAPManager:

        builder.AddProduct("<replace_with_product_id>", ProductType.Consumable);

Here is how you'd initiate a Purchase:

    public void Purchase()
    {
        var product = controller.products.WithID("<replace_with_product_id>");

        if (product is { availableToPurchase: true })
        {
            controller.InitiatePurchase(product);
        }
    }

This snippet shows you how you'll connect LootLocker IAP to the flow:

In this example LootLockerIAPManager.cs has a RedeemPurchase(Product product) method which will be described underneath Redeem Purchase For Player and Redeem Purchase For Class

    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
    {
        LootLockerIAPManager.RedeemPurchase(e.purchasedProduct);
        return PurchaseProcessingResult.Complete;
    }

This is the complete script:

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);
        }
        //Specifically creates IAP for Google Play Store, using ifdefs to determine which platform is being run is preferable.
        var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance(AppStore.AppleAppStore));

        builder.AddProduct("<replace_with_product_id>", ProductType.Consumable);

        StandardPurchasingModule.Instance().useFakeStoreAlways = true;

        UnityPurchasing.Initialize(this, builder);
    }

    public void OnPurchaseClicked(string productId)
    {
        controller.InitiatePurchase(productId);
    }

    public void Purchase()
    {
        var product = controller.products.WithID("<replace_with_product_id>");

        if (product is { availableToPurchase: true })
        {
            controller.InitiatePurchase(product);
        }
    }

    /// <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 unrecoverable initialization error.
    ///
    /// Note that this will not be called if Internet is unavailable; Unity IAP
    /// will attempt initialization until it becomes available.
    /// </summary>
    public void OnInitializeFailed(InitializationFailureReason error)
    {
    }

    /// <summary>
    /// Called when a purchase completes.
    ///
    /// May be called at any time after OnInitialized().
    /// </summary>
    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs e)
    {
        LootLockerIAPManager.RedeemPurchase(e.purchasedProduct);
        return PurchaseProcessingResult.Complete;
    }

    /// <summary>
    /// Called when a purchase fails.
    /// </summary>
    public void OnPurchaseFailed(Product i, PurchaseFailureReason p)
    {
    }

    public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription)
    {

    }

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

Redeem Purchase For Player

Product is from using UnityEngine.Purchasing;

Which is a Package called "In-App Purchasing", read more here.

public void RedeemPurchase(Product product)
{
    string purchaseToken = product.transactionID;
    LootLockerSDKManager.RedeemAppleAppStorePurchaseForPlayer(purchaseToken, (result) =>
    {
        if (!result.success)
        {
            Debug.Log("Redeem Purchase unsuccessful!");
            return;
        }

    });
}

Redeem Purchase For Class

Product is from using UnityEngine.Purchasing;

Which is a Package called "In-App Purchasing", read more here.

public void RedeemPurchase(Product product)
{
    string purchaseToken = product.transactionID;
    int classID = 123;
    LootLockerSDKManager.RedeemAppleAppStorePurchaseForClass(purchaseToken, classID, (result) =>
    {
        if (!result.success)
        {
            Debug.Log("Redeem Purchase unsuccessful!");
            return;
        }

    });
}

Last updated