[英]PayPal Smart buttons adding items description to button
嗨,我想为我的智能按钮添加描述,但我的 items 数组代码不起作用。 请参考这个问题: 为什么 PayPal 智能按钮无法识别我的项目数组?
我遇到了基本相同的问题,这个人的问题从未得到解决。 每当我将 function 作为 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;
};
从控制台中的 function 返回的数组如下所示。 (第一个由 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"}]
当您在代码中准确显示您在做什么或传递时,我们将能够给出更准确的答案,但您的 purchase_units 数组似乎无效。
它应该是一个包含单个(0 索引)项目的数组,并且该项目应该是一个 purchase_unit object 至少有两个键, amount
和items
。
amount
应该是一个 object 包含所需的breakdown
键和分解 object,并且items
应该是一个 item 对象的数组
调试代码:
createOrder: function(data, actions) {
var req = {
purchase_units: [{
amount: {
value: showTotals(),
currency_code: "USD",
breakdown: {
item_total: {
currency_code: "USD",
value: showTotals()
}
}
},
items: arrayOfItems()
}]
}
console.log(JSON.stringify(req,null,4));
return actions.order.create(req);
},
// 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 示例(在此处测试):
purchase_units: [{
amount: {
value: 259.96,
currency_code: "USD",
breakdown: {
item_total: {
currency_code: "USD",
value: 259.96
}
}
},
items: [{"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"}]
}]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.