feat: adds admin billing section (#26747)
* feat: adds admin billing section * use loading prop * add log on resend error * add error log in billing portal
This commit is contained in:
1 parent
80941c17a0
commit
aaf5958b7d
10 files changed
+315
-18
No files matched your search
@@ -0,0 +1,25 @@
|
||||
import { _generateMetadata, getTranslate } from "app/_utils";
|
||||
|
||||
import SettingsHeader from "@calcom/features/settings/appDir/SettingsHeader";
|
||||
|
||||
import AdminBillingView from "~/settings/admin/billing-view";
|
||||
|
||||
export const generateMetadata = async () =>
|
||||
await _generateMetadata(
|
||||
(t) => t("admin_billing_title"),
|
||||
(t) => t("admin_billing_description"),
|
||||
undefined,
|
||||
undefined,
|
||||
"/settings/admin/billing"
|
||||
);
|
||||
|
||||
const Page = async () => {
|
||||
const t = await getTranslate();
|
||||
return (
|
||||
<SettingsHeader title={t("admin_billing_title")} description={t("admin_billing_description")}>
|
||||
<AdminBillingView />
|
||||
</SettingsHeader>
|
||||
);
|
||||
};
|
||||
|
||||
export default Page;
|
||||
+12
-6
@@ -220,6 +220,11 @@ const getTabs = (orgBranding: OrganizationBranding | null) => {
|
||||
href: "/auth/setup?step=1",
|
||||
trackingMetadata: { section: "admin", page: "license" },
|
||||
},
|
||||
{
|
||||
name: "admin_billing",
|
||||
href: "/settings/admin/billing",
|
||||
trackingMetadata: { section: "admin", page: "billing" },
|
||||
},
|
||||
{
|
||||
name: "impersonation",
|
||||
href: "/settings/admin/impersonation",
|
||||
@@ -673,12 +678,13 @@ const SettingsSidebarContainer = ({
|
||||
const searchParams = useCompatSearchParams();
|
||||
const orgBranding = useOrgBranding();
|
||||
const { t } = useLocale();
|
||||
const [otherTeamMenuState, setOtherTeamMenuState] = useState<
|
||||
{
|
||||
teamId: number | undefined;
|
||||
teamMenuOpen: boolean;
|
||||
}[]
|
||||
>();
|
||||
const [otherTeamMenuState, setOtherTeamMenuState] =
|
||||
useState<
|
||||
{
|
||||
teamId: number | undefined;
|
||||
teamMenuOpen: boolean;
|
||||
}[]
|
||||
>();
|
||||
const session = useSession();
|
||||
|
||||
const organizationId = session.data?.user?.org?.id;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
"use client";
|
||||
|
||||
import LicenseView from "./license-view";
|
||||
|
||||
export default function AdminBillingView() {
|
||||
return <LicenseView />;
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
|
||||
import { IS_CALCOM } from "@calcom/lib/constants";
|
||||
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
||||
import { trpc } from "@calcom/trpc/react";
|
||||
import { Button } from "@calcom/ui/components/button";
|
||||
import { PanelCard } from "@calcom/ui/components/card";
|
||||
import { TextField } from "@calcom/ui/components/form";
|
||||
import { showToast } from "@calcom/ui/components/toast";
|
||||
|
||||
export default function LicenseView() {
|
||||
const { t } = useLocale();
|
||||
const [billingEmail, setBillingEmail] = useState("");
|
||||
|
||||
const resendEmailMutation = trpc.viewer.admin.resendPurchaseCompleteEmail.useMutation({
|
||||
onSuccess: () => {
|
||||
showToast(t("admin_license_resend_success"), "success");
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast(error.message || t("admin_license_resend_error"), "error");
|
||||
},
|
||||
});
|
||||
|
||||
const billingPortalMutation = trpc.viewer.admin.billingPortalLink.useMutation({
|
||||
onSuccess: (data) => {
|
||||
if (!data?.url) {
|
||||
showToast(t("admin_license_portal_missing_url"), "error");
|
||||
return;
|
||||
}
|
||||
|
||||
window.open(data.url, "_blank", "noopener,noreferrer");
|
||||
},
|
||||
onError: (error) => {
|
||||
showToast(error.message || t("admin_license_portal_error"), "error");
|
||||
},
|
||||
});
|
||||
|
||||
const showResendSection = IS_CALCOM;
|
||||
|
||||
return (
|
||||
<div className="flex flex-col gap-4">
|
||||
{showResendSection && (
|
||||
<PanelCard title={t("admin_license_resend_title")} subtitle={t("admin_license_resend_description")}>
|
||||
<div className="p-4">
|
||||
<div className="flex flex-col gap-3 sm:flex-row sm:items-end">
|
||||
<TextField
|
||||
containerClassName="w-full"
|
||||
label={t("admin_license_billing_email_label")}
|
||||
name="billingEmail"
|
||||
type="email"
|
||||
placeholder={t("admin_license_billing_email_placeholder")}
|
||||
value={billingEmail}
|
||||
onChange={(event) => setBillingEmail(event.target.value)}
|
||||
/>
|
||||
<Button
|
||||
type="button"
|
||||
loading={resendEmailMutation.isPending}
|
||||
disabled={!billingEmail.trim()}
|
||||
onClick={() => {
|
||||
resendEmailMutation.mutate({
|
||||
billingEmail: billingEmail.trim(),
|
||||
});
|
||||
}}>
|
||||
{t("admin_license_resend_button")}
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</PanelCard>
|
||||
)}
|
||||
|
||||
<PanelCard title={t("admin_license_portal_title")} subtitle={t("admin_license_portal_description")}>
|
||||
<div className="p-4">
|
||||
<Button
|
||||
type="button"
|
||||
loading={billingPortalMutation.isPending}
|
||||
onClick={() => billingPortalMutation.mutate({})}>
|
||||
{t("admin_license_portal_button")}
|
||||
</Button>
|
||||
</div>
|
||||
</PanelCard>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -2150,6 +2150,23 @@
|
||||
"admin_orgs_edit_description": "Here you can edit an organization.",
|
||||
"admin_oAuth_description": "Add new OAuth clients",
|
||||
"admin_lockedSMS_description": "Lock or unlock SMS sending for users",
|
||||
"admin_license_description": "Manage license billing for this deployment",
|
||||
"admin_billing": "Billing",
|
||||
"admin_billing_title": "Admin billing",
|
||||
"admin_billing_description": "Manage billing emails and Stripe portal access for licenses.",
|
||||
"admin_license_resend_title": "Resend purchase confirmation",
|
||||
"admin_license_resend_description": "Send the purchase confirmation email to a billing address.",
|
||||
"admin_license_billing_email_label": "Billing email",
|
||||
"admin_license_billing_email_placeholder": "[email protected]",
|
||||
"admin_license_resend_button": "Resend email",
|
||||
"admin_license_resend_success": "Purchase confirmation email sent.",
|
||||
"admin_license_resend_error": "Failed to resend purchase confirmation email.",
|
||||
"admin_license_portal_title": "Billing portal",
|
||||
"admin_license_portal_description": "Open the Stripe billing portal for this license.",
|
||||
"admin_license_portal_button": "Open billing portal",
|
||||
"admin_license_portal_missing_url": "Billing portal link was not returned.",
|
||||
"admin_license_portal_error": "Failed to fetch billing portal link.",
|
||||
"admin_license_self_hosted_only": "License billing tools are available for self-hosted deployments only.",
|
||||
"no_available_apps": "There are no available apps",
|
||||
"no_available_apps_description": "Please ensure there are apps in your deployment under 'packages/app-store'",
|
||||
"no_apps": "There are no apps enabled in this instance of Cal.com",
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { authedAdminProcedure } from "../../../procedures/authedProcedure";
|
||||
import { router } from "../../../trpc";
|
||||
import { ZAdminAssignFeatureToTeamSchema } from "./assignFeatureToTeam.schema";
|
||||
import { ZBillingPortalLinkSchema } from "./billingPortalLink.schema";
|
||||
import { ZCreateSelfHostedLicenseSchema } from "./createSelfHostedLicenseKey.schema";
|
||||
import { ZAdminGetTeamsForFeatureSchema } from "./getTeamsForFeature.schema";
|
||||
import { ZListMembersSchema } from "./listPaginated.schema";
|
||||
import { ZAdminLockUserAccountSchema } from "./lockUserAccount.schema";
|
||||
import { ZAdminRemoveTwoFactor } from "./removeTwoFactor.schema";
|
||||
import { ZResendPurchaseCompleteEmailSchema } from "./resendPurchaseCompleteEmail.schema";
|
||||
import { ZAdminPasswordResetSchema } from "./sendPasswordReset.schema";
|
||||
import { ZSetSMSLockState } from "./setSMSLockState.schema";
|
||||
import { toggleFeatureFlag } from "./toggleFeatureFlag.procedure";
|
||||
@@ -56,6 +58,16 @@ export const adminRouter = router({
|
||||
const { default: handler } = await import("./createSelfHostedLicenseKey.handler");
|
||||
return handler(opts);
|
||||
}),
|
||||
resendPurchaseCompleteEmail: authedAdminProcedure
|
||||
.input(ZResendPurchaseCompleteEmailSchema)
|
||||
.mutation(async (opts) => {
|
||||
const { default: handler } = await import("./resendPurchaseCompleteEmail.handler");
|
||||
return handler(opts);
|
||||
}),
|
||||
billingPortalLink: authedAdminProcedure.input(ZBillingPortalLinkSchema).mutation(async (opts) => {
|
||||
const { default: handler } = await import("./billingPortalLink.handler");
|
||||
return handler(opts);
|
||||
}),
|
||||
verifyWorkflows: authedAdminProcedure.input(ZAdminVerifyWorkflowsSchema).mutation(async (opts) => {
|
||||
const { default: handler } = await import("./verifyWorkflows.handler");
|
||||
return handler(opts);
|
||||
@@ -64,18 +76,14 @@ export const adminRouter = router({
|
||||
const { default: handler } = await import("./whitelistUserWorkflows.handler");
|
||||
return handler(opts);
|
||||
}),
|
||||
getTeamsForFeature: authedAdminProcedure
|
||||
.input(ZAdminGetTeamsForFeatureSchema)
|
||||
.query(async (opts) => {
|
||||
const { default: handler } = await import("./getTeamsForFeature.handler");
|
||||
return handler(opts);
|
||||
}),
|
||||
assignFeatureToTeam: authedAdminProcedure
|
||||
.input(ZAdminAssignFeatureToTeamSchema)
|
||||
.mutation(async (opts) => {
|
||||
const { default: handler } = await import("./assignFeatureToTeam.handler");
|
||||
return handler(opts);
|
||||
}),
|
||||
getTeamsForFeature: authedAdminProcedure.input(ZAdminGetTeamsForFeatureSchema).query(async (opts) => {
|
||||
const { default: handler } = await import("./getTeamsForFeature.handler");
|
||||
return handler(opts);
|
||||
}),
|
||||
assignFeatureToTeam: authedAdminProcedure.input(ZAdminAssignFeatureToTeamSchema).mutation(async (opts) => {
|
||||
const { default: handler } = await import("./assignFeatureToTeam.handler");
|
||||
return handler(opts);
|
||||
}),
|
||||
unassignFeatureFromTeam: authedAdminProcedure
|
||||
.input(ZAdminUnassignFeatureFromTeamSchema)
|
||||
.mutation(async (opts) => {
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { createSignature, generateNonce } from "@calcom/features/ee/common/server/private-api-utils";
|
||||
import {
|
||||
getDeploymentKey,
|
||||
getDeploymentSignatureToken,
|
||||
} from "@calcom/features/ee/deployment/lib/getDeploymentKey";
|
||||
import { DeploymentRepository } from "@calcom/features/ee/deployment/repositories/DeploymentRepository";
|
||||
import { CALCOM_PRIVATE_API_ROUTE } from "@calcom/lib/constants";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../types";
|
||||
import type { TBillingPortalLinkSchema } from "./billingPortalLink.schema";
|
||||
|
||||
type GetOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TBillingPortalLinkSchema;
|
||||
};
|
||||
|
||||
const billingPortalLinkHandler = async ({ input }: GetOptions) => {
|
||||
if (!CALCOM_PRIVATE_API_ROUTE) {
|
||||
throw new Error("Private API route does not exist in .env");
|
||||
}
|
||||
|
||||
const deploymentRepo = new DeploymentRepository(prisma);
|
||||
const [licenseKey, signatureToken] = await Promise.all([
|
||||
getDeploymentKey(deploymentRepo),
|
||||
getDeploymentSignatureToken(deploymentRepo),
|
||||
]);
|
||||
|
||||
if (!licenseKey) {
|
||||
throw new Error("License key is missing");
|
||||
}
|
||||
|
||||
if (!signatureToken) {
|
||||
throw new Error("Signature token is missing");
|
||||
}
|
||||
|
||||
const nonce = generateNonce();
|
||||
const signature = createSignature(input, nonce, signatureToken);
|
||||
|
||||
const response = await fetch(`${CALCOM_PRIVATE_API_ROUTE}/v1/license/billing-portal-link`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
nonce,
|
||||
signature,
|
||||
"x-cal-license-key": licenseKey,
|
||||
},
|
||||
body: JSON.stringify(input),
|
||||
signal: AbortSignal.timeout(2000),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
logger.warn("Failed to fetch billing portal link", {
|
||||
message: data?.message,
|
||||
status: response.status,
|
||||
});
|
||||
throw new Error(data?.message || "Failed to fetch billing portal link");
|
||||
}
|
||||
|
||||
const schema = z.object({
|
||||
url: z.string(),
|
||||
});
|
||||
|
||||
return schema.parse(data);
|
||||
};
|
||||
|
||||
export default billingPortalLinkHandler;
|
||||
@@ -0,0 +1,5 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZBillingPortalLinkSchema = z.object({});
|
||||
|
||||
export type TBillingPortalLinkSchema = z.infer<typeof ZBillingPortalLinkSchema>;
|
||||
@@ -0,0 +1,63 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import { createSignature, generateNonce } from "@calcom/features/ee/common/server/private-api-utils";
|
||||
import { getDeploymentSignatureToken } from "@calcom/features/ee/deployment/lib/getDeploymentKey";
|
||||
import { DeploymentRepository } from "@calcom/features/ee/deployment/repositories/DeploymentRepository";
|
||||
import { CALCOM_PRIVATE_API_ROUTE } from "@calcom/lib/constants";
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { prisma } from "@calcom/prisma";
|
||||
|
||||
import type { TrpcSessionUser } from "../../../types";
|
||||
import type { TResendPurchaseCompleteEmailSchema } from "./resendPurchaseCompleteEmail.schema";
|
||||
|
||||
type GetOptions = {
|
||||
ctx: {
|
||||
user: NonNullable<TrpcSessionUser>;
|
||||
};
|
||||
input: TResendPurchaseCompleteEmailSchema;
|
||||
};
|
||||
|
||||
const resendPurchaseCompleteEmailHandler = async ({ input }: GetOptions) => {
|
||||
if (!CALCOM_PRIVATE_API_ROUTE) {
|
||||
throw new Error("Private API route does not exist in .env");
|
||||
}
|
||||
|
||||
const deploymentRepo = new DeploymentRepository(prisma);
|
||||
const signatureToken = await getDeploymentSignatureToken(deploymentRepo);
|
||||
|
||||
if (!signatureToken) {
|
||||
throw new Error("Signature token is missing");
|
||||
}
|
||||
|
||||
const nonce = generateNonce();
|
||||
const signature = createSignature(input, nonce, signatureToken);
|
||||
|
||||
const response = await fetch(`${CALCOM_PRIVATE_API_ROUTE}/v1/license/purchase-complete-email/resend`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
nonce,
|
||||
signature,
|
||||
},
|
||||
body: JSON.stringify(input),
|
||||
signal: AbortSignal.timeout(2000),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!response.ok) {
|
||||
logger.warn("Failed to resend purchase complete email", {
|
||||
message: data?.message,
|
||||
status: response.status,
|
||||
});
|
||||
throw new Error(data?.message || "Failed to resend purchase complete email");
|
||||
}
|
||||
|
||||
const schema = z.object({
|
||||
success: z.boolean(),
|
||||
});
|
||||
|
||||
return schema.parse(data);
|
||||
};
|
||||
|
||||
export default resendPurchaseCompleteEmailHandler;
|
||||
@@ -0,0 +1,7 @@
|
||||
import { z } from "zod";
|
||||
|
||||
export const ZResendPurchaseCompleteEmailSchema = z.object({
|
||||
billingEmail: z.string().email(),
|
||||
});
|
||||
|
||||
export type TResendPurchaseCompleteEmailSchema = z.infer<typeof ZResendPurchaseCompleteEmailSchema>;
|
||||
Reference in new issue
Block a user