Skip to content

Instantly share code, notes, and snippets.

@xaphod
Last active April 14, 2020 15:44
Show Gist options
  • Save xaphod/715863b1b9a6304adcf8119e83497314 to your computer and use it in GitHub Desktop.
Save xaphod/715863b1b9a6304adcf8119e83497314 to your computer and use it in GitHub Desktop.
const express = require('express');
const bodyParser = require('body-parser');
const { stripe, webhookSecret } = require('./index');
const router = express();
router.post('/stripe/webhook', bodyParser.raw({ type: 'application/json' }), async (req, res, next) => {
const sig = req.headers['stripe-signature'];
let event;
try {
event = stripe.webhooks.constructEvent(req.rawBody, sig, webhookSecret); // fails if changed to req.body
} catch (err) {
console.error('stripe webhook error during construct event:');
console.error(err);
return res.status(400).send(`Webhook Error: ${err.message}`);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment