#dodo54-webhook

1 messages ยท Page 1 of 1 (latest)

manic flax
#

Hey, are you using the CLI or is this a webhook configured in your Dashboard?

trail oak
#

cli

manic flax
#

Are you using the signing secret (endpointSecret) the CLI spits out when you do stripe listen?

manic flax
#

And it's definitely defined as a variable in your handler? I can't see it in what you shared

#

Can you log it out successfully

trail oak
#

It is defined on the top :

const endpointSecret = process.env.STRIPE_ENDPOINT_SECRET;
#

And my stripe :

const stripe = require("stripe")(process.env.STRIPE_SECRET_KEY);
manic flax
#

And is the value you log for endpointSecret correct to what the CLI states?

trail oak
#

yeah it is successfully printing when I log

manic flax
#

There's so many quirks with this because constructEvent basically expects the raw payload and a lot of these frameworks parse it before you can even access it ๐Ÿ˜ซ

trail oak
#

How can I turn it to raw

manic flax
#

Could you try switching that?

trail oak
#

I am already using bodyParser.raw

#
bodyParser.raw({ type: 'application/json' })
heady remnant
#

Sorry to butt in but I've noticed some web servers don't handle case sensitivity in http headers well. Noticed you have stripe-signature - try Stripe-Signature

trail oak
#

Now I am receiving unable to extract timestamp and signatures from header

trail oak
#
router.post(
  "/webhook",
  express.raw({ type: 'application/json' }),(req, res) => {
    const payload = req.body;
    const sig = req.headers["stripe-signature"];
    let event;
    try {
      event = stripe.webhooks.constructEvent(payload, sig, endpointSecret);
      res.status(200);
    } catch (e) {
      console.log(e)
        return res.status(400).send(`Webhook Error: ${e.message}`);
    }
    if (event.type === 'checkout.session.completed') {
        const session = event.data.object;
        // Fulfill the purchase...
        fulfillOrder(session);
      }
    res.status(200);
  }
);
#

same stripe signature error

manic flax
#

In 90% of cases like this, the secret value is wrong. Can you share an Event ID?

trail oak
#

how to grab that ?

trail oak
#

my secret key value is also right

manic flax
#

In the CLI, you'll see the log of the events it's receiving (evt_xxx)

trail oak
#

yeah I am seeing many id

#

customer created , invoice finalized ..... checkout.session.completed

#

invoice.payment.succeedeed

manic flax
#

Can you share one?

trail oak
#

[evt_1LEC7OSIhtQJ1AoPWjW5M****]

#

invoice.payment.succeedeed

forest heath
#

Hi there

#

stepping in here as ynnoj needs to step away soon

#

Give me a moment to catch up and I'll respond as soon as I can

#

Thanks ๐Ÿ™‚

#

Seems like the event id is invalid

#

what version of express are you running?

trail oak
#

[evt_3LECTLSIhtQJ1AoP0oZ2Wl**]

#

look

#

I am running in test mode

forest heath
#

No I mean the express JS package

trail oak
#

express - 4.17.1

forest heath
#

okay, on my test app I'm running the same version and I remember I had to change my code to this
instead of setting bodyParser property inline

  app.use('/stripe-events', express.raw({type: "*/*"}))
#

and the route was just

app.post('/stripe-events', async (req,res) => {
trail oak
#

Same error

#

It worked but

#

then after sometime I get this from cli :
2022-06-24 19:02:51 [ERROR] Failed to POST: Post "http://localhost:5000/api/webhook": context deadline exceeded (Client.Timeout exceeded while awaiting headers)

forest heath
#

Seems like that's a node specific error though.

#

let me take a look

trail oak
#

here is code :

router.post(
  "/webhook",
  bodyParser.raw({ type: 'application/json' }),(req, res) => {
    const payload = req.body;
    const sig = req.headers["stripe-signature"];
    let event;
    try {
      event = stripe.webhooks.constructEvent(payload, sig, "whsec_7a0b4901079792abb5164e1e0aa7c1262588fdd325ba86600148d2cef8076473");
      res.status(200);
    } catch (e) {
      console.log(e)
        return res.status(400).send(`Webhook Error: ${e.message}`);
    }
    if (event.type === 'checkout.session.completed') {
        const session = event.data.object;
        // Fulfill the purchase...
        fulfillOrder(session);
      }
    res.status(200);
  }
);
forest heath
#

can you set res.status(200).end()

trail oak
#

res.status(200).end() ? right ?

forest heath
#

yup

trail oak
#

aah done

#

thanks brother for giving me time