[Huawei Developers] Freemind R:
The purpose of this article is how to use Huawei in App Purchase integration in Unity app using Unity UDP. Here we are going to create a sample project in Unity with the help of UDP Package.

In this section, following are covered:

1. How to create new project in UDP Console?

2. How to Import UDP Package?

3. How to link UDP Console Project in unity editor and how to create IAP Products?

4. How to Implement UDP IAP in Client Side?

5. How to Configure IAP in Huawei AGC Console?

6. How to Link UDP Console with Huawei AGC Console?

1. How to create new project in UDP Console?

a) Navigate to below URL and click to sign in to access UDP console

https://distribute.dashboard.unity.com/

b) To create new game, navigate to My Games > Create New Game (You can create a game on the UDP console first, and later link it to an actual UDP project in unity)

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928100641.47301988760850546739015006860977:50511021015629:2800:7CAA28DB9F29710ADCFA90E337AD9895183F40F267F5728A70C9B4D7FD60EF27.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928100641.47301988760850546739015006860977:50511021015629:2800:7CAA28DB9F29710ADCFA90E337AD9895183F40F267F5728A70C9B4D7FD60EF27.png[/IMG]

c) Enter required fields in Edit Game Information and click SAVE.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928100851.87250354524862223115615932166329:50511021015629:2800:A2EDC104C4807BD5937122953E5371E14EFD3D35A3966648DBC65EBAFCAC1CFE.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928100851.87250354524862223115615932166329:50511021015629:2800:A2EDC104C4807BD5937122953E5371E14EFD3D35A3966648DBC65EBAFCAC1CFE.png[/IMG]

d) Copy the client id from the Integration Information panel of the Game Info section, in the UDP console.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928104821.86309024024977442756503697547495:50511021015629:2800:86533CB5D129BBBA6E2612BBC20F35387A1BFA7D2A4BDA7887CDC4C59E9C69D5.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928104821.86309024024977442756503697547495:50511021015629:2800:86533CB5D129BBBA6E2612BBC20F35387A1BFA7D2A4BDA7887CDC4C59E9C69D5.png[/IMG]

2. How to Import UDP Package?

a) Navigate to the unity editor and import Unity Distribution Portal (UDP) from asset store.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928101646.64619508915096870649325903333598:50511021015629:2800:605AE9CCF733BB14CC2A1FDEB7F71AA41967257CE6FC8F332016F2C920822A38.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928101646.64619508915096870649325903333598:50511021015629:2800:605AE9CCF733BB14CC2A1FDEB7F71AA41967257CE6FC8F332016F2C920822A38.png[/IMG]

b) Once it is imported you can see the sample IAP Client side code which contains all the basic modules like initialization, purchase, query, consume etc.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928102615.75585375688065814992525318980637:50511021015629:2800:0E7D78FCB017E63B001BF21CD8E7EBAB70CD58185338F2D1CB55709F190D7FBB.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928102615.75585375688065814992525318980637:50511021015629:2800:0E7D78FCB017E63B001BF21CD8E7EBAB70CD58185338F2D1CB55709F190D7FBB.png[/IMG]

3. How to link UDP Console Project in unity editor and How to create IAP Products?

a) Now we need to link UDP project with Unity (Window > Unity Distribution Portal > Settings). Paste the client id which you copied from UDP console and link it to UDP Client.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928102759.59781517282835034766981619734737:50511021015629:2800:593B1FCB92228F427E5758023C9DA9D2B81258FD308812975B33E0702BBE139D.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928102759.59781517282835034766981619734737:50511021015629:2800:593B1FCB92228F427E5758023C9DA9D2B81258FD308812975B33E0702BBE139D.png[/IMG]

b) Once your Unity project is linked to a UDP client, the UDP Settings inspector loads more settings (like IAP Catalog, UDP Sandbox test account, push, pull).

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200929052227.01474224071945675008089865991489:50511021015629:2800:7F349A3510BC83BDAC0F29BC7CDDF3E3FB6D0C930845B6D58B70FDC751E90317.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200929052227.01474224071945675008089865991489:50511021015629:2800:7F349A3510BC83BDAC0F29BC7CDDF3E3FB6D0C930845B6D58B70FDC751E90317.png[/IMG]

c) Now you can add and define IAP products in the IAP Catalog section of the UDP Settings. The Pull and Push buttons in the top section of the UDP Settings window sync your IAP Catalog with the UDP server. You can add sandbox test account in UDP Sandbox Test Accounts.

d) Once IAP details are configured and pushed. You can see the IAP details in Udp Console.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928103007.35943833864769242536217459297576:50511021015629:2800:45405F3CD788A08C44DEA257B937012BC08D5A8FCD672050D1BC1A7204794A62.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928103007.35943833864769242536217459297576:50511021015629:2800:45405F3CD788A08C44DEA257B937012BC08D5A8FCD672050D1BC1A7204794A62.png[/IMG]

4. How to Implement UDP IAP in Client Side?

a) Now it’s time to implement client side logic.

Please refer the below link and sample code in Unity Editor (Projects > Assets > UDP > Sample > Scripts > UDPSampleScript.cs) which I referred for the implementation.

URL: https://docs.unity3d.com/Packages/com.unity.purchasing.udp@1.0/manual/Implementing-UDP-IAP-on-the-client-side.html

b) Now create buttons and script file in unity editor.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928103126.90407620889263593395980207380503:50511021015629:2800:83697F59848B87E4AADA2C166E32126D3FC6AE13881BF8558A8E422651F3FE7D.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/873/444/2640091000001873444.20200928103126.90407620889263593395980207380503:50511021015629:2800:83697F59848B87E4AADA2C166E32126D3FC6AE13881BF8558A8E422651F3FE7D.png[/IMG]

