import { redirect } from 'next/navigation'; import { match } from 'ts-pattern'; import { getPricesByInterval } from '@documenso/ee/server-only/stripe/get-prices-by-interval'; import { getProductByPriceId } from '@documenso/ee/server-only/stripe/get-product-by-price-id'; import { getRequiredServerComponentSession } from '@documenso/lib/next-auth/get-server-component-session'; import { getServerComponentFlag } from '@documenso/lib/server-only/feature-flags/get-server-component-feature-flag'; import type { Stripe } from '@documenso/lib/server-only/stripe'; import { getSubscriptionByUserId } from '@documenso/lib/server-only/subscription/get-subscription-by-user-id'; import { LocaleDate } from '~/components/formatter/locale-date'; import { BillingPlans } from './billing-plans'; import { BillingPortalButton } from './billing-portal-button'; export default async function BillingSettingsPage() { const { user } = await getRequiredServerComponentSession(); const isBillingEnabled = await getServerComponentFlag('app_billing'); // Redirect if subscriptions are not enabled. if (!isBillingEnabled) { redirect('/settings/profile'); } const [subscription, prices] = await Promise.all([ getSubscriptionByUserId({ userId: user.id }), getPricesByInterval(), ]); let subscriptionProduct: Stripe.Product | null = null; if (subscription?.priceId) { subscriptionProduct = await getProductByPriceId({ priceId: subscription.priceId }).catch( () => null, ); } const isMissingOrInactiveOrFreePlan = !subscription || subscription.status === 'INACTIVE'; return (

Billing

{isMissingOrInactiveOrFreePlan && (

You are currently on the Free Plan.

)} {!isMissingOrInactiveOrFreePlan && match(subscription.status) .with('ACTIVE', () => (

{subscriptionProduct ? ( You are currently subscribed to{' '} {subscriptionProduct.name} ) : ( You currently have an active plan )} {subscription.periodEnd && ( {' '} which is set to{' '} {subscription.cancelAtPeriodEnd ? ( end on{' '} . ) : ( automatically renew on{' '} . )} )}

)) .with('PAST_DUE', () => (

Your current plan is past due. Please update your payment information.

)) .otherwise(() => null)}

{isMissingOrInactiveOrFreePlan ? : }
); }