How to use Huawei In-App Purchasing (IAP) on Unity

YusufAltun
Huawei Developers
Published in
4 min readSep 24, 2020

In this article we will create in app purchasing module on Unity.

Introduction

Huawei’s In-App Purchases service allows you to offer in-app purchases and facilitates in-app payment. Users can purchase a variety of virtual products, including one-time virtual products and subscriptions, directly within your app.

General Workflow

HMS Unity workflow:

Plugin Details

Common Preconditions

  1. Huawei Account activated
  2. Merchant Account activated
  3. In App purchasing API enabled
  4. Add Products to AppGallery Connect

This is a common operation in HMS and is not described here. For details please read this article.

Required Configurations

  1. Import the Plugin to your Unity project
  2. Configure AndroidManifest file
  3. Add Agconnect-service.json file
  4. Use IAPManager Prefabs
  5. Connect your game with the HMS Manager

1. Import the Plugin to your Unity project

This plugin is open source and new package is released when major changes occur. We have a two way to import plugin. We can use released package one or we can use source code.

Download Released package:

HMS Unity Package

After download we will import: Asset-Import Packge-Custom Package.

Download Source code:

We can use source code for latest updates.

Source doe

Unzip source file than drag and drop source codes under the your project asset folder.

2. Configure AndroidManifest file

We should update three parameter on this file: App ID, CP ID and package name, this informations exist in agconnect-service.json file.

AndroidManifest:

3. Add Agconnect-service.jon file

We already create an app in AppGallery Connect, we also generate a signing certificate fingerprint and add the certificate file to the project. We will download agconnect-service.json file from AppGallery Connect we will put it this path : project_path\Assets\Huawei\agconnect-service.json

4. Use IAPManager Prefabs

IAPManager Prefabs exist in plugin we can use this prefab directly for our project. (project_path\Assets\Huawei\Prefabs\IAPManager Prefab).

Example usage:

5. Connect your game with the HMS Manager

HMS manager can invoke Huawei In-App Purchasing sdk functions. IapManager.cs is our bridge for HMS sdk.

Understanding IAPManager script

First steps is checking IAP availability.

HMS Unity Plugin have two scripts “IAPManager” and “IAPDemoManager”. In my example project I changed “IAPDemoManager name to “IAPController”.

Call the obtainProductInfo API to query products configured in AppGallery Connect. If the API call is successful, all products are displayed.

After that we will create obtain product request. IAPController invoke “ObtainProductConsumablesInfo”, “ObtainProductNonConsumablesInfo” and “ObtainProductSubscriptionInfo” functions for each type of product.

In-app product type contains:
0: consumable
1: non-consumable
2: auto-renewable subscription

Purchase the product

A user can tap the buy button to purchase the product. Button onClick() method invoke “BuyProduct” function.

Buy item button

IapController “BuyProduct” function:

This function invoke “IAPManager.BuyProduct” for using the createPurchaseIntent API to complete the purchase.

  1. Create a task for requesting the createPurchaseIntent API.
  2. Set OnSuccessListener and OnFailureListener for the task to receive the API request result.
  3. A PurchaseIntentResult object is returned in the success scenario. You need to bring up the returned page to complete payment. The “parsePurchaseResultInfoFromIntent” method of IapClient to obtain the PurchaseResultInfo object, which contains the payment result. If the payment is successful, signature verification must be performed on the returned data using the public payment key based on Verifying the Signature in the Returned Result. After successful verification, the product is delivered and consumed. If the payment fails, a returnCode will be received. Rectify the fault by referring to Result Codes.

4. An Exception object is returned in the failure scenario. If the object is an HMSException object, use ErrorCode to obtain the result code.

Consume the purchased product

After a user successfully purchases the your product, it will be delivered to the user, and consumed using the consumeOwnedPurchase API once the delivery is successful. The user will then be able to purchase the product again.

We integrated Huawei IAP and Unity game engine. For testing we can use huawei sandbox here.

You can follow the article with the completed code here.

Configure “agconnect-service.json” and “Androidmanifest” files

That’s all for now folks.

See you on the next adventure!

--

--