#tungtn1099
1 messages ยท Page 1 of 1 (latest)
What is the issue you're facing?
it tells me that im not passing the raw request i received from stripe when i cast an event
i made sure that the stripe secret key and webhook key are mine
its not logging any of these
oh wait, i reran the service and it does log 123
but same error not passing raw request
In your if condition, you didn't set the request to parse as raw.
For example.
express.raw({type: 'application/json'})(req, res, next);
// Use JSON parser for all non-webhook routes
app.use(
(
req: express.Request,
res: express.Response,
next: express.NextFunction
): void => {
if (req.originalUrl === '/webhook') {
next();
} else {
express.json()(req, res, next);
}
}
);
app.post(
'/webhook',
// Stripe requires the raw body to construct the event
express.raw({type: 'application/json'}),
(req: express.Request, res: express.Response): void => {
const sig = req.headers['stripe-signature']
? req.headers['stripe-signature']
: '';
let event: Stripe.Event;
try {
event = stripe.webhooks.constructEvent(req.body, sig, webhookSecret);
} catch (err) {
// On error, log and return the error message
console.log(`โ Error message: ${err.message}`);
res.status(400).send(`Webhook Error: ${err.message}`);
return;
}
// Successfully constructed event
console.log(':white_check_mark: Success:', event.id);
// Cast event data to Stripe object
if (event.type === 'payment_intent.succeeded') {
const stripeObject: Stripe.PaymentIntent = event.data
.object as Stripe.PaymentIntent;
console.log(`๐ฐ PaymentIntent status: ${stripeObject.status}`);
} else if (event.type === 'charge.succeeded') {
const charge = event.data.object as Stripe.Charge;
console.log(`๐ต Charge id: ${charge.id}`);
} else {
console.warn(`๐คทโโ๏ธ Unhandled event type: ${event.type}`);
}
// Return a response to acknowledge receipt of the event
res.json({received: true});
}
);
i saw it in app.post()
ah i see! i wasn't aware it's set in another part of your code
How do you forward the event? Is it using CLI or endpoint set up in Dashboard?
i used CLI
Do you get the webhookSecret from the CLI in the format of whsec_xxx?
yes i do
How about the secret key initiating stripe client? Does the secret key belong to the same account as the webhook secret?
it does, but i found the issue
i copy pasted the .env from my project into this one so the webhook key name is different ๐
Ah! Great to hear that you found the issue!
i have another question tho
What is the question?
(
req: express.Request,
res: express.Response,
next: express.NextFunction
): void => {
if (req.originalUrl === '/webhook') {
next();
} else {
express.json()(req, res, next);
}
}
);```
is there any other way i can get the raw req.body
if i use this my swaggerUI just dies
wait, this one is more js question
thanks for your time ๐
No worries! I'm not familiar how Swagger UI takes in the request body. I'd recommend checking online/stackover flow that probably others might face similar issue