-
Notifications
You must be signed in to change notification settings - Fork 30
Emily/20250104 handle membership payment confirmation #1993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
adarshm11
merged 14 commits into
SCE-Development:pending-payment
from
codebyemily:Emily/20250104_handle_membership_payment_confirmation
Jan 7, 2026
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ccd7b6f
initial commit
codebyemily f2cccc7
fixed spacing
codebyemily 026fe55
fixed spacing 2
codebyemily 6e3675f
fixed spacing 2
codebyemily a38266b
updated logic and merged find and Verify functions
codebyemily 81da36f
fix spacing
codebyemily 7fe7f3a
refactor: improve payment verification and rejection logic
codebyemily d5f8c14
fix
codebyemily 4105945
refactor: improve error handling in membership payment verification a…
codebyemily eec1164
fix
codebyemily 06fc5ee
final fixes
codebyemily 5d7079e
final fixes
codebyemily 9a831fe
refactored updateMembershipExpiration
codebyemily 367630c
fix spacing
codebyemily File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| const express = require('express'); | ||
| const router = express.Router(); | ||
| const bodyParser = require('body-parser'); | ||
| router.use(bodyParser.json()); | ||
| const { | ||
| BAD_REQUEST, | ||
| SERVER_ERROR, | ||
| NOT_FOUND, | ||
| OK, | ||
| } = require('../../util/constants').STATUS_CODES; | ||
| const membershipState = require('../../util/constants').MEMBERSHIP_STATE; | ||
| const User = require('../models/User'); | ||
| const { getMemberExpirationDate, updateMembershipExpiration } = require('../util/userHelpers'); | ||
| const { findVerifyPayment, rejectPayment } = require('../util/membershipPaymentQueries'); | ||
| const { decodeToken } = require('../util/token-functions.js'); | ||
|
|
||
| router.post('/verifyMembership', async (req, res) => { | ||
| const decoded = await decodeToken(req, membershipState.PENDING); | ||
| if (decoded.status !== OK) { | ||
| return res.sendStatus(decoded.status); | ||
| } | ||
|
|
||
| const { confirmationCode } = req.body; | ||
| const userId = decoded.token._id; | ||
|
|
||
| if (!confirmationCode) { | ||
| return res.sendStatus(BAD_REQUEST); | ||
| } | ||
|
|
||
| const paymentDocument = await findVerifyPayment(confirmationCode, userId); | ||
| if (paymentDocument === null){ | ||
| return res.sendStatus(SERVER_ERROR); | ||
| } | ||
| if (paymentDocument === false){ | ||
| return res.sendStatus(NOT_FOUND); | ||
| } | ||
|
|
||
| const paymentId = paymentDocument._id; | ||
| const { amount } = paymentDocument; | ||
|
|
||
| if (amount < 20){ | ||
| const rejected = await rejectPayment(paymentId); | ||
| if (rejected === null){ | ||
| return res.sendStatus(SERVER_ERROR); | ||
| } | ||
| if (rejected === false){ | ||
| return res.sendStatus(NOT_FOUND); | ||
| } | ||
| return res.sendStatus(BAD_REQUEST); | ||
| } | ||
|
|
||
| let semestersToAdd = 0; | ||
| if (amount >= 30) { | ||
| semestersToAdd = 2; | ||
| } else { | ||
| semestersToAdd = 1; | ||
| } | ||
|
|
||
| const membershipUpdateResult = await updateMembershipExpiration( | ||
| decoded.token._id, | ||
| semestersToAdd | ||
| ); | ||
|
|
||
| if (membershipUpdateResult === null) { | ||
| return res.sendStatus(SERVER_ERROR); | ||
| } | ||
| if (membershipUpdateResult === false) { | ||
| return res.status(NOT_FOUND).send('User not found.'); | ||
| } | ||
| return res.sendStatus(OK); | ||
| }); | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import MembershipPayment from '../main_endpoints/models/MembershipPayment.js'; | ||
|
|
||
| const status = { | ||
| PENDING: 'pending', | ||
| COMPLETED: 'completed', | ||
| REJECTED: 'rejected', | ||
| }; | ||
|
|
||
| function findVerifyPayment(confirmationCode, userId) { | ||
| return new Promise((resolve) => { | ||
| try { | ||
| MembershipPayment.findOneAndUpdate( | ||
| { | ||
| confirmationCode, | ||
| status: status.PENDING, | ||
| }, | ||
| { | ||
| $set: { userId, status: status.COMPLETED }, | ||
| }, | ||
| { | ||
| new: true, | ||
| runValidators: true, | ||
| }, | ||
| (error, result) => { | ||
| if (error) { | ||
| return resolve(null); | ||
| } | ||
| if (!result) { | ||
| return resolve(false); | ||
| } | ||
| return resolve(result); | ||
| } | ||
| ); | ||
| } catch (error) { | ||
| return resolve(null); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function rejectPayment(paymentId) { | ||
| return new Promise((resolve) => { | ||
| try { | ||
| MembershipPayment.findByIdAndUpdate( | ||
| paymentId, | ||
| { $set: { status: status.REJECTED } }, | ||
| (error, result) => { | ||
| if (error) { | ||
| return resolve(null); | ||
| } | ||
| if (!result) { | ||
| return resolve(false); | ||
| } | ||
| return resolve(true); | ||
| } | ||
| ); | ||
| } catch (error) { | ||
| return resolve(null); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
adarshm11 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| module.exports = { findVerifyPayment, rejectPayment }; | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.