* WIP teams billing page * WIP * Create settings page * Remove unused imports * Create stripe customer on team creation * Add Stripe ids to team record * Add Stripe price ids for team to .env * Create & delete Stripe customers * Add string * Merge branch 'main' into v2/teams-billing * Create checkout session when creating team * Create webhook to update team with Stripe ids * Add Stripe migration files * Move deleting team from Stripe under ee * Some cleanup * Merge branch 'v2/teams-billing' of https://github.com/calcom/cal.com into v2/teams-billing * Small clean up * Link to team's portal page * Fix types * Fix type errors * Fix type errors * Fix type error * Delete old files & type fixes * Address feedback * Fix type errors * Removes team creation modal * WIP * Removed billing frequency from team creation * Add Stripe check for delete team customer * Merge branch 'v2/teams-billing' of https://github.com/calcom/cal.com into v2/teams-billing * Add high level form to create new team * WIP * Add new team to form * Validate for invited members * Add translations * WIP * Add validation for team name * Add validation to team slug * Clean up * Fix type error * Fix type errors * WIP * Abstract invite members function * Add subscription status column * Hide pending teams from settings * Send email on paid subscription * WIP * Sync packages * Add team subscription cols to schema * WIP * Matches locks vite version to <3 * Removed subscriptionStatus * WIP * Fix warning * Query optimizations * WIP * Cleanup * Wip * WIP * Runtime error fixes * Cancellation fixes * Delete team fixes * Cleanup * Type fixes * Allows to check memebership in getTeamWithMembers * Adds team creation tests * Cleanup * Cleanup * Restored change * Updated copy * Moved component * Cleanup * Fix team members view * Cleanup * Adds failsafe for skipping publishing on update * Cleanup * Feedback * More feedback * Cleanup * Cleanup * Feedback * Feedback * Feedback * Adds edge-case for slug conflicts * Feedback * e2e fixes Co-authored-by: Joe Au-Yeung <j.auyeung419@gmail.com> Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com>
72 lines
2.6 KiB
TypeScript
72 lines
2.6 KiB
TypeScript
import type { NextApiRequest, NextApiResponse } from "next";
|
|
import Stripe from "stripe";
|
|
import { z } from "zod";
|
|
|
|
import { getRequestedSlugError } from "@calcom/app-store/stripepayment/lib/team-billing";
|
|
import stripe from "@calcom/features/ee/payments/server/stripe";
|
|
import { ensureSession } from "@calcom/lib/auth";
|
|
import { WEBAPP_URL } from "@calcom/lib/constants";
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
import { defaultHandler, defaultResponder } from "@calcom/lib/server";
|
|
import { closeComUpdateTeam } from "@calcom/lib/sync/SyncServiceManager";
|
|
import prisma from "@calcom/prisma";
|
|
import { teamMetadataSchema } from "@calcom/prisma/zod-utils";
|
|
|
|
const querySchema = z.object({
|
|
team: z.string().transform((val) => parseInt(val)),
|
|
session_id: z.string().min(1),
|
|
});
|
|
|
|
async function handler(req: NextApiRequest, res: NextApiResponse) {
|
|
await ensureSession({ req });
|
|
|
|
const { team: id, session_id } = querySchema.parse(req.query);
|
|
|
|
const checkoutSession = await stripe.checkout.sessions.retrieve(session_id, {
|
|
expand: ["subscription"],
|
|
});
|
|
if (!checkoutSession) throw new HttpError({ statusCode: 404, message: "Checkout session not found" });
|
|
|
|
const subscription = checkoutSession.subscription as Stripe.Subscription;
|
|
if (checkoutSession.payment_status !== "paid")
|
|
throw new HttpError({ statusCode: 402, message: "Payment required" });
|
|
|
|
/* Check if a team was already upgraded with this payment intent */
|
|
let team = await prisma.team.findFirst({
|
|
where: { metadata: { path: ["paymentId"], equals: checkoutSession.id } },
|
|
});
|
|
|
|
if (!team) {
|
|
const prevTeam = await prisma.team.findFirstOrThrow({ where: { id } });
|
|
const metadata = teamMetadataSchema.parse(prevTeam.metadata);
|
|
if (!metadata?.requestedSlug) throw new HttpError({ statusCode: 400, message: "Missing requestedSlug" });
|
|
|
|
try {
|
|
team = await prisma.team.update({
|
|
where: { id },
|
|
data: {
|
|
slug: metadata.requestedSlug,
|
|
metadata: {
|
|
paymentId: checkoutSession.id,
|
|
subscriptionId: subscription.id || null,
|
|
subscriptionItemId: subscription.items.data[0].id || null,
|
|
},
|
|
},
|
|
});
|
|
} catch (error) {
|
|
const { message, statusCode } = getRequestedSlugError(error, metadata.requestedSlug);
|
|
return res.status(statusCode).json({ message });
|
|
}
|
|
|
|
// Sync Services: Close.com
|
|
closeComUpdateTeam(prevTeam, team);
|
|
}
|
|
|
|
// redirect to team screen
|
|
res.redirect(302, `${WEBAPP_URL}/settings/teams/${team.id}/profile?upgraded=true`);
|
|
}
|
|
|
|
export default defaultHandler({
|
|
GET: Promise.resolve({ default: defaultResponder(handler) }),
|
|
});
|