Skip to content

armgit / papra

Public Imported from GitHub
0

The minimalistic document archiving platform.

feat(subscriptions): add billing interval options (#533)

Corentin Thomasset630f9cc328c6
diff --git a/apps/papra-client/src/locales/de.dictionary.ts b/apps/papra-client/src/locales/de.dictionary.ts
index ed7008d4..a9d1de17 100644
--- a/apps/papra-client/src/locales/de.dictionary.ts
+++ b/apps/papra-client/src/locales/de.dictionary.ts
@@ -625,4 +625,6 @@ export const translations: Partial<TranslationsDictionary> = {
'subscriptions.features.support-community': 'Community-Support',
'subscriptions.features.support-email': 'E-Mail-Support',
+ 'subscriptions.billing-interval.monthly': 'Monatlich',
+ 'subscriptions.billing-interval.annual': 'Jährlich',
};
diff --git a/apps/papra-client/src/locales/en.dictionary.ts b/apps/papra-client/src/locales/en.dictionary.ts
index dadb7854..acf76c7a 100644
--- a/apps/papra-client/src/locales/en.dictionary.ts
+++ b/apps/papra-client/src/locales/en.dictionary.ts
@@ -623,4 +623,6 @@ export const translations = {
'subscriptions.features.support-community': 'Community support',
'subscriptions.features.support-email': 'Email support',
+ 'subscriptions.billing-interval.monthly': 'Monthly',
+ 'subscriptions.billing-interval.annual': 'Annual',
} as const;
diff --git a/apps/papra-client/src/locales/es.dictionary.ts b/apps/papra-client/src/locales/es.dictionary.ts
index f482d2ec..7c875c1c 100644
--- a/apps/papra-client/src/locales/es.dictionary.ts
+++ b/apps/papra-client/src/locales/es.dictionary.ts
@@ -625,4 +625,6 @@ export const translations: Partial<TranslationsDictionary> = {
'subscriptions.features.support-community': 'Soporte de la comunidad',
'subscriptions.features.support-email': 'Soporte por correo',
+ 'subscriptions.billing-interval.monthly': 'Mensual',
+ 'subscriptions.billing-interval.annual': 'Anual',
};
diff --git a/apps/papra-client/src/locales/fr.dictionary.ts b/apps/papra-client/src/locales/fr.dictionary.ts
index 686f542b..c132280d 100644
--- a/apps/papra-client/src/locales/fr.dictionary.ts
+++ b/apps/papra-client/src/locales/fr.dictionary.ts
@@ -625,4 +625,6 @@ export const translations: Partial<TranslationsDictionary> = {
'subscriptions.features.support-community': 'Support communautaire',
'subscriptions.features.support-email': 'Support par email',
+ 'subscriptions.billing-interval.monthly': 'Mensuel',
+ 'subscriptions.billing-interval.annual': 'Annuel',
};
diff --git a/apps/papra-client/src/locales/it.dictionary.ts b/apps/papra-client/src/locales/it.dictionary.ts
index 03fb8ecc..4b8d6b4f 100644
--- a/apps/papra-client/src/locales/it.dictionary.ts
+++ b/apps/papra-client/src/locales/it.dictionary.ts
@@ -625,4 +625,6 @@ export const translations: Partial<TranslationsDictionary> = {
'subscriptions.features.support-community': 'Supporto della comunità',
'subscriptions.features.support-email': 'Supporto via email',
+ 'subscriptions.billing-interval.monthly': 'Mensile',
+ 'subscriptions.billing-interval.annual': 'Annuale',
};
diff --git a/apps/papra-client/src/locales/pl.dictionary.ts b/apps/papra-client/src/locales/pl.dictionary.ts
index a20fe8b6..91d22f1b 100644
--- a/apps/papra-client/src/locales/pl.dictionary.ts
+++ b/apps/papra-client/src/locales/pl.dictionary.ts
@@ -625,4 +625,6 @@ export const translations: Partial<TranslationsDictionary> = {
'subscriptions.features.support-community': 'Wsparcie społeczności',
'subscriptions.features.support-email': 'Wsparcie e-mail',
+ 'subscriptions.billing-interval.monthly': 'Miesięcznie',
+ 'subscriptions.billing-interval.annual': 'Rocznie',
};
diff --git a/apps/papra-client/src/locales/pt-BR.dictionary.ts b/apps/papra-client/src/locales/pt-BR.dictionary.ts
index 96d0246f..cf09876a 100644
--- a/apps/papra-client/src/locales/pt-BR.dictionary.ts
+++ b/apps/papra-client/src/locales/pt-BR.dictionary.ts
@@ -625,4 +625,6 @@ export const translations: Partial<TranslationsDictionary> = {
'subscriptions.features.support-community': 'Suporte da comunidade',
'subscriptions.features.support-email': 'Suporte por e-mail',
+ 'subscriptions.billing-interval.monthly': 'Mensal',
+ 'subscriptions.billing-interval.annual': 'Anual',
};
diff --git a/apps/papra-client/src/locales/pt.dictionary.ts b/apps/papra-client/src/locales/pt.dictionary.ts
index 61c07023..0ba95e14 100644
--- a/apps/papra-client/src/locales/pt.dictionary.ts
+++ b/apps/papra-client/src/locales/pt.dictionary.ts
@@ -625,4 +625,6 @@ export const translations: Partial<TranslationsDictionary> = {
'subscriptions.features.support-community': 'Suporte da comunidade',
'subscriptions.features.support-email': 'Suporte por e-mail',
+ 'subscriptions.billing-interval.monthly': 'Mensal',
+ 'subscriptions.billing-interval.annual': 'Anual',
};
diff --git a/apps/papra-client/src/locales/ro.dictionary.ts b/apps/papra-client/src/locales/ro.dictionary.ts
index 11450840..a2f48e82 100644
--- a/apps/papra-client/src/locales/ro.dictionary.ts
+++ b/apps/papra-client/src/locales/ro.dictionary.ts
@@ -625,4 +625,6 @@ export const translations: Partial<TranslationsDictionary> = {
'subscriptions.features.support-community': 'Asistență comunitate',
'subscriptions.features.support-email': 'Asistență email',
+ 'subscriptions.billing-interval.monthly': 'Lunar',
+ 'subscriptions.billing-interval.annual': 'Anual',
};
diff --git a/apps/papra-client/src/modules/subscriptions/components/upgrade-dialog.component.tsx b/apps/papra-client/src/modules/subscriptions/components/upgrade-dialog.component.tsx
index 96196e8d..61e19a2a 100644
--- a/apps/papra-client/src/modules/subscriptions/components/upgrade-dialog.component.tsx
+++ b/apps/papra-client/src/modules/subscriptions/components/upgrade-dialog.component.tsx
@@ -4,6 +4,7 @@ import { safely } from '@corentinth/chisels';
import { createSignal } from 'solid-js';
import { useI18n } from '@/modules/i18n/i18n.provider';
import { PLUS_PLAN_ID } from '@/modules/plans/plans.constants';
+import { cn } from '@/modules/shared/style/cn';
import { Button } from '@/modules/ui/components/button';
import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger } from '@/modules/ui/components/dialog';
import { getCheckoutUrl } from '../subscriptions.services';
@@ -19,7 +20,7 @@ type PlanCardProps = {
};
isRecommended?: boolean;
isCurrent?: boolean;
- monthlyPrice: number;
+ price: number;
onUpgrade?: () => Promise<void>;
};
@@ -75,7 +76,7 @@ const PlanCard: Component<PlanCardProps> = (props) => {
</div>
<div class="text-xl font-semibold flex items-center gap-2">
$
- {props.monthlyPrice}
+ {props.price}
<span class="text-sm font-normal text-muted-foreground">{t('subscriptions.upgrade-dialog.per-month')}</span>
</div>
@@ -101,7 +102,7 @@ const PlanCard: Component<PlanCardProps> = (props) => {
<>
<hr class="my-4" />
- <Button onClick={upgrade} class="w-full" auto-focus isLoading={getIsUpgradeLoading()}>
+ <Button onClick={upgrade} class="w-full" autofocus isLoading={getIsUpgradeLoading()}>
{t('subscriptions.upgrade-dialog.upgrade-now')}
<div class="i-tabler-arrow-right size-5 ml-2"></div>
</Button>
@@ -120,9 +121,11 @@ type UpgradeDialogProps = {
export const UpgradeDialog: Component<UpgradeDialogProps> = (props) => {
const { t } = useI18n();
const [getIsOpen, setIsOpen] = createSignal(false);
+ const defaultBillingInterval: 'monthly' | 'annual' = 'annual';
+ const [getBillingInterval, setBillingInterval] = createSignal<'monthly' | 'annual'>(defaultBillingInterval);
const onUpgrade = async () => {
- const { checkoutUrl } = await getCheckoutUrl({ organizationId: props.organizationId, planId: PLUS_PLAN_ID });
+ const { checkoutUrl } = await getCheckoutUrl({ organizationId: props.organizationId, planId: PLUS_PLAN_ID, billingInterval: getBillingInterval() });
window.location.href = checkoutUrl;
};
@@ -130,6 +133,7 @@ export const UpgradeDialog: Component<UpgradeDialogProps> = (props) => {
const currentPlan = {
name: t('subscriptions.plan.free.name'),
monthlyPrice: 0,
+ annualPrice: 0,
features: {
storageSize: 0.5, // 500MB = 0.5GB
members: 3,
@@ -142,7 +146,8 @@ export const UpgradeDialog: Component<UpgradeDialogProps> = (props) => {
const plusPlan = {
name: t('subscriptions.plan.plus.name'),
- monthlyPrice: 12,
+ monthlyPrice: 9,
+ annualPrice: 90,
features: {
storageSize: 5,
members: 10,
@@ -153,6 +158,10 @@ export const UpgradeDialog: Component<UpgradeDialogProps> = (props) => {
isRecommended: true,
};
+ const getPlanPrice = (plan: { monthlyPrice: number; annualPrice: number }) => {
+ return getBillingInterval() === 'monthly' ? plan.monthlyPrice : Math.round(100 * plan.annualPrice / 12) / 100;
+ };
+
return (
<Dialog open={getIsOpen()} onOpenChange={setIsOpen}>
<DialogTrigger as={props.children} />
@@ -171,9 +180,31 @@ export const UpgradeDialog: Component<UpgradeDialogProps> = (props) => {
</div>
</DialogHeader>
- <div class="pt-4 grid grid-cols-1 md:grid-cols-2 gap-2 ">
+ <div class="mt-2 flex flex-col items-center">
+ <div class="inline-flex items-center justify-center border rounded-lg bg-muted p-1 gap-2">
+ <Button
+ size="sm"
+ variant="ghost"
+ class={cn('text-sm', { 'bg-primary/10 text-primary hover:(bg-primary/10 text-primary)': getBillingInterval() === 'monthly' })}
+ onClick={() => setBillingInterval('monthly')}
+ >
+ {t('subscriptions.billing-interval.monthly')}
+ </Button>
+ <Button
+ size="sm"
+ variant="ghost"
+ class={cn('text-sm pr-1.5', { 'bg-primary/10 text-primary hover:(bg-primary/10 text-primary)': getBillingInterval() === 'annual' })}
+ onClick={() => setBillingInterval('annual')}
+ >
+ {t('subscriptions.billing-interval.annual')}
+ <span class="ml-2 text-xs text-muted-foreground rounded bg-primary/10 text-primary px-1 py-0.5">-20%</span>
+ </Button>
+ </div>
+ </div>
+
+ <div class="mt-2 grid grid-cols-1 md:grid-cols-2 gap-2 ">
<div>
- <PlanCard {...currentPlan} />
+ <PlanCard {...currentPlan} price={getPlanPrice(currentPlan)} />
<p class="text-muted-foreground text-xs p-4 ml-1">
<a href="https://papra.app/contact" class="underline" target="_blank" rel="noreferrer">{t('subscriptions.upgrade-dialog.contact-us')}</a>
@@ -181,7 +212,7 @@ export const UpgradeDialog: Component<UpgradeDialogProps> = (props) => {
{t('subscriptions.upgrade-dialog.enterprise-plans')}
</p>
</div>
- <PlanCard {...plusPlan} onUpgrade={onUpgrade} />
+ <PlanCard {...plusPlan} onUpgrade={onUpgrade} price={getPlanPrice(plusPlan)} />
</div>
</DialogContent>
</Dialog>
diff --git a/apps/papra-client/src/modules/subscriptions/subscriptions.services.ts b/apps/papra-client/src/modules/subscriptions/subscriptions.services.ts
index 4634d77b..696f9d09 100644
--- a/apps/papra-client/src/modules/subscriptions/subscriptions.services.ts
+++ b/apps/papra-client/src/modules/subscriptions/subscriptions.services.ts
@@ -1,12 +1,13 @@
import type { OrganizationSubscription } from './subscriptions.types';
import { apiClient } from '../shared/http/api-client';
-export async function getCheckoutUrl({ organizationId, planId }: { organizationId: string; planId: string }) {
+export async function getCheckoutUrl({ organizationId, planId, billingInterval }: { organizationId: string; planId: string; billingInterval: 'monthly' | 'annual' }) {
const { checkoutUrl } = await apiClient<{ checkoutUrl: string }>({
method: 'POST',
path: `/api/organizations/${organizationId}/checkout-session`,
body: {
planId,
+ billingInterval,
},
});
diff --git a/apps/papra-server/src/modules/plans/plans.config.ts b/apps/papra-server/src/modules/plans/plans.config.ts
index 806d2c8c..e281ff2a 100644
--- a/apps/papra-server/src/modules/plans/plans.config.ts
+++ b/apps/papra-server/src/modules/plans/plans.config.ts
@@ -9,16 +9,16 @@ export const organizationPlansConfig = {
default: true,
env: 'IS_FREE_PLAN_UNLIMITED',
},
- plusPlanPriceId: {
- doc: 'The price id of the plus plan (useless for self-hosting)',
+ plusPlanMonthlyPriceId: {
+ doc: 'The monthly price id of the plus plan (useless for self-hosting)',
schema: z.string(),
default: 'change-me',
- env: 'PLANS_PLUS_PLAN_PRICE_ID',
+ env: 'PLANS_PLUS_PLAN_MONTHLY_PRICE_ID',
},
- familyPlanPriceId: {
- doc: 'The price id of the family plan (useless for self-hosting)',
+ plusPlanAnnualPriceId: {
+ doc: 'The annual price id of the plus plan (useless for self-hosting)',
schema: z.string(),
default: 'change-me',
- env: 'PLANS_FAMILY_PLAN_PRICE_ID',
+ env: 'PLANS_PLUS_PLAN_ANNUAL_PRICE_ID',
},
} as const satisfies ConfigDefinition;
diff --git a/apps/papra-server/src/modules/plans/plans.errors.ts b/apps/papra-server/src/modules/plans/plans.errors.ts
index 72cb5ab0..db003085 100644
--- a/apps/papra-server/src/modules/plans/plans.errors.ts
+++ b/apps/papra-server/src/modules/plans/plans.errors.ts
@@ -5,3 +5,10 @@ export const createPlanNotFoundError = createErrorFactory({
message: 'Plan not found',
statusCode: 404,
});
+
+export const createOrganizationPlanPriceIdNotSetError = createErrorFactory({
+ code: 'plans.organization_plan_price_id_not_set',
+ message: 'Organization plan price ID is not set',
+ statusCode: 500,
+ isInternal: true,
+});
diff --git a/apps/papra-server/src/modules/plans/plans.models.ts b/apps/papra-server/src/modules/plans/plans.models.ts
new file mode 100644
index 00000000..90b73462
--- /dev/null
+++ b/apps/papra-server/src/modules/plans/plans.models.ts
@@ -0,0 +1,20 @@
+import { isNil } from '../shared/utils';
+import { createOrganizationPlanPriceIdNotSetError } from './plans.errors';
+
+export function getPriceIdForBillingInterval({
+ plan,
+ billingInterval,
+}: {
+ plan: { monthlyPriceId?: string; annualPriceId?: string };
+ billingInterval: 'monthly' | 'annual';
+}) {
+ const priceId = billingInterval === 'annual' ? plan.annualPriceId : plan.monthlyPriceId;
+
+ if (isNil(priceId)) {
+ // Very unlikely to happen, as only the free plan does not have a price ID, and we check for the plans in the route validation
+ // but for type safety, we assert that the price ID is set
+ throw createOrganizationPlanPriceIdNotSetError();
+ }
+
+ return { priceId };
+}
diff --git a/apps/papra-server/src/modules/plans/plans.repository.ts b/apps/papra-server/src/modules/plans/plans.repository.ts
index a055f15b..01de0a80 100644
--- a/apps/papra-server/src/modules/plans/plans.repository.ts
+++ b/apps/papra-server/src/modules/plans/plans.repository.ts
@@ -39,7 +39,8 @@ export function getOrganizationPlansRecords({ config }: { config: Config }) {
[PLUS_PLAN_ID]: {
id: PLUS_PLAN_ID,
name: 'Plus',
- priceId: config.organizationPlans.plusPlanPriceId,
+ monthlyPriceId: config.organizationPlans.plusPlanMonthlyPriceId,
+ annualPriceId: config.organizationPlans.plusPlanAnnualPriceId,
limits: {
maxDocumentStorageBytes: 1024 * 1024 * 1024 * 5, // 5 GiB
maxIntakeEmailsCount: 10,
@@ -63,7 +64,7 @@ async function getOrganizationPlanById({ planId, organizationPlans }: { planId:
}
async function getOrganizationPlanByPriceId({ priceId, organizationPlans }: { priceId: string; organizationPlans: Record<string, OrganizationPlanRecord> }) {
- const organizationPlan = Object.values(organizationPlans).find(plan => plan.priceId === priceId);
+ const organizationPlan = Object.values(organizationPlans).find(plan => plan.monthlyPriceId === priceId || plan.annualPriceId === priceId);
if (!organizationPlan) {
throw createPlanNotFoundError();
diff --git a/apps/papra-server/src/modules/plans/plans.types.ts b/apps/papra-server/src/modules/plans/plans.types.ts
index 7f6203fd..4357fd02 100644
--- a/apps/papra-server/src/modules/plans/plans.types.ts
+++ b/apps/papra-server/src/modules/plans/plans.types.ts
@@ -1,7 +1,8 @@
export type OrganizationPlanRecord = {
id: string;
name: string;
- priceId?: string;
+ monthlyPriceId?: string;
+ annualPriceId?: string;
limits: {
maxDocumentStorageBytes: number;
maxFileSize: number;
diff --git a/apps/papra-server/src/modules/plans/plans.usecases.test.ts b/apps/papra-server/src/modules/plans/plans.usecases.test.ts
index 6135ef62..14b8d2b1 100644
--- a/apps/papra-server/src/modules/plans/plans.usecases.test.ts
+++ b/apps/papra-server/src/modules/plans/plans.usecases.test.ts
@@ -29,7 +29,8 @@ describe('plans usecases', () => {
const config = overrideConfig({
organizationPlans: {
- plusPlanPriceId: 'price_123',
+ plusPlanAnnualPriceId: 'price_123',
+ plusPlanMonthlyPriceId: 'price_456',
},
});
@@ -50,7 +51,8 @@ describe('plans usecases', () => {
const config = overrideConfig({
organizationPlans: {
- plusPlanPriceId: 'price_123',
+ plusPlanAnnualPriceId: 'price_123',
+ plusPlanMonthlyPriceId: 'price_456',
},
});
diff --git a/apps/papra-server/src/modules/subscriptions/subscriptions.routes.ts b/apps/papra-server/src/modules/subscriptions/subscriptions.routes.ts
index 8a275478..8f10d283 100644
--- a/apps/papra-server/src/modules/subscriptions/subscriptions.routes.ts
+++ b/apps/papra-server/src/modules/subscriptions/subscriptions.routes.ts
@@ -8,12 +8,11 @@ import { createOrganizationNotFoundError } from '../organizations/organizations.
import { createOrganizationsRepository } from '../organizations/organizations.repository';
import { ensureUserIsInOrganization, ensureUserIsOwnerOfOrganization, getOrCreateOrganizationCustomerId } from '../organizations/organizations.usecases';
import { FREE_PLAN_ID, PLUS_PLAN_ID } from '../plans/plans.constants';
+import { getPriceIdForBillingInterval } from '../plans/plans.models';
import { createPlansRepository } from '../plans/plans.repository';
import { getOrganizationPlan } from '../plans/plans.usecases';
-import { createError } from '../shared/errors/errors';
import { getHeader } from '../shared/headers/headers.models';
import { createLogger } from '../shared/logger/logger';
-import { isNil } from '../shared/utils';
import { validateJsonBody, validateParams } from '../shared/validation/validation';
import { createInvalidWebhookPayloadError, createOrganizationAlreadyHasSubscriptionError } from './subscriptions.errors';
import { isSignatureHeaderFormatValid } from './subscriptions.models';
@@ -67,6 +66,7 @@ function setupCreateCheckoutSessionRoute({ app, config, db, subscriptionsService
requireAuthentication(),
validateJsonBody(z.object({
planId: z.enum([PLUS_PLAN_ID]),
+ billingInterval: z.enum(['monthly', 'annual']).default('monthly'),
})),
validateParams(z.object({
organizationId: organizationIdSchema,
@@ -78,7 +78,7 @@ function setupCreateCheckoutSessionRoute({ app, config, db, subscriptionsService
const plansRepository = createPlansRepository ({ config });
const subscriptionsRepository = createSubscriptionsRepository({ db });
- const { planId } = context.req.valid('json');
+ const { planId, billingInterval } = context.req.valid('json');
const { organizationId } = context.req.valid('param');
await ensureUserIsOwnerOfOrganization({
@@ -101,22 +101,16 @@ function setupCreateCheckoutSessionRoute({ app, config, db, subscriptionsService
const { organizationPlan: organizationPlanToSubscribeTo } = await plansRepository.getOrganizationPlanById({ planId });
- if (isNil(organizationPlanToSubscribeTo.priceId)) {
- // Very unlikely to happen, as only the free plan does not have a price ID, and we check for the plans in the route validation
- // but for type safety, we assert that the price ID is set
- throw createError({
- message: 'Organization plan price ID is not set',
- code: 'plans.organization_plan_price_id_not_set',
- statusCode: 500,
- isInternal: true,
- });
- }
+ const { priceId } = getPriceIdForBillingInterval({
+ plan: organizationPlanToSubscribeTo,
+ billingInterval,
+ });
const { customerId } = await getOrCreateOrganizationCustomerId({ organizationId, subscriptionsServices, organizationsRepository });
const { checkoutUrl } = await subscriptionsServices.createCheckoutUrl({
customerId,
- priceId: organizationPlanToSubscribeTo.priceId,
+ priceId,
organizationId,
});