PayPal Pass项目SKU和订单参考ID
我正尝试使用Java脚本API Purchase_Units向PayPal发送订单,但当PayPal重定向到成功页面时,我收到错误未知Purchase_Units。当我在控制台中检查API调用时,我在带有Purchase_Units的调用旁边看到感叹号
以下是我的代码
paypal.Buttons({
env: 'sendbox',
style: {
layout: 'horizontal',
size: 'responsive',
shape: 'pill',
color: 'gold',
fundingicons: false,
tagline: false
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [ {
reference_id: "PUHF",
description: "Some description",
custom_id: "Something7364",
soft_descriptor: "Great description 1",
amount: {
currency_code: "USD",
value: "200.00",
breakdown: {
item_total: {
currency_code: "USD",
value: "200.00"
}
}
}, items: [{
name: "Item 1",
description: "The best item ever",
sku: "xyz-2654",
unit_amount: {
currency_code: "USD",
value: "100.00"
},
quantity: "1"
}, {
name: "Item 2",
description: "Not bad too",
sku: "zdc-3942",
unit_amount: {
currency_code: "USD",
value: "50.00"
}, quantity: "2"
}
],
}
]
})}, onApprove: function(data, actions) {
return fetch('<?= $rootPath.URL['services']['orderGet'] ?>', {
method: 'GET'
}
).then(function(res) {
return res.json();
}).then(function(res) {
window.location.href = 'pages/success.php';
});
}
}).render('#paypalCheckoutContainer');
解决方案
我将您的Purchase_Units数组复制到一个完整的有效Html示例中(下面,不要尝试在StackOverflow中运行它),并且没有任何问题。
查看代码的其余部分,env: sendbox
是一个打字错误。OnApprove部分给我带来了麻烦,我看到里面有PHP,但您的代码在删除整个onApprove部分时工作正常--所以也许可以尝试在没有该部分的情况下进行测试,然后修复它。
<!DOCTYPE html>
<!-- example from https://developer.paypal.com/demo/checkout/#/pattern/client -->
<head>
<!-- Add meta tags for mobile and IE -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
</head>
<body>
<!-- Set up a container element for the button -->
<div id="paypal-button-container"></div>
<!-- Include the PayPal JavaScript SDK -->
<script src="https://www.paypal.com/sdk/js?client-id=sb¤cy=USD"></script>
<script>
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Set up the transaction
createOrder: function(data, actions) {
return actions.order.create({
// You can find a working example purchase_units at https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/
purchase_units: [ {
reference_id: "PUHF",
description: "Some description",
custom_id: "Something7364",
soft_descriptor: "Great description 1",
amount: {
currency_code: "USD",
value: "200.00",
breakdown: {
item_total: {
currency_code: "USD",
value: "200.00"
}
}
}, items: [{
name: "Item 1",
description: "The best item ever",
sku: "xyz-2654",
unit_amount: {
currency_code: "USD",
value: "100.00"
},
quantity: "1"
}, {
name: "Item 2",
description: "Not bad too",
sku: "zdc-3942",
unit_amount: {
currency_code: "USD",
value: "50.00"
}, quantity: "2"
}
],
}
]
,
application_context: {
brand_name: "MyBusiness",
/*shipping_preference: 'NO_SHIPPING'*/
}
}/*end of parameters to actions.order.create*/);
},
// 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');
</script>
</body>
相关文章