-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Stripe webhook signing secret (#21)
* refactor: Add stripe-client * feat: Add middleware to handle Stripe webhook signing
- Loading branch information
Jonathan Steele
authored
Mar 18, 2021
1 parent
8b3ba42
commit a75467e
Showing
6 changed files
with
46 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import Stripe from 'stripe' | ||
|
||
export default new Stripe(process.env.STRIPE_SECRET_KEY) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import stripe from '@/lib/stripe-client' | ||
|
||
const stripeSigningSecret = (handler) => async (req, res) => { | ||
const chunks = [] | ||
|
||
for await (const chunk of req) { | ||
chunks.push(typeof chunk === 'string' ? Buffer.from(chunk) : chunk) | ||
} | ||
|
||
let event | ||
|
||
try { | ||
event = stripe.webhooks.constructEvent( | ||
Buffer.concat(chunks), | ||
req.headers['stripe-signature'], | ||
process.env.STRIPE_WEBHOOK_SIGNING_SECRET | ||
) | ||
} catch (err) { | ||
return res.status(400).json({ message: err.message }) | ||
} | ||
|
||
return handler(req, res, event) | ||
} | ||
|
||
export default stripeSigningSecret |
This file contains 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
This file contains 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
a75467e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: