functions-samples icon indicating copy to clipboard operation
functions-samples copied to clipboard

PayPal function CORS

Open ericwallen opened this issue 6 years ago • 0 comments

Hi, I am trying to get the example here to work. https://github.com/firebase/functions-samples/tree/master/paypal

When I make the POST request I get a CORS error though.

has been blocked by CORS policy: Request header field access-control-allow-origin is not allowed by Access-Control-Allow-Headers in preflight response.

If I add this code and install cors it doesn't respond at all. I kept seeing this as a suggestion while I was searching for a solution.

const cors = require('cors')({origin: true});

exports.pay = functions.https.onRequest((req, res) => {
  return cors(req, res, () => { 
       //  code
   });
});

If I add this code res.set('Access-Control-Allow-Origin', '*'); The error changes to

Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

If I add this code res.set('Access-Control-Allow-Origin', 'https://semester-at-sea.web.app'); The error changes to

net::ERR_ABORTED 500

This is what my fetch request looks like

 fetch('https://us-central1-semester-at-sea.cloudfunctions.net/pay', {
            method: 'POST',
            body: {price: 5},
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
            }
        })
            .then(response => {
                return response.json()
            })
            .then(data => {
                console.log(data);
            })
            .catch(message => {
                console.log(message);
            })

I tested the endpoint with Postman and it gets status 200 ok and redirects. Because of that, I am pretty sure this is strictly a CORS issue with the provided pay pal function. Take CORS out of the question using Postman and everything works fine.

When I check the Firebase function logs I see this:

{ Error: Response Status : 400
    at IncomingMessage.<anonymous> (/srv/node_modules/paypal-rest-sdk/lib/client.js:130:23)
    at emitNone (events.js:111:20)
    at IncomingMessage.emit (events.js:208:7)
    at endReadableNT (_stream_readable.js:1064:12)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)
  response: 
   { name: 'VALIDATION_ERROR',
     message: 'Invalid request - see details',
     debug_id: '2c2ef59c9994c',
     information_link: 'https://developer.paypal.com/docs/api/payments/#errors',
     details: [ [Object] ],
     httpStatusCode: 400 },
  httpStatusCode: 400 }

Any ideas on how I can fix this?

Thanks in advance!

ericwallen avatar Oct 18 '19 22:10 ericwallen