#cookernetes-event-object
1 messages · Page 1 of 1 (latest)
Please post the actual code and not screenshots, it's really hard to debug a picture when I can't copy/paste into a text editor.
As far as I can tell, you should be using dataPayLoad.data.object.id to get the ID from the Event
cookernetes-event-object
So sorry, here's the code:
app.post("/downProduct", bodyParser.raw({ type: "application/json" }), async (req, res) => {
const dataPayload = req.body;
const sig = req.headers["stripe-signature"];
let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(dataPayload, sig, endpointSecret);
} catch (err) {
return res.status(400).send(`Webhook Error: ${err.message}`);
}
if (event.type === "checkout.session.completed") {
// Retrieve session
const sessionWithLineItems = await stripe.checkout.sessions.retrieve(event.data.object.id, {
expand: ["line_items"],
});
const lineItems = sessionWithLineItems.line_items;
// TODO: Order Fulfillment
}
res.status(200).end();
});
This is a typescript error too, however for some reason the docs are being extremely unhelpful with node and is suggesting to use variables that don't even exist 🤣
Docs URL: https://stripe.com/docs/payments/checkout/fulfill-orders?locale=en-GB
But anyway yeah, I don't know how to specifically state that that 'Object' will be a checkout.session.completed object to TS so that I can access the properties without issues
I think if you check the type of dataPayLoad.data.object, it will be a Stripe Checkout Session. There is no such thing as a checkout.session.completed object. There is only an Event object and a Checkout Session object (plus whatever objects you have nested in your Checkout Session).
The error you're getting is saying that there's no id on that object, because you're instantiating an empty Stripe.Event object
Try this:
const sessionWithLineItems = await stripe.checkout.sessions.retrieve(dataPayLoad.data.object.id, { expand: ["line_items"], });