【问题标题】:Nodejs Paypal apiNodejs 支付宝 api
【发布时间】:2019-06-26 05:27:41
【问题描述】:

所以我正在尝试向 Paypal 发送 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 我在这里做错了什么因为 PayPal 内部错误不清楚我无法准确找出错误是什么

【问题讨论】:

  • 400 错误代码表示请求错误或语法无效。因此,很可能您的 JSON 有效负载格式不正确,您缺少字段,或者您发送的数据格式错误。如果您可以附上响应正文和请求正文,我们可能会提供更多帮助。
  • @Adam 我知道这意味着错误的请求,问题出在items,因为在那之前一切正常
  • 我不熟悉 paypal API。但是.map(...) 会返回一个数组。在您的情况下,您似乎将在数组中返回一个数组;所以也许删除你的products.map 方法周围的[]
  • 删除 products.map() 调用周围的[]
  • @Adam 它是数组中的一个数组,我从 Paypal 文档中发布了正确的方法

标签: javascript node.js paypal


【解决方案1】:

根据您需要从请求中删除[] 的文档。您实际上是在数组 ([]) 中创建一个数组 (products.map)。

试试这个:

"items": products.map((product) => {
        return {
          name: product.productname,
          sku: product._id,
          price: product.price,
          currency: "AED",
          quantity: product.quantity
        }
      })

【讨论】:

  • 是的,我刚刚注意到它,我删除了 [] 但仍然无法正常工作
【解决方案2】:

这是正确的请求:

    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});
        }
      }
  }
});

});

我需要将产品的总价与小计相匹配

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-08
    • 2020-07-19
    • 2010-11-04
    • 2014-04-26
    • 2011-05-06
    • 2015-03-31
    • 2013-01-24
    • 2016-02-25
    相关资源
    最近更新 更多