import type { Metadata } from 'next'; import { redirect } from 'next/navigation'; import { match } from 'ts-pattern'; import { getStripeCustomerByUser } from '@documenso/ee/server-only/stripe/get-customer'; import { getPricesByInterval } from '@documenso/ee/server-only/stripe/get-prices-by-interval'; import { getPrimaryAccountPlanPrices } from '@documenso/ee/server-only/stripe/get-primary-account-plan-prices'; import { getProductByPriceId } from '@documenso/ee/server-only/stripe/get-product-by-price-id'; import { STRIPE_PLAN_TYPE } from '@documenso/lib/constants/billing'; 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 { getSubscriptionsByUserId } from '@documenso/lib/server-only/subscription/get-subscriptions-by-user-id'; import { SubscriptionStatus } from '@documenso/prisma/client'; import { LocaleDate } from '~/components/formatter/locale-date'; import { BillingPlans } from './billing-plans'; import { BillingPortalButton } from './billing-portal-button'; export const metadata: Metadata = { title: 'Billing', }; export default async function BillingSettingsPage() { let { user } = await getRequiredServerComponentSession(); const isBillingEnabled = await getServerComponentFlag('app_billing'); // Redirect if subscriptions are not enabled. if (!isBillingEnabled) { redirect('/settings/profile'); } if (!user.customerId) { user = await getStripeCustomerByUser(user).then((result) => result.user); } const [subscriptions, prices, primaryAccountPlanPrices] = await Promise.all([ getSubscriptionsByUserId({ userId: user.id }), getPricesByInterval({ plan: STRIPE_PLAN_TYPE.COMMUNITY }), getPrimaryAccountPlanPrices(), ]); const primaryAccountPlanPriceIds = primaryAccountPlanPrices.map(({ id }) => id); let subscriptionProduct: Stripe.Product | null = null; const primaryAccountPlanSubscriptions = subscriptions.filter(({ priceId }) => primaryAccountPlanPriceIds.includes(priceId), ); const subscription = primaryAccountPlanSubscriptions.find(({ status }) => status === SubscriptionStatus.ACTIVE) ?? primaryAccountPlanSubscriptions[0]; if (subscription?.priceId) { subscriptionProduct = await getProductByPriceId({ priceId: subscription.priceId }).catch( () => null, ); } const isMissingOrInactiveOrFreePlan = !subscription || subscription.status === SubscriptionStatus.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 ? : }
); }