How to make a redirect in nodejs from front end browser app request

jwest2016
Contributor
Contributor

I have an app with nodejs as backend and angularjs as frontend. From my browser I post a request to node to make a paypal payment. Something as $htpp.post('/makepayment', cart). The problem is that paypal api, needs to call a "res.redirect" and It causes a CORS Origin problem, since I am make the request from my angular frontend app. I have CORS settings im my node server, but, off course, it will do no difference in respect to my problem because the redirect make a request to paypal. The error is: "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-YYYYYY: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access." How could I do it without have this CORS problem? In NodeJs I have this route with the code: router.post('/makepayment', (req, res) => { const create_payment_json = { "intent": "sale", "payer": { "payment_method": "paypal" }, "redirect_urls": { "return_url": "http://localhost/paypal/success", "cancel_url": "http://localhost/paypal/cancel" }, "transactions": [{ "item_list": { "items": [{ "name": "Red Sox Hat", "sku": "001", "price": "25.00", "currency": "BRL", "quantity": 1 }] }, "amount": { "currency": "BRL", "total": "25.00" }, "description": "Hat for the best team ever" }] }; paypal.payment.create(create_payment_json, function (error, payment) { if (error) { throw error; } else { for(let i = 0;i < payment.links.length;i++){ if(payment.links[i].rel === 'approval_url'){ res.redirect(payment.links[i].href); } } } });

Login to Me Too
1 REPLY 1

jwest2016
Contributor
Contributor

Sorry, I´d like to edit my post to a better layout, but I can´t  do it here.

Login to Me Too

Haven't Found your Answer?

It happens. Hit the "Login to Ask the community" button to create a question for the PayPal community.