We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
quickApp ReferencesQuick GameRuntime Quick GameOpen CapabilitiesPayment

Payment

NOTICE

This function depends on the services provided by the IAP SDK. The IAP SDK may collect personal data of players. Before using this function in a quick game, notify players that their personal data will be collected and guide them to read the corresponding privacy policy in detail.

Restrictions

Applicable Device

Mobile phones and tablets

Applicable Region

Global

Definition

Expand

API

Description

qg.isEnvReady(Object object) (1103+)

Checks whether the country/region to which the current HUAWEI ID belongs supports Huawei IAP.

qg.isSandboxActivated(Object object) (1103+)

Checks whether the HUAWEI ID and the quick game RPK version meet the sandbox testing requirements.

qg.obtainOwnedPurchases(Object object) (1103+)

Queries the data of a user's purchased products, including consumables and non-consumables.

qg.obtainProductInfo(Object object) (1103+)

Queries the details of a product configured in AppGallery Connect.

qg.createPurchaseIntent(Object object) (1103+)

Creates orders for purchasing products (consumables and non-consumables) configured in AppGallery Connect.

qg.consumeOwnedPurchase(Object object) (1103+)

Consumes a consumable that has been successfully purchased.

qg.isEnvReady(Object object) (1103+)

  • Description

    Checks whether the country/region to which the current HUAWEI ID belongs supports IAP.

  • Parameters
    Expand

    Parameter

    Type

    Mandatory

    Description

    isEnvReadyReq

    json

    Yes

    Indicates whether requests from Huawei IAP are supported. For details, please refer to Parameters in isEnvReadyReq.

    success

    function

    No

    Callback function triggered upon a successful API call. For details, please refer to Parameters in the success callback function.

    fail

    function

    No

    Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function.

    • Parameters in isEnvReadyReq
      Expand

      Parameter

      Type

      Mandatory

      Description

      applicationID

      string

      Yes

      Unique ID allocated to a created quick game. For details about how to obtain the app ID, please refer to Obtaining the App ID.

    • Parameters in the success callback function
      Expand

      Parameter

      Type

      Description

      returnCode

      number

      Return code. If the current user's HUAWEI ID supports IAP, the return code is 0.

    • Parameters in the fail callback function
      Expand

      Parameter

      Type

      Description

      data

      string

      Result message.

      code

      number

      Result code.

  • Sample code
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. qg.isEnvReady({
    2. isEnvReadyReq: {
    3. // Replace it with a valid app ID.
    4. "applicationID": "101***751"
    5. },
    6. success: function (data) {
    7. console.log("isEnvReady data =", JSON.stringify(data));
    8. },
    9. fail: function (data, code) {
    10. console.log("isEnvReady fail data =" + data, "code =" + code);
    11. }
    12. })

qg.isSandboxActivated(Object object) (1103+)

  • Description

    Checks whether the signed-in HUAWEI ID and quick game RPK version meet the sandbox testing requirements.

  • Parameters
    Expand

    Parameter

    Type

    Mandatory

    Description

    isSandboxActivatedReq

    json

    Yes

    Indicates whether the signed-in HUAWEI ID meets the sandbox testing requirements. For details, please refer to Parameters in isSandboxActivatedReq.

    success

    function

    No

    Callback function triggered upon a successful API call. For details, please refer to Parameters in the success callback function.

    fail

    function

    No

    Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function.

    • Parameters in isSandboxActivatedReq
      Expand

      Parameter

      Type

      Mandatory

      Description

      applicationID

      string

      Yes

      Unique ID allocated to a created quick game. For details about how to obtain the app ID, please refer to Obtaining the App ID.

    • Parameters in the success callback function
      Expand

      Parameter

      Type

      Description

      returnCode

      number

      Return code.

      errMsg

      string

      Return code description.

      isSandboxUser

      boolean

      Indicates whether a sandbox account is used.

      isSandboxApk

      boolean

      Indicates whether the APK version of a quick game client, such as Quick App Center or Petal Lite Games, meets the sandbox testing requirements. The value is always true.

      versionInApk

      string

      Version information of a quick game client.

      versionFrMarket

      string

      Latest quick game version information on AppGallery.

    • Parameters in the fail callback function
      Expand

      Parameter

      Type

      Description

      data

      string

      Result message.

      code

      number

      Result code.

  • Sample code
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. qg.isSandboxActivated({
    2. isSandboxActivatedReq: {
    3. // Replace it with a valid app ID.
    4. "applicationID": "101***751",
    5. },
    6. success: function (data) {
    7. console.log("isSandboxActivated data =", JSON.stringify(data));
    8. },
    9. fail: function (data, code) {
    10. console.log("isSandboxActivated fail data =" + JSON.stringify(data), "code =" + code);
    11. }
    12. })

