|
| 1 | +import { |
| 2 | + checkExternalMembership, |
| 3 | + checkPaidMembershipFromEntra, |
| 4 | + checkPaidMembershipFromTable, |
| 5 | + setPaidMembershipInTable, |
| 6 | + MEMBER_CACHE_SECONDS, |
| 7 | + checkPaidMembershipFromRedis, |
| 8 | +} from "api/functions/membership.js"; |
| 9 | +import { FastifyPluginAsync } from "fastify"; |
| 10 | +import { |
| 11 | + BaseError, |
| 12 | + InternalServerError, |
| 13 | + UnauthenticatedError, |
| 14 | + ValidationError, |
| 15 | +} from "common/errors/index.js"; |
| 16 | +import { getEntraIdToken } from "api/functions/entraId.js"; |
| 17 | +import { genericConfig, roleArns } from "common/config.js"; |
| 18 | +import { getRoleCredentials } from "api/functions/sts.js"; |
| 19 | +import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager"; |
| 20 | +import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; |
| 21 | +import rateLimiter from "api/plugins/rateLimiter.js"; |
| 22 | +import { createCheckoutSession } from "api/functions/stripe.js"; |
| 23 | +import { getSecretValue } from "api/plugins/auth.js"; |
| 24 | +import stripe, { Stripe } from "stripe"; |
| 25 | +import rawbody from "fastify-raw-body"; |
| 26 | +import { FastifyZodOpenApiTypeProvider } from "fastify-zod-openapi"; |
| 27 | +import * as z from "zod/v4"; |
| 28 | +import { |
| 29 | + illinoisNetId, |
| 30 | + notAuthenticatedError, |
| 31 | + withTags, |
| 32 | +} from "api/components/index.js"; |
| 33 | +import { getKey, setKey } from "api/functions/redisCache.js"; |
| 34 | +import { verifyUiucIdToken } from "./mobileWallet.js"; |
| 35 | + |
| 36 | +function splitOnce(s: string, on: string) { |
| 37 | + const [first, ...rest] = s.split(on); |
| 38 | + return [first, rest.length > 0 ? rest.join(on) : null]; |
| 39 | +} |
| 40 | +function trim(s: string) { |
| 41 | + return (s || "").replace(/^\s+|\s+$/g, ""); |
| 42 | +} |
| 43 | + |
| 44 | +const membershipV2Plugin: FastifyPluginAsync = async (fastify, _options) => { |
| 45 | + const limitedRoutes: FastifyPluginAsync = async (fastify) => { |
| 46 | + await fastify.register(rateLimiter, { |
| 47 | + limit: 15, |
| 48 | + duration: 30, |
| 49 | + rateLimitIdentifier: "membershipV2", |
| 50 | + }); |
| 51 | + fastify.withTypeProvider<FastifyZodOpenApiTypeProvider>().get( |
| 52 | + "/checkout", |
| 53 | + { |
| 54 | + schema: withTags(["Membership"], { |
| 55 | + headers: z.object({ |
| 56 | + "x-uiuc-id-token": z.jwt().min(1).meta({ |
| 57 | + description: |
| 58 | + "An ID token for the user in the UIUC Entra ID tenant.", |
| 59 | + }), |
| 60 | + }), |
| 61 | + summary: |
| 62 | + "Create a checkout session to purchase an ACM @ UIUC membership.", |
| 63 | + response: { |
| 64 | + 200: { |
| 65 | + description: "Stripe checkout link.", |
| 66 | + content: { |
| 67 | + "text/plain": { |
| 68 | + schema: z.url().meta({ |
| 69 | + example: |
| 70 | + "https://buy.stripe.com/test_14A00j9Hq9tj9ZfchM3AY0s", |
| 71 | + }), |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + 403: notAuthenticatedError, |
| 76 | + }, |
| 77 | + }), |
| 78 | + }, |
| 79 | + async (request, reply) => { |
| 80 | + const idToken = request.headers["x-uiuc-id-token"]; |
| 81 | + const verifiedData = await verifyUiucIdToken({ |
| 82 | + idToken, |
| 83 | + redisClient: fastify.redisClient, |
| 84 | + logger: request.log, |
| 85 | + }); |
| 86 | + const { preferred_username: upn, email, name } = verifiedData; |
| 87 | + const netId = upn.replace("@illinois.edu", ""); |
| 88 | + if (netId.includes("@")) { |
| 89 | + request.log.error( |
| 90 | + `Found UPN ${upn} which cannot be turned into NetID via simple replacement.`, |
| 91 | + ); |
| 92 | + throw new ValidationError({ |
| 93 | + message: "ID token could not be parsed.", |
| 94 | + }); |
| 95 | + } |
| 96 | + let isPaidMember = await checkPaidMembershipFromRedis( |
| 97 | + netId, |
| 98 | + fastify.redisClient, |
| 99 | + request.log, |
| 100 | + ); |
| 101 | + if (isPaidMember === null) { |
| 102 | + isPaidMember = await checkPaidMembershipFromTable( |
| 103 | + netId, |
| 104 | + fastify.dynamoClient, |
| 105 | + ); |
| 106 | + } |
| 107 | + if (isPaidMember) { |
| 108 | + throw new ValidationError({ |
| 109 | + message: `${upn} is already a paid member.`, |
| 110 | + }); |
| 111 | + } |
| 112 | + let firstName: string = ""; |
| 113 | + let lastName: string = ""; |
| 114 | + if (!name.includes(",")) { |
| 115 | + const splitted = splitOnce(name, " "); |
| 116 | + firstName = splitted[0] || ""; |
| 117 | + lastName = splitted[1] || ""; |
| 118 | + } |
| 119 | + firstName = trim(name.split(",")[1]); |
| 120 | + lastName = name.split(",")[0]; |
| 121 | + |
| 122 | + return reply.status(200).send( |
| 123 | + await createCheckoutSession({ |
| 124 | + successUrl: "https://acm.illinois.edu/paid", |
| 125 | + returnUrl: "https://acm.illinois.edu/membership", |
| 126 | + customerEmail: upn, |
| 127 | + stripeApiKey: fastify.secretConfig.stripe_secret_key as string, |
| 128 | + items: [ |
| 129 | + { |
| 130 | + price: fastify.environmentConfig.PaidMemberPriceId, |
| 131 | + quantity: 1, |
| 132 | + }, |
| 133 | + ], |
| 134 | + customFields: [ |
| 135 | + { |
| 136 | + key: "firstName", |
| 137 | + label: { |
| 138 | + type: "custom", |
| 139 | + custom: "Member First Name", |
| 140 | + }, |
| 141 | + type: "text", |
| 142 | + text: { |
| 143 | + default_value: firstName, |
| 144 | + }, |
| 145 | + }, |
| 146 | + { |
| 147 | + key: "lastName", |
| 148 | + label: { |
| 149 | + type: "custom", |
| 150 | + custom: "Member Last Name", |
| 151 | + }, |
| 152 | + type: "text", |
| 153 | + text: { |
| 154 | + default_value: lastName, |
| 155 | + }, |
| 156 | + }, |
| 157 | + ], |
| 158 | + initiator: "purchase-membership", |
| 159 | + allowPromotionCodes: true, |
| 160 | + }), |
| 161 | + ); |
| 162 | + }, |
| 163 | + ); |
| 164 | + }; |
| 165 | + fastify.register(limitedRoutes); |
| 166 | +}; |
| 167 | + |
| 168 | +export default membershipV2Plugin; |
0 commit comments