#Peta Stewart
1 messages · Page 1 of 1 (latest)
Hi there!
Hi bismarck
You are using the endpoint secret that the CLI provides?
yes, the same as in stripe
yes
Got it, Express is tricky here and usually messes with the raw body which leads to this issue.
So for instance I do something like: app.use((req, res, next) => { if (req.originalUrl === '/webhook') { next(); // Do nothing with the body because I need it in a raw state. } else { express.json()(req, res, next); // ONLY do express.json() if the received request is NOT a WebHook from Stripe. } });
router.post('/', express.raw({type: 'application/json'}), (request, response) => {
let event = request.body;
// Only verify the event if you have an endpoint secret defined.
// Otherwise use the basic event deserialized with JSON.parse
if (endpointSecret) {
//const signature = request.headers['stripe-signature'];
//testing below - replace line above
const payload = {
id: 'evt_test_webhook',
object: 'event',
};
const payloadString = JSON.stringify(payload, null, 2);
const header = stripe.webhooks.generateTestHeaderString({
payload: payloadString,
endpointSecret,
});
try {
event = stripe.webhooks.constructEvent(
request.body,
//signature,
header,
endpointSecret
);
} catch (err) {
console.log(`⚠️ Webhook signature verification failed.`, err.message);
return response.sendStatus(400);
}
}
Do you mean this?
The issue likely is from your route handler
I get the correct error message from my code
console.log(⚠️ Webhook signature verification failed., err.message);
So the router is working fine
Right, can you share your Express initialization? Like I shared above?
const webhookRouter = require('./routes/webhook')
app.use('/webhook', webhookRouter)
Are you setting express.json() anywhere?
// express app setup -----------------------
const app = express()
app.use(express.static('public'))
app.use(express.json())
app.use(express.urlencoded({extended: true}))
app.use(cors(corsOptionsDelegate))
yes
Yep okay so see my snippet above?
yes
if (req.originalUrl === '/webhook') {
next(); // Do nothing with the body because I need it in a raw state.
} else {
express.json()(req, res, next); // ONLY do express.json() if the received request is NOT a WebHook from Stripe.
}
});```
Try using that in place of your app.use(express.json())
You'll need to adjust that slightly since looks like your endpoint route here is the index
Thank you, this allowed the events to listen. I'll try address the new issues.