gpt4 book ai didi

javascript - Nodejs Paypal api

转载 作者:太空宇宙 更新时间:2023-11-03 16:04:54 25 4
gpt4 key购买 nike

所以我正在尝试向 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 内部错误不明确我无法找出到底是什么错误

最佳答案

根据文档,您需要从请求中删除 []。您实质上是在数组 ([]) 中创建一个数组 (products.map)。

试试这个:

"items": products.map((product) => {
return {
name: product.productname,
sku: product._id,
price: product.price,
currency: "AED",
quantity: product.quantity
}
})

关于javascript - Nodejs Paypal api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54482834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com