How to solve a CORS issue with paypal-rest-sdk on react/express local development setup?












1















I am trying to set up the paypal-rest-sdk in an express server with a react front end, am getting a CORS error. The react server is using a proxy.



I have tried the npm cor() package (couldn't get it to work) and two chrome extension. They stop the error message but nothing else happens either. I don't understand how to use the proper headers...but it seems like one of the other methods should work.



The desired result is to be sent to a paypal pay page.



This code works:



const express = require("express");

const app = express();

app.get("/", (req, res) => {});

app.post("/pay", (req, res) => {
console.log("you made a successful post request"
});

const port = 5000;

app.listen(port, () => `Server running on port ${port}`);


this code works, it logs the payment json object:



const express = require("express");
const paypal = require("paypal-rest-sdk");

const app = express();

app.get("/", (req, res) => {});

paypal.configure({
mode: "sandbox",
client_id:
"___clientID___",
client_secret:
"___clientSECRET___"
});

app.post("/pay", (req, res) => {
var create_payment_json = {
intent: "sale",
payer: {
payment_method: "paypal"
},
redirect_urls: {
return_url: "http://localhost:3000/success",
cancel_url: "http://localhost:3000/cancel"
},
transactions: [
{
item_list: {
items: [
{
name: "item",
sku: "item",
price: "1.00",
currency: "USD",
quantity: 1
}
]
},
amount: {
currency: "USD",
total: "1.00"
},
description: "This is the payment description."
}
]
};

paypal.payment.create(create_payment_json, function(error, payment) {
if (error) {
throw error;
} else {
console.log(payment)
}
}
}
});
});

const port = 5000;

app.listen(port, () => `Server running on port ${port}`);


But this code breaks it with a CORS error:



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


I assume that the res.redirect is the problem.



EDIT
I recreated the same express server in a separate project folder, but instead of trying to use it with a React proxy, I created a very simple HTML form to hit '/pay'



This worked, which tells me that the problem is in having react run on localhost:3000 with a proxy to a server that is listening on localhost:5000



SO, the problem is not with PayPal, I can rule that out.



My next step is to learn more about integrating React apps into express...



Any other ideas would be very appreciated!
Thanks!!!










