文档管理中心

此版本文档已归档不再维护,推荐您使用最新版本

API参考应用内支付服务手机、平板客户端API参考iapIapClient

IapClient

interface info
public interface IapClient
包含了Huawei In-App Purchases的所有接口。

Nested Class Summary

TypeNameDesc
interfaceIapClient.PriceType定义商品类型。

Public Method Summary

Public Methods

Method
Task<IsEnvReadyResult> isEnvReady()
通过该接口可以判断当前设备登录的华为帐号所在的服务国家是否在华为IAP支付服务支持结算的国家和地区中。

Return

typedesc
Task<IsEnvReadyResult>为Task设置回调监听可获取接口请求结果。使用OnSuccessListener可获取到成功场景返回的数据,该场景下返回一个IsEnvReadyResult实例对象。使用OnFailureListener可获取失败场景的相关信息,该场景返回一个IapApiException异常对象。

Call Example

收起
自动换行
深色代码主题
复制
  1. // To get the Activity instance that calls this API.
  2. Activity activity = getActivity();
  3. Task<IsEnvReadyResult> task = Iap.getIapClient(activity).isEnvReady();
  4. task.addOnSuccessListener(new OnSuccessListener<IsEnvReadyResult>() {
  5.    @Override
  6.    public void onSuccess(IsEnvReadyResult result) {
  7.        // Obtain the execution result.
  8.    }
  9. }).addOnFailureListener(new OnFailureListener() {
  10.    @Override
  11.    public void onFailure(Exception e) {
  12.        if (e instanceof IapApiException) {
  13.            IapApiException apiException = (IapApiException) e;
  14.            Status status = apiException.getStatus();
  15.            if (status.getStatusCode() == OrderStatusCode.ORDER_HWID_NOT_LOGIN) {
  16.                // Not logged in.
  17.                if (status.hasResolution()) {
  18.                    try {
  19.                        // 6666 is an int constant defined by the developer.
  20.                        status.startResolutionForResult(getActivity(), 6666);
  21.                    } catch (IntentSender.SendIntentException exp) {
  22.                    }
  23.                }
  24.            } else if (status.getStatusCode() == OrderStatusCode.ORDER_ACCOUNT_AREA_NOT_SUPPORTED) {
  25.                // The current region does not support HUAWEI IAP.            
  26.            }
  27.        }
  28.    }
  29. });
  30. @Override
  31. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  32.    super.onActivityResult(requestCode, resultCode, data);
  33.    if (requestCode == 6666) {
  34.        if (data != null) {
  35.            // Obtain the execution result.
  36.            int returnCode = data.getIntExtra("returnCode", 1);
  37.        }
  38.    }
  39. }


Method
Task<IsSandboxActivatedResult>isSandboxActivated(IsSandboxActivatedReq isSandboxActivatedReq)
通过该接口可以判断当前设备登录的华为帐号及应用的APK版本是否满足沙盒条件。

Parameters

Parameter nameParameter desc
isSandboxActivatedReq包含请求信息的IsSandboxActivatedReq对象。

Return

typedesc
Task<IsSandboxActivatedResult>为Task设置回调监听可获取接口请求结果。使用OnSuccessListener可获取到成功场景返回的数据,该场景下返回一IsSandboxActivatedResult实例对象。使用OnFailureListener可获取失败场景的相关信息,该场景返回一个IapApiException异常对象。

Call Example

收起
自动换行
深色代码主题
复制
  1. // To get the Activity instance that calls this API.
  2. IapClient mClient = Iap.getIapClient(activity);
  3. Task<IsSandboxActivatedResult> task = mClient.isSandboxActivated(new IsSandboxActivatedReq());
  4. task.addOnSuccessListener(new OnSuccessListener<IsSandboxActivatedResult>() {
  5.    @Override
  6.    public void onSuccess(IsSandboxActivatedResult result) {
  7.        Log.i(TAG, "isSandboxActivated success");
  8.        dealSandboxResult(result);
  9.    }
  10. }).addOnFailureListener(new OnFailureListener() {
  11.    @Override
  12.    public void onFailure(Exception e) {
  13.        Log.e(TAG, "isSandboxActivated fail");
  14.        if (e instanceof IapApiException) {
  15.            IapApiException apiException = (IapApiException)e;
  16.            int returnCode = apiException.getStatusCode();
  17.        } else {
  18.            // Other external errors
  19.        }    
  20. }
  21. });
