[Huawei Developers] Freemind R:
Introduction

HUAWEI In-App Purchases (IAP) aggregates multiple payment methods for global payments, and can be easily integrated into your app to help revenue increase. Users can purchase a variety of products or services, including common virtual products and subscriptions and directly within your app. For details, please refer to HUAWEI IAP 5.0.

First you need to understand three types of in-app products supported by HUAWEI In-App Purchases.

1. Consumable: Consumables are used once, are depleted, and can be purchased again.

Example: Extra lives and gems in a game

2. Non-consumable: Non-consumables are purchased once and do not expire.

Example: Extra game levels in a game or permanent membership of an app

3. 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.

Example: Non-permanent membership of an app, such as a monthly video membership

For details, refer to

https://www.b4x.com/b4a.html

Follow all the steps mentioned in Basic Setup to start HMS integration on B4A Platform.

Refer to

https://forums.developer.huawei.com/forumPortal/en/topicview?tid=0201286424114350051&fid=0101246461018590361

Enable In-App Purchases in App gallery connect.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113060001.55634454066512801835169755386853:50511124024410:2800:59B368218329B192CC223E7278AFEE23E2F0810F478A73C1E8FF3B1BB8478A47.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113060001.55634454066512801835169755386853:50511124024410:2800:59B368218329B192CC223E7278AFEE23E2F0810F478A73C1E8FF3B1BB8478A47.png[/IMG]

Tips & Tricks:

Before enable In-App Purchases create marchant account. It will take two days to approve then only we can enable In-App Purchases in AppGallery Connect.

Creating Wrapper Class

1. Downloading the AAR Packages and JSON File

Sign in to HUAWEI Developer and download the AAR packages required.

AAR packages related to HMS In-App Purchases kit, as follows.

HMSSDK Agconnect-Core:

https://developer.huawei.com/repo/com/huawei/agconnect/agconnect-core/1.4.0.300/agconnect-core-1.4.0.300.aar

HMSSDK Tasks:

https://developer.huawei.com/repo/com/huawei/hmf/tasks/1.4.1.300/tasks-1.4.1.300.aar

HMSSDK Available Update:

https://developer.huawei.com/repo/com/huawei/hms/availableupdate/5.0.0.301/availableupdate-5.0.0.301.aar

HMSSDK Base:

https://developer.huawei.com/repo/com/huawei/hms/base/5.0.0.301/base-5.0.0.301.aar

HMSSDK Device:

https://developer.huawei.com/repo/com/huawei/hms/device/5.0.0.301/device-5.0.0.301.aar

HMSSDK Log:

https://developer.huawei.com/repo/com/huawei/hms/log/5.0.0.301/log-5.0.0.301.aar

HMSSDK Network-Common:

https://developer.huawei.com/repo/com/huawei/hms/network-common/4.0.2.300/network-common-4.0.2.300.aar

HMSSDK Network-Grs:

https://developer.huawei.com/repo/com/huawei/hms/network-grs/4.0.2.300/network-grs-4.0.2.300.aar

HMSSDK Stats:

https://developer.huawei.com/repo/com/huawei/hms/stats/5.0.0.301/stats-5.0.0.301.aar

HMSSDK UI:

https://developer.huawei.com/repo/com/huawei/hms/ui/5.0.0.301/ui-5.0.0.301.aar

HMSSDK Update:

https://developer.huawei.com/repo/com/huawei/hms/update/2.0.6.302/update-2.0.6.302.aar

HMSSDK IAP:

https://developer.huawei.com/repo/com/huawei/hms/iap/5.0.2.300/iap-5.0.2.300.aar

2. Open AAR packages with rar tool and rename the class.jar and AndroidManifest.xml files. And save those jar file inside libs folder (It is recommended that the two files be renamed consistently with the AAR package names.).

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113060958.46099879658767430060706314650950:50511124024410:2800:1E626269612A5DEB185BA495F79A166C6B7AB78F641566DC154ED51DAC2053E2.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113060958.46099879658767430060706314650950:50511124024410:2800:1E626269612A5DEB185BA495F79A166C6B7AB78F641566DC154ED51DAC2053E2.png[/IMG]

