Skip to content

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

✨ Keep invoices reachable after a subscription is cancelled (#3478)

Luke Vella4dbe389c8682
diff --git a/apps/web/src/app/[locale]/(space)/settings/billing/page.tsx b/apps/web/src/app/[locale]/(space)/settings/billing/page.tsx
index a81c7abec..364613e26 100644
--- a/apps/web/src/app/[locale]/(space)/settings/billing/page.tsx
+++ b/apps/web/src/app/[locale]/(space)/settings/billing/page.tsx
@@ -21,7 +21,7 @@ import {
} from "@/features/billing/loaders";
import { loadActiveSpace, loadSeatUsage } from "@/features/space/loaders";
import { defineAbilityForMember } from "@/features/space/member/ability";
-import { loadUser } from "@/features/user/loaders";
+import { loadOptionalUser, loadUser } from "@/features/user/loaders";
import { Trans } from "@/i18n/client";
import { getTranslation } from "@/i18n/server";
import { isFeatureEnabled } from "@/lib/feature-flags/server";
@@ -63,10 +63,13 @@ export default async function BillingSettingsPage() {
);
}
- const [overview, seatUsage, paymentMethods] = await Promise.all([
+ // Database fresh: the session snapshot can predate the customer ID the
+ // checkout webhook writes.
+ const [overview, seatUsage, paymentMethods, billingUser] = await Promise.all([
loadSubscriptionOverview(),
loadSeatUsage(),
loadPaymentMethods(),
+ loadOptionalUser(),
]);
return (
@@ -105,7 +108,7 @@ export default async function BillingSettingsPage() {
<HobbyPlanCard />
)}
<BillingFlashAlert />
- {overview?.subscription.active ? (
+ {overview && billingUser?.customerId ? (
<PaymentAndBillingCard paymentMethods={paymentMethods} />
) : null}
<SupportCard />
diff --git a/apps/web/src/features/billing/actions.ts b/apps/web/src/features/billing/actions.ts
index 0f79ff7e1..0a4b94b9a 100644
--- a/apps/web/src/features/billing/actions.ts
+++ b/apps/web/src/features/billing/actions.ts
@@ -188,12 +188,17 @@ export const upgradeToProAction = authActionClient
/**
* The active space's subscription, proven manageable by the caller: the
* caller must hold `manage Billing` on the space (owner) and the subscription
- * must belong to that space. Every subscription changing action starts here.
+ * must belong to that space. Inactive subscriptions are only accepted when
+ * `allowInactive` is set, for actions that stay useful after cancellation
+ * (invoices, billing details). Every subscription changing action starts here.
*/
-async function requireManagedSubscription(user: {
- id: string;
- customerId?: string;
-}) {
+async function requireManagedSubscription(
+ user: {
+ id: string;
+ customerId?: string;
+ },
+ { allowInactive = false }: { allowInactive?: boolean } = {},
+) {
const space = await getActiveSpaceForUser(user.id);
if (!space) {
@@ -214,10 +219,12 @@ async function requireManagedSubscription(user: {
const subscription = await getSpaceSubscription(space.id);
- if (!subscription?.active) {
+ if (!subscription || (!subscription.active && !allowInactive)) {
throw new AppError({
code: "NOT_FOUND",
- message: "No active subscription for this space",
+ message: allowInactive
+ ? "No subscription for this space"
+ : "No active subscription for this space",
});
}
@@ -322,7 +329,10 @@ export const resumePlanAction = authActionClient
export const openBillingDetailsAction = authActionClient
.metadata({ actionName: "open_billing_details" })
.action(async ({ ctx }) => {
- const { space, customerId } = await requireManagedSubscription(ctx.user);
+ // Invoices stay reachable after the subscription ends.
+ const { space, customerId } = await requireManagedSubscription(ctx.user, {
+ allowInactive: true,
+ });
track(ctx.user, {
event: "space_billing:billing_details_click",