应用内购买阵列Chrome扩展程序出错

时间:2016-11-04 04:28:28

标签: javascript google-chrome google-chrome-extension in-app-purchase

在我的 buy.js 中,我首先检查用户之前是否购买了我的包裹。如果没有,我打电话给买方法开始购买。

我目前收到错误:

Error in event handler for (unknown): TypeError: Cannot read property 'details' of undefined
    at Object.onLicenseUpdate [as success] (buy.js:52:45)
    at buy.js:1:397

控制台中收到的数组的屏幕截图及错误:

**Screenshot of the array received in the console and the error.**

以下是有问题的代码:

function onLicenseUpdate(response) {
        console.log("onLicenseUpdate", response);
        var licenses = response.response.details;
        var count = licenses.length;
        for (var i = 0; i < count; i++) {
          var license = licenses[i];
          purchasedPackages = licenses[i].response.details.sku;
          if (purchasedPackages == skuInit) {
            purchased = true;
          }
          addLicenseDataToProduct(license);

        }
        if (purchased == false) {
          buyProduct(skuInit);
        }
        //console.log("");
      }

完成Buy.js代码:

/*...google chrome's code...*/

/*CODE FOR IN-APP PURCHASES*/


var prodButPrefix = "btnProdID-";
var statusDiv;

function init() {
  console.log("App Init");
  //statusDiv = $("#status");
  //  getProductList();

}

/*******************************
INITIATE PURCHASE
******************************/
$("#xmas").click(function() {
  checkIfPurchased("xmas");
  localStorage.purchasedXmas2016 = true;
});
/*******************************/



/********************************
Get purchased items
********************************/
var purchasedPackages;
var purchased = false;
function checkIfPurchased(sku){
  var skuInit = sku;
  getLicenses();
function getLicenses() {
  console.log("google.payments.inapp.getPurchases");
  console.log("Retreiving list of purchased products...");
  google.payments.inapp.getPurchases({
    'parameters': {env: "prod"},
    'success': onLicenseUpdate,
    'failure': onLicenseUpdateFailed
  });
}

function onLicenseUpdate(response) {
  console.log("onLicenseUpdate", response);
  var licenses = response.response.details;
  var count = licenses.length;
  for (var i = 0; i < count; i++) {
    var license = licenses[i];
    purchasedPackages = licenses[i].response.details.sku;
    if(purchasedPackages == skuInit){
      purchased = true;
    }
    addLicenseDataToProduct(license);

  }
  if (purchased == false){
    buyProduct(skuInit);
  }
  //console.log("");
}

function onLicenseUpdateFailed(response) {
  console.log("onLicenseUpdateFailed", response);
  console.log("Error retreiving list of purchased products.");
}
}



function getLicenses() {
  console.log("google.payments.inapp.getPurchases");
  console.log("Retreiving list of purchased products...");
  google.payments.inapp.getPurchases({
    'parameters': {env: "prod"},
    'success': onLicenseUpdate,
    'failure': onLicenseUpdateFailed
  });
}

function onLicenseUpdate(response) {
  console.log("onLicenseUpdate", response);
  var licenses = response.response.details;
  var count = licenses.length;
  for (var i = 0; i < count; i++) {
    var license = licenses[i];
    addLicenseDataToProduct(license);
  }
  //console.log("");
}

function onLicenseUpdateFailed(response) {
  console.log("onLicenseUpdateFailed", response);
  console.log("Error retreiving list of purchased products.");
}



/******************
* Purchase an item
****************/


function buyProduct(sku) {
  console.log("google.payments.inapp.buy", sku);
  //var sku = "";
  google.payments.inapp.buy({
    'parameters': {'env': 'prod'},
    'sku': sku,
    'success': onPurchase,
    'failure': onPurchaseFailed
  });
}


function onPurchase(purchase) {
  console.log("onPurchase", purchase);
  var jwt = purchase.jwt;
  var cartId = purchase.request.cardId;
  var orderId = purchase.response.orderId;
  console.log("Purchase completed. Order ID: " + orderId);
  getLicenses();
}

function onPurchaseFailed(purchase) {
  console.log("onPurchaseFailed", purchase);
  var reason = purchase.response.errorType;
  console.log("Purchase failed. " + reason);
}


/*******************************
Update UI Buttons
********************************/
if(localStorage.purchasedXmas2016 == true){
  var text = document.getElementById("xmas").firstChild;
  text.data = "purchased";
  document.getElementById("xmas").style = "border-style:none";
}

的manifest.json

{
  "manifest_version": 2,

  "name": "",
  "options_page": "options.html",

  "background": {
        "scripts": [
            "javascripts/background.js"
        ]
    },
  "description": "",
  "version": "4.0.4.1",
  "offline_enabled": true,
  "browser_action": {
    "default_icon": "icons/icon.png",
    "default_popup": "popups/popup1.html"
  },
  "content_security_policy": "script-src 'self' https://ajax.googleapis.com; object-src 'self'",

  "icons": { "16": "icons/icon16.png",
              "32": "icons/icon32.png",
           "48": "icons/icon48.png",
           "64": "icons/icon64.png",
          "128": "icons/icon128.png" },

  "permissions": [
    "activeTab",
    "https://ajax.googleapis.com/",
    "https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js",
    "storage",
    "tabs"
  ]

}

我也知道这不是最有效的方法。有没有办法可以将所有购买的SKU存储在一个阵列中(或任何其他有效的方式)?

1 个答案:

答案 0 :(得分:0)

details数组没有response属性。该数组已经是包含sku属性的对象列表。

要解码response,您需要onLicenseUpdate()功能更像:

function onLicenseUpdate(response) {
  console.log("onLicenseUpdate", response);
  var purchased = false;
  var licenses = response.response.details;
  var count = licenses.length;
  for (var i = 0; i < count; i++) {
    var license = licenses[i];
    purchasedPackages = license.sku;
    if(purchasedPackages == skuInit){
      purchased = true;
    }
    addLicenseDataToProduct(license);
  }
  if (purchased == false){
    buyProduct(skuInit);
  }
  //console.log("");
}

虽然,我会使用forEach代替for

function onLicenseUpdate(response) {
  console.log("onLicenseUpdate", response);
  var purchased = false;
  var licenses = response.response.details;
  licenses.forEach(license=>{
    purchasedPackages = license.sku;
    if(purchasedPackages == skuInit){
      purchased = true;
    }
    addLicenseDataToProduct(license);
  }
  if (purchased == false){
    buyProduct(skuInit);
  }
  //console.log("");
}