-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for both live and test Stripe webhooks on a single deployment
- Loading branch information
Showing
5 changed files
with
59 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,46 @@ | ||
import { Router } from "express"; | ||
import { RequestHandler, Router } from "express"; | ||
import { raw } from "body-parser"; | ||
import { validateWebhookEvent } from "@letsgo/stripe"; | ||
import { StripeMode, validateWebhookEvent } from "@letsgo/stripe"; | ||
import createError from "http-errors"; | ||
import { enqueue } from "@letsgo/queue"; | ||
import { MessageType } from "@letsgo/types"; | ||
|
||
const router = Router(); | ||
|
||
router.post( | ||
"/webhook", | ||
raw({ type: "application/json" }), | ||
async (req, res, next) => { | ||
const stripeHandler: (mode: StripeMode) => RequestHandler = | ||
(mode: StripeMode) => async (req, res, next) => { | ||
let event: any; | ||
try { | ||
event = await validateWebhookEvent({ | ||
body: req.body, | ||
signature: req.headers["stripe-signature"] as string, | ||
mode, | ||
}); | ||
} catch (e: any) { | ||
console.log("INVALID STRIPE WEBHOOK EVENT:", e.message); | ||
console.log(`INVALID ${mode} STRIPE WEBHOOK EVENT:`, e.message); | ||
next(createError(400, "Invalid Stripe webhook event")); | ||
return; | ||
} | ||
await enqueue({ | ||
type: MessageType.Stripe, | ||
payload: event, | ||
payload: { | ||
...event, | ||
stripeMode: mode, | ||
}, | ||
}); | ||
res.status(201).end(); | ||
} | ||
}; | ||
|
||
router.post( | ||
"/webhook/live", | ||
raw({ type: "application/json" }), | ||
stripeHandler("LIVE") | ||
); | ||
|
||
router.post( | ||
"/webhook/test", | ||
raw({ type: "application/json" }), | ||
stripeHandler("TEST") | ||
); | ||
|
||
export default router; |
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
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