IapClient

  • Supported devices: phones, tablets, and HUAWEI Vision
  • Supported operating systems: EMUI 3.0 or later and Android 4.4 or later
Expand

Interface Info

public interface IapClient

Contains all APIs of HUAWEI IAP.

Nested Class Summary

Expand

Qualifier and Type

Interface Name and Description

interface

IapClient.PriceType

Defines the product type.

Public Method Summary

Expand

Qualifier and Type

Method Name and Description

Task<ConsumeOwnedPurchaseResult>

consumeOwnedPurchase(ConsumeOwnedPurchaseReq consumeOwnedPurchaseReq)

Consumes a consumable after it is successfully delivered.

Task<PurchaseIntentResult>

createPurchaseIntent(PurchaseIntentReq purchaseIntentReq)

Creates orders for products managed by the product management system (PMS), including consumables, non-consumables, and subscriptions.

void

enablePendingPurchase()

Enables pending purchase.

Task<IsEnvReadyResult>

isEnvReady()

Checks whether the currently signed-in HUAWEI ID is located in a country or region where HUAWEI IAP is available.

Task<IsSandboxActivatedResult>

isSandboxActivated(IsSandboxActivatedReq isSandboxActivatedReq)

Checks whether the signed-in HUAWEI ID and the app version meet the requirements of sandbox testing.

Task<OwnedPurchasesResult>

obtainOwnedPurchaseRecord(OwnedPurchasesReq ownedPurchasesReq)

Obtains the purchase data of all consumed products or the receipts of all subscriptions.

Task<OwnedPurchasesResult>

obtainOwnedPurchases(OwnedPurchasesReq ownedPurchasesReq)

Queries purchase data of the products that a user has bought.

For consumables, this method returns purchase data of those already bought but not consumed.

For non-consumables, this method returns purchase data of all the products that have been bought.

For subscriptions, this method returns only the currently active subscription relationships.

Task<ProductInfoResult>

obtainProductInfo(ProductInfoReq productInfoReq)

Obtains product details configured in AppGallery Connect.

PurchaseResultInfo

parsePurchaseResultInfoFromIntent(Intent intent)

Parses the payment result.

Task<StartIapActivityResult>

startIapActivity(StartIapActivityReq startIapActivityReq)

Displays the subscription editing screen or subscription management screen of HUAWEI IAP.

Public Methods

consumeOwnedPurchase

Expand

Method

Task<ConsumeOwnedPurchaseResult> consumeOwnedPurchase(ConsumeOwnedPurchaseReq consumeOwnedPurchaseReq)

Consumes a consumable after it is successfully delivered.

NOTICE
  1. Consume a product only after a successful delivery. If you consume a product before delivery, the following exception may occur: When the consumption API is called, the consumption is successfully executed on the server but the result is not returned to the app due to network exceptions. As a result, the product is not delivered. This exception is difficult to handle, so you should consume the product after a successful delivery.
  2. Send the purchase token of the delivered product to your server. Even if the consumption fails, you can obtain data from your server for comparison. You are advised to check your app server before delivery to ensure that each involved purchase token has not been used, that is, no involved product is delivered.

Parameters

Expand

Name

Description

consumeOwnedPurchaseReq

ConsumeOwnedPurchaseReq object that contains request information.

Returns

Expand

Type

Description

Task<ConsumeOwnedPurchaseResult>

Task containing the API request result.

  • You can use OnSuccessListener to obtain a ConsumeOwnedPurchaseResult object returned in the success scenario.
  • You can use OnFailureListener to obtain an IapApiException object returned in the failure scenario.

Sample code:

Java
Kotlin
// Construct a ConsumeOwnedPurchaseReq object.
ConsumeOwnedPurchaseReq req = new ConsumeOwnedPurchaseReq();
String purchaseToken = "";
try {
    // Obtain the value of purchaseToken from inAppPurchaseData. You can obtain inAppPurchaseData from a payment request or by calling the obtainOwnedPurchases API.
    InAppPurchaseData inAppPurchaseDataBean = new InAppPurchaseData(inAppPurchaseData);
    purchaseToken = inAppPurchaseDataBean.getPurchaseToken();
} catch (JSONException e) {
}
req.setPurchaseToken(purchaseToken);

// Obtain the Activity object that calls the API.
final Activity activity = getActivity();
// Call the consumeOwnedPurchase API.
Task<ConsumeOwnedPurchaseResult> task = Iap.getIapClient(activity).consumeOwnedPurchase(req);
task.addOnSuccessListener(new OnSuccessListener<ConsumeOwnedPurchaseResult>() {
    @Override
    public void onSuccess(ConsumeOwnedPurchaseResult result) {
        // Obtain the result returned upon a successful API call.
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        if (e instanceof IapApiException) {
            IapApiException apiException = (IapApiException) e;
            Status status = apiException.getStatus();
            int returnCode = apiException.getStatusCode();
        } else {
            // Other external errors.
        }
    }
});
// Construct a ConsumeOwnedPurchaseReq object.
val req = ConsumeOwnedPurchaseReq()
var purchaseToken = ""
try {
    // Obtain the value of purchaseToken from inAppPurchaseData. You can obtain inAppPurchaseData from a payment request or by calling the obtainOwnedPurchases API.
    val inAppPurchaseDataBean = InAppPurchaseData(inAppPurchaseData)
    purchaseToken = inAppPurchaseDataBean.purchaseToken
} catch (e: JSONException) {
}
req.setPurchaseToken(purchaseToken)

// Obtain the Activity object that calls the API.
val activity: Activity = getActivity()
// Call the consumeOwnedPurchase API.
val task = Iap.getIapClient(activity).consumeOwnedPurchase(req)
task.addOnSuccessListener {
    // Obtain the result returned upon a successful API call.
}.addOnFailureListener { e ->
    if (e is IapApiException) {
        val apiException = e as IapApiException
        val status: Status = apiException.status
        val returnCode = apiException.statusCode
    } else {
        // Other external errors.
    }
}

createPurchaseIntent

Expand

Method

Task<PurchaseIntentResult> createPurchaseIntent(PurchaseIntentReq purchaseIntentReq)

Creates orders for products managed by the product management system (PMS), including consumables, non-consumables, and subscriptions.

After creating a product in AppGallery Connect, you can call this API to open the HUAWEI IAP checkout screen and display the product, price, and payment method. Huawei can adjust product prices by foreign exchange rate changes. To ensure price consistency, your app needs to call the obtainProductInfo API to obtain product details from Huawei instead of your own server.

This API only returns the Status object that contains the IAP checkout screen. Showing this screen is implemented by your app. For details about how to show it, please refer to the description of Status in PurchaseIntentResult. After the user completes payment, your app can obtain the payment result using parsePurchaseResultInfoFromIntent from the onActivityResult callback method.

NOTICE
  • After a successful verification of the signature in the payment result, it is a must to check the consistency of the information including productId, price, and currency in InAppPurchaseData between HUAWEI IAP and your app, to make sure that the user who has successfully paid for a product can successfully get the product.
  • The Status object does not support serialization. Please do not serialize the Status object returned by IAP APIs.

Parameters

Expand

Name

Description

purchaseIntentReq

PurchaseIntentReq object that contains request information.

Returns

Expand

Type

Description

Task<PurchaseIntentResult>

Task containing the API request result.

  • You can use OnSuccessListener to obtain a PurchaseIntentResult object returned in the success scenario.
  • You can use OnFailureListener to obtain an IapApiException object returned in the failure scenario.

Sample code:

Java
Kotlin
// Construct a PurchaseIntentReq object.
PurchaseIntentReq req = new PurchaseIntentReq();
// Only those products already configured in AppGallery Connect can be purchased through the createPurchaseIntent API.
req.setProductId("CProduct1");
// priceType: 0: consumable; 1: non-consumable; 2: subscription
req.setPriceType(0);
req.setDeveloperPayload("test");
// Obtain the Activity object that calls the API.
final Activity activity = getActivity();
// Call the createPurchaseIntent API to create a PMS product order.
Task<PurchaseIntentResult> task = Iap.getIapClient(activity).createPurchaseIntent(req);
task.addOnSuccessListener(new OnSuccessListener<PurchaseIntentResult>() {
    @Override
    public void onSuccess(PurchaseIntentResult result) {
        // Obtain the order creation result.
        // The Status object does not support serialization.
        Status status = result.getStatus();
        if (status.hasResolution()) {
            try {
                // 6666 is a constant defined by yourself.
                // Open the checkout page returned.
                status.startResolutionForResult(activity, 6666);
            } catch (IntentSender.SendIntentException exp) {
            }
        }
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        if (e instanceof IapApiException) {
            IapApiException apiException = (IapApiException) e;
            // The Status object does not support serialization.
            Status status = apiException.getStatus();
            int returnCode = apiException.getStatusCode();
        } else {
            // Other external errors.
        }
    }
});

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 6666) {
        if (data == null) {
            Log.e("onActivityResult", "data is null");
            return;
        }
        // Call the parsePurchaseResultInfoFromIntent method to parse the payment result.
        PurchaseResultInfo purchaseResultInfo = Iap.getIapClient(this).parsePurchaseResultInfoFromIntent(data);
        switch(purchaseResultInfo.getReturnCode()) {
            case OrderStatusCode.ORDER_STATE_CANCEL:
                // The user cancels the purchase.
                break;
            case OrderStatusCode.ORDER_STATE_FAILED:
            case OrderStatusCode.ORDER_PRODUCT_OWNED:
                // Check whether the delivery is successful.
                break;
            case OrderStatusCode.ORDER_STATE_SUCCESS:
                // The payment is successful.
                String inAppPurchaseData = purchaseResultInfo.getInAppPurchaseData();
                String inAppPurchaseDataSignature = purchaseResultInfo.getInAppDataSignature();
                // Verify the signature using your app's IAP public key.
                // If the verification is successful, check the consistency of the information including productId, price, and currency in InAppPurchaseData between HUAWEI IAP and your app. If relevant information is consistent, deliver the product.
                // If the user purchased a consumable, call the consumeOwnedPurchase API to consume it after delivery.
                break;
            default:
                break;
        }
    }
}
// Construct a PurchaseIntentReq object.
val req = PurchaseIntentReq()
// Only those products already configured in AppGallery Connect can be purchased through the createPurchaseIntent API.
req.setProductId("CProduct1")
// priceType: 0: consumable; 1: non-consumable; 2: subscription
req.setPriceType(0)
req.setDeveloperPayload("test")
// Obtain the Activity object that calls the API.
val activity: Activity = getActivity()
// Call the createPurchaseIntent API to create a PMS product order.
val task = Iap.getIapClient(activity).createPurchaseIntent(req)
task.addOnSuccessListener { result ->
    // Obtain the order creation result.
    // The Status object does not support serialization.
    val status: Status = result.status
    if (status.hasResolution()) {
        try {
                // 6666 is a constant defined by yourself.
            // Open the checkout page returned.
            status.startResolutionForResult(activity, 6666)
        } catch (exp: SendIntentException) {
        }
    }
}.addOnFailureListener { e ->
    if (e is IapApiException) {
        val apiException = e as IapApiException
        // The Status object does not support serialization.
        val status: Status = apiException.status
        val returnCode = apiException.statusCode
    } else {
        // Other external errors.
    }
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == 6666) {
        if (data == null) {
            Log.e("onActivityResult", "data is null")
            return
        }
        // Call the parsePurchaseResultInfoFromIntent method to parse the payment result.
        val purchaseResultInfo = Iap.getIapClient(this).parsePurchaseResultInfoFromIntent(data)
        when (purchaseResultInfo.returnCode) {
            OrderStatusCode.ORDER_STATE_CANCEL -> {
            }
            OrderStatusCode.ORDER_STATE_FAILED, OrderStatusCode.ORDER_PRODUCT_OWNED -> {
            }
            OrderStatusCode.ORDER_STATE_SUCCESS -> {
                // The payment is successful.
                val inAppPurchaseData = purchaseResultInfo.inAppPurchaseData
                val inAppPurchaseDataSignature = purchaseResultInfo.inAppDataSignature
            }
            else -> {
            }
        }
    }
}

enablePendingPurchase

Expand

Method

void enablePendingPurchase()

Enables pending purchase. To implement pending purchase in your app, call this method before a purchase request is made. The ideal option is to call this method during app initialization.

NOTE
  • The pending purchase function is supported only on mobile phones and tablets.
  • The function is available when createPurchaseIntent is called to purchase a one-time product which can be a consumable or non-consumable.

Sample code:

Java
Kotlin
// Obtain the Activity object that calls the API.
Activity activity = getActivity();
Iap.getIapClient(activity).enablePendingPurchase();
// Obtain the Activity object that calls the API.
val activity: Activity = getActivity()
Iap.getIapClient(activity).enablePendingPurchase()

isEnvReady

Expand

Method

Task<IsEnvReadyResult> isEnvReady()

Checks whether the currently signed-in HUAWEI ID is located in a country or region where HUAWEI IAP is available.

Returns

Expand

Type

Description

Task<IsEnvReadyResult>

Task containing the API request result.

  • You can use OnSuccessListener to obtain an IsEnvReadyResult object returned in the success scenario.
  • You can use OnFailureListener to obtain an IapApiException object returned in the failure scenario.

Sample code:

Java
Kotlin
// Obtain the Activity object that calls the API.
final Activity activity = getActivity();
Task<IsEnvReadyResult> task = Iap.getIapClient(activity).isEnvReady();
task.addOnSuccessListener(new OnSuccessListener<IsEnvReadyResult>() {
    @Override
    public void onSuccess(IsEnvReadyResult result) {
        // Obtain the result of the API request.
        String carrierId = result.getCarrierId();
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        if (e instanceof IapApiException) {
            IapApiException apiException = (IapApiException) e;
            // The Status object does not support serialization.
            Status status = apiException.getStatus();
            if (status.getStatusCode() == OrderStatusCode.ORDER_HWID_NOT_LOGIN) {
                // No HUAWEI ID is signed in.
                if (status.hasResolution()) {
                    try {
                        // 6666 is a constant defined by yourself.
                        // Open the sign-in page returned.
                        status.startResolutionForResult(activity, 6666);
                    } catch (IntentSender.SendIntentException exp) {
                    }
                }
            } else if (status.getStatusCode() == OrderStatusCode.ORDER_ACCOUNT_AREA_NOT_SUPPORTED) {
                // The service area of the signed-in HUAWEI ID is not a country or region where IAP is available.
            }
        } else {
            // Other external errors.
        }
    }
});
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 6666) {
        if (data != null) {
            // Call the parseRespCodeFromIntent method to obtain the result of the API request.
            int returnCode = IapClientHelper.parseRespCodeFromIntent(data);
        }
    }
}
// Obtain the Activity object that calls the API.
val activity: Activity = getActivity()
val task = Iap.getIapClient(activity).isEnvReady
task.addOnSuccessListener { result ->
    // Obtain the result of the API request.
    val carrierId = result.carrierId
}.addOnFailureListener { e ->
    if (e is IapApiException) {
        val apiException = e as IapApiException
        // The Status object does not support serialization.
        val status: Status = apiException.status
        if (status.getStatusCode() === OrderStatusCode.ORDER_HWID_NOT_LOGIN) {
            // No HUAWEI ID is signed in.
            if (status.hasResolution()) {
                try {
                    // 6666 is a constant defined by yourself.
                    // Open the sign-in page returned.
                    status.startResolutionForResult(activity, 6666)
                } catch (exp: SendIntentException) {
                }
            }
        } else if (status.getStatusCode() === OrderStatusCode.ORDER_ACCOUNT_AREA_NOT_SUPPORTED) {
            // The service area of the signed-in HUAWEI ID is not a country or region where IAP is available.
        }
    } else {
            // Other external errors.
    }
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == 6666) {
        if (data != null) {
            // Call the parseRespCodeFromIntent method to obtain the result of the API request.
            val returnCode = IapClientHelper.parseRespCodeFromIntent(data)
        }
    }
}

isSandboxActivated

Expand

Method

Task<IsSandboxActivatedResult> isSandboxActivated(IsSandboxActivatedReq isSandboxActivatedReq)

Checks whether the signed-in HUAWEI ID and the app version meet the requirements of sandbox testing.

Parameters

Expand

Name

Description

isSandboxActivatedReq

IsSandboxActivatedReq object that contains request information.

Return

Expand

Type

Description

Task<IsSandboxActivatedResult>

Task containing the API request result.

  • You can use OnSuccessListener to obtain an IsSandboxActivatedResult object returned in the success scenario.
  • You can use OnFailureListener to obtain an IapApiException object returned in the failure scenario.

Sample code:

Java
Kotlin
// Obtain the Activity object that calls the API.
final Activity activity = getActivity();
IapClient mClient = Iap.getIapClient(activity);
Task<IsSandboxActivatedResult> task = mClient.isSandboxActivated(new IsSandboxActivatedReq());
task.addOnSuccessListener(new OnSuccessListener<IsSandboxActivatedResult>() {
    @Override
    public void onSuccess(IsSandboxActivatedResult result) {
        Log.i("IAP", "isSandboxActivated success");
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        Log.e("IAP", "isSandboxActivated fail");
        if (e instanceof IapApiException) {
            IapApiException apiException = (IapApiException) e;
            int returnCode = apiException.getStatusCode();
        } else {
            // Other external errors.
        }
    }
});
// Obtain the Activity object that calls the API.
val activity: Activity = getActivity()
val mClient = Iap.getIapClient(activity)
val task = mClient.isSandboxActivated(IsSandboxActivatedReq())
task.addOnSuccessListener { result ->
    Log.i("IAP", "isSandboxActivated success") 
}.addOnFailureListener { e ->
    Log.e("IAP", "isSandboxActivated fail")
    if (e is IapApiException) {
        val apiException = e as IapApiException
        val returnCode = apiException.statusCode
    } else {
            // Other external errors.
    }
}

obtainOwnedPurchaseRecord

Expand

Method

Task<OwnedPurchasesResult> obtainOwnedPurchaseRecord(OwnedPurchasesReq ownedPurchasesReq)

Obtains the purchase data of all consumed products or the receipts of all subscriptions.

  • For consumables, this method returns the purchase data of products that have been delivered or consumed in the product list.
  • For subscriptions, this method returns all subscription receipts of the current user in this app.
NOTE

To query the purchase data of non-consumables, you should call obtainOwnedPurchases.

Parameters

Expand

Name

Description

ownedPurchasesReq

OwnedPurchasesReq object that contains request information.

Returns

Expand

Type

Description

Task<OwnedPurchasesResult>

Task containing the API request result.

  • You can use OnSuccessListener to obtain an OwnedPurchasesResult object returned in the success scenario.
  • You can use OnFailureListener to obtain an IapApiException object returned in the failure scenario.

Sample code:

Java
Kotlin
// Construct an OwnedPurchasesReq object.
OwnedPurchasesReq req = new OwnedPurchasesReq();
// 0: consumable; 2: subscription
req.setPriceType(0);
// Obtain the Activity object that calls the API.
final Activity activity = getActivity();
// Call the obtainOwnedPurchaseRecord API.
Task<OwnedPurchasesResult> task = Iap.getIapClient(activity).obtainOwnedPurchaseRecord(req);
task.addOnSuccessListener(new OnSuccessListener<OwnedPurchasesResult>() {
    @Override
    public void onSuccess(OwnedPurchasesResult result) {
        // Obtain the result returned upon a successful API call.
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        if (e instanceof IapApiException) {
            IapApiException apiException = (IapApiException) e;
            int returnCode = apiException.getStatusCode();
        } else {
            // Other external errors.
        }
    }
});
// Construct an OwnedPurchasesReq object.
val req = OwnedPurchasesReq()
// 0: consumable; 2: subscription
req.setPriceType(0)
// Obtain the Activity object that calls the API.
val activity: Activity = getActivity()
// Call the obtainOwnedPurchaseRecord API.
val task = Iap.getIapClient(activity).obtainOwnedPurchaseRecord(req)
task.addOnSuccessListener {
    // Obtain the result returned upon a successful API call.
}.addOnFailureListener { e ->
    if (e is IapApiException) {
        val apiException = e as IapApiException
        val returnCode = apiException.statusCode
    } else {
            // Other external errors.
    }
}

obtainOwnedPurchases

Expand

Method

Task<OwnedPurchasesResult> obtainOwnedPurchases(OwnedPurchasesReq ownedPurchasesReq)

Queries purchase data of the products that a user has bought, including consumables, non-consumables, and subscriptions. Only one type of products can be queried per API call.

  • For consumables, this method returns purchase data of those already bought but not consumed. If some purchase data is returned, relevant products may not be delivered due to some issues. Your app should check whether these products have been delivered, and if not, deliver them and call consumeOwnedPurchase to consume them. To obtain order information of those successfully consumed products, call obtainOwnedPurchaseRecord.
NOTICE

If you consume a product before delivery, the following exception may occur: When the consumption API is called, the consumption is successfully executed on the server but the result is not returned to the app due to network exceptions. As a result, the product is not delivered. This exception is difficult to handle, so you should consume the product after a successful delivery.

  • For non-consumables, this method returns purchase data of all the products that have been bought.
  • For subscriptions, this method returns the current subscription relationship, which may be:
    • Renewal (The user still has access to the subscription and it will be renewed at the end of this billing cycle.)
    • Expiring (The subscription will enter the expiring state if it fails to renew at the end of a billing cycle.)
    • Grace period (During this period, HUAWEI IAP tries to renew the subscription. If successfully renewed, the subscription will still work as before. If all renewal tries fail at the end of the grace period, the subscription will expire, and the user will lose access to the subscription.)

Parameters

Expand

Name

Description

ownedPurchasesReq

OwnedPurchasesReq object that contains request information.

Returns

Expand

Type

Description

Task<OwnedPurchasesResult>

Task containing the API request result.

  • You can use OnSuccessListener to obtain an OwnedPurchasesResult object returned in the success scenario.
  • You can use OnFailureListener to obtain an IapApiException object returned in the failure scenario.

Sample code:

Java
Kotlin
// Construct an OwnedPurchasesReq object.
OwnedPurchasesReq ownedPurchasesReq = new OwnedPurchasesReq();
// priceType: 0: consumable; 1: non-consumable; 2: subscription
ownedPurchasesReq.setPriceType(0);
// Obtain the Activity object that calls the API.
final Activity activity = getActivity();
// Call the obtainOwnedPurchases API to obtain the order information about all consumables that have been purchased but not delivered.
Task<OwnedPurchasesResult> task = Iap.getIapClient(activity).obtainOwnedPurchases(ownedPurchasesReq);
task.addOnSuccessListener(new OnSuccessListener<OwnedPurchasesResult>() {
    @Override
    public void onSuccess(OwnedPurchasesResult result) {
        // Obtain the execution result.
        if (result != null && result.getInAppPurchaseDataList() != null) {
            for (int i = 0; i < result.getInAppPurchaseDataList().size(); i++) {
                String inAppPurchaseData = result.getInAppPurchaseDataList().get(i);
                String InAppSignature = result.getInAppSignature().get(i);
                // Verify the signature using your app's IAP public key.
                // Check the purchase status of each product if the verification is successful. When the payment has been made, deliver the required product. After the delivery is performed, consume the product.
                try {
                    InAppPurchaseData inAppPurchaseDataBean = new InAppPurchaseData(inAppPurchaseData);
                    int purchaseState = inAppPurchaseDataBean.getPurchaseState();
                } catch (JSONException e) {
                }
            }
        }
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        if (e instanceof IapApiException) {
            IapApiException apiException = (IapApiException) e;
            Status status = apiException.getStatus();
            int returnCode = apiException.getStatusCode();
        } else {
            // Other external errors.
        }
    }
});
// Construct an OwnedPurchasesReq object.
val ownedPurchasesReq = OwnedPurchasesReq()
// priceType: 0: consumable; 1: non-consumable; 2: subscription
ownedPurchasesReq.setPriceType(0)
// Obtain the Activity object that calls the API.
val activity: Activity = getActivity()
// Call the obtainOwnedPurchases API to obtain the order information about all consumables that have been purchased but not delivered.
val task = Iap.getIapClient(activity).obtainOwnedPurchases(ownedPurchasesReq)
task.addOnSuccessListener { result ->
    // Obtain the execution result.
    if (result != null && result.getInAppPurchaseDataList() != null) {
        for (i in result.getInAppPurchaseDataList().indices) {
            val inAppPurchaseData = result.getInAppPurchaseDataList()[i]
            val InAppSignature = result.getInAppSignature()[i]
            // Verify the signature using your app's IAP public key.
                // Check the purchase status of each product if the verification is successful. When the payment has been made, deliver the required product. After the delivery is performed, consume the product.
            try {
                val inAppPurchaseDataBean = InAppPurchaseData(inAppPurchaseData)
                val purchaseState = inAppPurchaseDataBean.purchaseState
            } catch (e: JSONException) {
            }
        }
    }
}.addOnFailureListener { e ->
    if (e is IapApiException) {
        val apiException = e as IapApiException
        val status: Status = apiException.status
        val returnCode = apiException.statusCode
    } else {
            // Other external errors.
    }
}

obtainProductInfo

Expand

Method

Task<ProductInfoResult> obtainProductInfo(ProductInfoReq productInfoReq)

Obtains product details configured in AppGallery Connect. If you use Huawei's PMS to price products, you can use this API to obtain product details from the PMS to ensure that the product information in your app is the same as that displayed on the checkout page of HUAWEI IAP.

NOTE

It is recommended that your app do not obtain product information from your own server. Otherwise, price information may be inconsistent between your app and the checkout screen of IAP.

Parameters

Expand

Name

Description

productInfoReq

ProductInfoReq object that contains request information.

Returns

Expand

Type

Description

Task<ProductInfoResult>

Task containing the API request result.

  • You can use OnSuccessListener to obtain a ProductInfoResult object returned in the success scenario.
  • You can use OnFailureListener to obtain an IapApiException object returned in the failure scenario.

Sample code:

Java
Kotlin
List<String> productIdList = new ArrayList<>();
// Only those products already configured in AppGallery Connect can be queried.
productIdList.add("ConsumeProduct1001");
ProductInfoReq req = new ProductInfoReq();
// priceType: 0: consumable; 1: non-consumable; 2: subscription
req.setPriceType(0);
req.setProductIds(productIdList);
// Obtain the Activity object that calls the API.
final Activity activity = getActivity();
// Call the obtainProductInfo API to obtain product details configured in AppGallery Connect.
Task<ProductInfoResult> task = Iap.getIapClient(activity).obtainProductInfo(req);
task.addOnSuccessListener(new OnSuccessListener<ProductInfoResult>() {
    @Override
    public void onSuccess(ProductInfoResult result) {
        // Obtain the product details returned upon a successful API call.
        List<ProductInfo> productList = result.getProductInfoList();
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        if (e instanceof IapApiException) {
            IapApiException apiException = (IapApiException) e;
            int returnCode = apiException.getStatusCode();
        } else {
            // Other external errors.
        }
    }
});
val productIdList: MutableList<String> = ArrayList()
// Only those products already configured in AppGallery Connect can be queried.
productIdList.add("ConsumeProduct1001")
val req = ProductInfoReq()
// priceType: 0: consumable; 1: non-consumable; 2: subscription
req.setPriceType(0)
req.productIds = productIdList
// Obtain the Activity object that calls the API.
val activity: Activity = getActivity()
// Call the obtainProductInfo API to obtain product details configured in AppGallery Connect.
val task = Iap.getIapClient(activity).obtainProductInfo(req)
task.addOnSuccessListener { result -> 
    // Obtain the product details returned upon a successful API call.
    val productList = result.getProductInfoList()
}.addOnFailureListener { e ->
    if (e is IapApiException) {
        val apiException = e as IapApiException
        val returnCode = apiException.statusCode
    } else {
            // Other external errors.
    }
}

parsePurchaseResultInfoFromIntent

Expand

Method

PurchaseResultInfo parsePurchaseResultInfoFromIntent(Intent intent)

Parses payment result data and returns a PurchaseResultInfo object in the following scenario: After the createPurchaseIntent API is called and the payment page is displayed successfully, the payment result will be called back using the onActivityResult method of Activity.

Parameters

Expand

Name

Description

intent

Intent object that contains payment result information.

Returns

Expand

Type

Description

PurchaseResultInfo

Object that contains payment result information.

Sample code:

Java
Kotlin
// The payment result is returned in the onActivityResult method of the Activity object that opens the IAP page.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    PurchaseResultInfo purchaseResultInfo = Iap.getIapClient(this).parsePurchaseResultInfoFromIntent(data);
}
// The payment result is returned in the onActivityResult method of the Activity object that opens the IAP page.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    val purchaseResultInfo = Iap.getIapClient(this).parsePurchaseResultInfoFromIntent(data)
}

startIapActivity

Expand

Method

Task<StartIapActivityResult> startIapActivity(StartIapActivityReq startIapActivityReq)

Displays the subscription editing screen or subscription management screen of HUAWEI IAP.

NOTE

This method is added in IAP SDK 4.0.4.300 to replace the subscription management screen redirection method provided in earlier versions.

Parameters

Expand

Name

Description

startIapActivityReq

StartIapActivityReq object that contains request information.

Returns

Expand

Type

Description

Task<StartIapActivityResult>

Task containing the API request result.

  • You can use OnSuccessListener to obtain a StartIapActivityResult object returned in the success scenario.
  • You can use OnFailureListener to obtain an IapApiException object returned in the failure scenario.

Sample code:

Java
Kotlin
// Redirect to the subscription management screen.
StartIapActivityReq req = new StartIapActivityReq();
req.setType(StartIapActivityReq.TYPE_SUBSCRIBE_MANAGER_ACTIVITY);
// Obtain the Activity object that calls the API.
final Activity activity = getActivity();
IapClient mClient = Iap.getIapClient(activity);
Task<StartIapActivityResult> task = mClient.startIapActivity(req);
task.addOnSuccessListener(new OnSuccessListener<StartIapActivityResult>() {
    @Override
    public void onSuccess(StartIapActivityResult result) {
        Log.i("startIapActivity", "onSuccess");
        if (result != null) {
            result.startActivity(activity);
        }
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        Log.e("startIapActivity", "onFailure");
    }
});

// Redirect to the subscription editing screen.
StartIapActivityReq req = new StartIapActivityReq();
req.setSubscribeProductId("XXX");
req.setType(StartIapActivityReq.TYPE_SUBSCRIBE_EDIT_ACTIVITY);
// Obtain the Activity object that calls the API.
final Activity activity = getActivity();
IapClient mClient = Iap.getIapClient(activity);
Task<StartIapActivityResult> task = mClient.startIapActivity(req);
task.addOnSuccessListener(new OnSuccessListener<StartIapActivityResult>() {
    @Override
    public void onSuccess(StartIapActivityResult result) {
        Log.i("startIapActivity", "onSuccess");
        if (result != null) {
            result.startActivity(activity);
        }
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(Exception e) {
        Log.e("startIapActivity", "onFailure");
    }
});
// Redirect to the subscription management screen.
val req = StartIapActivityReq()
req.setType(StartIapActivityReq.TYPE_SUBSCRIBE_MANAGER_ACTIVITY)
// Obtain the Activity object that calls the API.
val activity: Activity = getActivity()
val mClient = Iap.getIapClient(activity)
val task = mClient.startIapActivity(req)
task.addOnSuccessListener { result ->
    Log.i("startIapActivity", "onSuccess")
    result?.startActivity(activity)
}.addOnFailureListener { Log.e("startIapActivity", "onFailure") }

// Redirect to the subscription editing screen.
val req = StartIapActivityReq()
req.setSubscribeProductId("XXX")
req.setType(StartIapActivityReq.TYPE_SUBSCRIBE_EDIT_ACTIVITY)
// Obtain the Activity object that calls the API.
val activity: Activity = getActivity()
val mClient = Iap.getIapClient(activity)
val task = mClient.startIapActivity(req)
task.addOnSuccessListener { result ->
    Log.i("startIapActivity", "onSuccess")
    result?.startActivity(activity)
}.addOnFailureListener { Log.e("startIapActivity", "onFailure") }