使用 paypal-rest-sdk 时出错。请帮助我
Posted
技术标签:
【中文标题】使用 paypal-rest-sdk 时出错。请帮助我【英文标题】:Getting error while using paypal-rest-sdk.Please help me 【发布时间】:2021-12-26 06:21:43 【问题描述】:当我在下面的代码中使用应付金额为 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;
我不知道这里出了什么问题。我完全迷失了。我尝试了很多,但无法解决这个错误。请帮助我。 谢谢。
【问题讨论】:
【参考方案1】:您正在使用已弃用的 SDK,用于已弃用的 v1/payments REST API。
改用 v2/checkout/orders API。有一个 Checkout-NodeJS-SDK 可用。
按照Set up standard payments 指南在您的服务器上创建两条路线,一条用于“创建订单”,一条用于“捕获订单”documented here。两个路由都应该只返回 JSON 数据(没有 html 或文本)。在第二条路线中,当捕获 API 成功时,您应该将其生成的付款详细信息存储在您的数据库中(特别是 purchase_units[0].payments.captures[0].id
,这是 PayPal 交易 ID)并执行任何必要的业务逻辑(例如发送确认电子邮件或预订产品)立即在将您的返回 JSON 转发给前端调用者。
将这 2 条路由与前端审批流程配对:https://developer.paypal.com/demo/checkout/#/pattern/server
此解决方案不使用任何重定向。不应从您的网站重定向到批准 URL。
【讨论】:
以上是关于使用 paypal-rest-sdk 时出错。请帮助我的主要内容,如果未能解决你的问题,请参考以下文章
react 和 paypal-rest-sdk:如何将数据从支付页面传回应用程序