Guides & References

A full guide and reference page for in-app purchases in the HMS Unity Plugin, providing the necessary kits and services for a seamless integration.

Before we begin, ensure you have the following:

  1. Open the plugin editor window under Huawei > Kit Settings

  2. For activating and using the IAP module of hms-unity-plugin, firstly developer has to activate the "Enable Plugin" checkbox (shown as 1 in the following picture) and "In-App Purchase" checkbox (shown as 2 in the following picture)

Creating Products in AppGallery Connect

  1. Go to your app by following My apps > Operate >Product Management in AppGallery Connect.

  1. Click on Add Products and fill in the information needed and save.

To create products in the Unity Editor, please take note of your ProductID. This unique identifier will be essential in identifying and managing your products within the Unity ecosystem.

Creating Products in Unity Editor

  1. Click the IAP tab: Huawei > Kit Settings > IAP tab.

  1. Create all products (all products at App Gallery Connect) in the IAP section.

You can create products in 2 ways.

  1. Writing product detail in the IAP section by writing productId into Identifier and then clicking Add button. (shown as 1 in the following picture)

  2. If you are using Google IAP, export IAP items to Excel then import into the Unity editor. (shown as 2 in the following picture)

Initializing IAP Kit at Runtime

You have 2 options.

1- If you check the "Initialize On Start" checkbox, it'll automatically retrieve registered products on Start.

2 - If you want to initialize the IAP by yourself, call the function mentioned below. You can also set callbacks as well.

HMSIAPManager.Instance.InitializeIAP();

HMSIAPManager.Instance.OnInitializeIAPSuccess += OnInitializeIAPSuccess;
HMSIAPManager.Instance.OnInitializeIAPFailure += OnInitializeIAPFailure;

private void OnInitializeIAPFailure(HMSException ex)
{
        
}

private void OnInitializeIAPSuccess()
{
 // IAP is ready 
}

Purchase Products

You can purchase products by calling HMSIAPManager.Instance.PurchaseProduct(productID).

Then you can catch purchase success at OnBuyProductSuccess callback with productId. In this callback, you can make what you want...

HMSIAPManager.Instance.OnBuyProductSuccess += OnBuyProductSuccess;

public void PurchaseProduct(string productID)
{
  Debug.Log($"PurchaseProduct");

  HMSIAPManager.Instance.PurchaseProduct(productID);
}

private void OnBuyProductSuccess(PurchaseResultInfo obj)
{
    Debug.Log($"OnBuyProductSuccess");

    if (obj.InAppPurchaseData.ProductId == "removeads")
    {
        IAPLog?.Invoke("Ads Removed!");
    }
    else if (obj.InAppPurchaseData.ProductId == "coins100")
    {
        IAPLog?.Invoke("coins100 Purchased!");
    }
    else if (obj.InAppPurchaseData.ProductId == "premium")
    {
        IAPLog?.Invoke("premium subscribed!");
    }
}

Restore Products

  1. Consumable: Retrieve consumable product records with ;

HMSIAPManager.Instance.RestorePurchaseRecords();
  1. Non-Consumable: Retrieve active non-consumable products with

HMSIAPManager.Instance.RestoreOwnedPurchases();
  1. Subscriptions: Retrieve active subscriptions with

HMSIAPManager.Instance.RestoreOwnedPurchases();

Restoring Products Example

List<InAppPurchaseData> consumablePurchaseRecord = new List<InAppPurchaseData>();
List<InAppPurchaseData> activeNonConsumables = new List<InAppPurchaseData>();
List<InAppPurchaseData> activeSubscriptions = new List<InAppPurchaseData>();

    private void RestoreProducts()
    {

        HMSIAPManager.Instance.RestorePurchaseRecords((restoredProducts) =>
        {
            foreach (var item in restoredProducts.InAppPurchaseDataList)
            {
                if ((IAPProductType)item.Kind == IAPProductType.Consumable)
                {
                    Debug.Log($"Consumable: ProductId {item.ProductId} , SubValid {item.SubValid} , PurchaseToken {item.PurchaseToken} , OrderID  {item.OrderID}");
                    consumablePurchaseRecord.Add(item);
                }
            }
        });

        HMSIAPManager.Instance.RestoreOwnedPurchases((restoredProducts) =>
        {
            foreach (var item in restoredProducts.InAppPurchaseDataList)
            {
                if ((IAPProductType)item.Kind == IAPProductType.Subscription)
                {
                    Debug.Log($"Subscription: ProductId {item.ProductId} , ExpirationDate {item.ExpirationDate} , AutoRenewing {item.AutoRenewing} , PurchaseToken {item.PurchaseToken} , OrderID {item.OrderID}");
                    activeSubscriptions.Add(item);
                }

                else if ((IAPProductType)item.Kind == IAPProductType.NonConsumable)
                {
                    Debug.Log($"NonConsumable: ProductId {item.ProductId} , DaysLasted {item.DaysLasted} , SubValid {item.SubValid} , PurchaseToken {item.PurchaseToken} ,OrderID {item.OrderID}");
                    activeNonConsumables.Add(item);
                }
            }
        });

    }

Managing Subscriptions

  1. Access the subscription management screen by calling;

HMSIAPManager.Instance.RedirectingtoSubscriptionManagementScreen(string SubscribeProductId);

This screen displays all subscriptions.

  1. To access a specific subscription screen directly, call

HMSIAPManager.Instance.RedirectingtoSubscriptionManagementScreen();

Last updated