Skip to content

Instantly share code, notes, and snippets.

@fred-stripe
Created January 17, 2020 19:53
Show Gist options
  • Save fred-stripe/7d4f2c62f4858c56ed630b5135333bda to your computer and use it in GitHub Desktop.
Save fred-stripe/7d4f2c62f4858c56ed630b5135333bda to your computer and use it in GitHub Desktop.
Google Cloud Functions + Stripe Webhook signing
/**
* Responds to any HTTP request that can provide a "message" field in the body.
*
* @param {!Object} req Cloud Function request context.
* @param {!Object} res Cloud Function response context.
*/
exports.webhook = (req, res) => {
// Required environment variables:
// WEBHOOK_SIGNING_KEY -- the whsec_xxx value displayed when you enable webhook signing for your endpoint
// STRIPE_SECRET_KEY -- your Stripe secret key
const endpointSecret = process.env.WEBHOOK_SIGNING_KEY;
try {
const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY);
// GCF lowercases all header names, just like AWS Lambda
const event = stripe.webhooks.constructEvent(req.rawBody, req.headers['stripe-signature'], endpointSecret);
res.send(JSON.stringify(event));
}
catch (err) {
res.status(500).send('Bad Failure: ' + err);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment