[Huawei Developers] muraliameakula:
What Does a Delivery Failure Mean?

A delivery failure refers to a situation where a user does not receive the item they have purchased.

A user purchases a virtual product or service within an app, but the user does not receive the purchased item due to an error (such as network exception or process termination) occurring in data synchronization between the app and the in-app purchases server.

Delivery failures can be damaging to both users and developers, and may lead to your app receiving a one-star rating, or users directly turning away from it. This may in turn have a knock-on effect on potential users, driving them away from your app before even giving it a chance. Clearly, this is something that needs to be avoided.

Common Way of Handling a Delivery Failure

The user claims that they have paid but did not receive the purchased item, and subsequently requests a refund. This will undoubtedly break the user's trust in your app.

Integrating HUAWEI IAP to Eliminate Delivery Failures

With HUAWEI IAP, you can completely eliminate the chance of a missing delivery occurring.

Your app calls the APIs of HUAWEI IAP to query order information from the HUAWEI IAP server. The server will return orders that are paid but not consumed to your app, which will then redeliver the products based on the query result, and tell the server to update the order status.

You can complete integration by following these instructions:

HUAWEI IAP: Purchase Process

For a purchased consumable, your app will call the consumption API to consume the product. If the API call fails, product delivery will fail. A non-consumable product or subscription service will not experience a delivery failure.

A typical consumable purchasing process:

1. A user initiates a purchase request from your app, which your app then sends to HMS Core (APK).

2. Request a delivery. Verify the signature of purchase data before delivering the requested product. 3. Deliver the product and send the purchase token to your app server. This token can be used to obtain the product delivery status so that you can determine whether to redeliver a product if its consumption fails due to delivery failure.

3. After a product is successfully delivered, call the consumeOwnedPurchaseAPI to consume the product and send a notification to the Huawei IAP server to update its delivery status. purchaseToken is passed in the API call request, and once consumption is complete, the Huawei IAP server resets the product status to available for purchase. The product can then be purchased again.

Besides the consumeOwnedPurchase API provided by the IAP client, your app can also use the API provided by the IAP server for product consumption. For details, please refer to Confirming the Purchase for the Order Service.

How HUAWEI IAP redelivers a product:

With HUAWEI IAP you can redeliver a consumable when a delivery failure occurs because of data synchronization errors (caused by network exception, process termination, or others) between your app and the IAP server. The following figure illustrates the redelivery process.

Your app needs to trigger a redelivery in the following scenarios:

The app launches.

Result code -1 (OrderStatusCode. ORDER_STATE_FAILED) is returned for a purchase request.

Result code 60051 (OrderStatusCode. ORDER_PRODUCT_OWNED) is returned for a purchase request.

Implement the redelivery function as follows:

1. Use obtainOwnedPurchases to obtain the purchase information about the purchased but undelivered consumable. Specify priceType as 0 in OwnedPurchasesReq.

If this API is successfully called, HUAWEI IAP will return an OwnedPurchasesResult object, which contains the purchase data and signature data of all purchased products that have not being delivered. Use the public key allocated by AppGallery Connect to verify the signature. For more information about the verification method, please refer to Verifying the Signature in the Returned Result.

Each purchase’s data is a character string in JSON format that contains the parameters listed in InAppPurchaseData. You need to parse the purchaseState field from the InAppPurchaseData character string. If purchaseState of a purchase is 0, the purchase is successful and delivery will be performed.

// 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 consumable products 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 the request is successful.
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);
// Use the IAP public key to verify the signature of inAppPurchaseData.
// Check the purchase status of each product if the verification is successful. When the payment has been made, deliver the required product. After a successful delivery, 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.
}
}
});

2. Call the consumeOwnedPurchase API to consume a delivered product.

Perform a delivery confirmation for all products queried through the obtainOwnedPurchases API. If a product is already delivered, call the consumeOwnedPurchase API to consume the product and instruct the Huawei IAP server to update the delivery status. Following consumption, the Huawei IAP server resets the product status to available for purchase, allowing the product to be purchased again.

For more details, please click:

HUAWEI IAP official website

HUAWEI IAP Development Guide

HUAWEI HMS Core Community

To learn more, please visit:

HUAWEI Developers official website

Development Guide

Reddit to join developer discussions

GitHub or Gitee to download the demo and sample code

Stack Overflow to solve integration problems

Follow our official account for the latest HMS Core-related news and updates.

Original Source