|
| 1 | +import { defaultResponderForAppDir } from "app/api/defaultResponderForAppDir"; |
| 2 | +import type { NextRequest } from "next/server"; |
| 3 | +import { NextResponse } from "next/server"; |
| 4 | +import { z } from "zod"; |
| 5 | + |
| 6 | +import stripe from "@calcom/features/ee/payments/server/stripe"; |
| 7 | +import { FeaturesRepository } from "@calcom/features/flags/features.repository"; |
| 8 | +import { WEBAPP_URL } from "@calcom/lib/constants"; |
| 9 | +import { HttpError } from "@calcom/lib/http-error"; |
| 10 | +import { prisma } from "@calcom/prisma"; |
| 11 | + |
| 12 | +const querySchema = z.object({ |
| 13 | + session_id: z.string().min(1), |
| 14 | + paymentStatus: z.enum(["success", "failed"]), |
| 15 | +}); |
| 16 | + |
| 17 | +async function getHandler(req: NextRequest) { |
| 18 | + try { |
| 19 | + const searchParams = req.nextUrl.searchParams; |
| 20 | + const { session_id, paymentStatus } = querySchema.parse({ |
| 21 | + session_id: searchParams.get("session_id"), |
| 22 | + paymentStatus: searchParams.get("paymentStatus"), |
| 23 | + }); |
| 24 | + |
| 25 | + // Retrieve checkout session from Stripe to get metadata |
| 26 | + const checkoutSession = await stripe.checkout.sessions.retrieve(session_id); |
| 27 | + if (!checkoutSession) { |
| 28 | + throw new HttpError({ statusCode: 404, message: "Checkout session not found" }); |
| 29 | + } |
| 30 | + |
| 31 | + // Extract organizationOnboardingId from metadata |
| 32 | + const organizationOnboardingId = checkoutSession.metadata?.organizationOnboardingId; |
| 33 | + |
| 34 | + // Check if onboarding-v3 feature flag is enabled |
| 35 | + const featuresRepository = new FeaturesRepository(prisma); |
| 36 | + const isOnboardingV3Enabled = await featuresRepository.checkIfFeatureIsEnabledGlobally("onboarding-v3"); |
| 37 | + |
| 38 | + // Build query params to preserve |
| 39 | + const params = new URLSearchParams({ |
| 40 | + session_id, |
| 41 | + paymentStatus, |
| 42 | + }); |
| 43 | + |
| 44 | + // If onboarding-v3 is enabled AND organizationOnboardingId exists, redirect to onboarding flow |
| 45 | + if (isOnboardingV3Enabled && organizationOnboardingId) { |
| 46 | + const redirectUrl = new URL( |
| 47 | + `/onboarding/personal/settings?${params.toString()}`, |
| 48 | + WEBAPP_URL |
| 49 | + ).toString(); |
| 50 | + return NextResponse.redirect(redirectUrl); |
| 51 | + } |
| 52 | + |
| 53 | + // Otherwise, redirect to the current flow |
| 54 | + // Preserve any additional query params that were passed |
| 55 | + searchParams.forEach((value, key) => { |
| 56 | + if (key !== "session_id" && key !== "paymentStatus") { |
| 57 | + params.append(key, value); |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + const redirectUrl = new URL( |
| 62 | + `/settings/organizations/new/status?${params.toString()}`, |
| 63 | + WEBAPP_URL |
| 64 | + ).toString(); |
| 65 | + return NextResponse.redirect(redirectUrl); |
| 66 | + } catch (error) { |
| 67 | + if (error instanceof HttpError) { |
| 68 | + throw error; |
| 69 | + } |
| 70 | + // If there's an error, fall back to the default status page |
| 71 | + const searchParams = req.nextUrl.searchParams; |
| 72 | + const fallbackParams = new URLSearchParams({ |
| 73 | + session_id: searchParams.get("session_id") || "", |
| 74 | + paymentStatus: searchParams.get("paymentStatus") || "failed", |
| 75 | + }); |
| 76 | + const fallbackUrl = new URL( |
| 77 | + `/settings/organizations/new/status?${fallbackParams.toString()}`, |
| 78 | + WEBAPP_URL |
| 79 | + ).toString(); |
| 80 | + return NextResponse.redirect(fallbackUrl); |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +export const GET = defaultResponderForAppDir(getHandler); |
0 commit comments