Skip to content

The Open Source DocuSign Alternative.

feat: simplify billing ux (#2117)

Ephraim Duncane4e04cdddc84
diff --git a/apps/remix/app/components/general/org-menu-switcher.tsx b/apps/remix/app/components/general/org-menu-switcher.tsx
index c6c7c0040..68db67f9b 100644
--- a/apps/remix/app/components/general/org-menu-switcher.tsx
+++ b/apps/remix/app/components/general/org-menu-switcher.tsx
@@ -282,18 +282,6 @@ export const OrgMenuSwitcher = () => {
</DropdownMenuItem>
)}
- <DropdownMenuItem className="text-muted-foreground px-4 py-2" asChild>
- <Link to="/inbox">
- <Trans>Personal Inbox</Trans>
- </Link>
- </DropdownMenuItem>
-
- <DropdownMenuItem className="text-muted-foreground px-4 py-2" asChild>
- <Link to="/settings/profile">
- <Trans>Account</Trans>
- </Link>
- </DropdownMenuItem>
-
{currentOrganisation &&
canExecuteOrganisationAction(
'MANAGE_ORGANISATION',
@@ -314,6 +302,18 @@ export const OrgMenuSwitcher = () => {
</DropdownMenuItem>
)}
+ <DropdownMenuItem className="text-muted-foreground px-4 py-2" asChild>
+ <Link to="/inbox">
+ <Trans>Personal Inbox</Trans>
+ </Link>
+ </DropdownMenuItem>
+
+ <DropdownMenuItem className="text-muted-foreground px-4 py-2" asChild>
+ <Link to="/settings/profile">
+ <Trans>Account</Trans>
+ </Link>
+ </DropdownMenuItem>
+
<DropdownMenuItem
className="text-muted-foreground px-4 py-2"
onClick={() => setLanguageSwitcherOpen(true)}
diff --git a/apps/remix/app/components/general/organisations/organisation-billing-portal-button.tsx b/apps/remix/app/components/general/organisations/organisation-billing-portal-button.tsx
index 7e4e1cb53..c2e927d7d 100644
--- a/apps/remix/app/components/general/organisations/organisation-billing-portal-button.tsx
+++ b/apps/remix/app/components/general/organisations/organisation-billing-portal-button.tsx
@@ -3,6 +3,8 @@ import { useLingui } from '@lingui/react';
import { Trans } from '@lingui/react/macro';
import { useCurrentOrganisation } from '@documenso/lib/client-only/providers/organisation';
+import { useSession } from '@documenso/lib/client-only/providers/session';
+import { isPersonalLayout } from '@documenso/lib/utils/organisations';
import { canExecuteOrganisationAction } from '@documenso/lib/utils/organisations';
import { trpc } from '@documenso/trpc/react';
import { Button } from '@documenso/ui/primitives/button';
@@ -15,6 +17,8 @@ export type OrganisationBillingPortalButtonProps = {
export const OrganisationBillingPortalButton = ({
buttonProps,
}: OrganisationBillingPortalButtonProps) => {
+ const { organisations } = useSession();
+
const organisation = useCurrentOrganisation();
const { _ } = useLingui();
@@ -30,7 +34,10 @@ export const OrganisationBillingPortalButton = ({
const handleCreatePortal = async () => {
try {
- const { redirectUrl } = await manageSubscription({ organisationId: organisation.id });
+ const { redirectUrl } = await manageSubscription({
+ organisationId: organisation.id,
+ isPersonalLayoutMode: isPersonalLayout(organisations),
+ });
window.open(redirectUrl, '_blank');
} catch (err) {
diff --git a/apps/remix/app/components/general/settings-nav-desktop.tsx b/apps/remix/app/components/general/settings-nav-desktop.tsx
index 7be14fe23..20000f6d8 100644
--- a/apps/remix/app/components/general/settings-nav-desktop.tsx
+++ b/apps/remix/app/components/general/settings-nav-desktop.tsx
@@ -16,7 +16,7 @@ import { Link } from 'react-router';
import { useSession } from '@documenso/lib/client-only/providers/session';
import { IS_BILLING_ENABLED } from '@documenso/lib/constants/app';
-import { isPersonalLayout } from '@documenso/lib/utils/organisations';
+import { canExecuteOrganisationAction, isPersonalLayout } from '@documenso/lib/utils/organisations';
import { cn } from '@documenso/ui/lib/utils';
import { Button } from '@documenso/ui/primitives/button';
@@ -29,6 +29,10 @@ export const SettingsDesktopNav = ({ className, ...props }: SettingsDesktopNavPr
const isPersonalLayoutMode = isPersonalLayout(organisations);
+ const hasManageableBillingOrgs = organisations.some((org) =>
+ canExecuteOrganisationAction('MANAGE_BILLING', org.currentOrganisationRole),
+ );
+
return (
<div className={cn('flex flex-col gap-y-2', className)} {...props}>
<Link to="/settings/profile">
@@ -127,21 +131,6 @@ export const SettingsDesktopNav = ({ className, ...props }: SettingsDesktopNavPr
<Trans>Webhooks</Trans>
</Button>
</Link>
-
- {IS_BILLING_ENABLED() && (
- <Link to="/settings/billing">
- <Button
- variant="ghost"
- className={cn(
- 'w-full justify-start',
- pathname?.startsWith('/settings/billing') && 'bg-secondary',
- )}
- >
- <CreditCardIcon className="mr-2 h-5 w-5" />
- <Trans>Billing</Trans>
- </Button>
- </Link>
- )}
</>
)}
@@ -158,6 +147,21 @@ export const SettingsDesktopNav = ({ className, ...props }: SettingsDesktopNavPr
</Button>
</Link>
+ {IS_BILLING_ENABLED() && hasManageableBillingOrgs && (
+ <Link to={isPersonalLayoutMode ? '/settings/billing-personal' : `/settings/billing`}>
+ <Button
+ variant="ghost"
+ className={cn(
+ 'w-full justify-start',
+ pathname?.startsWith('/settings/billing') && 'bg-secondary',
+ )}
+ >
+ <CreditCardIcon className="mr-2 h-5 w-5" />
+ <Trans>Billing</Trans>
+ </Button>
+ </Link>
+ )}
+
<Link to="/settings/security">
<Button
variant="ghost"
diff --git a/apps/remix/app/components/general/settings-nav-mobile.tsx b/apps/remix/app/components/general/settings-nav-mobile.tsx
index 5278a6af9..5ca5de332 100644
--- a/apps/remix/app/components/general/settings-nav-mobile.tsx
+++ b/apps/remix/app/components/general/settings-nav-mobile.tsx
@@ -17,7 +17,7 @@ import { Link, useLocation } from 'react-router';
import { useSession } from '@documenso/lib/client-only/providers/session';
import { IS_BILLING_ENABLED } from '@documenso/lib/constants/app';
-import { isPersonalLayout } from '@documenso/lib/utils/organisations';
+import { canExecuteOrganisationAction, isPersonalLayout } from '@documenso/lib/utils/organisations';
import { cn } from '@documenso/ui/lib/utils';
import { Button } from '@documenso/ui/primitives/button';
@@ -30,6 +30,10 @@ export const SettingsMobileNav = ({ className, ...props }: SettingsMobileNavProp
const isPersonalLayoutMode = isPersonalLayout(organisations);
+ const hasManageableBillingOrgs = organisations.some((org) =>
+ canExecuteOrganisationAction('MANAGE_BILLING', org.currentOrganisationRole),
+ );
+
return (
<div
className={cn('flex flex-wrap items-center justify-start gap-x-2 gap-y-4', className)}
@@ -127,21 +131,6 @@ export const SettingsMobileNav = ({ className, ...props }: SettingsMobileNavProp
<Trans>Webhooks</Trans>
</Button>
</Link>
-
- {IS_BILLING_ENABLED() && (
- <Link to="/settings/billing">
- <Button
- variant="ghost"
- className={cn(
- 'w-full justify-start',
- pathname?.startsWith('/settings/billing') && 'bg-secondary',
- )}
- >
- <CreditCardIcon className="mr-2 h-5 w-5" />
- <Trans>Billing</Trans>
- </Button>
- </Link>
- )}
</>
)}
@@ -158,6 +147,21 @@ export const SettingsMobileNav = ({ className, ...props }: SettingsMobileNavProp
</Button>
</Link>
+ {IS_BILLING_ENABLED() && hasManageableBillingOrgs && (
+ <Link to={isPersonalLayoutMode ? '/settings/billing-personal' : `/settings/billing`}>
+ <Button
+ variant="ghost"
+ className={cn(
+ 'w-full justify-start',
+ pathname?.startsWith('/settings/billing') && 'bg-secondary',
+ )}
+ >
+ <CreditCardIcon className="mr-2 h-5 w-5" />
+ <Trans>Billing</Trans>
+ </Button>
+ </Link>
+ )}
+
<Link to="/settings/security">
<Button
variant="ghost"
diff --git a/apps/remix/app/components/tables/user-billing-organisations-table.tsx b/apps/remix/app/components/tables/user-billing-organisations-table.tsx
new file mode 100644
index 000000000..dd1e5823a
--- /dev/null
+++ b/apps/remix/app/components/tables/user-billing-organisations-table.tsx
@@ -0,0 +1,111 @@
+import { useMemo } from 'react';
+
+import { Trans, useLingui } from '@lingui/react/macro';
+import { SubscriptionStatus } from '@prisma/client';
+import { Link } from 'react-router';
+import { match } from 'ts-pattern';
+
+import { useSession } from '@documenso/lib/client-only/providers/session';
+import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app';
+import { formatAvatarUrl } from '@documenso/lib/utils/avatars';
+import { canExecuteOrganisationAction } from '@documenso/lib/utils/organisations';
+import { AvatarWithText } from '@documenso/ui/primitives/avatar';
+import { Badge } from '@documenso/ui/primitives/badge';
+import { Button } from '@documenso/ui/primitives/button';
+import type { DataTableColumnDef } from '@documenso/ui/primitives/data-table';
+import { DataTable } from '@documenso/ui/primitives/data-table';
+
+export const UserBillingOrganisationsTable = () => {
+ const { t } = useLingui();
+ const { organisations } = useSession();
+
+ const billingOrganisations = useMemo(() => {
+ return organisations.filter((org) =>
+ canExecuteOrganisationAction('MANAGE_BILLING', org.currentOrganisationRole),
+ );
+ }, [organisations]);
+
+ const getSubscriptionStatusDisplay = (status: SubscriptionStatus | undefined) => {
+ return match(status)
+ .with(SubscriptionStatus.ACTIVE, () => ({
+ label: t`Active`,
+ variant: 'default' as const,
+ }))
+ .with(SubscriptionStatus.PAST_DUE, () => ({
+ label: t`Past Due`,
+ variant: 'warning' as const,
+ }))
+ .with(SubscriptionStatus.INACTIVE, () => ({
+ label: t`Inactive`,
+ variant: 'neutral' as const,
+ }))
+ .otherwise(() => ({
+ label: t`Free`,
+ variant: 'neutral' as const,
+ }));
+ };
+
+ const columns = useMemo(() => {
+ return [
+ {
+ header: t`Organisation`,
+ accessorKey: 'name',
+ cell: ({ row }) => (
+ <Link to={`/o/${row.original.url}`} preventScrollReset={true}>
+ <AvatarWithText
+ avatarSrc={formatAvatarUrl(row.original.avatarImageId)}
+ avatarClass="h-12 w-12"
+ avatarFallback={row.original.name.slice(0, 1).toUpperCase()}
+ primaryText={
+ <span className="text-foreground/80 font-semibold">{row.original.name}</span>
+ }
+ secondaryText={`${NEXT_PUBLIC_WEBAPP_URL()}/o/${row.original.url}`}
+ />
+ </Link>
+ ),
+ },
+ {
+ header: t`Subscription Status`,
+ accessorKey: 'subscription',
+ cell: ({ row }) => {
+ const subscription = row.original.subscription;
+ const status = subscription?.status;
+ const { label, variant } = getSubscriptionStatusDisplay(status);
+
+ return <Badge variant={variant}>{label}</Badge>;
+ },
+ },
+ {
+ header: t`Actions`,
+ id: 'actions',
+ cell: ({ row }) => (
+ <Button asChild variant="outline">
+ <Link to={`/o/${row.original.url}/settings/billing`}>
+ <Trans>Manage Billing</Trans>
+ </Link>
+ </Button>
+ ),
+ },
+ ] satisfies DataTableColumnDef<(typeof billingOrganisations)[number]>[];
+ }, [billingOrganisations]);
+
+ if (billingOrganisations.length === 0) {
+ return (
+ <div className="text-muted-foreground flex flex-col items-center justify-center rounded-lg border border-dashed py-12 text-center">
+ <p className="text-sm">
+ <Trans>You don't manage billing for any organisations.</Trans>
+ </p>
+ </div>
+ );
+ }
+
+ return (
+ <DataTable
+ columns={columns}
+ data={billingOrganisations}
+ perPage={billingOrganisations.length}
+ currentPage={1}
+ totalPages={1}
+ />
+ );
+};
diff --git a/apps/remix/app/routes/_authenticated+/settings+/_dynamic_personal_routes+/billing.tsx b/apps/remix/app/routes/_authenticated+/settings+/_dynamic_personal_routes+/billing-personal.tsx
similarity index 100%
rename from apps/remix/app/routes/_authenticated+/settings+/_dynamic_personal_routes+/billing.tsx
rename to apps/remix/app/routes/_authenticated+/settings+/_dynamic_personal_routes+/billing-personal.tsx
diff --git a/apps/remix/app/routes/_authenticated+/settings+/billing.tsx b/apps/remix/app/routes/_authenticated+/settings+/billing.tsx
new file mode 100644
index 000000000..92f666f97
--- /dev/null
+++ b/apps/remix/app/routes/_authenticated+/settings+/billing.tsx
@@ -0,0 +1,24 @@
+import { useLingui } from '@lingui/react/macro';
+
+import { SettingsHeader } from '~/components/general/settings-header';
+import { UserBillingOrganisationsTable } from '~/components/tables/user-billing-organisations-table';
+import { appMetaTags } from '~/utils/meta';
+
+export function meta() {
+ return appMetaTags('Billing');
+}
+
+export default function SettingsBilling() {
+ const { t } = useLingui();
+
+ return (
+ <div>
+ <SettingsHeader
+ title={t`Billing`}
+ subtitle={t`Manage billing and subscriptions for organisations where you have billing management permissions.`}
+ />
+
+ <UserBillingOrganisationsTable />
+ </div>
+ );
+}
diff --git a/packages/trpc/server/enterprise-router/create-subscription.ts b/packages/trpc/server/enterprise-router/create-subscription.ts
index ddaf603aa..7a01d824e 100644
--- a/packages/trpc/server/enterprise-router/create-subscription.ts
+++ b/packages/trpc/server/enterprise-router/create-subscription.ts
@@ -71,7 +71,7 @@ export const createSubscriptionRoute = authenticatedProcedure
}
const returnUrl = isPersonalLayoutMode
- ? `${NEXT_PUBLIC_WEBAPP_URL()}/settings/billing`
+ ? `${NEXT_PUBLIC_WEBAPP_URL()}/settings/billing-personal`
: `${NEXT_PUBLIC_WEBAPP_URL()}/o/${organisation.url}/settings/billing`;
const redirectUrl = await createCheckoutSession({
diff --git a/packages/trpc/server/enterprise-router/manage-subscription.ts b/packages/trpc/server/enterprise-router/manage-subscription.ts
index 2a66ebd9e..8d7e688a9 100644
--- a/packages/trpc/server/enterprise-router/manage-subscription.ts
+++ b/packages/trpc/server/enterprise-router/manage-subscription.ts
@@ -12,7 +12,7 @@ import { ZManageSubscriptionRequestSchema } from './manage-subscription.types';
export const manageSubscriptionRoute = authenticatedProcedure
.input(ZManageSubscriptionRequestSchema)
.mutation(async ({ ctx, input }) => {
- const { organisationId } = input;
+ const { organisationId, isPersonalLayoutMode } = input;
ctx.logger.info({
input: {
@@ -93,9 +93,13 @@ export const manageSubscriptionRoute = authenticatedProcedure
});
}
+ const returnUrl = isPersonalLayoutMode
+ ? `${NEXT_PUBLIC_WEBAPP_URL()}/settings/billing-personal`
+ : `${NEXT_PUBLIC_WEBAPP_URL()}/o/${organisation.url}/settings/billing`;
+
const redirectUrl = await getPortalSession({
customerId,
- returnUrl: `${NEXT_PUBLIC_WEBAPP_URL()}/o/${organisation.url}/settings/billing`,
+ returnUrl,
});
return {
diff --git a/packages/trpc/server/enterprise-router/manage-subscription.types.ts b/packages/trpc/server/enterprise-router/manage-subscription.types.ts
index 0d77166ef..580639272 100644
--- a/packages/trpc/server/enterprise-router/manage-subscription.types.ts
+++ b/packages/trpc/server/enterprise-router/manage-subscription.types.ts
@@ -2,4 +2,5 @@ import { z } from 'zod';
export const ZManageSubscriptionRequestSchema = z.object({
organisationId: z.string().describe('The organisation to manage the subscription for'),
+ isPersonalLayoutMode: z.boolean().optional(),
});