How to use HMS In-App Purchase

Yağmur Kılıç
Huawei Developers
Published in
6 min readAug 6, 2020

Hi everybody,

Today I will try to describe how you can use HMS In-App Purchase Kit.

You can see that at the end of the page with the Github link.

What is IAP? What is usage analysis? Why is it used?

Huawei In-App Purchases kit is the service that allows digital content to be purchased through apps. With this service, users can purchase various digital products and be included in various subscriptions directly using the application.

What kind of products can be purchased using Huawei In-App Purchase?

Using the in-app purchase kit, you can purchase consumables and non-consumables products, and subscribe to digital content.

Non-Consumable: Once purchased, they are always available. For example, removing ads or upgrading to the pro version.

Consumable Product: You need to buy these items whenever you need them. For example, additional hit points, jewelry and game coins, etc. in the game.

Subscription: Products that need to be purchased again in a certain period. Examples of this are monthly or weekly subscriptions. Most subscriptions are automatically renewed unless canceled

What is Digital Content/Product?

If a gamer needs a star to continue the game, the gamer can purchase through an hms in-app purchase kit so, the star is a digital product for the gamer.

What are the supported payment methods?

Huawei In-App Purchases service supports multiple payment methods.

These payment methods; payment by debit card, carrier billing Huawei points.

Product Configuration

You need to add the product or products you will offer to the users with the help of the Huawei Developer console. You can find how to add products and more here. You can choose from three different product types that are suitable for the product and product features you want to offer to users.

After all these steps, we can finally start coding :)

In-App Purchase processes

First of all, you need to log in using your Huawei ID before you pay. Otherwise, the 60050 error code is returned and means that you are not logged in with a valid Huawei ID.

Checking Huawei In-App-Purchase Support

Certain checks should be made after logging in. So what are these controls?

Is Huawei IAP active on the currently logged-in Huawei account?

Is Huawei IAP service available in the country or region of the user?

You can do all these checks using the using isEnvReady.

If not signed in with a valid Huawei ID, the system prompts the user to log in and redirects to the login page.

Product and Product Information Inquiry

First, product information that can be purchased should be displayed to the user. At this stage, the Product Info API comes into play. This API gets the product and product information that we added to the Huawei app gallery before. The API does this according to the productInfoReq parameter it receives as input.

The most important point here is the values ​​ ​​we set to the product info request parameter. We need to set two parameters in total named ProductID and PriceType.

At the beginning of my article, I mentioned to you that Huawei offers 3 different purchase options to users. Now, you need to set the price type according to the type of product you want to query.

If you want to query consumable products, you need to use the IapClient.PriceType.INAPP CONSUMABLE as an input parameter. Also, you need to use IapClient.PriceType.INAPP_NONCONSUMBLE and IapClient.PriceType.IN_APP_SUBSCRIPTION for non-consumable product type and subscription.

The product id is the same id you provided when adding products in AppGallery Connect.

Starting In-App Product Purchase

How does the purchase process begin? What are the stages? how to end?

The purchase begins when a user clicks the button named BUY on the page. First, create purchase intent API is called by creating a task of the PurchaseIntentResult type.

There are two possibilities for the API request result named Success and failure. To handle these two situations, OnSuccessListener and OnFailureListener should be used. Result named Success and failure. To handle these two situations, OnSuccessListener and OnFailureListener should be used.

If the API call is successful, a result of the PurchaseIntentResult object type is returned and the Huawei in-app purchase payment page is displayed using the startResolutionForResult method.

If the payment process is completed by entering the required information on the payment page or the payment process is canceled, an object of the PurchaseResultInfo type containing the payment result returns to onActivityResult.

IapClient’s parsePurchaseResultInfoFromIntent method is used to obtain the PurchaseResultInfo object containing the payment result.

A separate return code is returned for all results. In case of cancellation, before the payment process is completed, the return Code becomes 60000. This means that the payment was canceled by the user. If the payment is completed successfully, that is, if the user manages to purchase a product successfully, the return Code becomes 0.

In case of success, it is necessary to check whether the answer has been changed. The signature string must be verified for check operation.

In the error scenario, an exception object is returned. If the object is an Iap ApiException object, the getStatusCode 0 method is used to get the error code.

Consuming the Purchased Product

After the product has been successfully purchased and delivered to the user, the purchased product must be consumed. ConsumeOwnedPurchase API is used for this operation. If the API call is successful, the Huawei IAP server resets the product status to be bought. Thanks to the process the user can then buy the product again.

In addition, you can call the deliverConsumablePurchases method in the onCreate method to minimize possible ORDER_PRODUCT_OWNED errors.

In this method, using the obtainOwnedPurchases API, it is checked for any products that have not been delivered, and then the product is delivered.

Otherwise, if a user wants to buy this product again, error code 60051 will be returned. This error code means that the product cannot be purchased again because the user already owns the product.

Although the product is NonConsumable, the deliverNonConsumablePurchase method should be called, but non-consumable products cannot be purchased again.

If a product is tried to be consumed again, the error code 60053 is returned. In this case, you should use the acquOwnedPurchaseRecord API to check if the product has a consumption record.

Providing Subscription Service

The current subscription status should be checked before starting subscription transactions. You can do this using the obtainOwnedPurchases API. If InApppurchaseData.purchase state is 0, the subscription is already purchased.

If a subscription has been purchased and the subscription is still available, subscription-related services must be provided to the user and cannot be purchased again. InApppurchaseData.purchase time and InApppurchaseData.expirationDate values ​​give us subscription start and end dates.

The following lines should be added to your project so that users can purchase subscriptions that they have not previously purchased.

In this article, I tried to explain to you the necessary explanations about the use of In-App Purchases through the code. I hope it was useful to you.

See you in another blog post.

Github link for project source code.

--

--