Method
Task<OwnedPurchasesResultobtainOwnedPurchases(OwnedPurchasesReq ownedPurchasesReq)
查询用户所有已订购商品信息,商品包括消耗商品、非消耗商品和自动续费订阅商品。
● 若查询返回消耗型商品,说明可能存在因某些异常而导致未进行发货的情况,需要应用判断是否有发货。如果没有发货则需要进行补发货处理,并调用consumeOwnedPurchase接口进行消耗。
● 若查询返回非消耗型商品,无需进行消耗。

● 若查询返回订阅型商品,则返回该用户在此应用下已有的订阅关系,包括以下几种订阅关系:

    - 续期(正常使用,下周期会续费)。

    - 到期(下一周期不再续费,这个周期过后就变成已过期)。

    - 已过期(订阅服务不能再使用,但是仍能从订阅关系历史中查到)。

Parameters

Parameter nameParameter desc
ownedPurchasesReq包含请求信息的OwnedPurchasesReq对象。


Return

typedesc
Task<OwnedPurchasesResult>为Task设置回调监听可获取接口请求结果。使用OnSuccessListener可获取到成功场景返回的数据,该场景下返回一个OwnedPurchasesResult实例对象。使用OnFailureListener可获取失败场景的相关信息,该场景返回一个IapApiException异常对象。


Call Example

收起
自动换行
深色代码主题
复制
  1. // Constructs a OwnedPurchasesReq object.
  2. OwnedPurchasesReq ownedPurchasesReq = new OwnedPurchasesReq();
  3. // In-app product type contains:
  4. // priceType: 0: consumable; 1: non-consumable; 2: auto-renewable subscription
  5. ownedPurchasesReq.setPriceType(0);
  6. // to call the obtainOwnedPurchases API
  7. // To get the Activity instance that calls this API.
  8. Activity activity = getActivity();
  9. Task<OwnedPurchasesResult> task = Iap.getIapClient(activity).obtainOwnedPurchases(ownedPurchasesReq);
  10. task.addOnSuccessListener(new OnSuccessListener<OwnedPurchasesResult>() {
  11.    @Override
  12.    public void onSuccess(OwnedPurchasesResult result) {
  13.        // Obtain the execution result.
  14.        if (result != null && result.getInAppPurchaseDataList() != null) {
  15.            for (int i = 0; i < result.getInAppPurchaseDataList().size(); i++) {
  16.                String inAppPurchaseData = result.getInAppPurchaseDataList().get(i);
  17.                String InAppSignature = result.getInAppSignature().get(i);
  18.                // use the payment public key to verify the signature of the inAppPurchaseData.
  19.                // if success.
  20.                try {
  21.                    InAppPurchaseData inAppPurchaseDataBean = new InAppPurchaseData(inAppPurchaseData);
  22.                    int purchaseState = inAppPurchaseDataBean.getPurchaseState();
  23.                } catch (JSONException e) {
  24.                }
  25.            }
  26.        }
  27.    }
  28. }).addOnFailureListener(new OnFailureListener() {
  29.    @Override
  30.    public void onFailure(Exception e) {
  31.        if (e instanceof IapApiException) {
  32.            IapApiException apiException = (IapApiException)e;
  33.            Status status = apiException.getStatus();
  34.            int returnCode = apiException.getStatusCode();
  35.        } else {
  36.            // Other external errors
  37.        }
  38.    }
  39. });
Method
Task<ProductInfoResultobtainProductInfo(ProductInfoReq productInfoReq)
此接口用于获取在AppGallery Connect配置的商品详情。如果您使用了华为PMS系统进行商品定价,可通过此接口从华为PMS获取商品详情,以保证和华为支付收银台显示的商品信息一致。请您避免从自有服务端获取商品信息,否则可能导致应用内商品价格和收银台价格不一致。

Parameters

Parameter nameParameter desc
productInfoReq包含请求信息的ProductInfoReq对象。


Return

