How to use HMS IAP Plugin with Ionic?

Hüseyin Deniz
Huawei Developers
Published in
4 min readAug 26, 2020

While working with Ionic, it is recommended to use the Capacitor for new Ionic apps but sometimes you need to use some Cordova or Ionic Native plugins. In this guide you will learn how to use HMS IAP Cordova Plugin with an Ionic Capacitor application.

In order to use IAP Plugin with any application, you have to create an application on Huawei Developer Console and add some products.

You can read this guide for further details to create an application and add purchasable products to work with.

Huawei In-App Purchases

Huawei In-App Purchases (IAP) provides multiple payment methods for global payments and can be easily integrated into your application to help increase your revenue. Users can purchase a variety of products and services, including popular virtual products and subscriptions, from within your direct application.

Create an Application with Ionic

Install the Ionic CLI with npm if you haven’t already.

$ ionic start HMS-IAP-Demo blank --capacitor

$ cd HMS-IAP-Demo

$ npm run build

Add your agconnect-services.json and keystore files to project folder as explained in the guide I mentioned above.

Add the AppID you got from the Huawei Developer console to the AndroidManifest.xml file. (inside application tag)

Add HMS IAP Cordova Plugin & Ionic Native

$ npm i @hmscore/cordova-plugin-hms-iap --save

$ npm i @ionic-native/core --save-dev

Copy the “ionic/dist/hms-in-app-purchases” folder from library in node_modules to the “node_modules/@ionic-native” folder in your ionic project.

$ ionic capacitor add add android

$ ionic capacitor sync

$ ionic capacitor build android

Run the project on a connected device

Run the Ionic application with the command below to try the application with a Huawei device. We’re using the --livereload and --external options to see changes on tthe device when changes in the app are detected.

$ ionic capacitor run android --livereload --external

Exploring IAP Library

You have to login with your Huawei ID on the device you’re going to install application. Otherwise, you will get an error. (Error Code: 60050). You can check the error details and other error codes here.

Adding Library to Providers

You have to add library to the providers section in app.module.ts before using it.

Checking Environment

Before fetching the products, you should check the environment is ready.

Fetching Products

If you have configured products in AppGallery Connect, you need to use the obtainProductInfo API to obtain details of these products.

Perform the following steps:

  1. Construct a ProductInfoReq object and pass the product ID that has been defined and taken effect in AppGallery Connect to the ProductInfoReq object, and specify priceType for a product. “priceType” is type of the product. Check the code below for price types and their integer equivalents.
  2. If the operation is successful, obtainProductInfo will return a ProductInfoResult object that contains productInfoList. You can use it to display the product information within your application.

Initiating a Purchase

Your app can display the purchase screen through the createPurchaseIntent API.

Call createPurchaseIntent with required parameter. Parameter is an object includes productId, priceType and developerPayload. If parameters are correct it automatically brings the checkout page. If a problem occurs the createPurchaseIntent API will return a Status object describing the error.

Auto-renewable Subscriptions

Users can purchase access to value-added functions or content in a specified period of time. The subscriptions are automatically renewed on a recurring basis until users decide to cancel.

You can use the createPurchaseIntent API like the example above to display the subscription purchase page. HMS IAP Plugin provides startIapActivity API to display subscription management and subscription editing pages.

Simply call the startIapActivity API to display the subscription management page. If you want to display display the subscription editing page you just need to pass the ID of the product as a parameter.

References

https://developer.huawei.com/consumer/en/hms/huawei-iap/

https://developer.huawei.com/consumer/en/console

--

--