Created
January 17, 2020 19:53
-
-
Save fred-stripe/7d4f2c62f4858c56ed630b5135333bda to your computer and use it in GitHub Desktop.
Google Cloud Functions + Stripe Webhook signing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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