我正在尝试使用JavaScript按照手册实现PayPal结账的功能:
https://developer.paypal.com/docs/checkout/使用标准选项一切正常,例如这个也可以正常工作:
paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
currency_code: 'EUR',
value: '120.16'
},
description: 'Purchase Unit test description',
custom_id: '64735',
}]
})
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' +
details.payer.name.given_name)
// Call your server to save the transaction
return fetch('/api/paypal-transaction-complete', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
})
})
}
}).render('#paypal-button-container')
但是当我尝试更具体地描述订单细节时,它会给出一个错误:
Error: "Order Api response error:
{
"name": "INVALID_REQUEST",
"message": "Request is not well-formed, syntactically incorrect, or violates schema.",
"debug_id": "1ed03d18530c1",
"details": [
{
"location": "body",
"issue": "INVALID_SYNTAX",
"description": "Cannot deserialize instance of `com.paypal.api.platform.checkout.orders.v2.model.AmountBreakdown` out of START_ARRAY token line: 1, column: 82"
}
],
"links": [
{
"href": "https://developer.paypal.com/docs/api/orders/v2/#error-INVALID_SYNTAX", "rel": "information_link", "encType": "application/json"
}
]
}"
}
这是我的代码:paypal.Buttons({
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
currency_code: 'EUR',
value: '120.16',
breakdown: [{
item_total: {
unit_amount: 7,
currency_code: 'EUR',
value: '120.16'
}
}]
},
description: 'Purchase Unit test description',
custom_id: '64735',
items: [{
name: 'Test item 1',
unit_amount: {
currency_code: 'EUR',
value: '60.12'
},
quantity: 2,
description: 'Uaua item 1 description'
}, {
name: 'Test item 2',
unit_amount: {
currency_code: 'EUR',
value: '60.00'
},
quantity: 5,
description: 'Test item 2 description'
}]
}]
})
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
alert('Transaction completed by ' +
details.payer.name.given_name)
// Call your server to save the transaction
return fetch('/api/paypal-transaction-complete', {
method: 'post',
headers: {
'content-type': 'application/json'
},
body: JSON.stringify({
orderID: data.orderID
})
})
})
}
}).render('#paypal-button-container')
有人知道问题出在哪里吗?PayPal文件不够详细...
capture()
方法获得成功的then
response
,这笔交易就100%成功了,对吗?因此,我们可以自由地调用更多后端逻辑。您是否为相同的capture()
方法添加了任何catch方法? - Fer Toasted