fix: signup (#25334)

* Handle Stripe logic in `paymentCallback`

* Remove endpoint

* Do not send email verification email if premium username

* Remove logic from verify-view

* Callback send verification email

* Add `create` method to `VerificationToken` repository

* Create `VerificationTokenService`

* Early return if payment failed

* Refactor token generation

* Add tests

* Type fixes

* Type fixes
This commit is contained in:
Joe Au-Yeung authored and GitHub committed 2025-11-21 19:51:19 +00:00
1 parent 06348982a3
commit e5ebf7feb4
12 files changed
+714 -104

No files matched your search

@@ -0,0 +1,266 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { describe, it, expect, vi, beforeEach } from "vitest";
import sendVerificationRequest from "@calcom/features/auth/lib/sendVerificationRequest";
import { HttpError } from "@calcom/lib/http-error";
import { VerificationTokenService } from "@calcom/lib/server/service/VerificationTokenService";
import { prisma } from "@calcom/prisma";
import { getCustomerAndCheckoutSession } from "../../lib/getCustomerAndCheckoutSession";
// Mock dependencies
vi.mock("@calcom/prisma", () => ({
prisma: {
user: {
findFirst: vi.fn(),
update: vi.fn(),
},
},
}));
vi.mock("../../lib/getCustomerAndCheckoutSession");
vi.mock("@calcom/features/auth/lib/sendVerificationRequest");
vi.mock("@calcom/lib/server/service/VerificationTokenService");
const mockGetCustomerAndCheckoutSession = vi.mocked(getCustomerAndCheckoutSession);
const mockSendVerificationRequest = vi.mocked(sendVerificationRequest);
const mockVerificationTokenService = vi.mocked(VerificationTokenService);
// Type the mocked prisma properly
const mockPrisma = prisma as unknown as {
user: {
findFirst: ReturnType<typeof vi.fn>;
update: ReturnType<typeof vi.fn>;
};
};
describe("paymentCallback", () => {
let mockReq: Partial<NextApiRequest>;
let mockRes: Partial<NextApiResponse>;
beforeEach(() => {
vi.clearAllMocks();
mockReq = {
query: {
callbackUrl: "/premium-username-checkout",
checkoutSessionId: "cs_test_123",
},
url: "/api/payment/callback",
method: "GET",
};
mockRes = {
redirect: vi.fn().mockReturnThis(),
end: vi.fn().mockReturnThis(),
setHeader: vi.fn().mockReturnThis(),
status: vi.fn().mockReturnThis(),
json: vi.fn().mockReturnThis(),
};
// Default mock implementations
mockGetCustomerAndCheckoutSession.mockResolvedValue({
stripeCustomer: {
id: "cus_123",
email: "[email protected]",
metadata: {
username: "premium-user",
},
},
checkoutSession: {
payment_status: "paid",
},
} as any); // eslint-disable-line @typescript-eslint/no-explicit-any
mockPrisma.user.findFirst.mockResolvedValue({
id: 1,
email: "[email protected]",
locale: "en",
metadata: {},
} as any); // eslint-disable-line @typescript-eslint/no-explicit-any
mockPrisma.user.update.mockResolvedValue({
id: 1,
username: "premium-user",
} as any); // eslint-disable-line @typescript-eslint/no-explicit-any
mockVerificationTokenService.create.mockResolvedValue("test-token-123");
mockSendVerificationRequest.mockResolvedValue(undefined);
});
describe("VerificationTokenService integration", () => {
it("should call VerificationTokenService.create with correct parameters", async () => {
const { default: handler } = await import("../paymentCallback");
await handler(mockReq as NextApiRequest, mockRes as NextApiResponse);
expect(mockVerificationTokenService.create).toHaveBeenCalledWith({
identifier: "[email protected]",
expires: expect.any(Date),
});
const callArgs = mockVerificationTokenService.create.mock.calls[0][0];
const expiresDate = callArgs.expires;
const now = Date.now();
const oneDayInMs = 86400 * 1000;
// Verify expires is approximately 1 day from now (within 1 second tolerance)
expect(expiresDate.getTime()).toBeGreaterThan(now);
expect(expiresDate.getTime()).toBeLessThanOrEqual(now + oneDayInMs + 1000);
});
it("should send verification email with token from service", async () => {
const { default: handler } = await import("../paymentCallback");
await handler(mockReq as NextApiRequest, mockRes as NextApiResponse);
expect(mockSendVerificationRequest).toHaveBeenCalledWith({
identifier: "[email protected]",
url: expect.stringContaining("token=test-token-123"),
});
});
it("should create verification token before sending email", async () => {
const { default: handler } = await import("../paymentCallback");
const callOrder: string[] = [];
mockVerificationTokenService.create.mockImplementation(async () => {
callOrder.push("create-token");
return "test-token";
});
mockSendVerificationRequest.mockImplementation(async () => {
callOrder.push("send-email");
});
await handler(mockReq as NextApiRequest, mockRes as NextApiResponse);
expect(callOrder).toEqual(["create-token", "send-email"]);
});
it("should create verification token only after payment is confirmed", async () => {
mockGetCustomerAndCheckoutSession.mockResolvedValue({
stripeCustomer: {
id: "cus_123",
email: "[email protected]",
metadata: { username: "premium-user" },
},
checkoutSession: {
payment_status: "unpaid",
},
} as any); // eslint-disable-line @typescript-eslint/no-explicit-any
const { default: handler } = await import("../paymentCallback");
await handler(mockReq as NextApiRequest, mockRes as NextApiResponse);
expect(mockVerificationTokenService.create).not.toHaveBeenCalled();
expect(mockSendVerificationRequest).not.toHaveBeenCalled();
});
it("should handle user found by stripeCustomerId", async () => {
mockPrisma.user.findFirst
.mockResolvedValueOnce(null) // First call by email returns null
.mockResolvedValueOnce({
// Second call by stripeCustomerId succeeds
id: 2,
email: "[email protected]",
locale: "en",
metadata: { stripeCustomerId: "cus_123" },
} as any); // eslint-disable-line @typescript-eslint/no-explicit-any
const { default: handler } = await import("../paymentCallback");
await handler(mockReq as NextApiRequest, mockRes as NextApiResponse);
expect(mockVerificationTokenService.create).toHaveBeenCalledWith({
identifier: "[email protected]", // Should use user.email from found user
expires: expect.any(Date),
});
});
it("should update user with premium username before creating token", async () => {
const { default: handler } = await import("../paymentCallback");
const callOrder: string[] = [];
mockPrisma.user.update.mockImplementation(async () => {
callOrder.push("update-user");
return {} as any; // eslint-disable-line @typescript-eslint/no-explicit-any
});
mockVerificationTokenService.create.mockImplementation(async () => {
callOrder.push("create-token");
return "token";
});
await handler(mockReq as NextApiRequest, mockRes as NextApiResponse);
expect(callOrder).toEqual(["update-user", "create-token"]);
});
it("should redirect with correct parameters after successful payment", async () => {
const { default: handler } = await import("../paymentCallback");
await handler(mockReq as NextApiRequest, mockRes as NextApiResponse);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const redirectUrl = (mockRes.redirect as any).mock.calls[0][0] as string;
expect(redirectUrl).toContain("email=test%40example.com");
expect(redirectUrl).toContain("username=premium-user");
expect(redirectUrl).toContain("paymentStatus=paid");
});
it("should not create verification token when stripe customer is not found", async () => {
mockGetCustomerAndCheckoutSession.mockResolvedValue({
stripeCustomer: null,
checkoutSession: { payment_status: "paid" },
} as any); // eslint-disable-line @typescript-eslint/no-explicit-any
const { default: handler } = await import("../paymentCallback");
try {
await handler(mockReq as NextApiRequest, mockRes as NextApiResponse);
} catch (error) {
expect(error).toBeInstanceOf(HttpError);
expect((error as HttpError).statusCode).toBe(404);
}
expect(mockVerificationTokenService.create).not.toHaveBeenCalled();
});
it("should not create verification token when user is not found", async () => {
mockPrisma.user.findFirst.mockResolvedValue(null);
const { default: handler } = await import("../paymentCallback");
try {
await handler(mockReq as NextApiRequest, mockRes as NextApiResponse);
} catch (error) {
expect(error).toBeInstanceOf(HttpError);
expect((error as HttpError).statusCode).toBe(404);
}
expect(mockVerificationTokenService.create).not.toHaveBeenCalled();
});
it("should use user email if stripe customer email is missing", async () => {
mockGetCustomerAndCheckoutSession.mockResolvedValue({
stripeCustomer: {
id: "cus_123",
email: null,
metadata: { username: "premium-user" },
},
checkoutSession: { payment_status: "paid" },
} as any); // eslint-disable-line @typescript-eslint/no-explicit-any
const { default: handler } = await import("../paymentCallback");
await handler(mockReq as NextApiRequest, mockRes as NextApiResponse);
expect(mockVerificationTokenService.create).toHaveBeenCalledWith({
identifier: "[email protected]", // Should use user.email
expires: expect.any(Date),
});
});
});
});
@@ -2,10 +2,13 @@ import type { NextApiRequest, NextApiResponse } from "next";
import z from "zod";
import { getCustomerAndCheckoutSession } from "@calcom/app-store/stripepayment/lib/getCustomerAndCheckoutSession";
import sendVerificationRequest from "@calcom/features/auth/lib/sendVerificationRequest";
import { WEBAPP_URL } from "@calcom/lib/constants";
import { HttpError } from "@calcom/lib/http-error";
import logger from "@calcom/lib/logger";
import { defaultHandler } from "@calcom/lib/server/defaultHandler";
import { defaultResponder } from "@calcom/lib/server/defaultResponder";
import { VerificationTokenService } from "@calcom/lib/server/service/VerificationTokenService";
import { prisma } from "@calcom/prisma";
import type { Prisma } from "@calcom/prisma/client";
@@ -22,43 +25,76 @@ const querySchema = z.object({
// It handles premium user payment success/failure
async function getHandler(req: NextApiRequest, res: NextApiResponse) {
const { callbackUrl, checkoutSessionId } = querySchema.parse(req.query);
const log = logger.getSubLogger({ prefix: [`[paymentCallback] checkoutSessionId: ${checkoutSessionId}`] });
const { stripeCustomer, checkoutSession } = await getCustomerAndCheckoutSession(checkoutSessionId);
if (!stripeCustomer)
if (!stripeCustomer) {
log.error("Could not find stripeCustomer");
throw new HttpError({
statusCode: 404,
message: "Stripe customer not found or deleted",
message:
"Stripe customer not found or deleted. Please contact [email protected] and mention your premium username",
url: req.url,
method: req.method,
});
}
// first let's try to find user by metadata stripeCustomerId
let user = await prisma.user.findFirst({
where: {
metadata: {
path: ["stripeCustomerId"],
equals: stripeCustomer.id,
},
},
});
let user;
if (!user && stripeCustomer.email) {
// if user not found, let's try to find user by email
if (stripeCustomer?.email) {
user = await prisma.user.findFirst({
where: {
email: stripeCustomer.email,
},
select: {
id: true,
email: true,
locale: true,
metadata: true,
},
});
}
if (!user)
throw new HttpError({ statusCode: 404, message: "User not found", url: req.url, method: req.method });
// If we cannot find the user via email, query via stripeCustomerId
if (!user) {
user = await prisma.user.findFirst({
where: {
metadata: {
path: ["stripeCustomerId"],
equals: stripeCustomer.id,
},
},
select: {
id: true,
email: true,
locale: true,
metadata: true,
},
});
}
if (checkoutSession.payment_status === "paid" && stripeCustomer.metadata.username) {
if (!user) {
log.error("Could not find user");
throw new HttpError({ statusCode: 404, message: "User not found", url: req.url, method: req.method });
}
const username = stripeCustomer.metadata.username;
const email = user.email ?? stripeCustomer.email;
// If payment hasn't been completed, redirect to verify page with failure status
if (checkoutSession.payment_status !== "paid") {
log.warn("Payment not completed", { paymentStatus: checkoutSession.payment_status });
callbackUrl.searchParams.set("email", email || "");
callbackUrl.searchParams.set("username", username || "");
callbackUrl.searchParams.set("paymentStatus", checkoutSession.payment_status);
return res.redirect(callbackUrl.toString()).end();
}
if (stripeCustomer.metadata.username) {
try {
await prisma.user.update({
data: {
username: stripeCustomer.metadata.username,
username,
metadata: {
...(user.metadata as Prisma.JsonObject),
isPremium: true,
@@ -69,7 +105,7 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
},
});
} catch (error) {
console.error(error);
log.error(error);
throw new HttpError({
statusCode: 400,
url: req.url,
@@ -79,7 +115,29 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
});
}
}
const expires = new Date(Date.now() + 86400 * 1000); // 1 day
const token = await VerificationTokenService.create({ identifier: email, expires });
// Generate the callback URL with token
const params = new URLSearchParams({
callbackUrl: WEBAPP_URL || "https://app.cal.com",
token,
email,
});
const url = `${WEBAPP_URL}/api/auth/callback/email?${params.toString()}`;
await sendVerificationRequest({
identifier: email,
url,
});
// Pass email, username, and payment status in the redirect URL
callbackUrl.searchParams.set("email", email || "");
callbackUrl.searchParams.set("username", username);
callbackUrl.searchParams.set("paymentStatus", checkoutSession.payment_status);
return res.redirect(callbackUrl.toString()).end();
}