qg.obtainOwnedPurchases(Object object) (1103+)

  • Description

    Queries the data of a user's purchased products. Only one type of product can be obtained per API call.

    Expand

    Type of Product to Be Queried

    Return Result

    Consumable

    Data of the purchased but not consumed products. In this case, you need to:

    1. Determine whether the products are delivered. If the products are not delivered, deliver them first.
    2. After the delivery is confirmed, call qg.consumeOwnedPurchase to consume the products.

    Non-consumable

    Data of all products purchased by a user.

  • Parameters
    Expand

    Parameter

    Type

    Mandatory

    Description

    ownedPurchasesReq

    json

    Yes

    Queries data of the purchased but not consumed products. For details, please refer to Parameters in ownedPurchasesReq.

    success

    function

    No

    Callback function triggered upon a successful API call. For details, please refer to Parameters in the success callback function.

    fail

    function

    No

    Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function.

    • Parameters in ownedPurchasesReq
      Expand

      Parameter

      Type

      Mandatory

      Description

      applicationID

      string

      Yes

      Unique ID allocated to a created quick game. For details about how to obtain the app ID, please refer to Obtaining the App ID.

      priceType

      number

      Yes

      Product type.

      • 0: consumable
      • 1: non-consumable

      publicKey

      string

      Yes

      Public key obtained when you enable the IAP service. For details, please refer to Obtaining the Payment Public Key.

      continuationToken

      string

      No

      Data continuation token. This parameter is not required for the first request.

    • Parameters in the success callback function
      Expand

      Parameter

      Type

      Description

      returnCode

      number

      Return code.

      errMsg

      string

      Return code description.

      itemList

      array

      Product ID list.

      inAppPurchaseDataList

      array

      Product information list. For details about each product, please refer to Parameters in inAppPurchaseData.

      NOTICE

      Set the minimum platform version number minPlatformVersion in the manifest file to a value greater than or equal to 1103. If the platform version number is greater than or equal to 1078, the data returned in the array is a JSON string. If the platform version number is lower than 1078, the data returned in the array is of the object type.

      inAppSignature

      array

      Signature string corresponding to each product in inAppPurchaseDataList.

      • The signatures are generated using the IAP private key with the SHA256withRSA algorithm.
      • The app needs to perform signature verification on each product. If the verification fails, the app needs to obtain the product information again. For details about signature verification, please refer to Verifying the Result Returned by the Quick Game Engine.

      continuationToken

      string

      Data continuation token. If a user has a large number of products and continuationToken is returned, the app must continue to call this method and pass the current continuationToken until continuationToken is not returned.

    • Parameters in the fail callback function
      Expand

      Parameter

      Type

      Description

      data

      string

      Result message.

      code

      number

      Result code.

  • Sample code
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. qg.obtainOwnedPurchases({
    2. ownedPurchasesReq: {
    3. "priceType": 0,
    4. // Replace it with a valid app ID.
    5. "applicationID": "101***751",
    6. // Replace it with a valid payment public key.
    7. "publicKey": "MIIBojANBgkqhkiG9w0BAQ**********************GidLcEeKlAgMBAAE"
    8. },
    9. success: function (data) {
    10. console.log("obtainOwnedPurchases data =", JSON.stringify(data));
    11. },
    12. fail: function (data, code) {
    13. console.log("obtainOwnedPurchases fail data =" + data, "code =" + code);
    14. }
    15. })

qg.obtainProductInfo(Object object) (1103+)

  • Description

    Queries the details of a product configured in AppGallery Connect. If you use Huawei's Product Management System (PMS) to set product prices, you can call this API to obtain product details from the PMS. This ensures that the product information, such as prices, displayed in your app is consistent with what users see on the Huawei Pay checkout page, preventing any discrepancies between the two.

  • Parameters
    Expand

    Parameter

    Type

    Mandatory

    Description

    productInfoReq

    json

    Yes

    Request for obtaining product information. For details, please refer to Parameters in productInfoReq.

    success

    function

    No

    Callback function triggered upon a successful API call. For details, please refer to Parameters in the success callback function.

    fail

    function

    No

    Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function.

    • Parameters in productInfoReq
      Expand

      Parameter

      Type

      Mandatory

      Description

      applicationID

      string

      Yes

      Unique ID allocated to a created quick game. For details about how to obtain the app ID, please refer to Obtaining the App ID.

      priceType

      number

      Yes

      Product type.

      • 0: consumable
      • 1: non-consumable

      productIds

      array

      Yes

      Product ID list.

    • Parameters in the success callback function
      Expand

      Parameter

      Type

      Description

      returnCode

      number

      Return code.

      errMsg

      string

      Return code description.

      productInfoList

      array

      Product details. For details, please refer to Parameters in productInfo.

      • Parameters in productInfo
        Expand

        Parameter

        Type

        Description

        productId

        string

        Product ID.

        priceType

        number

        Product type.

        • 0: consumable
        • 1: non-consumable

        price

        string

        Displayed price of a product (tax included). The format is Currency Price, for example, EUR 0.15. In some countries/regions, the format is Currency symbolPrice, for example, ¥0.15 in the Chinese mainland.

        NOTE

        If a promotional price has been set for a product, the promotional price will be returned.

        microsPrice

        number

        Product price in micro unit. Product price in micro unit, which equals the actual product price multiplied by 1,000,000. For example, if the actual price of a product is US$1.99, the product price in micro unit is 1,990,000 (1.99 x 1,000,000).

        currency

        string

        Currency in which a product is purchased.

        productName

        string

        Product name.

        productDesc

        string

        Product description.

        originalMicroPrice

        string

        Original product price in micro unit. Product price in micro unit, which equals the original product price multiplied by 1,000,000. For example, if the original price of a product is US$1.99, the product price in micro unit is 1,990,000 (1.99 x 1,000,000).

        originalLocalPrice

        string

        Original price of a product (tax included). The format is Currency Price, for example, EUR 0.15. In some countries/regions, the format is Currency symbolPrice, for example, ¥0.15 in the Chinese mainland.

    • Parameters in the fail callback function
      Expand

      Parameter

      Type

      Description

      data

      string

      Result message.

      code

      number

      Result code.

  • Sample code
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. qg.obtainProductInfo({
    2. productInfoReq: {
    3. "priceType": 1,
    4. "productIds": ["dy1","dmy00","lcole"],
    5. // Replace it with a valid app ID.
    6. "applicationID": "101***751",
    7. },
    8. success: function (data) {
    9. console.log("obtainProductInfo =" + JSON.stringify(data));
    10. },
    11. fail: function (data, code) {
    12. console.log("obtainProductInfo fail data =" + JSON.stringify(data), "code =" + code);
    13. }
    14. })

