How to Integrate HUAWEI In-App Purchases in Flutter — Part 1

Tolunay Öztürk
Huawei Developers
Published in
6 min readSep 25, 2020

Hello everyone, in this article we will create a Flutter application to demonstrate the capabilities of HUAWEI In-App Purchases and better understand the implementation of this service in Flutter.

There will be 3 articles (3 parts) for each type of product; Consumables, Non-Consumables and Subscriptions. In this part, we are going to focus on Consumables that are used once, depleted and can be purchased again.

What is HUAWEI In-App Purchases and Why Should We Use It?

Huawei’s In-App Purchases (IAP) service allows you to offer in-app purchases directly within your app and assist you with facilitating in-app payment flow. Users can purchase a variety of virtual products, including one-time virtual products as well as subscriptions.

If you want to focus your attention on app innovation rather than time-consuming work of integrating global payment methods and guaranteeing regulation compliance, HUAWEI In-App Purchases service is cut out just for you. And HUAWEI IAP also provides a product management system (PMS) for allowing you to manage the prices and languages of in-app products (including games) in multiple locations.

To see all supported locations, please refer to HUAWEI In-App Purchases Supported Locations

Key Features

  • Product management: Supports automatic pricing for products at 195 price levels using 78 languages and local currencies in 170+ countries and regions.
  • Order management: Opens multiple order management APIs, records all order information, and proactively tracks the order status to prevent product deliveries from going missing.
  • Subscription management: Enables management of subscription relationships, subscription periods, subscribed products, and promotional activities to help you gain a stable revenue.

Key Advantages

  • No missing deliveries: Proactively tracks the order status to ensure that all purchased products are delivered to users.
  • Multiple payment methods: Supports most mainstream payment methods, including bank cards, carrier billing, HUAWEI Points, and other local payment methods.
  • Sandbox testing: Offers a sandbox testing environment and practical integration guide for you to easily access HUAWEI In-App Purchases.

Now that we’ve learned about what this service is, let’s start integrating IAP into our app.

Integration Preparations

Before you get started, you must first register as a HUAWEI developer and verify your identity on the HUAWEI Developer website. For more details, please refer to Register a HUAWEI ID.

Software Requirements

  • Android Studio 3.0 or later
  • Java SDK 1.7 or later
  • HMS Core (APK) 3.0.0.300 or later
  • HMS Core SDK 4.0.0.300 or later

Integrating HMS Core SDK

To integrate HMS Core SDK in your application and learn creating a new project on AppGallery Connect, follow this great guide below.

Enabling In-App Purchases

After you created an app and a project on AppGallery Connect, you need to apply for Merchant Service and enable HUAWEI IAP.

To enable Merchant Service, go to My Projects > [Your Project] > Manage APIs and toggle In-App Purchases. You will be asked to apply for Merchant Service. Here, you’ll need to enter your bank information and go through a review process. This review process can take up to 2 days.

Once you are accepted in using Merchant Service, go to Earning > In-App Purchases from the navigation tree on the left and click Settings. If this is the first time you configure HUAWEI IAP, a dialog box will be displayed for you to sign the agreement.

After the configuration is successful, the page displays the public key used for subsequent payment signature verification and a parameter for configuring the subscription notification URL.

Don’t forget to set your package name in defaultConfig > applicationId if it’s not set and set minSdkVersion to 19 or higher. Package name must match with the package_name entry in agconnect-services.json file.

Implementation

On your Flutter project directory, find and open your pubspec.yaml file and add the plugin to dependencies. For more details please refer to Using packages

dependencies:
huawei_iap: ^5.0.0+300

Keep in mind that the SDK version used in this article may be not the latest one. If you want to use the latest SDK version, please refer to flutter-hms-iap on GitHub

And run following command to update package info.

[project_path]> flutter pub get

Configuring Product Information

To add a product, go to My Apps > [Your App] > Operate > Product Operation > Product Management from the navigation tree on the left then click Products tab and click Add Product. Configure product information and click Save.

After the configuration is complete, Activate the product in the list to make it valid and purchasable.

Products

We successfully added 2 products in AppGallery Connect. Now let’s see how we can use IAP Client in our application.

Reminder before we dive into the implementation

I will be only showing you the parts that is necessary to explain the feature. If you want to take a look at the full source code, you can find the GitHub link at the end of this article.

Obtain Product Information

We can fetch the product information of our products with a call to IapClient.obtainProductInfo(productInfoRequest).

Don’t forget: The skuIds is the same as that set by the developer when configuring product information in AppGallery Connect.

Now, we listed our products with their descriptions and prices. The next step is how we can allow our users to purchase them right in our app. Let’s see how we can do that.

Purchase Consumable Product

To allow users to purchase products in your app, when users tap into purchase button, first create a purchase intent request and specify the type of the product (Consumable in this case) and product ID in the request.

Don’t forget: To test purchase functionality and complete end-to-end testing without real payments, you need to create a tester account in AppGallery Connect and specify the developerPayload as “Test” in the request like in the code below.

To learn how to create a tester account for your app, please refer to Sandbox Testing

After user completes the payment process, we get a PurchaseResultInfo that has the details of the purchase. We will use this result later to consume products that users have bought.

Don’t forget to call IapClient.isEnvReady before calling purchaseConsumableProduct that returns a response which indicates user’s login and capable of buy status.

You can also check for different return codes and handle each of them. To see all the return codes, please refer to Result Codes

Now that we successfully purchased a product, we need to consume it in order for it to be effective. Otherwise users will not be able to purchase this product again.

Consume Product

We can consume an owned product with a call to IapClient.consumeOwnedPurchase(consumeOwnedPurchaseRequest).

That also returns a result which we can use to verify the signature. For more information, please refer to Verifying the Signature in the Returned Result

We are going to consume the product immediately after the user purchases it.

Done. We successfully implemented “purchase” and “consume” functionalities for our users.

Keep in mind that if something goes wrong during consume process right after the purchase, the user won’t be able to purchase that product again until the product is consumed. So you are advised to implement a control and consume mechanism to check the users owned products and consume them if there are any unconsumed products.

You can find the full source code of this demo application in the link below. I’ll update the source code as each part is published.

That’s it for this part. As always, thank you for reading this article and using HMS. Please let me know if you have any questions, I’ll see you in Part 2!

RESOURCES

--

--