diff --git a/server/src/controllers/PaymentController.js b/server/src/controllers/PaymentController.js index 661111db..91b85872 100644 --- a/server/src/controllers/PaymentController.js +++ b/server/src/controllers/PaymentController.js @@ -13,10 +13,15 @@ const PaymentController = { try { switch (event.type) { case 'payment_intent.succeeded': { - const { id, amount_received } = event.data.object; + const { id, amount_received, payment_intent } = event.data.object; + const user = req.user; console.log('Handling payment intent'); + console.log('UserID:', user.id); // Log the userID + console.log('PaymentIntent:', payment_intent); // Log the paymentIntent + // update frosh model to have paid successfully + await FroshServices.addRetreatPayment(user.id, payment_intent); await PaymentServices.updatePayment(id, amount_received); break; } @@ -63,16 +68,8 @@ const PaymentController = { const user = req.user; const count = await PaymentServices.getNonExpiredPaymentsCountForItem('Retreat Ticket'); if (count < process.env.RETREAT_MAX_TICKETS) { - const { url, payment_intent } = await PaymentServices.createCheckoutSession( - user.email, - 'retreat', - ); - - const frosh = await FroshServices.addRetreatPayment(user, payment_intent); - if (!frosh) { - res.status(400).send({ message: 'Something went wrong!' }); - } - res.status(200).send({ url }); + const session = await PaymentServices.createCheckoutSession(user.email, 'retreat', user.id); + res.status(200).send({ url: session.url }); } else { res.status(400).send({ message: 'Sold out! Please check back later in case more tickets become available', diff --git a/server/src/services/FroshServices.js b/server/src/services/FroshServices.js index 26451f8e..3ea2c412 100644 --- a/server/src/services/FroshServices.js +++ b/server/src/services/FroshServices.js @@ -64,8 +64,10 @@ const FroshServices = { * @param {Payment} paymentIntent Payment object from stripe * @returns {User} updated user */ - async addRetreatPayment(user, paymentIntent) { - return FroshModel.findByIdAndUpdate(user.id, { + async addRetreatPayment(userID, paymentIntent) { + console.log('Adding retreat payment for userID:', userID); + console.log('PaymentIntent:', paymentIntent); + return FroshModel.findByIdAndUpdate(userID, { $push: { payments: [ { diff --git a/server/src/services/PaymentServices.js b/server/src/services/PaymentServices.js index e4f1d3e0..c1e1c7f2 100644 --- a/server/src/services/PaymentServices.js +++ b/server/src/services/PaymentServices.js @@ -99,7 +99,7 @@ const PaymentServices = { * @return {Promise} */ /* istanbul ignore next */ - async createCheckoutSession(email, type = 'orientation') { + async createCheckoutSession(email, type = 'orientation', userId) { const products = { orientation: { priceId: process.env.STRIPE_TICKET_PRICE_ID, @@ -131,6 +131,9 @@ const PaymentServices = { cancel_url: `${process.env.CLIENT_BASE_URL}${ products[type]?.relativeUrlFailure ?? products['orientation'].relativeUrlFailure }`, + metadata: { + userId, // Add user ID to metadata for webhook processing + }, }); // } catch (error) { // if (error.raw?.code === 'coupon_expired') {