Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

✨ Warn space owners when a subscription payment fails (#3316)

Luke Vellaf3686f384b90
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.json
index dc8c25104..b95ab02bd 100644
--- a/apps/web/public/locales/en/app.json
+++ b/apps/web/public/locales/en/app.json
@@ -667,6 +667,8 @@
"passwordStrengthWeak": "Weak",
"passwordTooWeak": "Password is too weak. Please use a stronger password.",
"past": "Past",
+ "pastDueAlertDescription": "We couldn't charge your card. Your space stays on Pro while we retry, but will drop to Hobby if the payment isn't updated. <a>Go to billing</a>",
+ "pastDueAlertTitle": "Payment failed",
"pastEventsEmptyStateDescription": "Past events will show up here.",
"pastEventsEmptyStateTitle": "No past events",
"paymentMethodUpdatedAlertDescription": "Future invoices will be charged to your new payment method.",
diff --git a/apps/web/src/app/[locale]/(space)/(dashboard)/layout.tsx b/apps/web/src/app/[locale]/(space)/(dashboard)/layout.tsx
index 1cdb4097d..f2f394ce8 100644
--- a/apps/web/src/app/[locale]/(space)/(dashboard)/layout.tsx
+++ b/apps/web/src/app/[locale]/(space)/(dashboard)/layout.tsx
@@ -13,6 +13,7 @@ import {
} from "@rallly/ui/sidebar";
import { SettingsIcon } from "lucide-react";
import { HoverPrefetchLink } from "@/components/hover-prefetch-link";
+import { PastDueAlert } from "@/features/billing/components/past-due-alert";
import { LicenseLimitWarning } from "@/features/licensing/components/license-limit-warning";
import { CommandMenu } from "@/features/navigation/components/command-menu";
import { SpaceDropdown } from "@/features/space/components/space-dropdown";
@@ -78,6 +79,7 @@ export default async function Layout({
</Sidebar>
<SidebarInset id="main-content" tabIndex={-1} className="min-w-0">
<LicenseLimitWarning />
+ <PastDueAlert />
<div className="flex flex-1 flex-col">
<div className="flex flex-1 flex-col">{children}</div>
</div>
diff --git a/apps/web/src/app/[locale]/(space)/settings/layout.tsx b/apps/web/src/app/[locale]/(space)/settings/layout.tsx
index 70c0823a5..0389f6df5 100644
--- a/apps/web/src/app/[locale]/(space)/settings/layout.tsx
+++ b/apps/web/src/app/[locale]/(space)/settings/layout.tsx
@@ -17,6 +17,7 @@ import {
import { ArrowLeftIcon, SettingsIcon } from "lucide-react";
import type React from "react";
import { HoverPrefetchLink } from "@/components/hover-prefetch-link";
+import { PastDueAlert } from "@/features/billing/components/past-due-alert";
import { NavUser } from "@/features/user/components/nav-user";
import { Trans } from "@/i18n/client";
import {
@@ -79,6 +80,7 @@ export default function Layout({ children }: { children?: React.ReactNode }) {
</SidebarFooter>
</Sidebar>
<SidebarInset id="main-content" tabIndex={-1}>
+ <PastDueAlert />
<div className="flex flex-1 flex-col">
<header className="sticky top-0 z-10 border-b bg-background/90 p-3 backdrop-blur-xs md:hidden">
<div className="flex items-center gap-4">
diff --git a/apps/web/src/features/billing/components/past-due-alert.tsx b/apps/web/src/features/billing/components/past-due-alert.tsx
new file mode 100644
index 000000000..fc5ae90cb
--- /dev/null
+++ b/apps/web/src/features/billing/components/past-due-alert.tsx
@@ -0,0 +1,43 @@
+import { Alert, AlertDescription, AlertTitle } from "@rallly/ui/alert";
+import { CreditCardIcon } from "lucide-react";
+import { Trans } from "react-i18next/TransWithoutContext";
+import { Link } from "@/components/link";
+import { loadIsSubscriptionPastDue } from "@/features/billing/loaders";
+import { getTranslation } from "@/i18n/server";
+
+export async function PastDueAlert() {
+ const isPastDue = await loadIsSubscriptionPastDue();
+
+ if (!isPastDue) {
+ return null;
+ }
+
+ const { t, i18n } = await getTranslation();
+
+ return (
+ <Alert variant="warning" className="m-1">
+ <CreditCardIcon />
+ <AlertTitle>
+ <Trans
+ t={t}
+ i18n={i18n}
+ ns="app"
+ i18nKey="pastDueAlertTitle"
+ defaults="Payment failed"
+ />
+ </AlertTitle>
+ <AlertDescription>
+ <Trans
+ t={t}
+ i18n={i18n}
+ ns="app"
+ i18nKey="pastDueAlertDescription"
+ defaults="We couldn't charge your card. Your space stays on Pro while we retry, but will drop to Hobby if the payment isn't updated. <a>Go to billing</a>"
+ components={{
+ a: <Link href="/settings/billing" />,
+ }}
+ />
+ </AlertDescription>
+ </Alert>
+ );
+}
diff --git a/apps/web/src/features/billing/loaders.ts b/apps/web/src/features/billing/loaders.ts
index f27d4115d..115fd62c6 100644
--- a/apps/web/src/features/billing/loaders.ts
+++ b/apps/web/src/features/billing/loaders.ts
@@ -8,7 +8,9 @@ import { isBillingEnabled } from "@/features/billing/constants";
import { getProPrices, getSpaceSubscription } from "@/features/billing/data";
import { isEarlySupporter } from "@/features/billing/utils";
import { getActiveSpace } from "@/features/space/loaders";
-import { getCurrentUser } from "@/features/user/loaders";
+import { defineAbilityForMember } from "@/features/space/member/ability";
+import { getCurrentUser, requireUser } from "@/features/user/loaders";
+import { isFeatureEnabled } from "@/lib/feature-flags/server";
/**
* Prices for the pay wall plus the currency to show first: the currency the
@@ -92,3 +94,28 @@ export const loadSubscriptionOverview = cache(async () => {
canResume: subscription.cancelAtPeriodEnd && !user?.deletedAt,
};
});
+
+/**
+ * Whether to warn the active space about a failed payment. Gated on the same
+ * ability as the billing page so the warning never links a member to a page
+ * they get denied on. Reads the flag off the space DTO, which the session
+ * gate already fetched, so the warning costs no extra query on any page.
+ */
+export const loadIsSubscriptionPastDue = cache(async () => {
+ if (!isFeatureEnabled("billing")) {
+ return false;
+ }
+
+ const [user, space] = await Promise.all([requireUser(), getActiveSpace()]);
+
+ if (!space.subscriptionPastDue) {
+ return false;
+ }
+
+ const ability = defineAbilityForMember({
+ user: { id: user.id },
+ space: { id: space.id, ownerId: space.ownerId, role: space.role },
+ });
+
+ return ability.can("manage", "Billing");
+});
diff --git a/apps/web/src/features/space/data.ts b/apps/web/src/features/space/data.ts
index acab9b6df..64d2ed567 100644
--- a/apps/web/src/features/space/data.ts
+++ b/apps/web/src/features/space/data.ts
@@ -221,6 +221,7 @@ export const listSpacesForUser = cache(async (userId: string) => {
subscriptions: {
where: { active: true },
select: { quantity: true },
+ orderBy: { createdAt: "desc" },
take: 1,
},
},
@@ -255,7 +256,11 @@ export const getActiveSpaceForUser = cache(async (userId: string) => {
_count: { select: { members: true } },
subscriptions: {
where: { active: true },
- select: { quantity: true },
+ select: { quantity: true, status: true },
+ // Newest first, matching getSpaceSubscription: nothing stops a
+ // space having two active rows, and an unordered take(1) could
+ // otherwise disagree with the billing page about which one counts.
+ orderBy: { createdAt: "desc" },
take: 1,
},
},
@@ -273,6 +278,8 @@ export const getActiveSpaceForUser = cache(async (userId: string) => {
role: spaceMember.role,
memberCount: spaceMember.space._count.members,
seatCount: spaceMember.space.subscriptions[0]?.quantity ?? 1,
+ subscriptionPastDue:
+ spaceMember.space.subscriptions[0]?.status === "past_due",
},
policy: await getInstancePolicy(),
});
diff --git a/apps/web/src/features/space/types.ts b/apps/web/src/features/space/types.ts
index fe258a2d7..68490ec56 100644
--- a/apps/web/src/features/space/types.ts
+++ b/apps/web/src/features/space/types.ts
@@ -20,4 +20,7 @@ export type SpaceDTO = {
primaryColor?: string;
showBranding: boolean;
hideAttribution: boolean;
+ // A renewal payment has failed and Stripe is retrying. The space keeps Pro
+ // throughout, so this drives a warning, never an access decision.
+ subscriptionPastDue: boolean;
};
diff --git a/apps/web/src/features/space/utils.ts b/apps/web/src/features/space/utils.ts
index fbe4c887a..52d4bf8ca 100644
--- a/apps/web/src/features/space/utils.ts
+++ b/apps/web/src/features/space/utils.ts
@@ -51,6 +51,7 @@ export function createSpaceDTO({
shared: boolean;
memberCount: number;
seatCount: number;
+ subscriptionPastDue?: boolean;
};
policy: Pick<InstancePolicy, "spacesAlwaysShared">;
}): SpaceDTO {
@@ -70,6 +71,7 @@ export function createSpaceDTO({
primaryColor: space.primaryColor ?? undefined,
showBranding: space.showBranding,
hideAttribution: space.hideAttribution,
+ subscriptionPastDue: space.subscriptionPastDue ?? false,
};
}