所以我试图发送一个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什么是我做错了,因为贝宝内部错误是不明确的,我不能找出什么是完全错误
根据该文件,你需要从你的要求删除[]
。你基本上是创建一个数组(products.map
)里的数组([]
)。
尝试这个:
"items": products.map((product) => {
return {
name: product.productname,
sku: product._id,
price: product.price,
currency: "AED",
quantity: product.quantity
}
})
这是正确的请求:
router.post('/pay', (req, res) => {
orderid = req.body.orderid;
const products = JSON.parse(req.body.products);
currency = req.body.currency;
total = req.body.total;
subtotal = req.body.subtotal;
shipping = req.body.shipping;
tax = req.body.tax;
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": total,
"details": {
"subtotal": subtotal,
"tax": tax,
"shipping": shipping,
}
},
"description": "Thank you for shopping from us.",
"invoice_number": orderid,
}]
};
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
console.log(JSON.stringify(error));
} else {
for(let i = 0;i < payment.links.length;i++){
if(payment.links[i].rel === 'approval_url'){
res.json({data: payment.links[i].href});
}
}
}
});
});
我需要的产品的总价格相匹配的小计