@@ -18,14 +18,15 @@ import {
18
18
WaitlistedCamperDTO ,
19
19
} from "../types" ;
20
20
import { createWaitlistedCampersDtoValidator } from "../middlewares/validators/waitlistedCampersValidators" ;
21
+ import {
22
+ stripeKey ,
23
+ verifyStripeWebhooksRequest ,
24
+ } from "../utilities/stripeUtils" ;
21
25
22
26
const camperRouter : Router = Router ( ) ;
23
27
24
28
const camperService : ICamperService = new CamperService ( ) ;
25
29
26
- // TODO: secure stripe keys
27
- const STRIPE_ENDPOINT_KEY = process . env . STRIPE_ENDPOINT_SECRET || "" ;
28
-
29
30
// ROLES: Leaving unprotected as the registration flow probs needs this endpoint to register @dhruv
30
31
/* Create a camper */
31
32
camperRouter . post ( "/register" , createCampersDtoValidator , async ( req , res ) => {
@@ -121,12 +122,19 @@ camperRouter.get("/:chargeId/:sessionId", async (req, res) => {
121
122
/* Initiated by Stripe webhook. On successful payment, mark camper as paid. */
122
123
camperRouter . post ( "/confirm-payment" , async ( req , res ) => {
123
124
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
+ }
125
133
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 ;
128
136
129
- if ( body . data . object . payment_status === "paid" ) {
137
+ if ( event . data . object . payment_status === "paid" ) {
130
138
await camperService . confirmCamperPayment (
131
139
( chargeId as unknown ) as string ,
132
140
) ;
0 commit comments