@@ -18,14 +18,15 @@ import {
1818 WaitlistedCamperDTO ,
1919} from "../types" ;
2020import { createWaitlistedCampersDtoValidator } from "../middlewares/validators/waitlistedCampersValidators" ;
21+ import {
22+ stripeKey ,
23+ verifyStripeWebhooksRequest ,
24+ } from "../utilities/stripeUtils" ;
2125
2226const camperRouter : Router = Router ( ) ;
2327
2428const camperService : ICamperService = new CamperService ( ) ;
2529
26- // TODO: secure stripe keys
27- const STRIPE_ENDPOINT_KEY = process . env . STRIPE_ENDPOINT_SECRET || "" ;
28-
2930// ROLES: Leaving unprotected as the registration flow probs needs this endpoint to register @dhruv
3031/* Create a camper */
3132camperRouter . post ( "/register" , createCampersDtoValidator , async ( req , res ) => {
@@ -121,12 +122,19 @@ camperRouter.get("/:chargeId/:sessionId", async (req, res) => {
121122/* Initiated by Stripe webhook. On successful payment, mark camper as paid. */
122123camperRouter . post ( "/confirm-payment" , async ( req , res ) => {
123124 try {
124- const { body } = req ;
125+ const event = verifyStripeWebhooksRequest (
126+ req . headers [ "stripe-signature" ] ,
127+ req . body ,
128+ ) ;
129+
130+ if ( ! event ) {
131+ res . status ( 400 ) . send ( "Webhook signature verification failed" ) ;
132+ }
125133
126- if ( body . type === "checkout.session.completed" ) {
127- const chargeId = body . data . object . id ;
134+ if ( event . type === "checkout.session.completed" ) {
135+ const chargeId = event . data . object . id ;
128136
129- if ( body . data . object . payment_status === "paid" ) {
137+ if ( event . data . object . payment_status === "paid" ) {
130138 await camperService . confirmCamperPayment (
131139 ( chargeId as unknown ) as string ,
132140 ) ;
0 commit comments