qg.createPurchaseIntent(Object object) (1103+)

  • Description

    Opens the Huawei Pay checkout page to display the product, price, and payment method after the product is created in AppGallery Connect. Huawei can adjust product prices as foreign exchange rates change. To ensure product price consistency, call obtainProductInfo to retrieve product details. Avoid fetching prices from your own server to prevent discrepancies when prices are adjusted due to exchange rate changes.

  • Parameters
    Expand

    Parameter

    Type

    Mandatory

    Description

    purchaseIntentReq

    json

    Yes

    Request information for initiating an in-app purchase. For details, please refer to Parameters in purchaseIntentReq.

    success

    function

    No

    Callback function triggered upon a successful API call. For details, please refer to Parameters in the success callback function.

    fail

    function

    No

    Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function.

    • Parameters in purchaseIntentReq
      Expand

      Parameter

      Type

      Mandatory

      Description

      applicationID

      string

      Yes

      Unique ID allocated to a created quick game. For details about how to obtain the app ID, please refer to Obtaining the App ID.

      publicKey

      string

      Yes

      Public key obtained when you enable the IAP service. For details, please refer to Obtaining the Payment Public Key.

      productId

      string

      Yes

      ID of the product pending payment.

      priceType

      number

      Yes

      Product type.

      • 0: consumable
      • 1: non-consumable

      developerPayload

      string

      No

      Merchant-side reserved information. The length range is [0,256]. If a value is passed, it will be returned to the app in the callback triggered when the payment is successful.

    • Parameters in the success callback function
      Expand

      Parameter

      Type

      Description

      returnCode

      number

      Return code.

      errMsg

      string

      Return code description.

      inAppDataSignature

      string

      Signature string generated using the private key for the purchased product. The signing algorithm used is SHA256withRSA. After the payment is successful, use the payment public key to verify the signature of the purchase data. For details about the signature verification method, please refer to Verifying the Result Returned by the Quick Game Engine.

      inAppPurchaseData

      string

      Purchase order details. For details, please refer to Parameters in inAppPurchaseData.

      • Parameters in inAppPurchaseData
        Expand

        Parameter

        Type

        Description

        applicationId

        number

        App ID.

        autoRenewing

        boolean

        Indicates whether a subscription is set to auto-renew. The value is always false.

        orderId

        string

        Order ID in Huawei Pay, which is generated after the payment is successful.

        kind

        number

        Product type.

        • 0: consumable
        • 1: non-consumable

        packageName

        string

        Client package name.

        productId

        string

        Product ID.

        productName

        string

        Product name.

        purchaseTime

        number

        Product purchase timestamp. Number of milliseconds from 00:00:00 on January 1, 1970 to the time when the product was purchased.

        purchaseState

        number

        Transaction status.

        • -1: initialized
        • 0: purchased
        • 1: canceled
        • 2: refunded

        developerPayload

        string

        Merchant-side reserved information, which is passed when the API is called.

        developerChallenge

        string

        Challenge defined when an app requests to consume a product, which uniquely identifies the consumption request. After the consumption is successful, the challenge is recorded in the purchase information and returned. If the challenge is the same as an existing one, it indicates a repeated consumption request. In this case, the same challenge will be returned unchanged.

        consumptionState

        number

        Consumption status.

        • 0: not consumed
        • 1: consumed

        purchaseToken

        string

        Purchase token containing 92 characters, which identifies the mapping between a product and a user. It is generated by the Huawei IAP server when the payment is complete. If the value needs to be stored, you are advised to reserve 128 characters.

        purchaseType

        number

        Purchase type.

        • 0: in the sandbox
        • 1: in the promotional period (currently unsupported)
        NOTE

        This parameter is not returned during normal purchases.

        currency

        string

        Currency in which a product is purchased. For the Chinese mainland, the value is CNY. For other countries/regions, please refer to Supported Countries/Regions.

        price

        number

        Displayed price of a product.

        country

        string

        Country/Region code. For the Chinese mainland, the value is CN. For other countries/regions, please refer to Supported Countries/Regions.

        payType

        string

        Payment method.

        • 0: HUAWEI Points
        • 3: credit card
        • 4: Alipay
        • 6: call fee
        • 13: PayPal
        • 16: debit card
        • 17: WeChat Pay
        • 19: gift card
        • 20: balance
        • 21: HUAWEI Gift Card
        • 24: WorldPay
        • 31: Huawei Pay
        • 32: Ant Credit Pay
        • 200: M-PESA
          NOTE
    • Parameters in the fail callback function
      Expand

      Parameter

      Type

      Description

      data

      string

      Result message.

      code

      number

      Result code.

  • Sample code
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. qg.createPurchaseIntent({
    2. purchaseIntentReq: {
    3. // Replace it with a valid app ID.
    4. "applicationID": "101***751",
    5. "productId": "dmy001",
    6. "priceType": 0,
    7. "developerPayload": "testPurchase",
    8. // Replace it with a valid payment public key.
    9. "publicKey": "MIIBojANBgkqhkiG***********************T7PzVAeGidLcEeKlAgMBAAE"
    10. },
    11. success: function (data) {
    12. that.inAppPurchaseData = data;
    13. console.log("createPurchaseIntent success =" + JSON.stringify(data));
    14. },
    15. fail: function (data, code) {
    16. console.log("createPurchaseIntent fail data =" + data, "code =" + code);
    17. }
    18. })

