我正在尝试使用javascript 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>