The capability of purchasing auto-renewable subscriptions is supported on phones, tablets, and PCs/2-in-1 devices. It is also supported on TVs starting from 5.1.1(19) and on car starting from 26.0.0.
The following service process also applies to standalone apps. In a standalone app, the communication between the app server and the app happens on the app side. The interaction between the app server and the IAP server might not be handled.

Displaying product information:
The app sends a queryEnvironmentStatus request to IAP Kit to check whether the service area to which the currently signed-in HUAWEI ID belongs is supported by IAP Kit.
If the API returns the error code 1001860054, the app needs to hide the IAP functions.
The app sends a queryProducts request to IAP Kit to obtain the product information configured in AppGallery Connect.
The app displays the list of products that can be purchased based on the returned product information, including the product name and price.
Checking the service delivery status:
The app sends a queryPurchases request to IAP Kit to obtain the list of active subscriptions. IAP Kit returns the PurchaseData list. PurchaseData is a JWS string that contains the relevant subscription information.
The app displays the product subscription status. The purchase entry of products in auto-renewal status needs to be shielded. Additionally, the app handles the delivery of the services.
If the delivery of a product is not acknowledged, after the product is delivered, the app sends a finishPurchase request to IAP Kit, notifying the IAP server to update the delivery status of the product and complete the purchase process. After the app successfully executes finishPurchase, the IAP server marks the purchased products as delivered.
This step can also be performed on the app server. The app server can call the IAP server's API for Acknowledging the Delivery for a Subscription to acknowledge the delivery and complete the purchase process.
For details, please refer to Product Delivery for an Active Subscription.
Initiating and confirming a purchase:
After a user initiates a purchase, the app sends a purchase request to IAP Kit through createPurchase or through the IAP cashier component (only for TV apps). The request contains information such as the product ID and product type. IAP Kit creates an order and displays the checkout screen.
Confirm the purchase result. If the purchase is successful, you can receive the purchase result through the app or app server. The app server is recommended for receiving the purchase result.
Method 1: Receive the purchase result through the app.
Method 2 (Recommended): Receive the purchase result through the app server.
To improve security, you can enable the key event notification function so that the IAP server sends a key event notification when the user successfully purchases a product.
The app server parses purchaseToken and purchaseOrderId from NotificationPayload.NotificationMetaData and queries the latest subscription status information from the IAP server through the API for Querying the Subscription Status to further guarantee the information accuracy.
The IAP server returns jwsSubGroupStatus.
The app server decodes jwsSubGroupStatus and performs signature verification by referring to JWS Decoding and Signature Verification. Upon a successful verification, the SubGroupStatusPayload JSON string will be returned.
If the purchase fails, ensure that the product can be delivered correctly. For details, please refer to Subscription Delivery Guarantee.
Delivering the product:
The app server processes product delivery once the purchase is confirmed. Check SubGroupStatusPayload.lastSubscriptionStatus.lastPurchaseOrder for whether the products are delivered. If not, deliver them and record the corresponding order information (PurchaseOrderPayload) for subsequent delivery status check.
It is recommended that user services be associated with subscription status for standalone apps. The user will be entitled to access relevant services as long as the subscription is active.
The app queries the order delivery status from the app server.
The app server returns the delivery status and order information (PurchaseOrderPayload).
After a successful delivery, the app sends a finishPurchase request to IAP Kit, notifying the IAP server to update the delivery status and complete the purchase process. After the app successfully executes [finishPurchase](../reference/iap-iap.md#iapfinishpurchase, the IAP server marks the purchased product as delivered. Then the products can be renewed properly.
This step can also be performed on the app server. The app server can call the IAP server's API for Acknowledging the Delivery for a Subscription to acknowledge the delivery and complete the purchase process.
If this step is not performed for auto-renewable subscriptions, the subscription renewal will fail, and subscriptions in the same subscription group will fail to be switched.
Check whether IAP Kit is available for your app.
To use IAP Kit, the app needs to send a queryEnvironmentStatus request to this kit to check whether the service area to which the currently signed-in HUAWEI ID belongs is supported by IAP Kit.
Currently, IAP Kit is available only in the Chinese mainland.
import { iap } from '@kit.IAPKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import Logger from '../common/Logger';
// ...
const queryEnvCode = await this.queryEnv();
if (queryEnvCode !== 0) {
let queryEnvFailedText = 'This app does not support iap';
if (queryEnvCode === iap.IAPErrorCode.ACCOUNT_TERRITORY_NOT_SUPPORTED) {
// If the API returns the error code 1001860054, indicating that the service area of the user account is not supported by IAP Kit, the app needs to hide the IAP functions.
queryEnvFailedText = 'The country or region of the signed-in HUAWEI ID does not support IAP.';
}
this.showFailedPage(queryEnvFailedText);
return;
}
// ...
async queryEnv(): Promise<number> {
return new Promise<number>((resolve) => {
iap.queryEnvironmentStatus(this.context).then(() => {
Logger.info(TAG, 'Succeeded in querying environment status.');
resolve(0);
}).catch((err: BusinessError) => {
Logger.error(TAG, `Failed to query environment status. Code is ${err.code}, message is ${err.message}`);
resolve(err.code);
});
});
} Display the product list.
The app calls the queryProducts API to obtain the product information configured in AppGallery Connect. For a query request, the product IDs must be contained in QueryProductsParameter and productType must be set to iap.ProductType.AUTORENEWABLE.
Upon a successful API call, IAP Kit will return the list of product information (Product), including the product price, name, and description. Based on the returned Product information, the app shows the list of products available for purchase.
Each request to queryProducts can query only one product type, with a limit of up to 200 products. Otherwise, an error will be reported.
import { iap } from '@kit.IAPKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import Logger from '../common/Logger';
// ...
async queryProducts(): Promise<number> {
const productIds: string[] = ['Sub001', 'KH003', 'FH301'];
return new Promise<number>((resolve) => {
iap.queryProducts(this.getUIContext().getHostContext() as common.UIAbilityContext, productIds).then((result) => {
Logger.info(TAG, 'Succeeded in querying products.');
// Display product details.
this.productInfoArray = result;
resolve(0);
}).catch((err: BusinessError) => {
// An error occurs during product query.
Logger.error(TAG, `Failed to query products. Code is ${err.code}, message is ${err.message}`);
this.showFailedPage();
resolve(err.code);
});
});
} The app obtains the list of active subscriptions.
The app displays the product subscription status. The purchase entry of products in auto-renewal status needs to be shielded.
The app processes the delivery of services for active subscriptions.
For details, please refer to Product Delivery for an Active Subscription.
After a user initiates a purchase, the app sends a createPurchase request to IAP Kit, asking it to show the checkout screen. Alternatively, the app sends a purchase request through the embedded IAP cashier component (only for TVs). For a purchase request, the ID (that you configure for an auto-renewable subscription beforehand in AppGallery Connect and must be still valid) must be contained in PurchaseParameter and productType must be set to iap.ProductType.AUTORENEWABLE.
During development, you need to control the API call frequency. For details, please refer to 1001860004 API Calls Too Frequent.
import { iap } from '@kit.IAPKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import Logger from '../common/Logger';
// ...
subscribe(id: string, type: iap.ProductType) {
try {
const parameter: iap.PurchaseParameter = {
productId: id,
productType: type,
developerPayload: 'test developer payload string.',
};
iap.createPurchase(this.getUIContext().getHostContext() as common.UIAbilityContext, parameter,
(err: BusinessError, data: iap.CreatePurchaseResult) => {
if (err) {
// The request failed.
const msg: string = `Failed to create purchase. Code is ${err.code}, message is ${err.message}`;
Logger.error(TAG, msg);
// The purchase failed.
// ...
return;
}
// The request succeeded.
const msg: string = 'Succeeded in creating purchase.';
Logger.info(TAG, msg);
// The purchase succeeded. Process the purchase result.
this.dealPurchaseData(data.purchaseData);
});
} catch (err) {
const e: BusinessError = err as BusinessError;
const msg: string = `Failed to create purchase. Code is ${e.code}, message is ${e.message}`;
Logger.error(TAG, msg);
}
} [Result 1: The purchase is successful.]
For security purposes, it is recommended that your app server enable key event notifications to receive the purchase success result, decode and verify the signature, and complete the purchase.
Make sure that the delivery is successful before the complete-the-purchase step. In this step, you can request the IAP server's API for Acknowledging the Delivery for a Subscription to acknowledge the delivery and complete the purchase process.
The following describes how to receive the purchase result through the app and process the result.
Upon a successful purchase, the app receives a CreatePurchaseResult object, in which purchaseData contains the purchase result.
Decode purchaseData.jwsSubscriptionStatus and perform signature verification by referring to JWS Decoding and Signature Verification. Upon a successful verification, the JSON string SubGroupStatusPayload will be returned. It is recommended that the app send purchaseData to the app server so that this task can be executed on the app server.
After the signature verification is successful, check whether the value of SubGroupStatusPayload.lastSubscriptionStatus.status is 1 (active). If so, the user will be entitled to access relevant services.
You are advised to check the product delivery status of the order. If the product is not delivered, deliver it. After the delivery is successful, record relevant information such as SubGroupStatusPayload.lastSubscriptionStatus.lastPurchaseOrder for subsequent check.
Complete the purchase.
After a successful delivery, the app sends the finishPurchase request to acknowledge the delivery, notifying the IAP server to update the delivery status and complete the purchase process. productType, purchaseToken, and purchaseOrderId in PurchaseOrderPayload must be passed to FinishPurchaseParameter in the finishPurchase request. Obtain PurchaseOrderPayload from SubGroupStatusPayload.lastSubscriptionStatus.lastPurchaseOrder. After the request is successful, the IAP server marks the product as delivered.
import { iap } from '@kit.IAPKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import Logger from '../common/Logger';
import { JWSUtil } from '../common/JWSUtil';
import {
FinishStatus,
PurchaseData,
PurchaseOrderPayload,
SubGroupStatusPayload,
SubStatus,
} from '../common/IapDataModel';
// ...
dealPurchaseData(purchaseData: string) {
try {
// You are advised to send purchaseData to your app server for signature verification.
const jwsSubscriptionStatus = (JSON.parse(purchaseData) as PurchaseData).jwsSubscriptionStatus;
if (!jwsSubscriptionStatus) {
Logger.error(TAG, 'dealPurchaseData, jwsSubscriptionStatus invalid');
return;
}
// Decode jwsSubscriptionStatus and verify the signature.
const subscriptionStatus = JWSUtil.decodeJwsObj(jwsSubscriptionStatus);
if (!subscriptionStatus) {
Logger.error(TAG, 'dealPurchaseData, subscriptionStatus invalid');
return;
}
// Customize the SubGroupStatusPayload class. Check SubGroupStatusPayload for the information included in this class.
const subGroupStatusPayload = JSON.parse(subscriptionStatus) as SubGroupStatusPayload;
const lastSubscriptionStatus = subGroupStatusPayload.lastSubscriptionStatus;
if (!lastSubscriptionStatus) {
Logger.error(TAG, 'dealPurchaseData, lastSubscriptionStatus is invalid');
return;
}
if (lastSubscriptionStatus.status === SubStatus.ACTIVE) {
// The subscription has taken effect, and you need to deliver the product.
const productId = lastSubscriptionStatus.renewalInfo?.productId;
if (productId) {
this.setProductInfoStatus(subGroupStatusPayload.subGroupId, productId, lastSubscriptionStatus.status);
}
// Before performing the following steps, ensure that the product has been successfully delivered.
}
const purchaseOrderPayload = lastSubscriptionStatus.lastPurchaseOrder;
if (purchaseOrderPayload && purchaseOrderPayload.finishStatus !== FinishStatus.FINISHED) {
// Send a finishPurchase request to IAP Kit to acknowledge the delivery and complete the purchase.
this.finishPurchase(purchaseOrderPayload);
}
} catch (e) {
Logger.error(TAG, 'dealPurchaseData json error');
}
}
finishPurchase(purchaseOrder: PurchaseOrderPayload) {
if (!purchaseOrder.productType) {
Logger.error(TAG, 'finishPurchase but productType is empty');
return;
}
const finishPurchaseParam: iap.FinishPurchaseParameter = {
productType: Number(purchaseOrder.productType),
purchaseToken: purchaseOrder.purchaseToken,
purchaseOrderId: purchaseOrder.purchaseOrderId,
};
iap.finishPurchase(this.context, finishPurchaseParam).then(() => {
Logger.info(TAG, 'Succeeded in finishing purchase.');
}).catch((err: BusinessError) => {
Logger.error(TAG, `Failed to finish purchase. Code is ${err.code}, message is ${err.message}`);
});
} [Result 2: The purchase fails.]
Upon a purchase failure, check whether the product needs to be delivered again for the scenario where the code is iap.IAPErrorCode.PRODUCT_OWNED or iap.IAPErrorCode.SYSTEM_ERROR. For details, please refer to Subscription Delivery Guarantee.
if (err.code === iap.IAPErrorCode.PRODUCT_OWNED || err.code === iap.IAPErrorCode.SYSTEM_ERROR) {
// Follow the instructions in the relevant subscription delivery check section to check whether the product needs to be redelivered.
// ...
// ...
}