3. Copy required permissions in the <manifest> section in B4A IDE.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113061753.50257359515943728648965082097866:50511124024410:2800:27D9B47A6CFC46592B0F60B92B6054755C56A2D699A94457DFD3B467D44C0EF1.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113061753.50257359515943728648965082097866:50511124024410:2800:27D9B47A6CFC46592B0F60B92B6054755C56A2D699A94457DFD3B467D44C0EF1.png[/IMG]

4. Copy all the configurations in <application> section.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113061856.52825095907200787346998897883344:50511124024410:2800:777730BDB7EB7DA3AD0A93022A33198329F9C12EEAA14B2EA885A1ECB16A9738.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113061856.52825095907200787346998897883344:50511124024410:2800:777730BDB7EB7DA3AD0A93022A33198329F9C12EEAA14B2EA885A1ECB16A9738.png[/IMG]

5. Change ${applicationId} to $PACKAGE$.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113062018.02232369967452919674812428506527:50511124024410:2800:212C545B6F79E359C34A8443C45E1A39479029A87424FAD42A516E6814537B6C.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113062018.02232369967452919674812428506527:50511124024410:2800:212C545B6F79E359C34A8443C45E1A39479029A87424FAD42A516E6814537B6C.png[/IMG]

6. Download the configuration file(agconnect-services.json) from App Gallery Connect and add the JSON File to the assets Folder of the AAR file as shown below.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113064251.49041938499082575000731297745302:50511124024410:2800:FBB070656C99CA65EB3C321EB4BCDF03DD9E12EBC136A49C5CCFB83F0E1857C7.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113064251.49041938499082575000731297745302:50511124024410:2800:FBB070656C99CA65EB3C321EB4BCDF03DD9E12EBC136A49C5CCFB83F0E1857C7.png[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113064337.75357224487157718714381774563747:50511124024410:2800:F03A2FC2572CB4BE6CA32D3D68B1E92BD7A57677267052E81DE757F915D1766F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113064337.75357224487157718714381774563747:50511124024410:2800:F03A2FC2572CB4BE6CA32D3D68B1E92BD7A57677267052E81DE757F915D1766F.png[/IMG]

B4A will automatically incorporate files in the assets folder of an AAR package to the assets folder of your main project.

Setting Package Signing Options

To create a signed application package perform the following steps:

1. Choose Project > Build Configurations and change the B4A project Package name to the project package name configured in App Gallery Connect.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113063020.11219299804474080945621766732889:50511124024410:2800:1D658FBA1D97CA83B52C0B4B7A2CCE320B39C4EDA52DD9415D7B6445D9374FA8.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113063020.11219299804474080945621766732889:50511124024410:2800:1D658FBA1D97CA83B52C0B4B7A2CCE320B39C4EDA52DD9415D7B6445D9374FA8.png[/IMG]

2. Choose Tools > Private Sign Key and select the SHA256 signature file specified in AppGallery Connect as the B4A project signature file.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113064138.06154978742875652806329011176987:50511124024410:2800:628384380C6496E90E88DF17AEF2CF84C2F1699A5EEC1B2A14E60185D4BDF1AB.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113064138.06154978742875652806329011176987:50511124024410:2800:628384380C6496E90E88DF17AEF2CF84C2F1699A5EEC1B2A14E60185D4BDF1AB.png[/IMG]

2. Declare MainApplication in the Manifest file.

The path of MainApplication must also be the path to src folder where MainApplication is located.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113064607.55758819033762904013239289326724:50511124024410:2800:C95CDA8EFC3339B2976D35072ADDAF4B7BE773289DE283DD6206BE1A42CB86D5.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113064607.55758819033762904013239289326724:50511124024410:2800:C95CDA8EFC3339B2976D35072ADDAF4B7BE773289DE283DD6206BE1A42CB86D5.png[/IMG]

Encapsulating Java Files Using SLC

1. Create Library as parent and then create bin, libs and src as subfolder in the project directory

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113064900.81662619502267351127856802692668:50511124024410:2800:99E0E160456D335DDF021A9A1CFA16F911F14D259F16097056334A7C4AC5C186.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113064900.81662619502267351127856802692668:50511124024410:2800:99E0E160456D335DDF021A9A1CFA16F911F14D259F16097056334A7C4AC5C186.png[/IMG]

2. Develop java project inside the following path:

Choose Library Folder > src > b4x > iap > demo

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113065028.50362201446587369263432242897298:50511124024410:2800:E554F71822F21172F1D91D46974CF0E1A39C586414545A37D8A7670510017B25.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/001/671/191/2640091000001671191.20201113065028.50362201446587369263432242897298:50511124024410:2800:E554F71822F21172F1D91D46974CF0E1A39C586414545A37D8A7670510017B25.png[/IMG]

3. Import the following two lines of code to each Java file.

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;
import com.huawei.hmf.tasks.Task;
import android.content.Intent;
import anywheresoftware.b4a.IOnActivityResult;

4. Add necessary annotations to the Java files.

@DependsOn(values={ "agconnect-core-1.4.0.300.aar",
"base-5.0.0.301.aar",
"network-common-4.0.2.300.aar",
"network-grs-4.0.2.300.aar",
"iap-5.0.2.300.aar",
"availableupdate-5.0.0.301.aar",
"device-5.0.0.301.aar",
"log-5.0.0.301.aar",
"stats-5.0.0.301.aar",
"tasks-1.4.1.300.aar",
"ui-5.0.0.301.aar",
"update-2.0.6.302.aar",
})

5. Modify the context.

B4A does not recognize the Context class. Therefore, when parsing a class that contains [MENTION=702169]version[/MENTION] (1.0f) annotation, it will report an error if a method of the class has referenced Context . In this case, you need to change Context to B4A object BA.

@ShortName("IAPWork")
public class IAPWork {
public static String eventName = "listener";
public static String TAG = "IAP_Kit";
public static BA ba;

public void init(Context context) {
AGConnectServicesConfig.fromContext(context).overlayWith(new LazyInputStream(context) {
@Override
public InputStream get(Context context) {
try {

return context.getAssets().open("agconnect-services.json");
} catch (IOException e) {
e.printStackTrace();
BA.Log(e.toString());
}
return null;
}
});
}

public void loadProduct(BA ba1) {

ba = ba1;

// obtain in-app product details configured in AppGallery Connect, and then show the products
IapClient iapClient = Iap.getIapClient(ba.context);
Task<ProductInfoResult> task = iapClient.obtainProductInfo(createProductInfoReq());
task.addOnSuccessListener(new OnSuccessListener<ProductInfoResult>() {
@Override
public void onSuccess(ProductInfoResult result) {
ba.raiseEventFromUI(this, eventName + "_iaparray", result.toString());
if (result != null && !result.getProductInfoList().isEmpty()) {

List<ProductInfo> productInfoList = result.getProductInfoList();
for (ProductInfo productInfo : productInfoList) {

ba.raiseEventFromUI(this, eventName + "_iaptext", productInfo.getProductName(), productInfo.getProductId(), productInfo.getPrice());
}
//ba.raiseEventFromUI(this,eventName+ "_iaparray",new Object[] {productInfoList} );
// showProduct(result.getProductInfoList());
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(Exception e) {
Log.e(TAG, e.getMessage());
if (e instanceof IapApiException) {
IapApiException iapApiException = (IapApiException) e;
int returnCode = iapApiException.getStatusCode();
if (returnCode == OrderStatusCode.ORDER_HWID_NOT_LOGIN) {

ba.raiseEventFromUI(this, eventName + "_iaparray", "Please sign in to the app with a HUAWEI ID.");

} else {
ba.raiseEventFromUI(this, eventName + "_iaparray", e.getMessage());
// Toast.makeText(ba.context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
} else {
ba.raiseEventFromUI(this, eventName + "_iaparray", "error");
// Toast.makeText(ba.context, "error", Toast.LENGTH_SHORT).show();
}
}
});
}

private ProductInfoReq createProductInfoReq() {
ProductInfoReq req = new ProductInfoReq();
// In-app product type contains:
// 0: consumable
// 1: non-consumable
// 2: auto-renewable subscription
req.setPriceType(IapClient.PriceType.IN_APP_CONSUMABLE);
ArrayList<String> productIds = new ArrayList<>();
// Pass in the item_productId list of products to be queried.
// The product ID is the same as that set by a developer when configuring product information in AppGallery Connect.
productIds.add("ProductID1");
productIds.add("ProductID2");
productIds.add("ProductID3");
productIds.add("ProductID4");
req.setProductIds(productIds);

ba.raiseEventFromUI(this, eventName + "_iaparray", productIds.get(0));

return req;
}

}

Tips & Tricks

The account for sandbox testing takes effect 30 minutes to 1 hour after being added. Check whether the current account can be used in the sandbox testing.