javascript - 使用 paypal-rest-sdk 时出错。请帮助我
问题描述
当我在下面的代码中使用应付金额为 1 美元时,它工作正常。 工作代码)
const express = require("express");
const router = express.Router();
const paypal = require("paypal-rest-sdk");
paypal.configure({
mode: "sandbox", //sandbox or live
client_id: process.env.ID,
client_secret: process.env.SECRET,
});
router.get("/success", (req, res) => {
const payerId = req.query.PayerID;
const paymentId = req.query.paymentId;
const execute_payment_json = {
payer_id: payerId,
transactions: [
{
amount: {
currency: "USD",
total: "1.00",
},
},
],
};
paypal.payment.execute(
paymentId,
execute_payment_json,
async (error, payment) => {
if (error) {
console.log(error.response);
throw error;
} else {
// await Cart.deleteMany({});
// req.flash("success", " order placed ");
res.send("success")
}
}
);
});
router.post("/pay", (req, res) => {
console.log("hii balajee.....")
const create_payment_json = {
intent: "sale",
payer: {
payment_method: "paypal",
},
redirect_urls: {
return_url:
process.env.RETURN_URL || "http://localhost:3000/success",
cancel_url:
process.env.CANCEL_URL || "http://localhost:3000/cancel",
},
transactions: [
{
item_list: {
items: [
{
name: "Red Sox Hat",
sku: "001",
price: "1.00",
currency: "USD",
quantity: 1,
},
],
},
amount: {
currency: "USD",
total: "1.00",
},
description: "This is the payment description.",
},
],
};
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);
}
}
}
});
});
router.get("/cancel", (req, res) => {
// req.flash("error", "payment cancelled try again");
// res.redirect("/");
res.send("done");
});
module.exports = router;
但是当应付金额不是硬编码时。我的意思是应付付款来自客户端,然后我收到错误):
{
name: 'VALIDATION_ERROR',
details: [
{
field: 'purchase_units[0]',
issue: 'Item amount must add up to specified amount subtotal (or total if amount details not specified)'
}
],
message: 'Invalid request - see details',
information_link: 'https://developer.paypal.com/docs/api/payments/#errors',
debug_id: 'c683329377177',
httpStatusCode: 400
}
(node:17812) UnhandledPromiseRejectionWarning: Error: Response Status : 400
当数据来自客户端时,我使用下面的代码。只是上面代码的一些修改):
const express = require("express");
const router = express.Router();
const paypal = require("paypal-rest-sdk");
var fees=0;
paypal.configure({
mode: "sandbox", //sandbox or live
client_id: process.env.ID,
client_secret: process.env.SECRET,
});
router.get("/success", (req, res) => {
const payerId = req.query.PayerID;
const paymentId = req.query.paymentId;
const execute_payment_json = {
payer_id: payerId,
transactions: [
{
amount: {
currency: "USD",
total: fees,
},
},
],
};
paypal.payment.execute(
paymentId,
execute_payment_json,
async (error, payment) => {
if (error) {
console.log(error.response);
throw error;
} else {
// await Cart.deleteMany({});
// req.flash("success", " order placed ");
res.send("success")
}
}
);
});
router.post("/pay", (req, res) => {
fees= parseInt(req.body.paided);
console.log(fees);
const create_payment_json = {
intent: "sale",
payer: {
payment_method: "paypal",
},
redirect_urls: {
return_url:
process.env.RETURN_URL || "http://localhost:3000/success",
cancel_url:
process.env.CANCEL_URL || "http://localhost:3000/cancel",
},
transactions: [
{
item_list: {
items: [
{
name: "Red Sox Hat",
sku: "001",
price: parseInt(req.body.paided),
currency: "USD",
quantity: 1,
},
],
},
amount: {
currency: "USD",
total: parseInt(req.body.paided),
},
description: "This is the payment description.",
},
],
};
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);
}
}
}
});
});
router.get("/cancel", (req, res) => {
// req.flash("error", "payment cancelled try again");
// res.redirect("/");
res.send("done");
});
module.exports = router;
我不知道这里出了什么问题。我完全迷路了。我尝试了很多但无法解决这个错误。请帮助我。谢谢你。
解决方案
您正在使用已弃用的 SDK,用于已弃用的 v1/payments REST API。
请改用 v2/checkout/orders API。有一个 Checkout-NodeJS-SDK 可用。
按照设置标准付款指南并在您的服务器上创建 2 条路线,一条用于“创建订单”,一条用于“捕获订单”,记录在此处。两个路由都应该只返回 JSON 数据(没有 HTML 或文本)。在第二条路线中,当捕获 API 成功时,您应该将其生成的付款详细信息存储在您的数据库中(特别是 PayPal 交易 ID)并在转发之前purchase_units[0].payments.captures[0].id
立即执行任何必要的业务逻辑(例如发送确认电子邮件或预订产品)您将 JSON 返回给前端调用者。
将这 2 条路由与前端审批流程配对:https ://developer.paypal.com/demo/checkout/#/pattern/server
此解决方案不使用任何重定向。不应从您的站点重定向到批准 URL。
推荐阅读
- c# - XmlSerializer:反序列化递归对象图
- angular - 动画在 Angular 项目中不起作用
- spring - 如何使用 Spring Framework 实现多点模式
- xamarin.forms - 无法在 iOS 上的 Xamarin UITest 中使用 ScrollDownTo() 向下滚动到 ScrollView 中不可见的控件
- node.js - 出现“无法在模块外使用 import 语句”错误并且不想升级 Node
- parsing - 解析类 lisp 语言中“Lex 组”的概念
- html - Flexbox flex 属性似乎只适用于 Firefox,不适用于 Chrome 或 Edge
- android - 即使在 Android 10 (API 29) 中卸载应用程序后,如何保持应用程序的存储数据仍然存在
- python - 如何在 R 中使用 Python 代码和包 reticulate 构建一个 lapply 样式的函数?
- java - 如何通过单击对话框[NOT DIALOG FRAGMENT](由fragment1调用)的按钮从fragment2替换fragment1