typedesc
Task<ProductInfoResult>为Task设置回调监听可获取接口请求结果。使用OnSuccessListener可获取到成功场景返回的数据,该场景下返回一个ProductInfoResult实例对象。使用OnFailureListener可获取失败场景的相关信息,该场景返回一个IapApiException异常对象。

Call Example

收起
自动换行
深色代码主题
复制
  1. // Pass in the productId list of products to be queried.
  2. List<String> productIdList = new ArrayList<>();
  3. // The product ID is the same as that set by a developer when configuring product information in AppGallery Connect.
  4. productIdList.add("ConsumeProduct1001");
  5. ProductInfoReq req = new ProductInfoReq();
  6. // priceType: 0: consumable; 1: non-consumable; 2: auto-renewable subscription
  7. req.setPriceType(0);
  8. req.setProductIds(productIdList);
  9. // to call the obtainProductInfo API.
  10. // To get the Activity instance that calls this API.
  11. Activity activity = getActivity();
  12. Task<ProductInfoResult> task = Iap.getIapClient(activity).obtainProductInfo(req);
  13. task.addOnSuccessListener(new OnSuccessListener<ProductInfoResult>() {
  14.    @Override
  15.    public void onSuccess(ProductInfoResult result) {
  16.        // Obtain the result
  17.        List<ProductInfo> productList = result.getProductInfoList();
  18.    }
  19. }).addOnFailureListener(new OnFailureListener() {
  20.    @Override
  21.    public void onFailure(Exception e) {
  22.        if (e instanceof IapApiException) {
  23.            IapApiException apiException = (IapApiException)e;
  24.            int returnCode = apiException.getStatusCode();
  25.        } else {
  26.            // Other external errors
  27.        }
  28.    }
  29. });
Method
Task<PurchaseIntentResult> createPurchaseIntent(PurchaseIntentReq purchaseIntentReq)

PMS商品创建订单接口,支持购买商品或订阅服务。

您在AppGallery Connect创建商品后,使用此接口调用华为支付收银台,显示商品、价格和支付方式。华为会根据国际汇率变动进行商品价格档调整,为了保证应用内商品价格显示一致,请您同时使用obtainProductInfo接口获取商品详情,避免从自有服务端获取价格,以免在汇率变动价格调整时,导致价格显示不一致。本接口只能返回收银台页面的PendingIntent对象,应用需要自行启动收银台页面,启动方法见PurchaseIntentResult中对status的说明。用户完成支付后应用可以在onActivityResult回调方法中通过parsePurchaseResultInfoFromIntent获取支付结果信息。

Parameters

Parameter nameParameter desc
purchaseIntentReq包含请求信息的PurchaseIntentReq对象。


Return

typedesc
Task<PurchaseIntentResult>为Task设置回调监听可获取接口请求结果。使用OnSuccessListener可获取到成功场景返回的数据,该场景下返回一个PurchaseIntentResult实例对象。使用OnFailureListener可获取失败场景的相关信息,该场景返回一个IapApiException异常对象。

Call Example

