Reputation: 21
I'm trying to integrate stripe to my organization's app, however, following stripe's own documentation, the way to integrate using nodeJS is by doing this:
https://stripe.com/docs/billing/subscriptions/checkout#create-session
I tried pretty much everything, but the server always throws a 404 error with the following message: "Cannot read property 'sessions' of undefined"
The thing is that when I console the stripe object in the backend I see that in fact there is no 'checkout' property for stripe, but the stripe docs say it does have it, which leads me to think that I must be doing something wrong.
This is my "go to checkout" button's frontend code
const createCheckoutSession = (priceId:any) => {
return axios.post("/create-checkout-session", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({
priceId: priceId
})
}).then(function(result) {
return result.data();
});
};
const handleClick = async (event:any) => {
// Get Stripe.js instance
const stripe = await stripePromise;
// Call your backend to create the Checkout Session
//const response = await fetch('/create-checkout-session', { method: 'POST' });
createCheckoutSession('price_1J7UmZFYFs5GcbAXvPronXSX').then(function(data) {
// Call Stripe.js method to redirect to the new Checkout page
stripe
.redirectToCheckout({
sessionId: data.sessionId
})
.then(()=>{console.log('handleResult')});
})
};
And here is my server.js code
//STRIPE CODE
const stripeKey = 'sk_test_51J7UfrFYFs5GcbAX45lXy6w2TV1hPqvUbqu3hdB4QRAXFR3QYmTgcNKXcVG7tL9vwaanjdYWGvkfiQ6Bd41dK4V7004yMs3Cbh'
const stripe = Stripe(stripeKey);
app.post('/create-checkout-session', async (req, res) => {
//console.log("--------STRIPE----------", stripe);
const { priceId } = req.body;
try {
const session = await stripe.checkout.sessions.create({
mode: 'subscription',
payment_method_types: ['card'],
line_items: [
{
price: priceId,
// For metered billing, do not pass quantity
quantity: 1,
},
],
// {CHECKOUT_SESSION_ID} is a string literal; do not change it!
// the actual Session ID is returned in the query parameter when your customer
// is redirected to the success page.
success_url: 'https://localhost:3000/success?session_id={CHECKOUT_SESSION_ID}',
cancel_url: 'https://localhost:3000/canceled',
});
res.send({
sessionId: session.id,
});
} catch (e) {
res.status(400);
return res.send({
error: {
message: e.message,
}
});
}
});
Upvotes: 1
Views: 2644
Reputation: 121
Reinstalling the Stripe Library did not solve the problem for me.
The problem was the Stripe API version.
export const stripe = new Stripe(process.env.STRIPE_API_KEY as string, {
apiVersion: "2023-08-16",
typescript: true,
});
For me the apiVersion number was set to the wrong date (2023-10-16). When I changed it to 2023-08-16 it started working.
Upvotes: 0
Reputation: 3830
I uninstalled stripe, reinstalled - no dice.
Make sure that, according to the docs:
The package needs to be configured with your account's secret key, which is available in the Stripe Dashboard. Require it with the key's value:
Like
const stripe = require('stripe')(process.env.STRIPE_PRIVATE_KEY);
After ensuring that, everything was fine.
Upvotes: 0
Reputation: 21
Alright guys, so I figured it out, stripe was already installed in my packages, however every time I tried to update it it would say I was on the latest version.
Anyway, I uninstalled and then reinstalled everything stripe related again using npm i <package>
and it suddenly worked just fine
Upvotes: 1
Reputation: 6475
It sounds like you're using an older version of the Stripe Node library that does not have support for Checkout Sessions. You should check the version of the library you're using an upgrade to the latest if possible, or v6.20.0 at minimum (the first version to add Checkout support).
Upvotes: 0