目录

Payment

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

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.

[h2]qg.isEnvReady(Object object) (1103+)

[h2]qg.isSandboxActivated(Object object) (1103+)

[h2]qg.obtainOwnedPurchases(Object object) (1103+)

[h2]qg.obtainProductInfo(Object object) (1103+)

[h2]qg.createPurchaseIntent(Object object) (1103+)

[h2]qg.consumeOwnedPurchase(Object object) (1103+)

Result Codes

For more result codes, please refer to Result Codes.

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

[h2]Purchasing Consumables

    isEnvReady() {
        console.log("isEnvReady start");
        let that = this;
        qg.isEnvReady({
          isEnvReadyReq: {
              "applicationID": "102107863",
          },
          success: function (data) {
            console.log("isEnvReady data =", JSON.stringify(data));
            that.obtainOwnedPurchases();
            that.obtainConsumeProductInfo();
            that.payForConsable();
          },
          fail: function (data, code) {
            console.log("isEnvReady fail data =" + JSON.stringify(data), "code =" + code);
          }
        });
    },

    obtainOwnedPurchases(token) {
        console.log("obtainOwnedPurchases token=" + token);
        var that = this;
        qg.obtainOwnedPurchases({
          ownedPurchasesReq: {
            "applicationID": "102107863",
            "priceType": 0,
			continuationToken: token,
            // Payment public key generated when you enable IAP on HUAWEI Developers.
            "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.
        },
          success: function (data) {
            that.parseOwnedPurchases(data);
          },
          fail: function (data, code) {
            console.log("obtainOwnedPurchases fail data =" + JSON.stringify(data), "code =" + code);
          }
        });
      },

      parseOwnedPurchases(data) {
        console.log("parseOwnedPurchases data =", JSON.stringify(data));
        var that = this;
        let purseDataArray = data.inAppPurchaseDataList; //jsonString array
        console.log("parseOwnedPurchases purseDataArray =", JSON.stringify(purseDataArray));

        let purseDataSignArray = data.inAppSignature;
        if (purseDataArray) {
            for (var j = 0, len = purseDataArray.length; j < len; j++) {
                // console.log("inAppPurchaseData[j]" + JSON.stringify(inAppPurchaseDataList[j]))
                // console.log("obtainOwnedPurchases data.inAppSignature=" + data.inAppSignature)
                // console.log("obtainOwnedPurchases publicKey" + publicKey)
                var purchaseState = JSON.parse(purseDataArray[j]).purchaseState;
                var purchaseToken = JSON.parse(purseDataArray[j]).purchaseToken;
                console.log("purchaseState" + purchaseState);
                console.log("purchaseToken" + purchaseToken);
               //console.log(purchaseState)
               // Verify the signature of product details before the delivery.
               // You can send a verification request to the Huawei IAP server to further verify the order.
               that.checkSign(purseDataArray[j], data.inAppSignature,JSON.parse(purseDataArray[j]).purchaseToken);
            }
        }
        if (data.continuationToken) {
          console.log("parseOwnedPurchases data.continuationToken");
          that.obtainOwnedPurchases(data.continuationToken);
        }
      },

    obtainConsumeProductInfo() {
      var that = this;
      qg.obtainProductInfo({
        productInfoReq: {
            "applicationID": "102107863",
            "priceType": 1,
            "productIds": "2022112302",
            "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.
        },
        success: function (data) {
          console.log("obtainProductInfo =" + JSON.stringify(data));
        },
        fail: function (data, code) {
          console.log("obtainProductInfo fail data =" + JSON.stringify(data), "code =" + code);
        }
      });
    },

    checkSign(signData, signStr, token) {
      console.log("checkSign sign signContent=" + signData + ", token=" + token);
      console.log("checkSign sign =" + signStr);
        // Verify the purchase result signature. For convenience, set result to true.
        let result = true;
        if (result) {
          let purchaseState = JSON.parse(signData).purchaseState;
          this.confirmTransct(token, purchaseState);
        }
    },

    confirmTransct(token, purchaseState) {
      console.log("confirmTransct:token:" + token);
      console.log("confirmTransct: purchaseState:" + purchaseState);
      // 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.
      if (purchaseState !== 0) {
        return;
      }
      // Consume a consumable after it is delivered to a user who has completed payment.
        qg.consumeOwnedPurchase({
          consumeOwnedPurchaseReq: {
            "purchaseToken": token,
            "applicationID": "102107863",
            "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.
          },
          success: function (data) {
            console.log("consumeOwnedPurchase data =", JSON.stringify(data));  
          },
          fail: function (data, code) {
            console.log("consumeOwnedPurchase fail data =" + JSON.stringify(data), "code =" + code);
          }
        });
    },

    payForConsable() {
      var that = this;
      qg.createPurchaseIntent({
        purchaseIntentReq: {
            "applicationID": "102107863",
            "priceType": 0,
            "productId": "2022112801",
            "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.
        },
        success: function (data) {
          that.inAppPurchaseData = data.inAppPurchaseData;
          console.log("createPurchaseIntent success =" + JSON.stringify(data));
          var sign = data.inAppDataSignature;
          let signData = data.inAppPurchaseData;
          let type = typeof signData;
          console.log("createPurchaseIntent type= " + type);
          let token = JSON.parse(JSON.stringify(signData)).purchaseToken;
          console.log("createPurchaseIntent token= " + token);
          that.checkSign(JSON.stringify(signData), sign, token);
        },
        fail: function (data, code) {
          console.log("createPurchaseIntent fail data =" + JSON.stringify(data), "code =" + code);
          if (code === 60051 || code === -1) {
            // If the information about consumables is returned, the consumables might not be delivered due to some exceptions.
            // In this case, your app needs to check whether the consumables are delivered. If not, the app needs to deliver
            // them and call the pay.consumeOwnedPurchase API to consume them.
            that.obtainOwnedPurchases();
          }
        }
      });
    },