收起
自动换行
深色代码主题
复制
  1. // Constructs a PurchaseIntentReq object.
  2. PurchaseIntentReq req = new PurchaseIntentReq();
  3. // The product ID is the same as that set by a developer when configuring product information in AppGallery Connect.
  4. req.setProductId("CProduct1");
  5. // In-app product type contains:
  6. // priceType: 0: consumable; 1: non-consumable; 2: auto-renewable subscription
  7. req.setPriceType(0);
  8. req.setDeveloperPayload("test");
  9. // to call the createPurchaseIntent API.
  10. // To get the Activity instance that calls this API.
  11. final Activity activity = getActivity();
  12. Task<PurchaseIntentResult> task = Iap.getIapClient(activity).createPurchaseIntent(req);
  13. task.addOnSuccessListener(new OnSuccessListener<PurchaseIntentResult>() {
  14.    @Override
  15.    public void onSuccess(PurchaseIntentResult result) {
  16.        // Obtain the payment result.
  17.        Status status = result.getStatus();
  18.        if (status.hasResolution()) {
  19.            try {
  20.                // 6666 is an int constant defined by the developer.
  21.                status.startResolutionForResult(activity, 6666);
  22.            } catch (IntentSender.SendIntentException exp) {
  23.            }
  24.        }
  25.    }
  26. }).addOnFailureListener(new OnFailureListener() {
  27.    @Override
  28.    public void onFailure(Exception e) {
  29.        if (e instanceof IapApiException) {
  30.            IapApiException apiException = (IapApiException)e;
  31.            Status status = apiException.getStatus();
  32.            int returnCode = apiException.getStatusCode();
  33.        } else {
  34.            // Other external errors
  35.        }
  36.    }
  37. });
  38. @Override
  39. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  40.    super.onActivityResult(requestCode, resultCode, data);
  41.    if (requestCode == 6666) {
  42.        if (data == null) {
  43.            Log.e("onActivityResult", "data is null");
  44.            return;
  45.        }
  46.        PurchaseResultInfo purchaseResultInfo = Iap.getIapClient(this).parsePurchaseResultInfoFromIntent(data);
  47.        switch(purchaseResultInfo.getReturnCode()) {
  48.            case OrderStatusCode.ORDER_STATE_CANCEL:
  49.                // User cancel payment.
  50.                break;
  51.            case OrderStatusCode.ORDER_STATE_FAILED:
  52.            case OrderStatusCode.ORDER_PRODUCT_OWNED:
  53.                // to check if there exists undelivered products.
  54.                break;
  55.            case OrderStatusCode.ORDER_STATE_SUCCESS:
  56.                // pay success.
  57.                String inAppPurchaseData = purchaseResultInfo.getInAppPurchaseData();
  58.                String inAppPurchaseDataSignature = purchaseResultInfo.getInAppDataSignature();
  59.                // use the public key of your app to verify the signature.
  60.                // If ok, you can deliver your products.
  61.                // If the user purchased a consumable product, call the consumeOwnedPurchase API to consume it after successfully delivering the product.
  62.                break;
  63.            default:
  64.                break;
  65.        }
  66.        return;
  67.    }
  68. }
Method
Task<PurchaseIntentResultcreatePurchaseIntentWithPrice(PurchaseIntentWithPriceReq purchaseIntentWithPriceReq)

应用可以调用此接口自行设置商品价格并完成支付,而不是从PMS获取价格。此接口不提供商品信息自动全球化能力,需要开发者在接口中指定价格、币种和国家。本接口只能返回收银台页面的PendingIntent对象,应用需要自行启动收银台页面,启动方法见PurchaseIntentResult中对status的说明。用户完成支付后应用可以在onActivityResult回调方法中通过parsePurchaseResultInfoFromIntent获取支付结果信息。

本接口当前只支持中国大陆地区发布的应用,发布中国大陆地区以外地区的应用必须使用createPurchaseIntent接口。

Parameters

Parameter nameParameter desc
purchaseIntentWithPriceReq包含请求信息的PurchaseIntentWithPriceReq对象。


Return

typedesc
Task<PurchaseIntentResult>为Task设置回调监听可获取接口请求结果。使用OnSuccessListener可获取到成功场景返回的数据,该场景下返回一个PurchaseIntentResult实例对象。使用OnFailureListener可获取失败场景的相关信息,该场景返回一个IapApiException异常对象。


Call Example

