Android | Huawei In App Purchases(IAP & Process)

Mahmut Can Sevin
Huawei Developers
Published in
8 min readJul 20, 2020

In this article, I will explain how you can integrate app purchases for the HMS ecosystem into your project. But first, before adding HMS kit to our project, we need to define HMS services to our project. You can read this article for identification procedures. 👇

Now that you have finished HMS integration, you need to activate the IAP service for the first time, Huawei will ask for your bank account information for the payment. ⚠️Verification of this information will take approximately 1–2 days.

So I continue to write assuming that you have done HMS services integration operations & IAP register process. First, I would like to explain In-App Purchase for those who don't know. Huawei’s In-App Purchases (IAP) 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. For more information, see the Huawei Developers documentation 🔗

This is the basic operating principle of the IAP service:

First, we need to add products to use the service. First, let me tell you how to add non-Subscriptions products:

  1. Sign in to AppGallery Connect and select My apps.
  2. Click an app from the app list to add a product.
Click Appname

3. Go to Operate > Products > Product Management and click Add Product.

⚠️ If you have not set the countries/regions to which your app will be distributed, a message will be displayed, prompting you to select distribution countries/regions for your app.

Click Operate then you will see Products Page

4. Configure product information and click Save.

Enter your app information

5. Click View and edit and configure product prices.

Add your price values

6. Click Save. In the dialog box that is displayed, click OK.

7. Check the product status on the product list. The status changes to Invalid. Click Activate in the row of the product. In the dialog box that is displayed, click OK. The product takes effect.

We’ve managed to add our Non-Subscriptions product so let me show you how to add Subscriptions:

Perform the same steps in the subscription-free product process except that the subscription requires different information from you.

So what is the path you need to follow when you want to add a Virtual Product? Let’s come together. Now I assume you’re on the product management page, assuming you’ve done the first three steps.

⚠️ If want a add Vitual Product you need Product.

Click Edit
  1. On the Edit Product page, complete product information and click Configure virtual product inventory.

2. Click Add next to Total inventory.

3. On the Add product Inventory page, set Redemption code validity period and click Download the template. Fill in the downloaded template with redemption codes to be imported, click Browse, and upload the configured redemption code template. In the dialog box that is displayed, click OK. After the upload is complete, click Save in the upper right corner. In the dialog box that is displayed, click OK.

  • Redemption code validity period: By default, the validity period lasts from 00:00:00 on the day of the upload to 23:59:59 on the 30th day after the successful upload. The validity period can be changed and must be longer than 48 hours.
  • Requirements for filling the template:

- The file must be a TXT file encoded using ANSI.

- Each line in the file contains only one record. Do not add other comments.

- A redemption code can contain only letters, digits, underscores (_), and separators.

- A redemption code must be unique in the template. The system will filter out duplicate codes automatically.

- The file size cannot exceed 100 MB.

4. Go back to the Configure virtual product inventory page, set Redemption code usage, and click Save in the upper right corner.

5. Check the added virtual product in the inventory table. The status of the product is To be sold. Click Release in the row of the product.

6. Confirm the values of Redemption codes and Validity period. The value of Validity period is editable. Click Release. In the dialog box that is displayed, click OK.

7. Check the virtual product on the inventory table again. The status of the virtual product changes to On sale, and the value of Remaining inventory is updated automatically as virtual products are sold.

⚠️ If you want Import your product or other operations click this link🔗

Now that we have added Our Products, How will we implement the IAP service to our project? Let’s start then:

First, let me tell you how the processes are:

  1. Consumable Product Purchase Process

2. Non-Consumable Product Purchase Process

3. Subscription Process

Now that you understand how this works, let’s see how we use IAP in our project.

First, check whether the user is logged in with Huawei id with isEnvReady. We do it this way:

Next, How to show our product information:

  1. Construct a ProductInfoReq object, send an obtainProductInfo request, and set callback listeners OnSuccessListener and OnFailureListener to receive the request result. Pass the product ID that has been defined and taken effect in AppGallery Connect to the ProductInfoReq object, and specify priceType for a product.
  2. If the request is successful, HUAWEI IAP will return a ProductInfoResult object. Using the getProductInfoList method of this object, your app can obtain the list of ProductInfo objects, each of which contains the information about a product, including the product price, name, and description. Based on the information, the list of products that are available for purchase can be displayed to users.

⚠️ You can only get one product info this method. If you want multiple product info you need to customize method.

Next, let’s look at how to start the purchase process through your application:

  1. Construct a PurchaseIntentReq object to send a createPurchaseIntent request. Pass the product ID that has been defined and taken effect in AppGallery Connect to the PurchaseIntentReq object. If the request is successful, your app will receive a PurchaseIntentResult object. Its getStatus method returns a Status object. Your app will display the checkout page of HUAWEI IAP using the startResolutionForResult method of the Status object.

2. After your app opens the checkout page of HUAWEI IAP and the user completes the payment process (that is, successfully purchases a product or cancels the purchase), HUAWEI IAP will return the payment result to your app through onActivityResult. You can use the parsePurchaseResultInfoFromIntent method to obtain the PurchaseResultInfo object that contains the result information.

If the purchase is successful, obtain the purchase data InAppPurchaseData and its signature data from the PurchaseResultInfo object. Use the public key allocated by AppGallery Connect to verify the signature. For more information about the verification method, please refer to Verifying the Signature in the Returned Result.

Finally, how do we approve or check success the purchase?

For a consumable, parse purchaseToken from InAppPurchaseData in JSON format to check the delivery status of the consumable.

After the consumable is successfully delivered and its purchaseToken is obtained, your app needs to use the consumeOwnedPurchase API to consume the product and instruct the Huawei IAP server to update the delivery status of the consumable. purchaseToken is passed in the API call request. After the consumption is complete, the Huawei IAP server resets the product status to available for purchase. Then the product can be purchased again.

For a non-consumable, the Huawei IAP server returns confirmed purchase data by default. You need to continuously provide corresponding product services for users after they successfully purchase the products. For details about how to deliver a non-consumable, please refer to Delivering a Non-Consumable.

Redirecting to the Subscription Management or Editing Page

Your app can be redirected to the subscription management page and subscription editing page of HUAWEI IAP through the startIapActivity API. The subscription management page displays the list of in-app subscriptions that a user has purchased. The subscription editing page displays the details about an in-app subscription that a user has purchased in your app and the information about other in-app subscriptions in the same subscription group.

  • Redirecting to the subscription management page:
  • Redirecting to the subscription editing page:

For now, I’ve told you the functions of the basic IAP service. For this and another functions, please visit the Huawei Developers page 🔗

I will develop an android application using the IAP service in the next article. You can contact me for any questions,advice or questions, or contact the Huawei Developers forum if you wish 🔗 See you soon 💻

If you want official sample code click here 🔗

--

--