[h2]Purchasing Non-Consumables

isEnvReady() {
        console.log("isEnvReady start");
        let that = this;
        qg.isEnvReady({
          isEnvReadyReq: {
              "applicationID": "102107863",
          },
          success: function (data) {
            console.log("isEnvReady data =", JSON.stringify(data));
            that.payForNonConsable();
          },
          fail: function (data, code) {
            console.log("isEnvReady fail data =" + JSON.stringify(data), "code =" + code);
          }
        });
    },

    payForNonConsable() {
        let that = this;
        qg.createPurchaseIntent({
          purchaseIntentReq: {
              "applicationID": "102107863",
              "priceType": 1,
              "productId": "2022112302",
              "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.
          },
          success: function (data) {
            console.log("payForNonConsable createPurchaseIntent success =" + JSON.stringify(data));
            var sign = data.inAppDataSignature;
            let signData = data.inAppPurchaseData;
            let token = JSON.parse(JSON.stringify(signData)).purchaseToken;
            this.checkSign(JSON.stringify(signData), sign, token);
          },
          fail: function (data, code) {
            console.log("payForNonConsable createPurchaseIntent fail data =" + JSON.stringify(data), "code =" + code);
            if (code === 60051 || code === -1) {
              that.obtainOwnedPurchases();
            }
          }
        });
      },

      checkSign(signData, signStr, token) {
          console.log("checkSign sign signContent=" + signData + ", token=" + token);
          console.log("checkSign sign =" + signStr);
          // Set this result to true for convenience here.
            let result = true;
            if (result) {
              // After a payment is complete, the developer needs to determine 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.
              let purchaseState = JSON.parse(signData).purchaseState;
              this.confirmTransct(token, purchaseState);
            }
        },

        confirmTransct(token, purchaseState) {
          console.log("confirmTransct:token:" + token);
          console.log("confirmTransct: purchaseState:" + purchaseState);
          if (purchaseState == 0) {
            console.log("You have purchased it and can use it permanently. You do not need to purchase it again.");
          }
        },

      obtainOwnedPurchases(ptype, token) {
        console.log("obtainOwnedPurchases ptype=" + ptype + ", token=" + token);
        var that = this;
        qg.obtainOwnedPurchases({
          ownedPurchasesReq: {
            "applicationID": "102107863",
            "priceType": 1,
            continuationToken: token,
            // Payment public key generated when you enable IAP on HUAWEI Developers.
            "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.
        },
          success: function (data) {
            that.parseOwnedPurchases(ptype, data);
          },
          fail: function (data, code) {
            console.log("obtainOwnedPurchases fail data =" + JSON.stringify(data), "code =" + code);
          }
        });
      },

      parseOwnedPurchases(ptype, data) {
          console.log("parseOwnedPurchases data =", JSON.stringify(data));
          let purseDataArray = data.inAppPurchaseDataList; //jsonString array

          let purseDataSignArray = data.inAppSignature;
          if (purseDataArray) {
               for (var j = 0, len = purseDataArray.length; j < len; j++) {
                   // console.log("inAppPurchaseData[j]" + JSON.stringify(inAppPurchaseDataList[j]))
                   // console.log("obtainOwnedPurchases data.inAppSignature=" + data.inAppSignature)
                   // console.log("obtainOwnedPurchases publicKey" + publicKey)
                   var purchaseState = JSON.parse(purseDataArray[j]).purchaseState;
                   var purchaseToken = JSON.parse(purseDataArray[j]).purchaseToken;
                   console.log("purchaseState" + purchaseState);
                   console.log("purchaseToken" + purchaseToken);
                   //console.log(purchaseState)
                   // Verify the signature of product details before the delivery.
                  // You can send a verification request to the Huawei IAP server to further verify the order.
                  that.checkSign(purseDataArray[j], data.inAppSignature,JSON.parse(purseDataArray[j]).purchaseToken);
              }
          }
          if (data.continuationToken) {
            this.obtainOwnedPurchases(ptype, data.continuationToken);
          }
        },