收起
自动换行
深色代码主题
复制
  1. // Constructs a PurchaseIntentWithPriceReq object.
  2. PurchaseIntentWithPriceReq req = new PurchaseIntentWithPriceReq();
  3. req.setCurrency("CNY");
  4. req.setDeveloperPayload("test");
  5. // In-app product type contains:
  6. // priceType: 0: consumable; 1: non-consumable;
  7. req.setPriceType(0);
  8. req.setSdkChannel("1");
  9. req.setProductName("test");
  10. req.setAmount("1.00");
  11. req.setProductId("ConsumableProduct1");
  12. req.setServiceCatalog("X38");
  13. req.setCountry("CN");
  14. // to call the createPurchaseIntentWithPrice API.
  15. // To get the Activity instance that calls this API.
  16. final Activity activity = getActivity();
  17. Task<PurchaseIntentResult> task = Iap.getIapClient(activity).createPurchaseIntentWithPrice(req);
  18. task.addOnSuccessListener(new OnSuccessListener<PurchaseIntentResult>() {
  19.    @Override
  20.    public void onSuccess(PurchaseIntentResult result) {
  21.        // Obtain the payment result.
  22.        String paymentData = result.getPaymentData();
  23.        String paymentSignature = result.getPaymentSignature();
  24.        // to verify signature of the paymentData
  25.        String publicKey = "XXX";
  26.        if (CipherUtil.doCheck(paymentData, paymentSignature, publicKey)) {
  27.            // if success, to open the checkout page of HUAWEI IAP.
  28.            Status status = result.getStatus();
  29.            if (status.hasResolution()) {
  30.                try {
  31.                    status.startResolutionForResult(activity, 6666);
  32.                } catch (IntentSender.SendIntentException exp) {
  33.                }
  34.            }
  35.        }
  36.    }
  37. }).addOnFailureListener(new OnFailureListener() {
  38.    @Override
  39.    public void onFailure(Exception e) {
  40.        if (e instanceof IapApiException) {
  41.            IapApiException apiException = (IapApiException) e;
  42.            int returnCode = apiException.getStatusCode();
  43.            switch (returnCode) {
  44.                case OrderStatusCode.ORDER_HWID_NOT_LOGIN:
  45.                case OrderStatusCode.ORDER_NOT_ACCEPT_AGREEMENT:
  46.                    // Account is not logged in or the user does not agree to payment agreement.
  47.                    Status status = apiException.getStatus();
  48.                    if (status.hasResolution()) {
  49.                        try {
  50.                            status.startResolutionForResult(activity, 8888);
  51.                        } catch (IntentSender.SendIntentException exp) {
  52.                        }
  53.                    }
  54.                    break;
  55.                case OrderStatusCode.ORDER_PRODUCT_OWNED:
  56.                    // to check if there exists undelivered products.
  57.                    break;
  58.                default:
  59.                    break;
  60.            }
  61.        }
  62.    }
  63. });
  64. // Payment result callback.
  65. @Override
  66. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  67.    if (requestCode == 6666) {
  68.        if (data == null) {
  69.            Log.e("onActivityResult", "data is null");
  70.            return;
  71.        }
  72.        PurchaseResultInfo purchaseResultInfo = Iap.getIapClient(this).parsePurchaseResultInfoFromIntent(data);
  73.        switch(purchaseResultInfo.getReturnCode()) {
  74.            case OrderStatusCode.ORDER_STATE_CANCEL:
  75.                // User cancel payment.
  76.                break;
  77.            case OrderStatusCode.ORDER_STATE_FAILED:
  78.            case OrderStatusCode.ORDER_PRODUCT_OWNED:
  79.                // to check if there exists undelivered products.
  80.                break;
  81.            case OrderStatusCode.ORDER_STATE_SUCCESS:
  82.                // pay success.
  83.                String inAppPurchaseData = purchaseResultInfo.getInAppPurchaseData();
  84.                String inAppPurchaseDataSignature = purchaseResultInfo.getInAppDataSignature();
  85.                // use the public key of your app to verify the signature.
  86.                // If ok, you can deliver your products.
  87.                // If the user purchased a consumable product, call the consumeOwnedPurchase API to consume it after successfully delivering the product.
  88.                break;
  89.            default:
  90.                break;
  91.        }
  92.        return;
  93.    }
  94.    if (requestCode == 8888) {
  95.        int returnCode = IapClientHelper.parseRespCodeFromIntent(data);
  96.        if (returnCode == OrderStatusCode.ORDER_STATE_SUCCESS) {
  97.            // The request was successful, and then you need to make another createPurchaseIntentWithPrice request
  98.        }
  99.        return;
  100.    }
  101. }
Method
Task<ConsumeOwnedPurchaseResultconsumeOwnedPurchase(ConsumeOwnedPurchaseReq consumeOwnedPurchaseReq)

针对消耗型商品,在商品支付成功后,应用需要在发放商品之后调用此接口对消耗商品执行消耗操作。

注意:

1、如果您在消耗之后再进行发货,可能存在以下异常场景:调用消耗接口过程中,服务器消耗成功但因网络异常等原因没有成功将结果回调给端侧,从而造成服务器已经消耗但是端侧没有发货的场景。此场景后续很难处理,建议您在发货之后再执行消耗操作。

