#pedgo - constructEvent

1 messages · Page 1 of 1 (latest)

rose stream
#

That sounds like req.headers['stripe-signature'] is undefined

#

Can you check what headers the req has?

shrewd rover
#

I haven't connected it to the express app yet

#

this error was triggered by typescript, not during the process of receiving a webhook

#

Here's the full file:

import { Request, Response, NextFunction } from 'express'
import Stripe from 'stripe'

const stripeSecretKey = process.env.STRIPE_SECRET_KEY
const stripeEndpointSecrete = process.env.STRIPE_ENDPOINT_SECRET

const stripe = new Stripe(stripeSecretKey + '', {
  apiVersion: '2020-08-27',
  typescript: true,
})

exports.verifyStrapiSignature = (
  req: Request,
  res: Response,
  next: NextFunction,
) => {
  try {

    req.body = stripe.webhooks.constructEvent(
      req.body,
      req.headers['stripe-signature'],
      stripeEndpointSecrete,
    )
    next()
  } catch (err) {
    res.status(400).send(`WARNING: Webhook signature verification failed!!!`)
  }
}
rose stream
#

Oh sorry I mixed up the ordering, it looks like stripeEndpointSecrete is undefined

#

Have you double checked that your process.env variables are getting read in?

shrewd rover
#

Just a sec

#

Yeah, the env variable STRIPE_ENDPOINT_SECRET is empty atm. I set it up only when I start stripe CLI (because it'll change multiple times)

#

But the error is in req.headers['stripe-signature'], not in stripeEndpointSecrete atm

#

Am I wrong about it?

rose stream
#

Ah apologies, I am less familiar with typescript. I think I am clearer now. So this is entirely a typing issue before the code is even being run?

shrewd rover
#

Exactly

#

typescript give warns or errors while we type the code. The variable req.headers['stripe-signature'] that extends the type Request that comes from the express package, is not allowed to be assigned to the second argument from the method stripe.webhooks.constructEvent(arg1, arg2, arg3), that comes from the stripe package. But in this example of yours, in Github, there is exactly the same code, thus my problem.
(https://github.com/stripe-samples/accept-a-payment/blob/7576392253e87b7025967f5f6fce14d1ff94fb9a/custom-payment-flow/server/node-typescript/src/server.ts#L106)

rose stream
#

Can you check the type on req.headers?

#

Trying to think of how the third type could be undefined here

shrewd rover
#

I don't completely know how to do that. I need to google just a minute

#

I believe it has the type of IncomingHttpHeaders:

rose stream
#

Can you try importing all of express like in our sample?
import express from "express";

#

That is part of request which you are importing, but that is one of the only real differences that I am seeing

shrewd rover
#

I see, but the error persists

#

Should I give up on using typescript for this middleware?

rose stream
#

Your exact code is not giving me errors in my own IDE. I am really trying to think of what this might be

#

Can you try reinstalling express?

shrewd rover
#

maybe it's the difference between our .tsconfig files?>

#

I'm using "strict": true

rose stream
#

Still works. I will reach out to a colleague about this for other things to check

shrewd rover
#

ok, thanks a lot

rose stream
#

And I was able to recreate that error and adding the use the non-null assertion operator made the type issues go away

#
      req.body,
      req.headers['stripe-signature']!,
      stripeEndpointSecrete!,
    )```
shrewd rover
#

oh awesome

#

let me try

rose stream
#

Hope that works, I'll mark that this should probably be updated in our samples if I can't get them to work without it.

shrewd rover
#

so, this assertion is forbitten in my environment. but I can use this similar method:

req.body = stripe.webhooks.constructEvent(
  req.body,
  <string | Buffer>req.headers['stripe-signature'],
  stripeEndpointSecrete!,
)
``` But I need to be sure what is the type of `req.headers['stripe-signature']`. I believe it's a buffer right?
#

I mean, the stripe library request raw data and parse it.

radiant swallow
#

the signature is just a string, it's not a buffer

#

it's the content of an HTTP header

shrewd rover
#

ok, thanks

#

Sorry if I caused any confusion. It's just a bit hard to me to grasp typescript on backend