firebase / firebase/functions-samples

PayPal function CORS

Open
#630 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
12.2k
Forks
3.8k
Avg merge
3d 23h
Merged PRs (30d)
1

Description

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. (/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!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.