검증 포인트
Express + Stripe 예시
import express from 'express';
import Stripe from 'stripe';
const app = express();
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY || '');
app.post('/webhook/stripe', express.raw({ type: 'application/json' }), (req, res) => {
const signature = req.headers['stripe-signature'];
const secret = process.env.STRIPE_WEBHOOK_SECRET || '';
try {
const event = stripe.webhooks.constructEvent(req.body, String(signature), secret);
// TODO: handle event with idempotency check
res.status(200).send('ok');
} catch (err) {
res.status(400).send('signature verification failed');
}
});주의
body parser를 먼저 적용하면 raw body가 변경되어 서명 검증이 실패할 수 있습니다. webhook 라우트에는 raw parser를 우선 적용하세요.