[Huawei Developers] Freemind R:
This article is originally from HUAWEI Developer Forum

Forum link: https://forums.developer.huawei.com/forumPortal/en/home​

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522091922.14915982861751078962659384618578:50510604022044:2800:60AF1FB2A0FD2160AF04C722B040D53FCEF180075B9E762508CC0ABF288D8FE0.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522091922.14915982861751078962659384618578:50510604022044:2800:60AF1FB2A0FD2160AF04C722B040D53FCEF180075B9E762508CC0ABF288D8FE0.png[/IMG]​

Back in the 90’s, it was a revolutionary to have your own website and putting an advertisement in the website was the only source of revenue. But today as space matures, it’s essential for brands to have a clear strategy to monetize their mobile presence. Some companies charge their users before downloading their apps. Others depend on in-app advertising as their sole revenue stream. But, for a lot of brands, their mobile monetization strategy relies entirely (or some part) on In App Purchase. To summarise, when a customer spends money within a mobile app, that’s an in-app purchase.

To make easier for users to make payments and developers to focus solely on app innovation, Huawei provided us with an extraordinary kit i.e. HMS Core In App Purchase (IAP) Kit.

HMS In App Purchase helps developers in variety of situation to earn revenue such as paying for access to a dating app’s special features, subscribing to a streaming music app’s premium tier, buying more gold bars in games etc.

DEMO

Check out the demo created by me to make you understand, How HMS Core In App Purchase works.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522091939.69477800694534292092735706565929:50510604022044:2800:C61BFA28C7402F906A5D05D2393C7A256DEC288EE3A46CA96C0BC4EEDC764456.gif"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522091939.69477800694534292092735706565929:50510604022044:2800:C61BFA28C7402F906A5D05D2393C7A256DEC288EE3A46CA96C0BC4EEDC764456.gif[/IMG]

Today, in this article we are going to see how to integrate HMS In App Purchase in a simple yet complex Quiz Game app. To be honest everything can’t be covered in a single article. So, I have prepared a sample project and will provided the code on Github soon. I also tried making it simple as much as possible so that every beginner can understand it.

Settings Needed

1) First we need to create an app or project in the Huawei app gallery connect.

2) Provide the SHA Key and App Package name of the android project in App Information Section.

3) Provide storage location in convention section under project setting.

4) Enable In-App Purchase setting in Manage APIs section.

5) After completing all the above points we need to download the agconnect-services.json from App Information Section. Copy and paste the json file in the app folder of the android project.

6) Copy and paste the below maven url inside the repositories of buildscript and allprojects respectively (project build.gradle file)

maven { url 'http://developer.huawei.com/repo/' }

7) Copy and paste the below class path inside the dependency section of project build.gradle file.

classpath 'com.huawei.agconnect:agcp:1.3.1.300'

8) Copy and paste the below plugin in the app build.gradle file

apply plugin: 'com.huawei.agconnect'

9) Copy and paste below library in the app build.gradle file dependencies section.implementation 'com.huawei.hms:iap:4.0.2.300'

10) Put the below permission in AndroidManifest file.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

11) Now Sync the gradle.

Essential Requirement

Become a Merchant

We need to be a merchant in order to use HMS IAP Kit. Also after making your account as merchant, it will take 1 or 2 business days for verification.

Steps:

a) Login to Huawei Developer website.

b) Go to console.

c) Under Settings you will find Merchant Service as shown below:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522092334.85750867241181295275282229975423:50510604022044:2800:7C0E2D0B39399B107DCE6D502ACB850AE6DB29371563D4D2C16C13166EC722C9.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522092334.85750867241181295275282229975423:50510604022044:2800:7C0E2D0B39399B107DCE6D502ACB850AE6DB29371563D4D2C16C13166EC722C9.png[/IMG]

d) Provide our bank details information here as shown below:

e) Provide our tax related information here as shown below:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522092408.78122281529314019584419542465428:50510604022044:2800:DFFC395EB8670D54EB69CF156B4DF9EAB3BF5E2BFC958E3FB2027AD34F5F8792.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522092408.78122281529314019584419542465428:50510604022044:2800:DFFC395EB8670D54EB69CF156B4DF9EAB3BF5E2BFC958E3FB2027AD34F5F8792.png[/IMG]

f) Finally click submit to save the record for verification.

Need Public Key

We need a public key which is used for subsequent payment signature verification and a parameter for configuring the subscription notification URL.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522092453.11611083745569534885261123461649:50510604022044:2800:EF1DD2B28C712BBC7586FBF0E7D447098672AB6076EB72AE77A7A8C021B0B855.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522092453.11611083745569534885261123461649:50510604022044:2800:EF1DD2B28C712BBC7586FBF0E7D447098672AB6076EB72AE77A7A8C021B0B855.png[/IMG]

b) After the configuration is successful, we will be able to see a public key. This public key we will use later.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522092506.17767941317444255735796925565448:50510604022044:2800:44F7B7172FCDCFB668E2D8A7F4BB82170C029EF872A7A0BCACBF4929B66AFDD4.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522092506.17767941317444255735796925565448:50510604022044:2800:44F7B7172FCDCFB668E2D8A7F4BB82170C029EF872A7A0BCACBF4929B66AFDD4.png[/IMG]

Steps:

a) On the Develop tab page, go to Earning > In-App Purchases from the navigation tree on the left and click Settings. If this is the first time we configure the IAP service, a dialog box is displayed for you to sign the agreement.

Need Sandbox Account

We need sandbox account in order to test HMS IAP. During app development and testing, we can test product payments with a test account in the sandbox environment. During the testing period, when a purchase is initiated by the test account, the Huawei IAP server will identify the test account and directly process a successful payment, without real payments made.

Steps:

a) Go to AGC and select users and permissions.

b) Select Test account as shown below:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522092520.36068806043240833997089255873061:50510604022044:2800:EA2412B1AB2B044CC9D5585D7F63E36B782BDA224D141B28C87237045EC4C30A.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522092520.36068806043240833997089255873061:50510604022044:2800:EA2412B1AB2B044CC9D5585D7F63E36B782BDA224D141B28C87237045EC4C30A.png[/IMG]

c) Click Add button to add test account as shown below:

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522092536.46092490377711828918343460753605:50510604022044:2800:300DEA47C09C8168E3AEC8CB2B42DB9AB1D964404F87F9AF53B298903BCF81F3.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/647/156/2640091000001647156.20200522092536.46092490377711828918343460753605:50510604022044:2800:300DEA47C09C8168E3AEC8CB2B42DB9AB1D964404F87F9AF53B298903BCF81F3.png[/IMG]

d) After this a testing account is added. Make sure to add account which is used in Huawei devices to login.

This is not the end. For full content, you can visit https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201255229704010231&fid=0101187876626530001