node.js - 尝试通过一笔交易购买多件商品时出错
问题描述
我已经让 PayPal API 与一个项目一起工作,我现在正试图让它与整个“购物车”一起工作。我遇到了一个我不知道如何解决的错误。我怀疑它可能与代表整个交易总成本的 payment-jsons 总值有关。但是我不知道该怎么做。
这是错误:
Error: Response Status : 400
at IncomingMessage.<anonymous> (E:\Users\willi\Documents\Node\Store\node_modules\paypal-rest-sdk\lib\client.js:130:23)
at IncomingMessage.emit (events.js:327:22)
at endReadableNT (internal/streams/readable.js:1327:12)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
response: {
name: 'MALFORMED_REQUEST',
message: 'Incoming JSON request does not map to API request',
information_link: 'https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST',
debug_id: '9e8898a463ee3',
httpStatusCode: 400
},
httpStatusCode: 400
}
这是有问题的代码
const pay = (req, res) => {
async function f() {
items = [];
req_items = req.body.body
let itemsProcessed = 0
req_items.forEach(item => {
console.log(item.id)
const param = item.id
Item.find({ _id: param })
.then((result) => {
const item_body = {
"name": result[0].title,
"sku": "001",
"price": parseFloat(result[0].price),
"currency": "EUR",
"quantity": item.amount
}
items.push(item_body)
itemsProcessed = itemsProcessed + 1
})
.catch((err) => {
console.log(err)
})
})
let promise = new Promise((resolve, reject) => {
setTimeout(() => resolve("done!"), 1000)
});
let result = await promise; // wait until the promise resolves (*)
console.log(items)
const create_payment_json = {
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://localhost:3000/success",
"cancel_url": "http://localhost:3000/cancel"
},
"transactions": [{
"item_list": {
"items": [items]
},
"amount": {
"currency": "EUR",
"total": parseFloat(req.body.subtotal) // 25
},
"description": "Purcahsed from the Store"
}]
};
// console.log(req.body)
// console.log(create_payment_json.transactions[0])
paypal.payment.create(create_payment_json, function (error, payment) {
if (error) {
throw error;
} else {
for(let i = 0;i < payment.links.length;i++){
if(payment.links[i].rel === 'approval_url'){
res.redirect(payment.links[i].href);
}
}
}
});
}
f();
}
解决方案
API 弃用通知
您正在集成已弃用的 v1/payments PayPal API。您不应该为新的集成这样做;当前的 API 是v2/checkout/orders
,记录在这里。
通常,您需要在自己的服务器上创建两个路由,“Create Order”和“Capture Order”,它们在调用时会返回它们自己的 JSON。然后,您可以将这两条路线与以下批准流程配对:https ://developer.paypal.com/demo/checkout/#/pattern/server
但是对于您的问题,如果您只是记录您的请求 JSON 以查看问题所在,那么调试这样的问题要简单得多。
如果这样做,您将看到您发送的“items”数组在一个数组中包含一个数组,该数组仅包含一个项(另一个数组)。那个数组不应该在那里。
这似乎是罪魁祸首:
"items": [items]
在这里,您决定创建一个数组,这在“项目”是单个项目(无数组)时很有用。但是当 items 已经是一个数组时,您不应该将该数组放入一个新数组中——生成的 JSON 不会映射到 API 请求,并且 PayPal 将返回错误。
你应该做的是去掉那些括号并确保在代码执行的这一点上,“items”已经是一个数组(如果之前不是的话)。
推荐阅读
- c++ - 具有交替位的二进制数,错误答案
- ios - 在 UIKit 中实现水平滚动视图?
- r - 使用 github 操作更新包数据
- python - 尝试从 python 中的已解析文本代码创建两个 CSV 文件
- python - 是否可以在 dict 中获得 dict 的选择性组合?
- amazon-web-services - 为 VPC 内的 AWS ElasticSearch 域创建一个公共终端节点
- python - 如何使用 TF-IDF 计算两个对应行之间的余弦相似度
- go - 如何在 golang 应用程序中重新启动 heroku 服务器?
- html - 使用 flex 和 css 网格制作响应式布局
- python - OpenCV 无法连接到 X 服务器