#pedgo - constructEvent
1 messages · Page 1 of 1 (latest)
That sounds like req.headers['stripe-signature'] is undefined
Can you check what headers the req has?
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!!!`)
}
}
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?
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?
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?
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)
Can you check the type on req.headers?
Trying to think of how the third type could be undefined here
I don't completely know how to do that. I need to google just a minute
I believe it has the type of IncomingHttpHeaders:
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
I see, but the error persists
Should I give up on using typescript for this middleware?
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?
sure
maybe it's the difference between our .tsconfig files?>
I'm using "strict": true
Still works. I will reach out to a colleague about this for other things to check
ok, thanks a lot
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!,
)```
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.
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.