email to complete payment on sign up

main
alex 2024-02-17 23:29:49 +01:00
parent ec879241c1
commit 617f521708
3 changed files with 39 additions and 4 deletions

View File

@ -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}`);

View File

@ -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 });
}

View File

@ -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,