qg.consumeOwnedPurchase(Object object) (1103+)

  • Description

    Consumes a consumable after it is successfully delivered. You need to 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.

    NOTE
    • 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.
    • Ensure that the consumption operation is executed only after a successful delivery. If consumption occurs before delivery, it may lead to complex exception scenarios that are difficult to handle.
  • Parameters
    Expand

    Parameter

    Type

    Mandatory

    Description

    consumeOwnedPurchaseReq

    json

    Yes

    Request for consuming a product. For details, please refer to Parameters in consumeOwnedPurchaseReq.

    success

    function

    No

    Callback function triggered upon a successful API call. For details, please refer to Parameters in the success callback function.

    fail

    function

    No

    Callback function triggered upon a failed API call. For details, please refer to Parameters in the fail callback function.

    • Parameters in consumeOwnedPurchaseReq
      Expand

      Parameter

      Type

      Mandatory

      Description

      applicationID

      string

      Yes

      Unique ID allocated to a created quick game. For details about how to obtain the app ID, please refer to Obtaining the App ID.

      developerChallenge

      string

      No

      Challenge defined when an app requests to consume a product. After the consumption is successful, the challenge is recorded in the purchase information and returned. The value contains a maximum of 64 characters. If the challenge is the same as an existing one, it indicates a repeated consumption request. In this case, the same challenge will be returned unchanged.

      purchaseToken

      string

      Yes

      ID of a product purchased by a user. This parameter is generated by the IAP server, included in Parameters in inAppPurchaseData, and returned to the app. After the IAP server updates the order status, the product is delivered.

      publicKey

      string

      Yes

      Public key obtained when you enable the IAP service. For details, please refer to Obtaining the Payment Public Key.

    • Parameters in the success callback function
      Expand

      Parameter

      Type

      Description

      returnCode

      number

      Return code.

      errMsg

      string

      Return code description.

      consumePurchaseData

      string

      Consumption result. For details, please refer to Parameters in consumePurchaseData.

      dataSignature

      string

      Signature string generated using the IAP private key.

      • Parameters in consumePurchaseData
        Expand

        Parameter

        Type

        Description

        applicationId

        number

        Unique ID allocated to a created quick game.

        autoRenewing

        boolean

        Indicates whether a subscription is set to auto-renew. The value is always false.

        orderId

        string

        Order ID in Huawei Pay, which is generated after the payment is successful.

        packageName

        string

        Client package name.

        productId

        string

        Product ID.

        purchaseTime

        number

        Product purchase timestamp. Number of milliseconds from 00:00:00 on January 1, 1970 to the time when the product was purchased.

        purchaseState

        number

        Order status.

        • -1: initialized and invisible
        • 0: purchased
        • 1: canceled
        • 2: refunded

        developerPayload

        string

        Merchant-side reserved information, which is passed when the API is called.

        purchaseToken

        string

        Purchase token, which uniquely identifies the mapping between a product and a user. It is generated by the Huawei IAP server when the payment is complete.

        developerChallenge

        string

        Challenge defined when an app requests to consume a product, which uniquely identifies the consumption request. After the consumption is successful, the challenge is recorded in the purchase information and returned. If the challenge is the same as an existing one, it indicates a repeated consumption request. In this case, the same challenge will be returned unchanged.

        consumptionState

        number

        Consumption status.

        • 0: not consumed
        • 1: consumed

        currency

        string

        Currency in which a product is purchased.

        price

        number

        Displayed price of a product (tax included). The format is Currency Price, for example, EUR 0.15. In some countries/regions, the format is Currency symbolPrice, for example, ¥0.15 in the Chinese mainland.

        country

        string

        Country/Region code. For the Chinese mainland, the value is CN. For other countries/regions, please refer to Supported Countries/Regions.

        responseCode

        string

        Return code. Value 0 indicates success.

        responseMessage

        string

        Return code description.

    • Parameters in the fail callback function
      Expand

      Parameter

      Type

      Description

      data

      string

      Result message.

      code

      number

      Result code.

  • Sample code
    Collapse
    Word wrap
    Dark theme
    Copy code
    1. qg.consumeOwnedPurchase({
    2. consumeOwnedPurchaseReq: {
    3. // Replace it with a valid app ID.
    4. "applicationID": "101***751",
    5. "developerPayload": "testPurchase",
    6. // Replace it with a valid purchase token.
    7. "purchaseToken": "0000017297d6a4f*****************e3d0x434e.1.101315751",
    8. // Replace it with a valid payment public key.
    9. "publicKey": "MIIBojANBgkqhkiG9w***********************0BAQZWT7PzVAeGidLcEeKlAgMBAAE"
    10. },
    11. success: function (data) {
    12. console.log("consumeOwnedPurchase data =", JSON.stringify(data));
    13. },
    14. fail: function (data, code) {
    15. console.log("consumeOwnedPurchase fail data =" + JSON.stringify(data), "code =" + code);
    16. }
    17. })

