#cookernetes

1 messages ยท Page 1 of 1 (latest)

forest lodgeBOT
sturdy narwhal
#

Where exactly is that crash happening?

tiny skiff
sturdy narwhal
#

It's trying to read properties of undefined so something isn't working like you expect

tiny skiff
#
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(dataPayload.data.object.id, {
            expand: ["line_items"],
        });

        const lineItems = sessionWithLineItems.line_items;

        // Order Fulfillment
        console.log(lineItems);
    }

    res.status(200).end();
});
#

That's the full code

#

Oh right, it might have something to do with dataPayload being:

<Buffer 7b 0a 20 20 22 69 64 22 3a 20 22 65 76 74 5f 31 4d 57 52 5a 47 45 4a 79 49 32 71 46 5a 65 68 6f 49 6e 78 51 6e 65 41 22 2c 0a 20 20 22 6f 62 6a 65 63 ... 3553 more bytes>
#

lol

#

How can I do this?

#

(@sturdy narwhal)

#

(sorry for the ping, this is something that needs to go into prod very soon)

sturdy narwhal
#

As a fallback, signature verification is optional. You could use parseEvent in the interim while you sort out signature verification

tiny skiff
#

I want to do it this way

#

I just want to get the ID from the data returned back

sturdy narwhal
#

Assuming you pass the raw body and the correct endpointSecret that's expected to work

tiny skiff
#

So I can do the rest

sturdy narwhal
#

I would expect that payload/body to be a string at that point, i think.

tiny skiff
#

Nah it's parsing in a buffer

#

It's getting raw... which is a buffer amiright?

#

Yeah

#

"Returns middleware that parses all bodies as a Buffer and only looks at requests where the Content-Type header matches the type option."

opal crater
#

Hi there ๐Ÿ‘‹ taking over, as my colleague needs to step away

Wasn't this working earlier when we chatted?

tiny skiff
#

It was working in the IDE, didn't run it ๐Ÿคฃ

#

Lmao

#

I just want to follow this guide in my code

opal crater
#

Can you post the URL that leads to this guide?

tiny skiff
#

Then lang is set to node on the site

#

But it's so weird as session is not defined anywhere in that code....

opal crater
#

Yeah, I think that's a placeholder that's intended to be changed

tiny skiff
#

Lol

#

So how am I gonna get that ID to put in that retrieve function?

sturdy narwhal
#

oh hang on just a sec before i go, if that's crashing in your retrieve() call (not constructEvent) then you need to replace dataPayload there with event

tiny skiff
#

However, surely from the data I am getting back, it would be event.data.object.id... right? Well it doesn't let me.

sturdy narwhal
#

event.data.object.id should be the session id

tiny skiff
#

This is what error I get

#

And I have defined event as Stripe.Event; like this:

let event: Stripe.Event;
#

Is that the wrong typedef for event?

#

Because the constructEvent returns it...

#

Is there a way to fix this? (I basically want to get the id from the object in data in event, but TS doesn't like that)

#

@opal crater

opal crater
#

Can you try ignoring typescript for that line and running the code to see if it works?

tiny skiff
#

Yeah it works

#

It's just a TS thing

#

Really annoying me though ๐Ÿ˜

opal crater
#

Yeah, that just sounds like it's not configured to know that the object will be a Stripe Event when the code receives a request. I'm not sure of a bette workaround beyond simply telling TS to ignore that line

tiny skiff
#

Yeah ok

#

I can also do object["id"] rather than .id and that doesn't error ig