Google Play Store Purchasing

In order to use IAP with Google Play Store, you must have an active Google Session, to do this read our Google Sign in Guide or take a look at our in-house feature UPA where you can start a Remote Login Session for Google!

It is presumed that you have at least made an Application in Google Play Console!

Create In App Purchases on Google

To utilize IAP you must make the In App Purchases in Google Play Console.

Some important information to find is your application name; it generally follows this style "com.company.app_name", this name can be found under the overview of all your applications, or inside the application page beneath the title.

Underneath the Monetization page in the left menu list, you will see Products which contains In-app products. This is where you set up your products to use IAP with.

Create new in-app product, supply all the required fields.

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.GooglePlay));

        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 productID = product.definition.id;
    string purchaseToken = product.transactionID;
    LootLockerSDKManager.RedeemGooglePlayStorePurchaseForPlayer(productID, 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 productID = product.definition.id;
    string purchaseToken = product.transactionID;
    int classID = 123;
    LootLockerSDKManager.RedeemGooglePlayStorePurchaseForClass(productID, purchaseToken, classID, (result) =>
    {
        if (!result.success)
        {
            Debug.Log("Redeem Purchase unsuccessful!");
            return;
        }

    });
}

Last updated