【问题标题】:Paypal rest sdk 400 error (node.js) Something about VALIDATION ERROR (The error is written in description)Paypal rest sdk 400 error (node.js) 关于 VALIDATION ERROR 的事情(错误写在描述中)
【发布时间】:2022-01-09 03:08:29
【问题描述】:

这是我得到的错误

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: '74ac8660674d8',
  httpStatusCode: 400

这是我的 /pay 发帖路线

app.post('/pay' , (req , res) => {

    let cart = new Cart(req.session.cart)
    
    
    const create_payment_json = {
    "intent": "sale",
    "payer": {
        "payment_method": "paypal"
    },
    "redirect_urls": {
        "return_url": "http://localhost:3000/success",
        "cancel_url": "http://localhost:3000/failed"
    },
    "transactions": [{
        "item_list": {
            "items": getItems(cart.items)
        },
        "amount": {
            "currency": "USD",
            "total": cart.totalPrice
        },
        "description": "This is the payment description."
    }]
};

这是 getItems 函数

function getItems(cart) {
    let itemsArray = [];
    for (const [idx, item] of Object.entries(cart)) {
      itemsArray.push({
                "name": item.item.product_name, 
                "price": item.price,
                "currency": "USD",
                "quantity": item.qty
            });
    }
    return itemsArray;

}

当我控制台记录 cart.totalPrice 时,它​​应该是正确的(这是购物车中所有物品的总和)知道这里出了什么问题。我对所有这些东西有点陌生,所以有点困惑

【问题讨论】:

  • 记录您的实际请求 JSON,因为这将显示问题所在。缺少某些内容或未添加内容,例如金额总计字段。您正在使用已弃用的 API
  • 您能否提供一个未弃用的 API 链接?我不知道在哪里可以找到,我为那个 API 使用了一个 youtube 视频@PrestonPHX

标签: node.js paypal paypal-sandbox


【解决方案1】:

对于当前(未弃用)的 API,请参阅 Set up standard payments 中的示例

在您的服务器上创建 2 条路由,一条用于“创建订单”,一条用于“捕获订单”,documented here。两个路由都应该只返回 JSON 数据(没有 HTML 或文本)。在第二条路线中,当捕获 API 成功时,您应该将其生成的付款详细信息存储在您的数据库中(特别是 purchase_units[0].payments.captures[0].id,这是 PayPal 交易 ID)并执行任何必要的业务逻辑(例如发送确认电子邮件或预订产品)立即将您的返回 JSON 转发给前端调用者。

将这 2 条路由与当前的 JS SDK 前端审批流程配对:https://developer.paypal.com/demo/checkout/#/pattern/server

【讨论】:

    猜你喜欢
    • 2014-02-17
    • 2014-11-23
    • 1970-01-01
    • 2014-12-04
    • 2013-03-12
    • 2020-01-16
    • 1970-01-01
    • 2017-09-26
    • 2013-11-12
    相关资源
    最近更新 更多