2、您需要把已发货的token传至您的服务器,后续即使消耗失败也可以从您的服务器拉取数据进行对比,避免出现异常。建议您在发货前检查您的应用服务器,以确保每个购买令牌都未使用过,即未发过货。



Parameters

Parameter nameParameter desc
consumeOwnedPurchaseReq包含请求信息的ConsumeOwnedPurchaseReq对象。


Return

typedesc
Task<ConsumeOwnedPurchaseResult>为Task设置回调监听可获取接口请求结果。使用OnSuccessListener可获取到成功场景返回的数据,该场景下返回一个ConsumeOwnedPurchaseResult实例对象。使用OnFailureListener可获取失败场景的相关信息,该场景返回一个IapApiException异常对象。

Call Example

收起
自动换行
深色代码主题
复制
  1. // Constructs a ConsumeOwnedPurchaseReq object.
  2. String purchaseToken = "";
  3. try {
  4.    InAppPurchaseData inAppPurchaseDataBean = new InAppPurchaseData(inAppPurchaseData);
  5.    purchaseToken = inAppPurchaseDataBean.getPurchaseToken();
  6. } catch (JSONException e) {
  7. }
  8. ConsumeOwnedPurchaseReq req = new ConsumeOwnedPurchaseReq();
  9. req.setPurchaseToken(purchaseToken);
  10. // to call the consumeOwnedPurchase API.
  11. // To get the Activity instance that calls this API.
  12. Activity activity = getActivity();
  13. Task<ConsumeOwnedPurchaseResult> task = Iap.getIapClient(activity).consumeOwnedPurchase(req);
  14. task.addOnSuccessListener(new OnSuccessListener<ConsumeOwnedPurchaseResult>() {
  15.    @Override
  16.    public void onSuccess(ConsumeOwnedPurchaseResult result) {
  17.        // Obtain the result
  18.    }
  19. }).addOnFailureListener(new OnFailureListener() {
  20.    @Override
  21.    public void onFailure(Exception e) {
  22.        if (e instanceof IapApiException) {
  23.            IapApiException apiException = (IapApiException)e;
  24.            Status status = apiException.getStatus();
  25.            int returnCode = apiException.getStatusCode();
  26.        } else {
  27.            // Other external errors
  28.        }
  29.    }
  30. });
Method
Task<OwnedPurchasesResult>obtainOwnedPurchaseRecord(OwnedPurchasesReq ownedPurchasesReq)
此接口用于获取消耗型商品的历史消耗商品信息或订阅商品的所有订阅收据。
● 若查询商品类型为消耗型商品,此接口将返回商品列表中执行过发货和消耗操作的商品信息。
● 若查询商品类型为非消耗型商品,此接口不会返回商品信息。
● 若查询商品类型为订阅型商品,此接口将返回当前用户在本应用中的所有订阅收据。

Parameters

Parameter nameParameter desc
ownedPurchasesReq包含请求信息的OwnedPurchasesReq对象。


Return

typedesc
Task<OwnedPurchasesResult>为Task设置回调监听可获取接口请求结果。使用OnSuccessListener可获取到成功场景返回的数据,该场景下返回一个OwnedPurchasesResult实例对象。使用OnFailureListener可获取失败场景的相关信息,该场景返回一个IapApiException异常对象。


Call Example

收起
自动换行
深色代码主题
复制
  1. // Constructs a OwnedPurchasesReq object.
  2. OwnedPurchasesReq req = new OwnedPurchasesReq();
  3. // In-app product type contains:
  4. // 0: consumable; 1: non-consumable; 2: auto-renewable subscription
  5. req.setPriceType(0);
  6. // to call the obtainOwnedPurchaseRecord API.
  7. // To get the Activity instance that calls this API.
  8. Activity activity = getActivity();
  9. Task<OwnedPurchasesResult> task = Iap.getIapClient(activity).obtainOwnedPurchaseRecord(req);
  10. task.addOnSuccessListener(new OnSuccessListener<OwnedPurchasesResult>() {
  11.    @Override
  12.    public void onSuccess(OwnedPurchasesResult result) {
  13.        // Obtain the execution result.
  14.    }
  15. }).addOnFailureListener(new OnFailureListener() {
  16.    @Override
  17.    public void onFailure(Exception e) {
  18.        if (e instanceof IapApiException) {
  19.            IapApiException apiException = (IapApiException)e;
  20.            int returnCode = apiException.getStatusCode();
  21.        } else {
  22.            // Other external errors
  23.        }
  24.    }
  25. });