Result Codes

For more result codes, please refer to Result Codes.

Expand

Result Code

Description

200

Common error.

202

The input parameter is incorrect.

1002

Failed to authenticate the app.

1001

Failed to verify the public key.

Sample Code

Purchasing Consumables

Collapse
Word wrap
Dark theme
Copy code
  1. isEnvReady() {
  2. console.log("isEnvReady start");
  3. let that = this;
  4. qg.isEnvReady({
  5. isEnvReadyReq: {
  6. "applicationID": "102107863",
  7. },
  8. success: function (data) {
  9. console.log("isEnvReady data =", JSON.stringify(data));
  10. that.obtainOwnedPurchases();
  11. that.obtainConsumeProductInfo();
  12. that.payForConsable();
  13. },
  14. fail: function (data, code) {
  15. console.log("isEnvReady fail data =" + JSON.stringify(data), "code =" + code);
  16. }
  17. });
  18. },
  19. obtainOwnedPurchases(token) {
  20. console.log("obtainOwnedPurchases token=" + token);
  21. var that = this;
  22. qg.obtainOwnedPurchases({
  23. ownedPurchasesReq: {
  24. "applicationID": "102107863",
  25. "priceType": 0,
  26. continuationToken: token,
  27. // Payment public key generated when you enable IAP on HUAWEI Developers.
  28. "publicKey": "MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAhK0+5cD+BBG5vPS3loMtpjidjDSau3U5Bxtm2ecR3DV1WVBAIDU3Vmn45mWS8Ww9zMWKPmyAF1w0/tkIV6hZimnKA5pMB3+nYXwHSABxrvD9KxPTF287M4y3zgHNtN/Zky0E3KF68L2C4ik8ToQHzwNMdPTM1SVkzhaDrhZV3WgkpFk7CnpPGhd6Tee5Bz10ZtjS+FFPOPZXekNoTHOrnF+BBpd6TWYV2tAzLWCH0gySgSg/SUMS/MCx8yTJuDtwkYmwLZa8+4vehwq+ANx7qncIrjo6aH3B2vxECXeVKJUnWzDEMVyfl6nZ/FDMjdisPW4OXHwegY9rvK2YTJd8h1h4IozuJ5R8JUKFEaGJmZ9KJJxV+NQvtNEwfa8rZ0CX6BlXhkYxaiGFIrHW3siLYxn7LpjBNKgKHnuNuuzWZb5WVEKmYHixpBReK6XSmybx8t+yddhaCe1kQC93b3kLyr9ulO79QffZdTFwl1a2fRFDSAIAPteeG8S+MG7iACz7AgMBAAE=",// Payment public key obtained when you enable IAP on HUAWEI Developers.
  29. },
  30. success: function (data) {
  31. that.parseOwnedPurchases(data);
  32. },
  33. fail: function (data, code) {
  34. console.log("obtainOwnedPurchases fail data =" + JSON.stringify(data), "code =" + code);
  35. }
  36. });
  37. },
  38. parseOwnedPurchases(data) {
  39. console.log("parseOwnedPurchases data =", JSON.stringify(data));
  40. var that = this;
  41. let purseDataArray = data.inAppPurchaseDataList; //jsonString array
  42. console.log("parseOwnedPurchases purseDataArray =", JSON.stringify(purseDataArray));
  43. let purseDataSignArray = data.inAppSignature;
  44. if (purseDataArray) {
  45. for (var j = 0, len = purseDataArray.length; j < len; j++) {
  46. // console.log("inAppPurchaseData[j]" + JSON.stringify(inAppPurchaseDataList[j]))
  47. // console.log("obtainOwnedPurchases data.inAppSignature=" + data.inAppSignature)
  48. // console.log("obtainOwnedPurchases publicKey" + publicKey)
  49. var purchaseState = JSON.parse(purseDataArray[j]).purchaseState;
  50. var purchaseToken = JSON.parse(purseDataArray[j]).purchaseToken;
  51. console.log("purchaseState" + purchaseState);
  52. console.log("purchaseToken" + purchaseToken);
  53. //console.log(purchaseState)
  54. // Verify the signature of product details before the delivery.
  55. // You can send a verification request to the Huawei IAP server to further verify the order.
  56. that.checkSign(purseDataArray[j], data.inAppSignature,JSON.parse(purseDataArray[j]).purchaseToken);
  57. }
  58. }
  59. if (data.continuationToken) {
  60. console.log("parseOwnedPurchases data.continuationToken");
  61. that.obtainOwnedPurchases(data.continuationToken);
  62. }
  63. },
  64. obtainConsumeProductInfo() {
  65. var that = this;
  66. qg.obtainProductInfo({
  67. productInfoReq: {
  68. "applicationID": "102107863",
  69. "priceType": 1,
  70. "productIds": "2022112302",
  71. "publicKey": "MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAhK0+5cD+BBG5vPS3loMtpjidjDSau3U5Bxtm2ecR3DV1WVBAIDU3Vmn45mWS8Ww9zMWKPmyAF1w0/tkIV6hZimnKA5pMB3+nYXwHSABxrvD9KxPTF287M4y3zgHNtN/Zky0E3KF68L2C4ik8ToQHzwNMdPTM1SVkzhaDrhZV3WgkpFk7CnpPGhd6Tee5Bz10ZtjS+FFPOPZXekNoTHOrnF+BBpd6TWYV2tAzLWCH0gySgSg/SUMS/MCx8yTJuDtwkYmwLZa8+4vehwq+ANx7qncIrjo6aH3B2vxECXeVKJUnWzDEMVyfl6nZ/FDMjdisPW4OXHwegY9rvK2YTJd8h1h4IozuJ5R8JUKFEaGJmZ9KJJxV+NQvtNEwfa8rZ0CX6BlXhkYxaiGFIrHW3siLYxn7LpjBNKgKHnuNuuzWZb5WVEKmYHixpBReK6XSmybx8t+yddhaCe1kQC93b3kLyr9ulO79QffZdTFwl1a2fRFDSAIAPteeG8S+MG7iACz7AgMBAAE=",// Payment public key obtained when you enable IAP on HUAWEI Developers.
  72. },
  73. success: function (data) {
  74. console.log("obtainProductInfo =" + JSON.stringify(data));
  75. },
  76. fail: function (data, code) {
  77. console.log("obtainProductInfo fail data =" + JSON.stringify(data), "code =" + code);
  78. }
  79. });
  80. },
  81. checkSign(signData, signStr, token) {
  82. console.log("checkSign sign signContent=" + signData + ", token=" + token);
  83. console.log("checkSign sign =" + signStr);
  84. // Verify the purchase result signature. For convenience, set result to true.
  85. let result = true;
  86. if (result) {
  87. let purchaseState = JSON.parse(signData).purchaseState;
  88. this.confirmTransct(token, purchaseState);
  89. }
  90. },
  91. confirmTransct(token, purchaseState) {
  92. console.log("confirmTransct:token:" + token);
  93. console.log("confirmTransct: purchaseState:" + purchaseState);
  94. // Check whether the order has been successfully paid based on the purchaseState field in inAppPurchaseData. After the payment is successful, the corresponding offering or service is provided, and the delivery confirmation request is sent to Huawei IAP.
  95. if (purchaseState !== 0) {
  96. return;
  97. }
  98. // Consume a consumable after it is delivered to a user who has completed payment.
  99. qg.consumeOwnedPurchase({
  100. consumeOwnedPurchaseReq: {
  101. "purchaseToken": token,
  102. "applicationID": "102107863",
  103. "publicKey": "MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAhK0+5cD+BBG5vPS3loMtpjidjDSau3U5Bxtm2ecR3DV1WVBAIDU3Vmn45mWS8Ww9zMWKPmyAF1w0/tkIV6hZimnKA5pMB3+nYXwHSABxrvD9KxPTF287M4y3zgHNtN/Zky0E3KF68L2C4ik8ToQHzwNMdPTM1SVkzhaDrhZV3WgkpFk7CnpPGhd6Tee5Bz10ZtjS+FFPOPZXekNoTHOrnF+BBpd6TWYV2tAzLWCH0gySgSg/SUMS/MCx8yTJuDtwkYmwLZa8+4vehwq+ANx7qncIrjo6aH3B2vxECXeVKJUnWzDEMVyfl6nZ/FDMjdisPW4OXHwegY9rvK2YTJd8h1h4IozuJ5R8JUKFEaGJmZ9KJJxV+NQvtNEwfa8rZ0CX6BlXhkYxaiGFIrHW3siLYxn7LpjBNKgKHnuNuuzWZb5WVEKmYHixpBReK6XSmybx8t+yddhaCe1kQC93b3kLyr9ulO79QffZdTFwl1a2fRFDSAIAPteeG8S+MG7iACz7AgMBAAE=",// Payment public key obtained when you enable IAP on HUAWEI Developers.
  104. },
  105. success: function (data) {
  106. console.log("consumeOwnedPurchase data =", JSON.stringify(data));
  107. },
  108. fail: function (data, code) {
  109. console.log("consumeOwnedPurchase fail data =" + JSON.stringify(data), "code =" + code);
  110. }
  111. });
  112. },
  113. payForConsable() {
  114. var that = this;
  115. qg.createPurchaseIntent({
  116. purchaseIntentReq: {
  117. "applicationID": "102107863",
  118. "priceType": 0,
  119. "productId": "2022112801",
  120. "publicKey": "MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAhK0+5cD+BBG5vPS3loMtpjidjDSau3U5Bxtm2ecR3DV1WVBAIDU3Vmn45mWS8Ww9zMWKPmyAF1w0/tkIV6hZimnKA5pMB3+nYXwHSABxrvD9KxPTF287M4y3zgHNtN/Zky0E3KF68L2C4ik8ToQHzwNMdPTM1SVkzhaDrhZV3WgkpFk7CnpPGhd6Tee5Bz10ZtjS+FFPOPZXekNoTHOrnF+BBpd6TWYV2tAzLWCH0gySgSg/SUMS/MCx8yTJuDtwkYmwLZa8+4vehwq+ANx7qncIrjo6aH3B2vxECXeVKJUnWzDEMVyfl6nZ/FDMjdisPW4OXHwegY9rvK2YTJd8h1h4IozuJ5R8JUKFEaGJmZ9KJJxV+NQvtNEwfa8rZ0CX6BlXhkYxaiGFIrHW3siLYxn7LpjBNKgKHnuNuuzWZb5WVEKmYHixpBReK6XSmybx8t+yddhaCe1kQC93b3kLyr9ulO79QffZdTFwl1a2fRFDSAIAPteeG8S+MG7iACz7AgMBAAE=",// Payment public key obtained when you enable IAP on HUAWEI Developers.
  121. },
  122. success: function (data) {
  123. that.inAppPurchaseData = data.inAppPurchaseData;
  124. console.log("createPurchaseIntent success =" + JSON.stringify(data));
  125. var sign = data.inAppDataSignature;
  126. let signData = data.inAppPurchaseData;
  127. let type = typeof signData;
  128. console.log("createPurchaseIntent type= " + type);
  129. let token = JSON.parse(JSON.stringify(signData)).purchaseToken;
  130. console.log("createPurchaseIntent token= " + token);
  131. that.checkSign(JSON.stringify(signData), sign, token);
  132. },
  133. fail: function (data, code) {
  134. console.log("createPurchaseIntent fail data =" + JSON.stringify(data), "code =" + code);
  135. if (code === 60051 || code === -1) {
  136. // If the information about consumables is returned, the consumables might not be delivered due to some exceptions.
  137. // In this case, your app needs to check whether the consumables are delivered. If not, the app needs to deliver
  138. // them and call the pay.consumeOwnedPurchase API to consume them.
  139. that.obtainOwnedPurchases();
  140. }
  141. }
  142. });
  143. },

