最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

贝宝的NodeJS API

IT培训 admin 4浏览 0评论

贝宝的NodeJS API

所以我试图发送一个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: '',
 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什么是我做错了,因为贝宝内部错误是不明确的,我不能找出什么是完全错误

回答如下:

根据该文件,你需要从你的要求删除[]。你基本上是创建一个数组(products.map)里的数组([])。

尝试这个:

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

贝宝的NodeJS API

所以我试图发送一个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: '',
 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什么是我做错了,因为贝宝内部错误是不明确的,我不能找出什么是完全错误

回答如下:

根据该文件,你需要从你的要求删除[]。你基本上是创建一个数组(products.map)里的数组([])。

尝试这个:

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

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论