Skip to content

Commit

Permalink
Current changes made to the retreat ticket issue
Browse files Browse the repository at this point in the history
  • Loading branch information
kexinxiang committed Jul 27, 2024
1 parent fb5391e commit bace853
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 14 deletions.
19 changes: 8 additions & 11 deletions server/src/controllers/PaymentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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',
Expand Down
6 changes: 4 additions & 2 deletions server/src/services/FroshServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down
5 changes: 4 additions & 1 deletion server/src/services/PaymentServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ const PaymentServices = {
* @return {Promise<Stripe.Checkout.Session & {lastResponse: {headers: {[p: string]: string}; requestId: string; statusCode: number; apiVersion?: string; idempotencyKey?: string; stripeAccount?: string}}>}
*/
/* istanbul ignore next */
async createCheckoutSession(email, type = 'orientation') {
async createCheckoutSession(email, type = 'orientation', userId) {
const products = {
orientation: {
priceId: process.env.STRIPE_TICKET_PRICE_ID,
Expand Down Expand Up @@ -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') {
Expand Down

0 comments on commit bace853

Please sign in to comment.