c) Add the below code to the Script and link the above buttons to the correct On Click listeners in Script.

using UnityEngine;
using UnityEngine.UI;
using System.Collections.Generic;
using UnityEngine.UDP;
public class NewBehaviourScript : MonoBehaviour
{
int n;
public Text myText1;
InitListener m_InitListener;
PurchaseListener m_PurchaseListener;

private static bool m_ConsumeOnPurchase;
private static bool m_ConsumeOnQuery;
private static bool m_Initialized;

public void OnInit()
{
Debug.Log("Oninitialize");
m_InitListener = new InitListener();
m_PurchaseListener = new PurchaseListener();
m_Initialized = false;

StoreService.Initialize(m_InitListener);
}

public void OnPurchase()
{
if (!m_Initialized)
{
Debug.Log("Please Initialize first");
return;
}
string prodcutId = "test_1";

Debug.Log("Buy button is clicked.");
m_ConsumeOnPurchase = false;
Debug.Log("test_1 will be bought");
StoreService.Purchase(prodcutId, "payload", m_PurchaseListener);
}

public void OnPurchaseNonConsumable()
{
if (!m_Initialized)
{
Debug.Log("Please Initialize first");
return;
}

string prodcutId = "test_2";
Debug.Log("Buy button is clicked.");
m_ConsumeOnPurchase = false;
Debug.Log("test_2 will be bought");
StoreService.Purchase(prodcutId, "payload", m_PurchaseListener);
}

public void OnBuyConsume()
{
if (!m_Initialized)
{
Debug.Log("Please Initialize first");
return;
}

string prodcutId = "test_1";
Debug.Log("Buy&Consume button is clicked.");
m_ConsumeOnPurchase = true;

StoreService.Purchase(prodcutId, "payload2", m_PurchaseListener);
}

List<string> productIds = new List<string> { "test_1", "test_2" };

public void OnQueryButton()
{
if (!m_Initialized)
{
Debug.Log("Please Initialize first");
return;
}

m_ConsumeOnQuery = false;
Debug.Log("Query button is clicked.");
StoreService.QueryInventory(productIds, m_PurchaseListener);
}

public void OnQueryConsumeButton()
{
if (!m_Initialized)
{
Debug.Log("Please Initialize first");
return;
}

m_ConsumeOnQuery = true;
Debug.Log("QueryConsume button is clicked.");
StoreService.QueryInventory(productIds, m_PurchaseListener);
}

public class InitListener : IInitListener
{
public void OnInitialized(UserInfo userInfo)
{
Debug.Log("[Game]On Initialized suceeded");
m_Initialized = true;
}

public void OnInitializeFailed(string message)
{
Debug.Log("[Game]OnInitializeFailed: " + message);
}
}

public class PurchaseListener : IPurchaseListener
{
public void OnPurchase(PurchaseInfo purchaseInfo)
{
string message = string.Format(
"[Game] Purchase Succeeded, productId: {0}, cpOrderId: {1}, developerPayload: {2}, storeJson: {3}",
purchaseInfo.ProductId, purchaseInfo.GameOrderId, purchaseInfo.DeveloperPayload,
purchaseInfo.StorePurchaseJsonString);

Debug.Log(message);
// Show(message);

/*
* If the product is consumable, consume it and deliver the product in OnPurchaseConsume().
* Otherwise, deliver the product here.
*/

if (m_ConsumeOnPurchase)
{
Debug.Log("Consuming");
StoreService.ConsumePurchase(purchaseInfo, this);
}
}

public void OnPurchaseFailed(string message, PurchaseInfo purchaseInfo)
{
Debug.Log("Purchase Failed: " + message);
}

public void OnPurchaseRepeated(string productCode)
{
throw new System.NotImplementedException();
}

public void OnPurchaseConsume(PurchaseInfo purchaseInfo)
{
Debug.Log("Consume success: " + purchaseInfo.ProductId);
}

public void OnPurchaseConsumeFailed(string message, PurchaseInfo purchaseInfo)
{
Debug.Log("Consume Failed: " + message);
}

public void OnQueryInventory(Inventory inventory)
{
Debug.Log("OnQueryInventory");
Debug.Log("[Game] Product List: ");
string message = "Product List: \n";

foreach (KeyValuePair<string, ProductInfo> productInfo in inventory.GetProductDictionary())
{
Debug.Log("[Game] Returned product: " + productInfo.Key + " " + productInfo.Value.ProductId);
message += string.Format("{0}:\n" +
"\tTitle: {1}\n" +
"\tDescription: {2}\n" +
"\tConsumable: {3}\n" +
"\tPrice: {4}\n" +
"\tCurrency: {5}\n" +
"\tPriceAmountMicros: {6}\n" +
"\tItemType: {7}\n",
productInfo.Key,
productInfo.Value.Title,
productInfo.Value.Description,
productInfo.Value.Consumable,
productInfo.Value.Price,
productInfo.Value.Currency,
productInfo.Value.PriceAmountMicros,
productInfo.Value.ItemType
);
}

message += "\nPurchase List: \n";

foreach (KeyValuePair<string, PurchaseInfo> purchaseInfo in inventory.GetPurchaseDictionary())
{
Debug.Log("[Game] Returned purchase: " + purchaseInfo.Key);
message += string.Format("{0}\n", purchaseInfo.Value.ProductId);
}

if (m_ConsumeOnQuery)
{
StoreService.ConsumePurchase(inventory.GetPurchaseList(), this);
}
}

public void OnQueryInventoryFailed(string message)
{
Debug.Log("OnQueryInventory Failed: " + message);
}
}
}

For more information, you can visit https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201366685963750355&fid=0101187876626530001