Giter Site home page Giter Site logo

CORS errrors when calling API about nodejs-sdk HOT 2 CLOSED

wepay avatar wepay commented on June 10, 2024
CORS errrors when calling API

from nodejs-sdk.

Comments (2)

StarNeit avatar StarNeit commented on June 10, 2024 2

You're correct, and I should have implemented them in server codes.
I solved my situation by implementing those codes in server without any issues now.

Thank you so much. 👍 💯

from nodejs-sdk.

camerona93 avatar camerona93 commented on June 10, 2024

Hey there @StarNeit!

What you're running into is a browser-initiated "Cross Origin Request" issue - browsers are designed to prevent a website from making an HTTP call to a resource that is from a different origin (wepay.com) than the one it is originating from (localhost:3000) unless they pass a pre-flight check (more reading here).

Given that your WePay credentials should stay secret on the server-side of your application, and that the WePay API does not allow API calls originating from a browser (which is why you're encountering CORS errors), you should move your WePay logic to your application's server, rather than the webpage.

For example:

var app = require('express')(); // expressJS, allows you to easily set up a server
var wepay = require('wepay').WEPAY;

var wepay_settings = {
	'client_id'     : 'XXXXXXX',
	'client_secret' : 'XXXXXXXXXXXX',
	'access_token'  : 'XXXXXXXXXXXX'
};

var wp = new wepay(wepay_settings);
wp.use_staging();

app.get('/', function(req, res) {
	wp.call('/checkout/create',
		{
			"account_id": "XXXXX",
			"short_description": "short",
			"amount": 100,
			"currency": "USD",
			"type": "donation"
		},
		function (response) {
			console.log("WePay response:\n", response);
			res.send("Success!");
		});
});

app.listen(3000, function() {
	console.log("Listening on port 3000");
});

The above node application will spin up a server at port 3000, and on a GET request to / will call WePay's /checkout/create endpoint using the Node SDK, print the response to console, and send a "Success!" response string. You can observe this by visiting http://localhost:3000 in a browser - you should see "Success!" printed on the screen and you should see WePay's API response in your server logs.

Please let me know if I've incorrectly assessed your situation or if you're still having trouble. Thanks!

from nodejs-sdk.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.