Skip to content

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

✨ Carry the currency chosen on the pricing page into the pay wall (#3208)

Luke Vella2410a32d29e3
diff --git a/apps/landing/.env.sample b/apps/landing/.env.sample
index a45a761a1..9d45c8289 100644
--- a/apps/landing/.env.sample
+++ b/apps/landing/.env.sample
@@ -9,3 +9,6 @@ DATABASE_URL=postgres://postgres:postgres@localhost:5450/rallly
# Optional. Lets the pricing page read live prices from Stripe; without it the page shows the built-in USD prices.
STRIPE_SECRET_KEY=
+
+# Optional. Cookie domain shared with the app (e.g. .rallly.co) so the currency the visitor picks on the pricing page carries into the app.
+NEXT_PUBLIC_COOKIE_DOMAIN=
diff --git a/apps/landing/package.json b/apps/landing/package.json
index 542d3bc28..ea9dd887b 100644
--- a/apps/landing/package.json
+++ b/apps/landing/package.json
@@ -29,6 +29,7 @@
"i18next-icu": "^2.4.3",
"i18next-resources-to-backend": "^1.2.1",
"intl-messageformat": "^10.7.15",
+ "js-cookie": "^3.0.1",
"lodash": "^4.17.21",
"lucide-react": "^0.479.0",
"motion": "^12.6.2",
@@ -47,6 +48,7 @@
"@next/bundle-analyzer": "16.3.3",
"@rallly/tsconfig": "workspace:*",
"@types/color-hash": "^1.0.2",
+ "@types/js-cookie": "^3.0.1",
"@types/lodash": "^4.14.178",
"@types/node": "^24.1.0",
"@types/react": "19.2.13",
diff --git a/apps/landing/src/app/[locale]/(main)/pricing/pricing-controls.tsx b/apps/landing/src/app/[locale]/(main)/pricing/pricing-controls.tsx
index 64c4eefed..830898327 100644
--- a/apps/landing/src/app/[locale]/(main)/pricing/pricing-controls.tsx
+++ b/apps/landing/src/app/[locale]/(main)/pricing/pricing-controls.tsx
@@ -12,7 +12,7 @@ import {
} from "@rallly/ui/select";
import { Switch } from "@rallly/ui/switch";
import React from "react";
-import { readCurrencyCookie } from "@/lib/currency";
+import { readCurrencyCookie, writeCurrencyCookie } from "@/lib/currency";
type BillingInterval = "monthly" | "yearly";
@@ -92,6 +92,7 @@ export function CurrencySelect({ label }: { label: string }) {
onValueChange={(value) => {
if (value) {
setCurrency(value);
+ writeCurrencyCookie(value);
}
}}
>
diff --git a/apps/landing/src/lib/currency.ts b/apps/landing/src/lib/currency.ts
index d181bfb09..a87a5608c 100644
--- a/apps/landing/src/lib/currency.ts
+++ b/apps/landing/src/lib/currency.ts
@@ -1,13 +1,20 @@
-// Set by the proxy from the request country so the cached pricing page can
-// still open in the visitor's currency: the page reads it on the client.
-export const CURRENCY_COOKIE_NAME = "currency";
+import { CURRENCY_COOKIE_NAME } from "@rallly/billing/pricing";
+import Cookies from "js-cookie";
+
+export { CURRENCY_COOKIE_NAME };
+
+export const currencyCookieAttributes = {
+ path: "/",
+ sameSite: "lax",
+ domain: process.env.NEXT_PUBLIC_COOKIE_DOMAIN || undefined,
+ expires: 365,
+} as const;
export function readCurrencyCookie() {
- if (typeof document === "undefined") {
- return undefined;
- }
- const match = document.cookie.match(
- new RegExp(`(?:^|; )${CURRENCY_COOKIE_NAME}=([a-z]{3})(?:;|$)`),
- );
- return match?.[1];
+ const value = Cookies.get(CURRENCY_COOKIE_NAME);
+ return value && /^[a-z]{3}$/.test(value) ? value : undefined;
+}
+
+export function writeCurrencyCookie(currency: string) {
+ Cookies.set(CURRENCY_COOKIE_NAME, currency, currencyCookieAttributes);
}
diff --git a/apps/landing/src/proxy.ts b/apps/landing/src/proxy.ts
index e14cc91d2..447c2148a 100644
--- a/apps/landing/src/proxy.ts
+++ b/apps/landing/src/proxy.ts
@@ -19,7 +19,12 @@ export async function proxy(req: NextRequest) {
getCountryCurrency(req.headers.get("x-vercel-ip-country") ?? undefined, [
...displayedCurrencies,
]),
- { path: "/", maxAge: ONE_YEAR_SECONDS, sameSite: "lax" },
+ {
+ path: "/",
+ maxAge: ONE_YEAR_SECONDS,
+ sameSite: "lax",
+ domain: process.env.NEXT_PUBLIC_COOKIE_DOMAIN || undefined,
+ },
);
}
diff --git a/apps/web/src/features/billing/loaders.ts b/apps/web/src/features/billing/loaders.ts
index cb8b5b55b..8368d373e 100644
--- a/apps/web/src/features/billing/loaders.ts
+++ b/apps/web/src/features/billing/loaders.ts
@@ -1,36 +1,43 @@
import "server-only";
-import { getCountryCurrency } from "@rallly/billing";
-import { headers } from "next/headers";
+import { CURRENCY_COOKIE_NAME, getCountryCurrency } from "@rallly/billing";
+import { cookies, headers } from "next/headers";
import { cache } from "react";
import { isBillingEnabled } from "@/features/billing/constants";
import { getProPrices } from "@/features/billing/data";
/**
- * Prices for the pay wall plus the currency to show first, from the request
- * country Vercel stamps on every request. Null when there is nothing to pay
- * for, and when Stripe is unreachable so the pay wall degrades to the built
- * in prices rather than the layout failing.
+ * Prices for the pay wall plus the currency to show first: the currency the
+ * visitor picked or was detected with on the pricing page (shared cookie),
+ * else the request country Vercel stamps on every request. Null when there
+ * is nothing to pay for, and when Stripe is unreachable so the pay wall
+ * degrades to the built in prices rather than the layout failing.
*/
export const loadPayWallPricing = cache(async () => {
if (!isBillingEnabled) {
return null;
}
try {
- const [prices, headersList] = await Promise.all([
+ const [prices, cookieStore, headersList] = await Promise.all([
getProPrices(),
+ cookies(),
headers(),
]);
const available = Object.keys(prices);
if (available.length === 0) {
return null;
}
+ // Own key only: the cookie is visitor controlled.
+ const chosen = cookieStore.get(CURRENCY_COOKIE_NAME)?.value;
return {
prices,
- defaultCurrency: getCountryCurrency(
- headersList.get("x-vercel-ip-country") ?? undefined,
- available,
- ),
+ defaultCurrency:
+ chosen && Object.hasOwn(prices, chosen)
+ ? chosen
+ : getCountryCurrency(
+ headersList.get("x-vercel-ip-country") ?? undefined,
+ available,
+ ),
};
} catch (error) {
console.error("Failed to load pay wall pricing", error);
diff --git a/packages/billing/src/pricing.ts b/packages/billing/src/pricing.ts
index e3c310752..35723f165 100644
--- a/packages/billing/src/pricing.ts
+++ b/packages/billing/src/pricing.ts
@@ -21,6 +21,11 @@ export function yearlySavingsPercent({
export const displayedCurrencies = ["usd", "eur", "gbp"] as const;
+// Set by the landing proxy from the request country and rewritten when the
+// visitor picks a currency on the pricing page. Shared with the app through
+// the cookie domain so the pay wall opens in the same currency.
+export const CURRENCY_COOKIE_NAME = "currency";
+
export type DisplayedCurrency = (typeof displayedCurrencies)[number];
export function isDisplayedCurrency(value: string): value is DisplayedCurrency {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2489dc00e..92366d032 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -116,6 +116,9 @@ importers:
i18next-resources-to-backend:
specifier: ^1.2.1
version: 1.2.1
+ js-cookie:
+ specifier: ^3.0.1
+ version: 3.0.5
intl-messageformat:
specifier: ^10.7.15
version: 10.7.18
@@ -168,6 +171,9 @@ importers:
'@types/color-hash':
specifier: ^1.0.2
version: 1.0.5
+ '@types/js-cookie':
+ specifier: ^3.0.1
+ version: 3.0.6
'@types/lodash':
specifier: ^4.14.178
version: 4.17.23