Search code examples
javascriptangularjsnode.jsexpresspayu

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


I am trying to make a payment application with PayU and don't know how can I send JSON data to PayU server. How I can do that?? Please help me or give me some advice. I should pass the information (from body: {...} below) POST to https://secure.snd.payu.com/api/v2_1/orders

Data which I should send to 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})
})

controller

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

Solution

  • In order to make HTTP request to another server from NodeJS application you can use request module (or request-promise-native if you prefer promises). Code might look like this:

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