【发布时间】:2019-06-26 05:27:41
【问题描述】:
所以我正在尝试向 Paypal 发送 api 调用,根据 Paypal 文档,这是正确的方法:
"items": [
{
"name": "hat",
"description": "Brown hat.",
"quantity": "5",
"price": "3",
"tax": "0.01",
"sku": "1",
"currency": "USD"
},
{
"name": "handbag",
"description": "Black handbag.",
"quantity": "1",
"price": "15",
"tax": "0.02",
"sku": "product34",
"currency": "USD"
}
]
收到错误:
{ Error: Response Status : 400
at IncomingMessage.<anonymous> (C:\Users\Kadiem\node_modules\paypal-rest-
sdk\lib\client.js:130:23)
at IncomingMessage.emit (events.js:187:15)
at endReadableNT (_stream_readable.js:1094:12)
at process._tickCallback (internal/process/next_tick.js:63:19)
response:
{ name: 'VALIDATION_ERROR',
details: [ [Object] ],
message: 'Invalid request - see details',
information_link: 'https://developer.paypal.com/docs/api/payments/#errors',
debug_id: 'b2698c8d3e7a4',
httpStatusCode: 400 },
httpStatusCode: 400 }
这是来自 Nodejs 的发送请求:
router.post('/pay', (req, res) => {
orderid = req.body.orderid;
var products = JSON.parse(req.body.products);
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "",
"cancel_url": ""
},
"transactions": [{
"item_list": {
"items":
products.map((product) => {
return {
name: product.productname,
sku: product._id,
price: product.price,
currency: "USD",
quantity: product.quantity
}
})
},
"amount": {
"currency": "USD",
"total": "1.00"
},
"description": "Test"
}]
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
console.log(error);
} else {
for(let i = 0;i < payment.links.length;i++){
if(payment.links[i].rel === 'approval_url'){
console.log('Link sent', payment.links[i].href);
res.json({data: payment.links[i].href});
}
}
}
});
});
我收到错误 400 我在这里做错了什么因为 PayPal 内部错误不清楚我无法准确找出错误是什么
【问题讨论】:
-
400 错误代码表示请求错误或语法无效。因此,很可能您的 JSON 有效负载格式不正确,您缺少字段,或者您发送的数据格式错误。如果您可以附上响应正文和请求正文,我们可能会提供更多帮助。
-
@Adam 我知道这意味着错误的请求,问题出在
items,因为在那之前一切正常 -
我不熟悉 paypal API。但是
.map(...)会返回一个数组。在您的情况下,您似乎将在数组中返回一个数组;所以也许删除你的products.map方法周围的[]。 -
删除 products.map() 调用周围的
[] -
@Adam 它是数组中的一个数组,我从 Paypal 文档中发布了正确的方法
标签: javascript node.js paypal