Skip to content

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

✨ Return from Stripe portal flows through a flash instead of a query parameter (#3304)

Luke Vellac33c6e92a733
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.json
index 4edd669db..d058df020 100644
--- a/apps/web/public/locales/en/app.json
+++ b/apps/web/public/locales/en/app.json
@@ -94,6 +94,8 @@
"basicPollsDescription": "Create simple scheduling polls",
"billing": "Billing",
"billingDescription": "Manage your billing information and subscription.",
+ "billingIntervalUpdatedAlertDescription": "Your subscription now renews once a year.",
+ "billingIntervalUpdatedAlertTitle": "Switched to yearly billing",
"billingPlanTitle": "Plan",
"billingSettings": "Billing settings",
"billingSettingsDescription": "View and manage your space's subscription and billing information.",
@@ -631,6 +633,8 @@
"past": "Past",
"pastEventsEmptyStateDescription": "Past events will show up here.",
"pastEventsEmptyStateTitle": "No past events",
+ "paymentMethodUpdatedAlertDescription": "Future invoices will be charged to your new payment method.",
+ "paymentMethodUpdatedAlertTitle": "Payment method updated",
"pending": "Pending",
"pendingDeletionNotice": "Your account and data will be permanently deleted on {deletionDate}.",
"pendingInvites": "Pending invites",
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 f36917d50..b7f44dc0e 100644
--- a/apps/web/src/app/[locale]/(space)/settings/billing/page.tsx
+++ b/apps/web/src/app/[locale]/(space)/settings/billing/page.tsx
@@ -1,5 +1,4 @@
-import { Alert, AlertDescription, AlertTitle } from "@rallly/ui/alert";
-import { CheckCircleIcon, ShieldXIcon } from "lucide-react";
+import { ShieldXIcon } from "lucide-react";
import type { Metadata } from "next";
import { notFound } from "next/navigation";
import {
@@ -24,6 +23,7 @@ import {
SettingsPageHeader,
SettingsPageTitle,
} from "@/components/settings-layout";
+import { BillingFlashAlert } from "@/features/billing/components/billing-flash-alert";
import { getSpaceSubscription } from "@/features/billing/data";
import { getActiveSpace, getSeatUsage } from "@/features/space/loaders";
import { defineAbilityForMember } from "@/features/space/member/ability";
@@ -35,11 +35,7 @@ import { ContactSupportLink } from "./components/contact-support-link";
import { HobbyPlanCard } from "./components/hobby-plan-card";
import { ProPlanCard } from "./components/pro-plan-card";
-export default async function BillingSettingsPage({
- searchParams,
-}: {
- searchParams: Promise<{ seats_updated?: string }>;
-}) {
+export default async function BillingSettingsPage() {
if (!isFeatureEnabled("billing")) {
notFound();
}
@@ -72,12 +68,10 @@ export default async function BillingSettingsPage({
);
}
- const [subscription, seatUsage, { seats_updated: didUpdateSeats }] =
- await Promise.all([
- getSpaceSubscription(space.id),
- getSeatUsage(),
- searchParams,
- ]);
+ const [subscription, seatUsage] = await Promise.all([
+ getSpaceSubscription(space.id),
+ getSeatUsage(),
+ ]);
return (
<SettingsPage>
@@ -123,23 +117,7 @@ export default async function BillingSettingsPage({
) : (
<HobbyPlanCard />
)}
- {didUpdateSeats !== undefined ? (
- <Alert variant="success">
- <CheckCircleIcon />
- <AlertTitle>
- <Trans
- i18nKey="seatsUpdatedAlertTitle"
- defaults="Seats updated"
- />
- </AlertTitle>
- <AlertDescription>
- <Trans
- i18nKey="seatsUpdatedAlertDescription"
- defaults="Your seat allocation has been successfully updated. The changes will be reflected in your next billing cycle."
- />
- </AlertDescription>
- </Alert>
- ) : null}
+ <BillingFlashAlert />
</PageSectionContent>
</PageSection>
<PageSectionDivider />
diff --git a/apps/web/src/app/api/stripe/return/route.ts b/apps/web/src/app/api/stripe/return/route.ts
new file mode 100644
index 000000000..e8f0a45d6
--- /dev/null
+++ b/apps/web/src/app/api/stripe/return/route.ts
@@ -0,0 +1,32 @@
+import type { NextRequest } from "next/server";
+import { NextResponse } from "next/server";
+import { BILLING_FLASH_KEY } from "@/features/billing/constants";
+import { billingReturnFlowSchema } from "@/features/billing/schema";
+import { FLASH_MAX_AGE, flashCookieName } from "@/lib/flash/constants";
+
+/**
+ * Where Stripe portal flows land after completion. Carries which flow finished
+ * as a one-shot flash so the billing page can confirm it without a query
+ * parameter surviving in the URL. Unknown or missing flows still redirect.
+ */
+export function GET(request: NextRequest) {
+ const response = NextResponse.redirect(
+ new URL("/settings/billing", request.nextUrl.origin),
+ );
+
+ const flow = billingReturnFlowSchema.safeParse(
+ request.nextUrl.searchParams.get("flow"),
+ );
+
+ if (flow.success) {
+ response.cookies.set(flashCookieName(BILLING_FLASH_KEY), flow.data, {
+ path: "/",
+ maxAge: FLASH_MAX_AGE,
+ sameSite: "lax",
+ httpOnly: false,
+ secure: request.nextUrl.protocol === "https:",
+ });
+ }
+
+ return response;
+}
diff --git a/apps/web/src/features/billing/components/billing-flash-alert.tsx b/apps/web/src/features/billing/components/billing-flash-alert.tsx
new file mode 100644
index 000000000..875a0eaf7
--- /dev/null
+++ b/apps/web/src/features/billing/components/billing-flash-alert.tsx
@@ -0,0 +1,65 @@
+"use client";
+
+import { Alert, AlertDescription, AlertTitle } from "@rallly/ui/alert";
+import { CheckCircleIcon } from "lucide-react";
+import { BILLING_FLASH_KEY } from "@/features/billing/constants";
+import { billingReturnFlowSchema } from "@/features/billing/schema";
+import { Trans } from "@/i18n/client";
+import { useFlash } from "@/lib/flash/client";
+
+export function BillingFlashAlert() {
+ const flow = billingReturnFlowSchema.safeParse(useFlash(BILLING_FLASH_KEY));
+
+ if (!flow.success) {
+ return null;
+ }
+
+ return (
+ <Alert variant="success">
+ <CheckCircleIcon />
+ {flow.data === "seats" ? (
+ <>
+ <AlertTitle>
+ <Trans i18nKey="seatsUpdatedAlertTitle" defaults="Seats updated" />
+ </AlertTitle>
+ <AlertDescription>
+ <Trans
+ i18nKey="seatsUpdatedAlertDescription"
+ defaults="Your seat allocation has been successfully updated. The changes will be reflected in your next billing cycle."
+ />
+ </AlertDescription>
+ </>
+ ) : flow.data === "interval" ? (
+ <>
+ <AlertTitle>
+ <Trans
+ i18nKey="billingIntervalUpdatedAlertTitle"
+ defaults="Switched to yearly billing"
+ />
+ </AlertTitle>
+ <AlertDescription>
+ <Trans
+ i18nKey="billingIntervalUpdatedAlertDescription"
+ defaults="Your subscription now renews once a year."
+ />
+ </AlertDescription>
+ </>
+ ) : (
+ <>
+ <AlertTitle>
+ <Trans
+ i18nKey="paymentMethodUpdatedAlertTitle"
+ defaults="Payment method updated"
+ />
+ </AlertTitle>
+ <AlertDescription>
+ <Trans
+ i18nKey="paymentMethodUpdatedAlertDescription"
+ defaults="Future invoices will be charged to your new payment method."
+ />
+ </AlertDescription>
+ </>
+ )}
+ </Alert>
+ );
+}
diff --git a/apps/web/src/features/billing/constants.ts b/apps/web/src/features/billing/constants.ts
index c866bbe81..3b44d08a5 100644
--- a/apps/web/src/features/billing/constants.ts
+++ b/apps/web/src/features/billing/constants.ts
@@ -5,3 +5,6 @@ export const isBillingEnabled = !isSelfHosted;
// Re-export plan names from the shared billing package
export { PLAN_NAMES } from "@rallly/billing";
+
+// Flash set by /api/stripe/return after a portal flow; value is the flow name.
+export const BILLING_FLASH_KEY = "billing";
diff --git a/apps/web/src/features/billing/mutations.ts b/apps/web/src/features/billing/mutations.ts
index bb0b29760..d0114f491 100644
--- a/apps/web/src/features/billing/mutations.ts
+++ b/apps/web/src/features/billing/mutations.ts
@@ -9,6 +9,7 @@ import {
import { prisma } from "@rallly/database";
import { absoluteUrl } from "@rallly/utils/absolute-url";
import { isBillingEnabled } from "@/features/billing/constants";
+import type { BillingReturnFlow } from "@/features/billing/schema";
import { getStripe } from "@/features/billing/service";
import { isStripeResourceMissingError } from "@/features/billing/utils";
@@ -113,6 +114,10 @@ function getSeatUpdatePortalConfigurationId(): Promise<string> {
return configurationIdPromise;
}
+export function billingReturnUrl(flow: BillingReturnFlow) {
+ return absoluteUrl("/api/stripe/return", { flow });
+}
+
/**
* Creates a billing portal session deep-linked to the seat-update confirmation
* screen for a specific quantity change, reusing the shared configuration.
@@ -147,11 +152,7 @@ export async function createStripeSubscriptionUpdateConfirmation({
},
after_completion: {
type: "redirect",
- redirect: {
- return_url: absoluteUrl("/settings/billing", {
- seats_updated: "true",
- }),
- },
+ redirect: { return_url: billingReturnUrl("seats") },
},
},
});
diff --git a/apps/web/src/features/billing/schema.ts b/apps/web/src/features/billing/schema.ts
index 588f819d8..bf4e2c1e4 100644
--- a/apps/web/src/features/billing/schema.ts
+++ b/apps/web/src/features/billing/schema.ts
@@ -35,3 +35,10 @@ export const subscriptionMetadataSchema = z.object({
});
export type SubscriptionMetadata = z.infer<typeof subscriptionMetadataSchema>;
+
+export const billingReturnFlowSchema = z.enum([
+ "seats",
+ "interval",
+ "payment_method",
+]);
+export type BillingReturnFlow = z.infer<typeof billingReturnFlowSchema>;