Purchasing Non-Consumables

Collapse
Word wrap
Dark theme
Copy code
  1. isEnvReady() {
  2. console.log("isEnvReady start");
  3. let that = this;
  4. qg.isEnvReady({
  5. isEnvReadyReq: {
  6. "applicationID": "102107863",
  7. },
  8. success: function (data) {
  9. console.log("isEnvReady data =", JSON.stringify(data));
  10. that.payForNonConsable();
  11. },
  12. fail: function (data, code) {
  13. console.log("isEnvReady fail data =" + JSON.stringify(data), "code =" + code);
  14. }
  15. });
  16. },
  17. payForNonConsable() {
  18. let that = this;
  19. qg.createPurchaseIntent({
  20. purchaseIntentReq: {
  21. "applicationID": "102107863",
  22. "priceType": 1,
  23. "productId": "2022112302",
  24. "publicKey": "MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAhK0+5cD+BBG5vPS3loMtpjidjDSau3U5Bxtm2ecR3DV1WVBAIDU3Vmn45mWS8Ww9zMWKPmyAF1w0/tkIV6hZimnKA5pMB3+nYXwHSABxrvD9KxPTF287M4y3zgHNtN/Zky0E3KF68L2C4ik8ToQHzwNMdPTM1SVkzhaDrhZV3WgkpFk7CnpPGhd6Tee5Bz10ZtjS+FFPOPZXekNoTHOrnF+BBpd6TWYV2tAzLWCH0gySgSg/SUMS/MCx8yTJuDtwkYmwLZa8+4vehwq+ANx7qncIrjo6aH3B2vxECXeVKJUnWzDEMVyfl6nZ/FDMjdisPW4OXHwegY9rvK2YTJd8h1h4IozuJ5R8JUKFEaGJmZ9KJJxV+NQvtNEwfa8rZ0CX6BlXhkYxaiGFIrHW3siLYxn7LpjBNKgKHnuNuuzWZb5WVEKmYHixpBReK6XSmybx8t+yddhaCe1kQC93b3kLyr9ulO79QffZdTFwl1a2fRFDSAIAPteeG8S+MG7iACz7AgMBAAE=",// Payment public key obtained when you enable IAP on HUAWEI Developers.
  25. },
  26. success: function (data) {
  27. console.log("payForNonConsable createPurchaseIntent success =" + JSON.stringify(data));
  28. var sign = data.inAppDataSignature;
  29. let signData = data.inAppPurchaseData;
  30. let token = JSON.parse(JSON.stringify(signData)).purchaseToken;
  31. this.checkSign(JSON.stringify(signData), sign, token);
  32. },
  33. fail: function (data, code) {
  34. console.log("payForNonConsable createPurchaseIntent fail data =" + JSON.stringify(data), "code =" + code);
  35. if (code === 60051 || code === -1) {
  36. that.obtainOwnedPurchases();
  37. }
  38. }
  39. });
  40. },
  41. checkSign(signData, signStr, token) {
  42. console.log("checkSign sign signContent=" + signData + ", token=" + token);
  43. console.log("checkSign sign =" + signStr);
  44. // Set this result to true for convenience here.
  45. let result = true;
  46. if (result) {
  47. // After a payment is complete, the developer needs to determine whether the order has been successfully paid
  48. // based on the purchaseState field in inAppPurchaseData.After the payment is successful,
  49. // the corresponding offering or service is provided, and the delivery confirmation request is sent to Huawei IAP.
  50. let purchaseState = JSON.parse(signData).purchaseState;
  51. this.confirmTransct(token, purchaseState);
  52. }
  53. },
  54. confirmTransct(token, purchaseState) {
  55. console.log("confirmTransct:token:" + token);
  56. console.log("confirmTransct: purchaseState:" + purchaseState);
  57. if (purchaseState == 0) {
  58. console.log("You have purchased it and can use it permanently. You do not need to purchase it again.");
  59. }
  60. },
  61. obtainOwnedPurchases(ptype, token) {
  62. console.log("obtainOwnedPurchases ptype=" + ptype + ", token=" + token);
  63. var that = this;
  64. qg.obtainOwnedPurchases({
  65. ownedPurchasesReq: {
  66. "applicationID": "102107863",
  67. "priceType": 1,
  68. continuationToken: token,
  69. // Payment public key generated when you enable IAP on HUAWEI Developers.
  70. "publicKey": "MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIBigKCAYEAhK0+5cD+BBG5vPS3loMtpjidjDSau3U5Bxtm2ecR3DV1WVBAIDU3Vmn45mWS8Ww9zMWKPmyAF1w0/tkIV6hZimnKA5pMB3+nYXwHSABxrvD9KxPTF287M4y3zgHNtN/Zky0E3KF68L2C4ik8ToQHzwNMdPTM1SVkzhaDrhZV3WgkpFk7CnpPGhd6Tee5Bz10ZtjS+FFPOPZXekNoTHOrnF+BBpd6TWYV2tAzLWCH0gySgSg/SUMS/MCx8yTJuDtwkYmwLZa8+4vehwq+ANx7qncIrjo6aH3B2vxECXeVKJUnWzDEMVyfl6nZ/FDMjdisPW4OXHwegY9rvK2YTJd8h1h4IozuJ5R8JUKFEaGJmZ9KJJxV+NQvtNEwfa8rZ0CX6BlXhkYxaiGFIrHW3siLYxn7LpjBNKgKHnuNuuzWZb5WVEKmYHixpBReK6XSmybx8t+yddhaCe1kQC93b3kLyr9ulO79QffZdTFwl1a2fRFDSAIAPteeG8S+MG7iACz7AgMBAAE=",// Payment public key obtained when you enable IAP on HUAWEI Developers.
  71. },
  72. success: function (data) {
  73. that.parseOwnedPurchases(ptype, data);
  74. },
  75. fail: function (data, code) {
  76. console.log("obtainOwnedPurchases fail data =" + JSON.stringify(data), "code =" + code);
  77. }
  78. });
  79. },
  80. parseOwnedPurchases(ptype, data) {
  81. console.log("parseOwnedPurchases data =", JSON.stringify(data));
  82. let purseDataArray = data.inAppPurchaseDataList; //jsonString array
  83. let purseDataSignArray = data.inAppSignature;
  84. if (purseDataArray) {
  85. for (var j = 0, len = purseDataArray.length; j < len; j++) {
  86. // console.log("inAppPurchaseData[j]" + JSON.stringify(inAppPurchaseDataList[j]))
  87. // console.log("obtainOwnedPurchases data.inAppSignature=" + data.inAppSignature)
  88. // console.log("obtainOwnedPurchases publicKey" + publicKey)
  89. var purchaseState = JSON.parse(purseDataArray[j]).purchaseState;
  90. var purchaseToken = JSON.parse(purseDataArray[j]).purchaseToken;
  91. console.log("purchaseState" + purchaseState);
  92. console.log("purchaseToken" + purchaseToken);
  93. //console.log(purchaseState)
  94. // Verify the signature of product details before the delivery.
  95. // You can send a verification request to the Huawei IAP server to further verify the order.
  96. that.checkSign(purseDataArray[j], data.inAppSignature,JSON.parse(purseDataArray[j]).purchaseToken);
  97. }
  98. }
  99. if (data.continuationToken) {
  100. this.obtainOwnedPurchases(ptype, data.continuationToken);
  101. }
  102. },