Reputation: 87
I'll preface this by saying I'm not great with Javascript but I know the basics, but I am also just starting to learn about Stripe. I am using a local environment with a Wordpress install to test in and am following this Stripe documentation. I have installed Node.js and in the command line I have used the npm install stripe
which adds the node_modules folder in my MAMP/htdocs/iga folder (iga is the name for the website). I then added the script to my functions.js file.
The problem is that when I run the page with the script in it, while checking the console, nothing appears in the console. It is supposed to give me a message to say success or failure, but I get nothing. I checked to see if the functions.js file was working by placing a console.log('Hello');
after the stripe script but that part does execute and I see it in the console.
I don't know whether it is an issue with the script, the npm install or something else.
Javascript code:
async () => {
try {
const stripe = require('stripe')('[test_key]');
const paymentIntent = await stripe.paymentIntents.create({
amount: 1477, // $14.77, an easily identifiable amount
currency: 'usd',
});
console.log('Worked! ', paymentIntent.id);
} catch(err) {
console.log('Error! ', err.message);
}
};
EDIT: I also receive a few errors when I do the npm install. I've tried installing it in different places but it hasn't changed it.
EDIT 2: Here is an image of the errors I get when installing the npm stripe
Upvotes: 0
Views: 175
Reputation: 5847
If you have errors when installing stripe-node
, then you should edit those in your question before anyone can help you.
Saying that, the code snippet you have creates an anonymous function calling the Stripe API, but doesn't actually execute it.
You can fix this by either calling it immediately using an Immediately Invoked Function Expression:
(async () => {
// snip
})();
Or give the anonymous function a name and then call it, which IMO is cleaner:
async function createPaymentIntent() {
// snip
}
createPaymentIntent();
Upvotes: 1