#cookernetes-event-object

1 messages · Page 1 of 1 (latest)

jagged pumiceBOT
ebon gulch
#

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

warm tulip
# ebon gulch Please post the actual code and not screenshots, it's really hard to debug a pic...

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();
});
#

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

ebon gulch
#

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"], });

warm tulip
#

Yeah that works, thanks - and thanks for the clear-up :D

#

have a nice day :)