💸In App Purchase

How purchases work

When users open their avatar editor, a banner will prompt them to purchase coins to buy clothing, accessories, and other avatar upgrades. This will trigger your app's in app purchase flow. You will collect the money, and Galaxy will bill you for 50% of the proceeds at the end of the month.

You'll need to use an IAP manager class to make this work. Open the class that implements the IDetailedStoreListener interface.

1. Add in app purchase products to the store & dashboard

Add your in app purchases to the Apple App Store and Google Play Store. Make both the Apple and Android IAPs the same product ID. Then, add these IDs in the Products section of your Galaxy dashboard.

2. Subscribe to the Galaxy delegate to initiate purchases

private void Start()
{
    GalaxyClientAPI.Controller.shouldPurchaseProduct += productId =>
    {
        controller.InitiatePurchase(productId);
    };
}

3. Complete the purchase with Galaxy

public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent)
{
    GalaxyPurchasing.VerifyIAP(purchaseEvent.purchasedProduct.receipt, 
        result => 
        {
            Debug.Log("Benefit awarded: " + result.BenefitAwarded);
            controller.ConfirmPendingPurchase(purchaseEvent.purchasedProduct);
        }, 
        error =>
        {
            Debug.LogError("Error verify IAP: " + error); 
        });
    return PurchaseProcessingResult.Pending;
}

See this example class:

using GalaxySDK;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.Purchasing;
using UnityEngine.Purchasing.Extension;
using UnityEngine.UI;

public class IAPManager : Singleton<IAPManager>, IDetailedStoreListener
{

    private IStoreController controller;
    private IExtensionProvider extensions;

    private void Start()
    {
        var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance())
            .AddProduct("com.anagram.pro", ProductType.Consumable)
            .AddProduct("com.anagram.fifty", ProductType.Consumable)
            .AddProduct("com.anagram.hundred", ProductType.Consumable)
            .AddProduct("com.anagram.fivehundred", ProductType.Consumable);

        UnityPurchasing.Initialize(this, builder);

        GalaxyClientAPI.Controller.shouldPurchaseProduct += productId =>
        {
            Debug.Log("Initiating purchase of " + productId);
            controller.InitiatePurchase(productId);
        };
    }
    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
        this.controller = controller;
        this.extensions = extensions;
    }

    public void OnInitializeFailed(InitializationFailureReason error)
    {
        Debug.LogError("Failed to initialize purchasing: " + error);
    }

    public void OnInitializeFailed(InitializationFailureReason error, string message)
    {
        Debug.LogError("Failed to initialize purchasing [" + error + "]: " + message);
    }

    public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
    {
        Debug.LogError("Purchased failed [" + failureReason + "]: " + product);

    }

    public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent)
    {
        GalaxyPurchasing.VerifyIAP(purchaseEvent.purchasedProduct.receipt, 
            result => 
            {
                Debug.Log("Benefit awarded: " + result.BenefitAwarded);
                controller.ConfirmPendingPurchase(purchaseEvent.purchasedProduct);
            }, 
            error =>
            {
                Debug.LogError("Error verify IAP: " + error); 
            });
        return PurchaseProcessingResult.Pending;
    }

    public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription)

    {
        Debug.LogError("Purchased failed [" + failureDescription.message + "]: " + product);
    }
}

Last updated