-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
import { cookies, headers } from "next/headers"; | ||
import { redirect } from "next/navigation"; | ||
import type { NextRequest } from "next/server"; | ||
import { NextResponse } from "next/server"; | ||
import type Stripe from "stripe"; | ||
import { z } from "zod"; | ||
|
||
import { getRequestedSlugError } from "@calcom/app-store/stripepayment/lib/team-billing"; | ||
import { getServerSession } from "@calcom/features/auth/lib/getServerSession"; | ||
import stripe from "@calcom/features/ee/payments/server/stripe"; | ||
import { WEBAPP_URL } from "@calcom/lib/constants"; | ||
import { HttpError } from "@calcom/lib/http-error"; | ||
import prisma from "@calcom/prisma"; | ||
import { teamMetadataSchema } from "@calcom/prisma/zod-utils"; | ||
|
||
import { buildLegacyRequest } from "@lib/buildLegacyCtx"; | ||
|
||
const querySchema = z.object({ | ||
team: z.string().transform((val) => parseInt(val)), | ||
session_id: z.string().min(1), | ||
}); | ||
|
||
export async function GET(req: NextRequest, { params }: { params: { team: string } }) { | ||
try { | ||
const searchParams = req.nextUrl.searchParams; | ||
const { team: id, session_id } = querySchema.parse({ | ||
team: params.team, | ||
session_id: searchParams.get("session_id"), | ||
}); | ||
|
||
const checkoutSession = await stripe.checkout.sessions.retrieve(session_id, { | ||
expand: ["subscription"], | ||
}); | ||
if (!checkoutSession) { | ||
return NextResponse.json({ message: "Checkout session not found" }, { status: 404 }); | ||
} | ||
|
||
const subscription = checkoutSession.subscription as Stripe.Subscription; | ||
if (checkoutSession.payment_status !== "paid") { | ||
return NextResponse.json({ message: "Payment required" }, { status: 402 }); | ||
} | ||
|
||
let team = await prisma.team.findFirst({ | ||
where: { metadata: { path: ["paymentId"], equals: checkoutSession.id } }, | ||
}); | ||
|
||
let metadata; | ||
|
||
if (!team) { | ||
const prevTeam = await prisma.team.findFirstOrThrow({ where: { id } }); | ||
|
||
metadata = teamMetadataSchema.safeParse(prevTeam.metadata); | ||
if (!metadata.success) { | ||
return NextResponse.json({ message: "Invalid team metadata" }, { status: 400 }); | ||
} | ||
|
||
const { requestedSlug, ...newMetadata } = metadata.data || {}; | ||
team = await prisma.team.update({ | ||
where: { id }, | ||
data: { | ||
metadata: { | ||
...newMetadata, | ||
paymentId: checkoutSession.id, | ||
subscriptionId: subscription.id || null, | ||
subscriptionItemId: subscription.items.data[0].id || null, | ||
}, | ||
}, | ||
}); | ||
|
||
const slug = prevTeam.slug || requestedSlug; | ||
if (slug) { | ||
try { | ||
team = await prisma.team.update({ where: { id }, data: { slug } }); | ||
} catch (error) { | ||
const { message, statusCode } = getRequestedSlugError(error, slug); | ||
return NextResponse.json({ message }, { status: statusCode }); | ||
} | ||
} | ||
} | ||
|
||
if (!metadata) { | ||
metadata = teamMetadataSchema.safeParse(team.metadata); | ||
if (!metadata.success) { | ||
return NextResponse.json({ message: "Invalid team metadata" }, { status: 400 }); | ||
} | ||
} | ||
|
||
const session = await getServerSession({ req: buildLegacyRequest(headers(), cookies()) }); | ||
|
||
if (!session) { | ||
return NextResponse.json({ message: "Team upgraded successfully" }); | ||
} | ||
|
||
const redirectUrl = team?.isOrganization | ||
? `${WEBAPP_URL}/settings/organizations/profile?upgraded=true` | ||
: `${WEBAPP_URL}/settings/teams/${team.id}/profile?upgraded=true`; | ||
|
||
return redirect(redirectUrl); | ||
} catch (error) { | ||
if (error instanceof HttpError) { | ||
return NextResponse.json({ error: error.message }, { status: error.statusCode }); | ||
} | ||
return NextResponse.json({ error: "Internal Server Error" }, { status: 500 }); | ||
} | ||
} |