share|improve this question

























  • What’s the exact CORS error message the browser is logging? What’s the URL of the endpoint?

    – sideshowbarker
    Nov 21 '18 at 10:48











  • The docs at developer.paypal.com/docs/api/rest-sdks give no indication that PayPal API endpoints are CORS-enabled nor that any of the REST APIs are meant to be used from frontend JavaScript running in a browser. Instead the docs say, “PayPal provides REST server SDKs for these languages: Java, .NET, Node, PHP, Python, Ruby”.

    – sideshowbarker
    Nov 21 '18 at 10:53











  • Thank you for your comment! I believe that the PayPal endpoint is referenced by for (let i = 0; i < payment.links.length; i++) { if (payment.links[i].rel === "approval_url") { res.redirect(payment.links[i].href); In my sandbox, the href is sandbox.paypal.com/cgi-bin/…... I recreated the code in a more simple fashion, omitting the react side of things, and used a simple HTML form, it worked. This tells me that the problem is likely from the react proxy. I will add this to my original question.

    – Hanley Soilsmith
    Nov 21 '18 at 18:45











  • IE, this tells me that the problem is not with PayPal. I think you already know this, but I guess the problem is that I don't completely understand all of this stuff and am definitely making some noob mistakes. Thanks for your help :)

    – Hanley Soilsmith
    Nov 21 '18 at 18:53
















1















I am trying to set up the paypal-rest-sdk in an express server with a react front end, am getting a CORS error. The react server is using a proxy.



I have tried the npm cor() package (couldn't get it to work) and two chrome extension. They stop the error message but nothing else happens either. I don't understand how to use the proper headers...but it seems like one of the other methods should work.



The desired result is to be sent to a paypal pay page.



This code works:



const express = require("express");

const app = express();

app.get("/", (req, res) => {});

app.post("/pay", (req, res) => {
console.log("you made a successful post request"
});

const port = 5000;

app.listen(port, () => `Server running on port ${port}`);


this code works, it logs the payment json object:



const express = require("express");
const paypal = require("paypal-rest-sdk");

const app = express();

app.get("/", (req, res) => {});

paypal.configure({
mode: "sandbox",
client_id:
"___clientID___",
client_secret:
"___clientSECRET___"
});

app.post("/pay", (req, res) => {
var create_payment_json = {
intent: "sale",
payer: {
payment_method: "paypal"
},
redirect_urls: {
return_url: "http://localhost:3000/success",
cancel_url: "http://localhost:3000/cancel"
},
transactions: [
{
item_list: {
items: [
{
name: "item",
sku: "item",
price: "1.00",
currency: "USD",
quantity: 1
}
]
},
amount: {
currency: "USD",
total: "1.00"
},
description: "This is the payment description."
}
]
};

paypal.payment.create(create_payment_json, function(error, payment) {
if (error) {
throw error;
} else {
console.log(payment)
}
}
}
});
});

const port = 5000;

app.listen(port, () => `Server running on port ${port}`);


But this code breaks it with a CORS error:



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


I assume that the res.redirect is the problem.



EDIT
I recreated the same express server in a separate project folder, but instead of trying to use it with a React proxy, I created a very simple HTML form to hit '/pay'



This worked, which tells me that the problem is in having react run on localhost:3000 with a proxy to a server that is listening on localhost:5000



SO, the problem is not with PayPal, I can rule that out.



My next step is to learn more about integrating React apps into express...



Any other ideas would be very appreciated!
Thanks!!!










share|improve this question

























  • What’s the exact CORS error message the browser is logging? What’s the URL of the endpoint?

    – sideshowbarker
    Nov 21 '18 at 10:48











  • The docs at developer.paypal.com/docs/api/rest-sdks give no indication that PayPal API endpoints are CORS-enabled nor that any of the REST APIs are meant to be used from frontend JavaScript running in a browser. Instead the docs say, “PayPal provides REST server SDKs for these languages: Java, .NET, Node, PHP, Python, Ruby”.

    – sideshowbarker
    Nov 21 '18 at 10:53











  • Thank you for your comment! I believe that the PayPal endpoint is referenced by for (let i = 0; i < payment.links.length; i++) { if (payment.links[i].rel === "approval_url") { res.redirect(payment.links[i].href); In my sandbox, the href is sandbox.paypal.com/cgi-bin/…... I recreated the code in a more simple fashion, omitting the react side of things, and used a simple HTML form, it worked. This tells me that the problem is likely from the react proxy. I will add this to my original question.

    – Hanley Soilsmith
    Nov 21 '18 at 18:45











  • IE, this tells me that the problem is not with PayPal. I think you already know this, but I guess the problem is that I don't completely understand all of this stuff and am definitely making some noob mistakes. Thanks for your help :)

    – Hanley Soilsmith
    Nov 21 '18 at 18:53














1












1








1


0






I am trying to set up the paypal-rest-sdk in an express server with a react front end, am getting a CORS error. The react server is using a proxy.



I have tried the npm cor() package (couldn't get it to work) and two chrome extension. They stop the error message but nothing else happens either. I don't understand how to use the proper headers...but it seems like one of the other methods should work.



The desired result is to be sent to a paypal pay page.



This code works:



const express = require("express");

const app = express();

app.get("/", (req, res) => {});

app.post("/pay", (req, res) => {
console.log("you made a successful post request"
});

const port = 5000;

app.listen(port, () => `Server running on port ${port}`);


this code works, it logs the payment json object:



const express = require("express");
const paypal = require("paypal-rest-sdk");

const app = express();

app.get("/", (req, res) => {});

paypal.configure({
mode: "sandbox",
client_id:
"___clientID___",
client_secret:
"___clientSECRET___"
});

app.post("/pay", (req, res) => {
var create_payment_json = {
intent: "sale",
payer: {
payment_method: "paypal"
},
redirect_urls: {
return_url: "http://localhost:3000/success",
cancel_url: "http://localhost:3000/cancel"
},
transactions: [
{
item_list: {
items: [
{
name: "item",
sku: "item",
price: "1.00",
currency: "USD",
quantity: 1
}
]
},
amount: {
currency: "USD",
total: "1.00"
},
description: "This is the payment description."
}
]
};

paypal.payment.create(create_payment_json, function(error, payment) {
if (error) {
throw error;
} else {
console.log(payment)
}
}
}
});
});

const port = 5000;

app.listen(port, () => `Server running on port ${port}`);


But this code breaks it with a CORS error:



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


I assume that the res.redirect is the problem.



EDIT
I recreated the same express server in a separate project folder, but instead of trying to use it with a React proxy, I created a very simple HTML form to hit '/pay'



This worked, which tells me that the problem is in having react run on localhost:3000 with a proxy to a server that is listening on localhost:5000



SO, the problem is not with PayPal, I can rule that out.



My next step is to learn more about integrating React apps into express...



Any other ideas would be very appreciated!
Thanks!!!










share|improve this question
















I am trying to set up the paypal-rest-sdk in an express server with a react front end, am getting a CORS error. The react server is using a proxy.



I have tried the npm cor() package (couldn't get it to work) and two chrome extension. They stop the error message but nothing else happens either. I don't understand how to use the proper headers...but it seems like one of the other methods should work.



The desired result is to be sent to a paypal pay page.



This code works:



const express = require("express");

const app = express();

app.get("/", (req, res) => {});

app.post("/pay", (req, res) => {
console.log("you made a successful post request"
});

const port = 5000;

app.listen(port, () => `Server running on port ${port}`);


this code works, it logs the payment json object:



const express = require("express");
const paypal = require("paypal-rest-sdk");

const app = express();

app.get("/", (req, res) => {});

paypal.configure({
mode: "sandbox",
client_id:
"___clientID___",
client_secret:
"___clientSECRET___"
});

app.post("/pay", (req, res) => {
var create_payment_json = {
intent: "sale",
payer: {
payment_method: "paypal"
},
redirect_urls: {
return_url: "http://localhost:3000/success",
cancel_url: "http://localhost:3000/cancel"
},
transactions: [
{
item_list: {
items: [
{
name: "item",
sku: "item",
price: "1.00",
currency: "USD",
quantity: 1
}
]
},
amount: {
currency: "USD",
total: "1.00"
},
description: "This is the payment description."
}
]
};

paypal.payment.create(create_payment_json, function(error, payment) {
if (error) {
throw error;
} else {
console.log(payment)
}
}
}
});
});

const port = 5000;

app.listen(port, () => `Server running on port ${port}`);


But this code breaks it with a CORS error:



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


I assume that the res.redirect is the problem.



EDIT
I recreated the same express server in a separate project folder, but instead of trying to use it with a React proxy, I created a very simple HTML form to hit '/pay'



This worked, which tells me that the problem is in having react run on localhost:3000 with a proxy to a server that is listening on localhost:5000



SO, the problem is not with PayPal, I can rule that out.



My next step is to learn more about integrating React apps into express...



Any other ideas would be very appreciated!
Thanks!!!







express cors paypal-rest-sdk






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 18:52







Hanley Soilsmith

















asked Nov 21 '18 at 9:20









Hanley SoilsmithHanley Soilsmith

18210




18210













  • What’s the exact CORS error message the browser is logging? What’s the URL of the endpoint?

    – sideshowbarker
    Nov 21 '18 at 10:48











  • The docs at developer.paypal.com/docs/api/rest-sdks give no indication that PayPal API endpoints are CORS-enabled nor that any of the REST APIs are meant to be used from frontend JavaScript running in a browser. Instead the docs say, “PayPal provides REST server SDKs for these languages: Java, .NET, Node, PHP, Python, Ruby”.

    – sideshowbarker
    Nov 21 '18 at 10:53











  • Thank you for your comment! I believe that the PayPal endpoint is referenced by for (let i = 0; i < payment.links.length; i++) { if (payment.links[i].rel === "approval_url") { res.redirect(payment.links[i].href); In my sandbox, the href is sandbox.paypal.com/cgi-bin/…... I recreated the code in a more simple fashion, omitting the react side of things, and used a simple HTML form, it worked. This tells me that the problem is likely from the react proxy. I will add this to my original question.

    – Hanley Soilsmith
    Nov 21 '18 at 18:45











  • IE, this tells me that the problem is not with PayPal. I think you already know this, but I guess the problem is that I don't completely understand all of this stuff and am definitely making some noob mistakes. Thanks for your help :)

    – Hanley Soilsmith
    Nov 21 '18 at 18:53



















  • What’s the exact CORS error message the browser is logging? What’s the URL of the endpoint?

    – sideshowbarker
    Nov 21 '18 at 10:48











  • The docs at developer.paypal.com/docs/api/rest-sdks give no indication that PayPal API endpoints are CORS-enabled nor that any of the REST APIs are meant to be used from frontend JavaScript running in a browser. Instead the docs say, “PayPal provides REST server SDKs for these languages: Java, .NET, Node, PHP, Python, Ruby”.

    – sideshowbarker
    Nov 21 '18 at 10:53











  • Thank you for your comment! I believe that the PayPal endpoint is referenced by for (let i = 0; i < payment.links.length; i++) { if (payment.links[i].rel === "approval_url") { res.redirect(payment.links[i].href); In my sandbox, the href is sandbox.paypal.com/cgi-bin/…... I recreated the code in a more simple fashion, omitting the react side of things, and used a simple HTML form, it worked. This tells me that the problem is likely from the react proxy. I will add this to my original question.

    – Hanley Soilsmith
    Nov 21 '18 at 18:45











  • IE, this tells me that the problem is not with PayPal. I think you already know this, but I guess the problem is that I don't completely understand all of this stuff and am definitely making some noob mistakes. Thanks for your help :)

    – Hanley Soilsmith
    Nov 21 '18 at 18:53

















What’s the exact CORS error message the browser is logging? What’s the URL of the endpoint?

– sideshowbarker
Nov 21 '18 at 10:48





What’s the exact CORS error message the browser is logging? What’s the URL of the endpoint?

– sideshowbarker
Nov 21 '18 at 10:48













The docs at developer.paypal.com/docs/api/rest-sdks give no indication that PayPal API endpoints are CORS-enabled nor that any of the REST APIs are meant to be used from frontend JavaScript running in a browser. Instead the docs say, “PayPal provides REST server SDKs for these languages: Java, .NET, Node, PHP, Python, Ruby”.

– sideshowbarker
Nov 21 '18 at 10:53





The docs at developer.paypal.com/docs/api/rest-sdks give no indication that PayPal API endpoints are CORS-enabled nor that any of the REST APIs are meant to be used from frontend JavaScript running in a browser. Instead the docs say, “PayPal provides REST server SDKs for these languages: Java, .NET, Node, PHP, Python, Ruby”.

– sideshowbarker
Nov 21 '18 at 10:53













Thank you for your comment! I believe that the PayPal endpoint is referenced by for (let i = 0; i < payment.links.length; i++) { if (payment.links[i].rel === "approval_url") { res.redirect(payment.links[i].href); In my sandbox, the href is sandbox.paypal.com/cgi-bin/…... I recreated the code in a more simple fashion, omitting the react side of things, and used a simple HTML form, it worked. This tells me that the problem is likely from the react proxy. I will add this to my original question.

– Hanley Soilsmith
Nov 21 '18 at 18:45





Thank you for your comment! I believe that the PayPal endpoint is referenced by for (let i = 0; i < payment.links.length; i++) { if (payment.links[i].rel === "approval_url") { res.redirect(payment.links[i].href); In my sandbox, the href is sandbox.paypal.com/cgi-bin/…... I recreated the code in a more simple fashion, omitting the react side of things, and used a simple HTML form, it worked. This tells me that the problem is likely from the react proxy. I will add this to my original question.

– Hanley Soilsmith
Nov 21 '18 at 18:45













IE, this tells me that the problem is not with PayPal. I think you already know this, but I guess the problem is that I don't completely understand all of this stuff and am definitely making some noob mistakes. Thanks for your help :)

– Hanley Soilsmith
Nov 21 '18 at 18:53





IE, this tells me that the problem is not with PayPal. I think you already know this, but I guess the problem is that I don't completely understand all of this stuff and am definitely making some noob mistakes. Thanks for your help :)

– Hanley Soilsmith
Nov 21 '18 at 18:53












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408750%2fhow-to-solve-a-cors-issue-with-paypal-rest-sdk-on-react-express-local-developmen%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53408750%2fhow-to-solve-a-cors-issue-with-paypal-rest-sdk-on-react-express-local-developmen%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?