#Charmon-Session-product
1 messages · Page 1 of 1 (latest)
Hi there!
Expanding the line items via a session retrieval is the correct way
🤔
what is the reason for this error
Can you log out event.data.object.id?
yeah seems like its alright
cs_test_b1bnciRCpKgJiRd4pP74y7A0XpYPuiPDa30AN3FBWORSRjdpgVoCKQMjAa
Yep that looks good. Your code looks fine to me...
What's your package.json look like?
"dependencies": {
"@stripe/stripe-js": "1.22.0",
"@supabase/supabase-auth-helpers": "^1.2.3",
"axios": "^0.27.2",
"classnames": "2.3.1",
"express": "^4.18.1",
"next": "^12.0.9",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-merge-refs": "1.1.0",
"stripe": "^8.201.0",
"swr": "1.2.0",
"tailwindcss": "3.0.18"
},
The error makes me think Stripe isn't initializing correctly on that file
Can you share your full webhook code?
yeah. its just the sample code from stripe, I just wanted to test how to retrieve these products.
// server.js
//
// Use this sample code to handle webhook events in your integration.
//
// 1) Paste this code into a new file (server.js)
//
// 2) Install dependencies
// npm install stripe
// npm install express
//
// 3) Run the server on http://localhost:4242
// node server.js
import stripe from 'stripe';
import express from 'express';
const app = express();
// This is your Stripe CLI webhook secret for testing your endpoint locally.
const endpointSecret = 'whsec_';
app.post(
'/webhook',
express.raw({ type: 'application/json' }),
(request, response) => {
const sig = request.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(request.body, sig, endpointSecret);
} catch (err) {
response.status(400).send(`Webhook Error: ${err.message}`);
return;
}
// Handle the event
switch (event.type) {
case 'payment_intent.succeeded':
// Then define and call a function to handle the event payment_intent.succeeded
break;
case 'checkout.session.completed':
const retrieveItems = async () => {
const session = await stripe.checkout.sessions.retrieve(
event.data.object.id,
{
expand: ['line_items']
}
);
console.log(session);
};
// Note that you'll need to add an async prefix to this route handler
retrieveItems();
// Then define and call a function to handle the event payment_intent.succeeded
break;
// ... handle other event types
default:
console.log(`Unhandled event type ${event.type}`);
}
// Return a 200 response to acknowledge receipt of the event
response.send();
}
);
app.listen(4242, () => console.log('Running on port 4242'));
K yeah looks like your import stripe from 'stripe'; isn't initializing stripe correctly
Can you replace with const stripe = require('stripe')('sk_test_123'); to make sure that is the issue?
Ah yeah or you can use your method and just do const stripe = new Stripe('sk_test_...');
Yeah, thanks a alot it works now, so its always best to use commonjs imports?