feat: billing tab for platform (#16755)

* modules for platform plans and billing pages

* add platform billing and plans related pages

* update platform navigation to include billing tab

* custom hook to upgrade team subscription

* refactors

* export cta row component

* fixup

* only pass in subscription id instead of whole subscription

* refactor :teamId/subscribe endpoint logic, add endpoint for upgrading stripe and shift all webhooks logic into services

* refactor team subscription logic, add logic for upgrading stripe and  webhooks logic from billing controller

---------

Co-authored-by: Peer Richelsen <[email protected]>
This commit is contained in:
Rajiv SahalandPeer Richelsen authored and GitHub committed 2024-09-23 13:36:03 +03:00
1 parent f642a6ebeb
commit 3371602e0f
13 files changed
+436 -107

No files matched your search

@@ -6,6 +6,7 @@ type PlatformBillingCardProps = {
pricing?: number;
includes: string[];
isLoading?: boolean;
currentPlan?: boolean;
handleSubscribe?: () => void;
};
@@ -16,12 +17,25 @@ export const PlatformBillingCard = ({
includes,
isLoading,
handleSubscribe,
currentPlan,
}: PlatformBillingCardProps) => {
return (
<div className="border-subtle mx-4 w-auto rounded-md border p-5 ">
<div className="border-subtle max-w-[450px] rounded-2xl border p-5 md:mx-4">
<div className="pb-5">
<h1 className="pb-3 pt-3 text-xl font-semibold">{plan}</h1>
<p className="pb-5 text-base">{description}</p>
<h1 className="border-b-[1px] pb-2 pt-1 text-center text-2xl font-bold">
{plan}
{currentPlan && (
<>
<Button
type="button"
StartIcon="circle-check"
className="bg-default hover:bg-default cursor-none text-green-500 hover:cursor-pointer"
tooltip="This is your current plan"
/>
</>
)}
</h1>
<p className="pb-5 pt-3 text-base">{description}</p>
<h1 className="text-3xl font-semibold">
{pricing && (
<>
@@ -30,14 +44,16 @@ export const PlatformBillingCard = ({
)}
</h1>
</div>
<div>
<Button
loading={isLoading}
onClick={handleSubscribe}
className="flex w-[100%] items-center justify-center">
{pricing ? "Subscribe" : "Schedule a time"}
</Button>
</div>
{!currentPlan && (
<div>
<Button
loading={isLoading}
onClick={handleSubscribe}
className="flex w-[100%] items-center justify-center">
{pricing ? "Subscribe" : "Schedule a time"}
</Button>
</div>
)}
<div className="mt-5">
<p>This includes:</p>
{includes.map((feature) => {
@@ -1,49 +1,78 @@
import { useRouter } from "next/navigation";
import { usePathname } from "next/navigation";
import type { ReactNode } from "react";
import { ErrorCode } from "@calcom/lib/errorCodes";
import { showToast } from "@calcom/ui";
import { useSubscribeTeamToStripe } from "@lib/hooks/settings/platform/oauth-clients/usePersistOAuthClient";
import {
useSubscribeTeamToStripe,
useUpgradeTeamSubscriptionInStripe,
} from "@lib/hooks/settings/platform/oauth-clients/usePersistOAuthClient";
import { platformPlans } from "@components/settings/platform/platformUtils";
import { PlatformBillingCard } from "@components/settings/platform/pricing/billing-card";
type PlatformPricingProps = { teamId?: number | null };
type PlatformPricingProps = { teamId?: number | null; teamPlan?: string; heading?: ReactNode };
export const PlatformPricing = ({ teamId }: PlatformPricingProps) => {
export const PlatformPricing = ({ teamId, teamPlan, heading }: PlatformPricingProps) => {
const pathname = usePathname();
const currentPage = pathname?.split("/").pop();
const router = useRouter();
const { mutateAsync, isPending } = useSubscribeTeamToStripe({
onSuccess: (redirectUrl: string) => {
router.push(redirectUrl);
},
onError: () => {
showToast(ErrorCode.UnableToSubscribeToThePlatform, "error");
},
teamId,
});
const { mutateAsync: createTeamSubscription, isPending: isCreateTeamSubscriptionLoading } =
useSubscribeTeamToStripe({
onSuccess: (redirectUrl: string) => {
router.push(redirectUrl);
},
onError: () => {
showToast(ErrorCode.UnableToSubscribeToThePlatform, "error");
},
teamId,
});
const { mutateAsync: upgradeTeamSubscription, isPending: isUpgradeTeamSubscriptionLoading } =
useUpgradeTeamSubscriptionInStripe({
onSuccess: (redirectUrl: string) => {
router.push(redirectUrl);
},
onError: () => {
showToast(ErrorCode.UnableToSubscribeToThePlatform, "error");
},
teamId,
});
const handleStripeSubscription = async (plan: string) => {
if (plan === "Enterprise") {
router.push("https://i.cal.com/sales/exploration");
}
if (currentPage === "platform") {
createTeamSubscription({ plan: plan.toLocaleUpperCase() });
} else {
upgradeTeamSubscription({ plan: plan.toLocaleUpperCase() });
}
};
if (!teamId) {
return <div className="m-5">Platform team not present, you need to create a team first.</div>;
}
return (
<div className="flex h-auto flex-col items-center justify-center px-5 py-10 md:px-10 lg:h-[100%]">
<div className="mb-5 text-center text-2xl font-semibold">
<h1>Subscribe to Platform</h1>
</div>
<div className="container mx-auto p-4">
{heading}
<div>
<div className="grid grid-cols-1 gap-5 md:grid-cols-2 lg:grid-cols-4">
{platformPlans.map((plan) => {
return (
<div key={plan.plan}>
<div key={plan.plan} className="mx-10 my-4 md:mx-0 md:my-0">
<PlatformBillingCard
plan={plan.plan}
description={plan.description}
pricing={plan.pricing}
includes={plan.includes}
isLoading={isPending}
handleSubscribe={() => {
!!teamId &&
(plan.plan === "Enterprise"
? router.push("https://i.cal.com/sales/exploration")
: mutateAsync({ plan: plan.plan.toLocaleUpperCase() }));
}}
isLoading={isCreateTeamSubscriptionLoading || isUpgradeTeamSubscriptionLoading}
currentPlan={plan.plan.toLocaleLowerCase() === teamPlan}
handleSubscribe={() => handleStripeSubscription(plan.plan)}
/>
</div>
);