Skip to content

Commit

Permalink
feat: support of Jitsu Classic billing
Browse files Browse the repository at this point in the history
  • Loading branch information
vklimontovich committed Mar 10, 2025
1 parent 655622e commit 9c3f167
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 11 deletions.
19 changes: 18 additions & 1 deletion webapps/console/components/Billing/BillingManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,11 @@ const AvailablePlans: React.FC<{}> = () => {
const { isLoading, error, data } = useQuery(
["availablePlans", workspace.id],
async () => {
if (billing.settings?.isLegacyPlan) {
return {
plans: {}
};
}
const plans = await rpc(`/api/${workspace.id}/ee/billing/plans`);
assertDefined(billing.settings.planId, `planId is not defined in ${JSON.stringify(billing.settings)}`);

Expand Down Expand Up @@ -402,13 +407,25 @@ const AvailablePlans: React.FC<{}> = () => {
},
{ cacheTime: 0, retry: false }
);
//if (billing.settings?.customBilling)
if (isLoading) {
return <Skeleton active />;
} else if (error) {
return <ErrorCard error={error} title="Failed to load available plans" />;
}
assertDefined(data, "Data is not defined");

if (billing.settings?.isLegacyPlan) {
return (
<div className="text-center">
<div className="text-textLight mt-6">
You're using a legacy plan. To upgrade to a new plan or cancel your subscription, please reach out to our{" "}
<Link className="text-primary underline" href={`/${workspace.slugOrId}/support`}>
Support Team
</Link>
</div>
</div>
);
}
return (
<div className="flex flex-row flex-nowrap justify-center space-x-6">
{Object.entries(data.plans).map(([planId, plan]) => (
Expand Down
1 change: 1 addition & 0 deletions webapps/console/lib/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const BillingSettings = z.object({
//if subscription starts some time in the future, for enterprise plans only
futureSubscriptionDate: z.string().optional(),
profileBuilderEnabled: z.boolean().default(false).optional(),
isLegacyPlan: z.boolean().default(false).optional(),
});

export type BillingSettings = z.infer<typeof BillingSettings>;
Expand Down
6 changes: 5 additions & 1 deletion webapps/ee-api/lib/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export const stripeDataTable =

export type SubscriptionStatus = {
planId: string;
isLegacyPlan?: boolean;
expiresAt?: string;
renewAfterExpiration?: boolean;
} & Record<string, any>;
Expand Down Expand Up @@ -83,6 +84,7 @@ export async function getOrCreateCurrentSubscription(
stripeCustomerId: string;
customBilling?: boolean;
noRestrictions?: boolean;
isLegacyPlan?: boolean;
subscriptionStatus: SubscriptionStatus;
}> {
let stripeOptions: StripeDataTableEntry = await store.getTable(stripeDataTable).get(workspaceId);
Expand Down Expand Up @@ -143,6 +145,7 @@ export async function getOrCreateCurrentSubscription(
return {
stripeCustomerId: stripeOptions.stripeCustomerId,
noRestrictions: !!stripeOptions.noRestrictions,
isLegacyPlan: !!stripeOptions.activeSubscription,
subscriptionStatus: {
...plan,
...(stripeOptions.customSettings || {}),
Expand Down Expand Up @@ -229,6 +232,7 @@ export async function getActivePlan(customerId: string): Promise<null | Subscrip
return {
planId: requireDefined(product.metadata?.jitsu_plan_id),
planName: product.name,
isLegacyPlan: product.metadata?.is_legacy === "true" || product.metadata?.is_legacy === "1",
expiresAt: new Date(subscription.current_period_end * 1000).toISOString(),
renewAfterExpiration: !subscription.cancel_at_period_end,
pastDue: pastDueSubscription && !activeSubscription,
Expand Down Expand Up @@ -340,7 +344,7 @@ export async function getOrCreatePortalConfiguration() {
payment_method_update: { enabled: true },
subscription_update: {
default_allowed_updates: ["price"],
enabled: true,
enabled: false,
products: allowedProducts,
proration_behavior: "always_invoice",
},
Expand Down
21 changes: 12 additions & 9 deletions webapps/ee-api/pages/api/billing/plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,18 @@ const handler = async function handler(req: NextApiRequest, res: NextApiResponse
);
const annual = prices.data.find(p => p.recurring?.interval === "year");
const planData = JSON.parse(requireDefined(product.metadata?.plan_data, `No data for ${product.id}`));
result.push({
id: product.metadata?.jitsu_plan_id,
data: planData,
name: product.name,
monthlyPrice: requireDefined(monthly.unit_amount, `No unit_amount on monthly price for ${product.id}`) / 100,
annualPrice: annual
? requireDefined(annual?.unit_amount, `No unit_amount on annual price for ${product.id}`) / 100
: undefined,
});
const isLegacy = product.metadata?.is_legacy === "true" || product.metadata?.is_legacy === "1";
if (!isLegacy) {
result.push({
id: product.metadata?.jitsu_plan_id,
data: planData,
name: product.name,
monthlyPrice: requireDefined(monthly.unit_amount, `No unit_amount on monthly price for ${product.id}`) / 100,
annualPrice: annual
? requireDefined(annual?.unit_amount, `No unit_amount on annual price for ${product.id}`) / 100
: undefined,
});
}
}

return { products: result };
Expand Down

0 comments on commit 9c3f167

Please sign in to comment.