【发布时间】:2021-03-23 21:44:09
【问题描述】:
您好,我想为我的智能按钮添加说明,但我的 items 数组代码不起作用。 请参考这个问题:Why will PayPal Smart buttons not recognise my items array?
我遇到了基本相同的问题,这个人的问题从未得到解决。 每当我将我的函数用于 Items 值时,它都会返回一个非常长的错误。我只是不确定我做错了什么。
function arrayOfItems(){
var arrayItems = []
var items = document.querySelectorAll(".cp-info");
items.forEach(function(item){
// console.log(item);
// console.log(item.children[1].children[1].children[1].textContent)
let currency = item.children[1].children[1].children[1].textContent;
let quantity = "1";
//console.log(currency);
//console.log(item.children[0].textContent);
let name = item.children[0].textContent;
let size = item.children[1].children[0].textContent;
//console.log(size);
name = name;
//console.log(name);
var itemInfo = {"unit_amount": {"currency_code": "USD", "value": currency},"quantity": "1", "name": name,};
//console.log(arrayItems);
arrayItems.push(itemInfo);
//console.log(arrayItems);
})
return arrayItems;
};
从控制台中的函数返回的数组如下所示。 (第一个 0 只是分解
(4) [{…}, {…}, {…}, {…}]
0:
name: "GB Stacked"
quantity: "1"
unit_amount: {currency_code: "USD", value: "49.99"}
__proto__: Object
1: {unit_amount: {…}, quantity: "1", name: "Stacked Jeans"}
2: {unit_amount: {…}, quantity: "1", name: "GB Stacked"}
3: {unit_amount: {…}, quantity: "1", name: "Blue Stacked Leggings"}
length: 4
__proto__: Array(0)
问题更新
这是我在 PayPal 方面传递的内容
createOrder: function(data, actions) {
return actions.order.create({
"purchase_units": [{
"amount": {
"value": showTotals(),
"currency_code": "USD",
"breakdown": {
"item_total": {
"currency_code": "USD",
"value": showTotals(),
},
},
"items": arrayOfItems()
}
}],
});
},
// Finalize the transaction
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
// Show a success message to the buyer
alert('Transaction completed by ' + details.payer.name.given_name + '!');
});
}
}).render('#paypal-button-container');
我不确定我做了什么更改,但它不再给出控制台错误并允许购买但订单详细信息仍然为空。
我传入的 JSON 字符串
var x = arrayItems;
console.log(JSON.stringify(x,4));
return arrayItems;
---------------------------
this returns to the console =
[{"unit_amount":{"currency_code":"USD","value":"49.99"},"quantity":"1","name":"GB Stacked"},{"unit_amount":{"currency_code":"USD","value":"79.99"},"quantity":"1","name":"Stacked Jeans"},{"unit_amount":{"currency_code":"USD","value":"49.99"},"quantity":"1","name":"GB Stacked"},{"unit_amount":{"currency_code":"USD","value":"79.99"},"quantity":"1","name":"Stacked Jeans"}]
this is a picture of the PayPal transaction screen, as you can see the order details is blank
【问题讨论】:
-
用你传递给 actions.order.create( 的实际 JSON 更新你的问题。用你传递的内容设置一个变量,然后 console.log(JSON.stringify(myVariable,null,4) )
标签: javascript paypal