Method
Task<StartIapActivityResult> startIapActivity(StartIapActivityReq startIapActivityReq)

可用此方法拉起华为应用内支付的相关页面,包括:

  • 编辑订阅页

  • 管理订阅页

说明:

该方法从IAP SDK4.0.4.300版本起开始使用,用于替换之前版本提供的订阅管理页面跳转方法。

Parameters

Parameter nameParameter desc
startIapActivityReq包含请求信息的StartIapActivityReq对象。


Return

typedesc
Task<StartIapActivityResult>为Task设置回调监听可获取接口请求结果。使用OnSuccessListener可获取到成功场景返回的数据,该场景下返回一个StartIapActivityResult实例对象。使用OnFailureListener可获取失败场景的相关信息,该场景返回一个IapApiException异常对象。


Call Example

收起
自动换行
深色代码主题
复制
  1. // Redirecting to the subscription management page.
  2. StartIapActivityReq req = new StartIapActivityReq();
  3. req.setType(StartIapActivityReq.TYPE_SUBSCRIBE_MANAGER_ACTIVITY);
  4. final Activity activity = getActivity();
  5. IapClient mClient = Iap.getIapClient(activity);
  6. Task<StartIapActivityResult> task = mClient.startIapActivity(req);
  7. task.addOnSuccessListener(new OnSuccessListener<StartIapActivityResult>() {
  8.    @Override
  9.    public void onSuccess(StartIapActivityResult result) {
  10.        Log.i("startIapActivity", "onSuccess");
  11.        if (result != null) {
  12.            result.startActivity(activity);
  13.        }
  14.    }
  15. }).addOnFailureListener(new OnFailureListener() {
  16.    @Override
  17.    public void onFailure(Exception e) {
  18.        Log.e("startIapActivity", "onFailure");
  19.    }
  20. });
  21. // Redirecting to the subscription editing page.
  22. StartIapActivityReq req = new StartIapActivityReq();
  23. req.setSubscribeProductId("XXX");
  24. req.setType(StartIapActivityReq.TYPE_SUBSCRIBE_EDIT_ACTIVITY);
  25. final Activity activity = getActivity();
  26. IapClient mClient = Iap.getIapClient(activity);
  27. Task<StartIapActivityResult> task = mClient.startIapActivity(req);
  28. task.addOnSuccessListener(new OnSuccessListener<StartIapActivityResult>() {
  29.    @Override
  30.    public void onSuccess(StartIapActivityResult result) {
  31.        Log.i("startIapActivity", "onSuccess");
  32.        if (result != null) {
  33.            result.startActivity(activity);
  34.        }
  35.    }
  36. }).addOnFailureListener(new OnFailureListener() {
  37.    @Override
  38.    public void onFailure(Exception e) {
  39.        Log.e("startIapActivity", "onFailure");
  40.    }
  41. });
Method
PurchaseResultInfo parsePurchaseResultInfoFromIntent(Intent intent)
调用createPurchaseIntentcreatePurchaseIntentWithPrice接口成功并拉起页面后,会通过Activity的onActivityResult方法回调支付结果,可用此方法解析支付结果数据,结果返回一个PurchaseResultInfo对象。

Parameters

Parameter nameParameter desc
data包含支付结果信息的Intent对象。


Return

typedesc
PurchaseResultInfo包含支付结果信息的对象。


Call Example

收起
自动换行
深色代码主题
复制
  1. // Call back the payment result in the onActivityResult method of the activity.
  2. @Override
  3. protected void onActivityResult(int requestCode, int resultCode, Intent data){
  4.    Activity activity = getActivity();
  5.    PurchaseResultInfo purchaseResultInfo = Iap.getIapClient(activity).parsePurchaseResultInfoFromIntent(data);
  6. }
本页面可能包含由第三方许可的内容,请参考具体描述
在 API参考 中进行搜索
请输入您想要搜索的关键词