使用 express 和 angular 将 JSON 数据发送到另一台服务器 (PayU)

Send JSON data to another server (PayU) using express and angular

我正在尝试使用 PayU 进行付款申请,但不知道如何将 JSON 数据发送到 PayU 服务器。我该怎么做??请帮助我或给我一些建议。我应该将信息(从下面的body: {...})POST传递给https://secure.snd.payu.com/api/v2_1/orders

我应该发送给 PayU 的数据 (body: {...})

userFactory.paypalPayment = function(payment) {
  return $http({
    method: 'POST',
    url: "/paynow",
    headers: {
        'Content-Type': 'application/json'
    },
    body: {  
        "notifyUrl": "https://your.eshop.com/notify",  
        "customerIp": "127.0.0.1",  
        "merchantPosId": "145227",  
        "description": "Toyota",  
        "currencyCode": "USD",  
        "totalAmount": "12",  
        "products":{      
            "name": "Wireless mouse",
            "unitPrice": "15000",      
            "quantity": "1"   
        },
     }
  });
}

return userFactory

app.js (ExpressJS)

router.post('/paynow', function(req, res){
    res.setHeader('Content-type', 'application/json; charset=utf-8');
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type');
    res.setHeader('Access-Control-Allow-Credentials', 'true');

    res.json({ success: true})
})

控制器

app.payment = function(payment){
    User.paypalPayment().then(function(data){
        console.log(data.data)
        if(data.data.success) {
            $window.location = 'https://secure.snd.payu.com/api/v2_1/orders'
        } else {
            console.log('Wrong way')
        } 
    })       
}

为了从 NodeJS 应用程序向另一台服务器发出 HTTP 请求,如果您更喜欢 promises,您可以使用 request module (or request-promise-native。代码可能如下所示:

router.post('/paynow', function(req, res){
    // your code here
    request({
        method: 'POST',
        json: { body: req.body },
        uri: 'https://secure.snd.payu.com/api/v2_1/orders',
        headers: { "Content-Type": "application/json" },
        (err, response, body) => {
          // Callback - you can check response.statusCode here or get body of the response.
          // Now you can send response to user.
        }
    });
});