传递 PayPal Buttons 项目对象
Passing PayPal Buttons items object
我有点头疼,我想开一家商店,以三种尺寸出售一种东西,然后将这些物品传递给 paypal 付款:
let itemsArray = [];
let pplrg = parseInt(document.getElementById('lrg').value);
let ppsml = parseInt(document.getElementById('sml').value);
let ppcld = parseInt(document.getElementById('cld').value);
let ppvalue = 0;
if(pplrg > 0) {
itemsArray.push({"name": "Adult Large", "description": "Adult Large size", "unit_amount": { "currency_code": "AUD", "value": "25.95"}, "quantity": pplrg.toString()});
ppvalue+= pplrg * 25.95
}
if(ppsml > 0) {
itemsArray.push({"name": "Adult Small", "description": "Adult Small size", "unit_amount": { "currency_code": "AUD", "value": "25.95"}, "quantity": ppsml.toString()});
ppvalue+= ppsml * 25.95
}
if(ppcld > 0) {
itemsArray.push({"name": "Child", "description": "Child size", "unit_amount": { "currency_code": "AUD", "value": "24.95"}, "quantity": ppcld.toString()});
ppvalue+= ppsml * 24.95
}
console.log(JSON.stringify(itemsArray));
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
"purchase_units": [{
"amount": {
"currency_code": "AUD",
"value": ppvalue.toFixed(2),
"breakdown": {
"item_total": {
"currency_code": "AUD",
"value": ppvalue.toFixed(2)
}
}
},
"items": JSON.stringify(itemsArray)
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For dev/demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
let transaction = orderData.purchase_units[0].payments.captures[0];
let email = transaction.payer[0].email_address;
console.log(email);
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
// When ready to go live, remove the alert and show a success message within this page. For example:
var element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Thank you for your payment!</h3>';
element.innerHTML+= '<p>Paypal should be sending you an receipt with details of your order to '+email+'.</p>';
});
}
}).render('#paypal-button-container');
这不起作用,但是,如您所见,我 console.log
JSON.stringify(itemsArray)
的内容,如果我将 paypal.Buttons 中的 JSON.stringify(itemsArray)
替换为 paypal.Buttons 的内容控制台,它工作正常。即:
let itemsArray = [];
let pplrg = parseInt(document.getElementById('lrg').value);
let ppsml = parseInt(document.getElementById('sml').value);
let ppcld = parseInt(document.getElementById('cld').value);
let ppvalue = 0;
if(pplrg > 0) {
itemsArray.push({"name": "Adult Large", "description": "Adult Large size", "unit_amount": { "currency_code": "AUD", "value": "25.95"}, "quantity": pplrg.toString()});
ppvalue+= pplrg * 25.95
}
if(ppsml > 0) {
itemsArray.push({"name": "Adult Small", "description": "Adult Small size", "unit_amount": { "currency_code": "AUD", "value": "25.95"}, "quantity": ppsml.toString()});
ppvalue+= ppsml * 25.95
}
if(ppcld > 0) {
itemsArray.push({"name": "Child", "description": "Child size", "unit_amount": { "currency_code": "AUD", "value": "24.95"}, "quantity": ppcld.toString()});
ppvalue+= ppsml * 24.95
}
console.log(JSON.stringify(itemsArray));
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
"purchase_units": [{
"amount": {
"currency_code": "AUD",
"value": ppvalue.toFixed(2),
"breakdown": {
"item_total": {
"currency_code": "AUD",
"value": ppvalue.toFixed(2)
}
}
},
"items": [{"name":"'Adult Large","description":"Adult Large size","unit_amount":{"currency_code":"AUD","value":"25.95"},"quantity":"1"},{"name":"Adult Small","description":"Adult Small size","unit_amount":{"currency_code":"AUD","value":"25.95"},"quantity":"1"}]
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For dev/demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
let transaction = orderData.purchase_units[0].payments.captures[0];
let email = transaction.payer[0].email_address;
console.log(email);
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
// When ready to go live, remove the alert and show a success message within this page. For example:
var element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Thank you for your payment!</h3>';
element.innerHTML+= '<p>Paypal should be sending you an receipt with details of your order to '+email+'.</p>';
});
}
}).render('#paypal-button-container');
这才是真正让我感到困惑的地方,显然我已经正确构建了我的 itemsArray
并且 JSON.stringify(itemsArray)
以 PayPal 期望的格式呈现文本,但是,它不起作用,有人吗有更多经验的人可以告诉我我做错了什么吗?
我已经对这个问题进行了相当多的搜索,我知道有一些关于这个的问题,但它们似乎都与我遇到的问题有点不同。
简单
"items": itemsArray
没有理由对将要分配给对象中的字符串值的内容进行字符串化,该对象稍后将再次进行字符串化以进行传输。
我有点头疼,我想开一家商店,以三种尺寸出售一种东西,然后将这些物品传递给 paypal 付款:
let itemsArray = [];
let pplrg = parseInt(document.getElementById('lrg').value);
let ppsml = parseInt(document.getElementById('sml').value);
let ppcld = parseInt(document.getElementById('cld').value);
let ppvalue = 0;
if(pplrg > 0) {
itemsArray.push({"name": "Adult Large", "description": "Adult Large size", "unit_amount": { "currency_code": "AUD", "value": "25.95"}, "quantity": pplrg.toString()});
ppvalue+= pplrg * 25.95
}
if(ppsml > 0) {
itemsArray.push({"name": "Adult Small", "description": "Adult Small size", "unit_amount": { "currency_code": "AUD", "value": "25.95"}, "quantity": ppsml.toString()});
ppvalue+= ppsml * 25.95
}
if(ppcld > 0) {
itemsArray.push({"name": "Child", "description": "Child size", "unit_amount": { "currency_code": "AUD", "value": "24.95"}, "quantity": ppcld.toString()});
ppvalue+= ppsml * 24.95
}
console.log(JSON.stringify(itemsArray));
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
"purchase_units": [{
"amount": {
"currency_code": "AUD",
"value": ppvalue.toFixed(2),
"breakdown": {
"item_total": {
"currency_code": "AUD",
"value": ppvalue.toFixed(2)
}
}
},
"items": JSON.stringify(itemsArray)
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For dev/demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
let transaction = orderData.purchase_units[0].payments.captures[0];
let email = transaction.payer[0].email_address;
console.log(email);
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
// When ready to go live, remove the alert and show a success message within this page. For example:
var element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Thank you for your payment!</h3>';
element.innerHTML+= '<p>Paypal should be sending you an receipt with details of your order to '+email+'.</p>';
});
}
}).render('#paypal-button-container');
这不起作用,但是,如您所见,我 console.log
JSON.stringify(itemsArray)
的内容,如果我将 paypal.Buttons 中的 JSON.stringify(itemsArray)
替换为 paypal.Buttons 的内容控制台,它工作正常。即:
let itemsArray = [];
let pplrg = parseInt(document.getElementById('lrg').value);
let ppsml = parseInt(document.getElementById('sml').value);
let ppcld = parseInt(document.getElementById('cld').value);
let ppvalue = 0;
if(pplrg > 0) {
itemsArray.push({"name": "Adult Large", "description": "Adult Large size", "unit_amount": { "currency_code": "AUD", "value": "25.95"}, "quantity": pplrg.toString()});
ppvalue+= pplrg * 25.95
}
if(ppsml > 0) {
itemsArray.push({"name": "Adult Small", "description": "Adult Small size", "unit_amount": { "currency_code": "AUD", "value": "25.95"}, "quantity": ppsml.toString()});
ppvalue+= ppsml * 25.95
}
if(ppcld > 0) {
itemsArray.push({"name": "Child", "description": "Child size", "unit_amount": { "currency_code": "AUD", "value": "24.95"}, "quantity": ppcld.toString()});
ppvalue+= ppsml * 24.95
}
console.log(JSON.stringify(itemsArray));
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
"purchase_units": [{
"amount": {
"currency_code": "AUD",
"value": ppvalue.toFixed(2),
"breakdown": {
"item_total": {
"currency_code": "AUD",
"value": ppvalue.toFixed(2)
}
}
},
"items": [{"name":"'Adult Large","description":"Adult Large size","unit_amount":{"currency_code":"AUD","value":"25.95"},"quantity":"1"},{"name":"Adult Small","description":"Adult Small size","unit_amount":{"currency_code":"AUD","value":"25.95"},"quantity":"1"}]
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(orderData) {
// Successful capture! For dev/demo purposes:
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
let transaction = orderData.purchase_units[0].payments.captures[0];
let email = transaction.payer[0].email_address;
console.log(email);
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
// When ready to go live, remove the alert and show a success message within this page. For example:
var element = document.getElementById('paypal-button-container');
element.innerHTML = '';
element.innerHTML = '<h3>Thank you for your payment!</h3>';
element.innerHTML+= '<p>Paypal should be sending you an receipt with details of your order to '+email+'.</p>';
});
}
}).render('#paypal-button-container');
这才是真正让我感到困惑的地方,显然我已经正确构建了我的 itemsArray
并且 JSON.stringify(itemsArray)
以 PayPal 期望的格式呈现文本,但是,它不起作用,有人吗有更多经验的人可以告诉我我做错了什么吗?
我已经对这个问题进行了相当多的搜索,我知道有一些关于这个的问题,但它们似乎都与我遇到的问题有点不同。
简单
"items": itemsArray
没有理由对将要分配给对象中的字符串值的内容进行字符串化,该对象稍后将再次进行字符串化以进行传输。