[英]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.