diff --git a/src/controllers/paymentController.ts b/src/controllers/paymentController.ts index 42e870d..770254f 100644 --- a/src/controllers/paymentController.ts +++ b/src/controllers/paymentController.ts @@ -109,10 +109,7 @@ export async function CreateCheckoutSession(priceId: string, userId: string) { return ""; } - if (cachedPrices.length === 0) { - logger.info("Loading prices from Stripe API"); - await loadPrices(); - } + await loadPrices(); const session = await stripe.checkout.sessions.create({ payment_method_types: ["card"], @@ -133,6 +130,7 @@ export async function CreateCheckoutSession(priceId: string, userId: string) { await UserPendingPayment.create({ payment_session_id: session.id, user_id: userId, + payment_session_url: session.url as string, }); logger.info(`CreateCheckoutSession: ${session.id}`); diff --git a/src/controllers/userController.ts b/src/controllers/userController.ts index 143ed61..f793921 100644 --- a/src/controllers/userController.ts +++ b/src/controllers/userController.ts @@ -43,6 +43,7 @@ import verifyCaptcha from "../utils/recaptcha"; import EmailVerification from "../models/emailVerification"; import UserPendingEmailChange from "../models/userPendingEmailChange"; import { CreateCheckoutSession, getPriceId } from "./paymentController"; +import UserPendingPayment from "../models/userPendingPayment"; export async function SignUp(req: Request, res: Response) { try { @@ -217,6 +218,36 @@ export async function Login(req: Request, res: Response) { // and only needs to enter their email to get the user state to know what to do next if (password === undefined) { + // user has signed up but not completed payment + // happens when user closed stripe checkout before completing payment + // and then tries to login + if (user.state === ACCOUNT_STATE.INIT_PAYMENT) { + const userPendingPayment = await UserPendingPayment.findOne({ + where: { + user_id: user.user_id, + }, + }); + + if ( + userPendingPayment && + userPendingPayment.payment_session_url !== "" + ) { + userLogger.info( + user.user_id, + "Sent email to complete init payment from signup" + ); + + rabbitmq.sendEmail( + email, + "dashboardSignUpUrlToCompletePayment", + user.language, + { + paymentSessionUrl: userPendingPayment.payment_session_url, + } + ); + } + } + return res.status(200).send({ state: user.state }); } diff --git a/src/models/userPendingPayment.ts b/src/models/userPendingPayment.ts index 6a81603..714828d 100644 --- a/src/models/userPendingPayment.ts +++ b/src/models/userPendingPayment.ts @@ -4,6 +4,7 @@ import sequelize from "../database/database"; interface UserPendingPaymentAttributes { payment_session_id: string; user_id: string; + payment_session_url?: string; } class UserPendingPayment @@ -12,6 +13,7 @@ class UserPendingPayment { declare payment_session_id: string; declare user_id: string; + declare payment_session_url: string; } UserPendingPayment.init( @@ -25,6 +27,10 @@ UserPendingPayment.init( type: DataTypes.STRING, allowNull: false, }, + payment_session_url: { + type: DataTypes.STRING, // varchar(1000) needs to be set manually + allowNull: true, + }, }, { sequelize,