Skip to content

Open-source status page, incident response and uptime monitoring

feat: geolocation currency (#1324)

Maximilian Kaskeb5c4f879b802
diff --git a/apps/dashboard/src/components/data-table/billing/data-table.tsx b/apps/dashboard/src/components/data-table/billing/data-table.tsx
index d548f298a..b4f0cbcb3 100644
--- a/apps/dashboard/src/components/data-table/billing/data-table.tsx
+++ b/apps/dashboard/src/components/data-table/billing/data-table.tsx
@@ -15,10 +15,12 @@ import {
} from "@/components/ui/table";
import { config as featureGroups, plans } from "@/data/plans";
+import { useCookieState } from "@/hooks/use-cookie-state";
import { getStripe } from "@/lib/stripe";
import { useTRPC } from "@/lib/trpc/client";
import { cn } from "@/lib/utils";
import type { WorkspacePlan } from "@openstatus/db/src/schema";
+import { getPriceConfig } from "@openstatus/db/src/schema/plan/utils";
import { useMutation, useQuery } from "@tanstack/react-query";
const BASE_URL =
@@ -27,6 +29,7 @@ const BASE_URL =
: "http://localhost:3000";
export function DataTable({ restrictTo }: { restrictTo?: WorkspacePlan[] }) {
+ const [currency] = useCookieState("x-currency", "USD");
const trpc = useTRPC();
const [isPending, startTransition] = useTransition();
const { data: workspace } = useQuery(trpc.workspace.get.queryOptions());
@@ -60,6 +63,7 @@ export function DataTable({ restrictTo }: { restrictTo?: WorkspacePlan[] }) {
</TableHead>
{filteredPlans.map(({ id, ...plan }) => {
const isCurrentPlan = workspace.plan === id;
+ const price = getPriceConfig(id, currency);
return (
<TableHead
key={id}
@@ -76,7 +80,12 @@ export function DataTable({ restrictTo }: { restrictTo?: WorkspacePlan[] }) {
</p>
</div>
<p className="text-right">
- <span className="font-cal text-lg">{plan.price}€</span>{" "}
+ <span className="font-cal text-lg">
+ {new Intl.NumberFormat(price.locale, {
+ style: "currency",
+ currency: price.currency,
+ }).format(price.value)}
+ </span>
<span className="font-light text-muted-foreground text-sm">
/month
</span>
diff --git a/apps/dashboard/src/middleware.ts b/apps/dashboard/src/middleware.ts
index 963425386..2aa6c643d 100644
--- a/apps/dashboard/src/middleware.ts
+++ b/apps/dashboard/src/middleware.ts
@@ -4,11 +4,19 @@ import { auth } from "@/lib/auth";
import { db, eq } from "@openstatus/db";
import { user, usersToWorkspaces, workspace } from "@openstatus/db/src/schema";
+import { getCurrency } from "@openstatus/db/src/schema/plan/utils";
export default auth(async (req) => {
const url = req.nextUrl.clone();
const response = NextResponse.next();
+ const continent = req.headers.get("x-vercel-ip-continent") || "NA";
+ const country = req.headers.get("x-vercel-ip-country") || "US";
+ const currency = getCurrency({ continent, country });
+
+ // NOTE: used in the pricing table to display the currency based on user's location
+ response.cookies.set("x-currency", currency);
+
if (url.pathname.includes("api/trpc")) {
return response;
}
diff --git a/apps/web/src/app/(pages)/(content)/page.tsx b/apps/web/src/app/(pages)/(content)/page.tsx
index 892eac926..1f147a70c 100644
--- a/apps/web/src/app/(pages)/(content)/page.tsx
+++ b/apps/web/src/app/(pages)/(content)/page.tsx
@@ -24,9 +24,9 @@ const jsonLdProduct: WithContext<Product> = {
},
offers: Object.entries(allPlans).map(([_, value]) => ({
"@type": "Offer",
- price: value.price,
+ price: value.price.USD,
name: value.title,
- priceCurrency: "EUR",
+ priceCurrency: "USD",
availability: "https://schema.org/InStock",
})),
};
diff --git a/apps/web/src/components/marketing/pricing/pricing-plan-radio.tsx b/apps/web/src/components/marketing/pricing/pricing-plan-radio.tsx
index be4034589..4d382695a 100644
--- a/apps/web/src/components/marketing/pricing/pricing-plan-radio.tsx
+++ b/apps/web/src/components/marketing/pricing/pricing-plan-radio.tsx
@@ -8,37 +8,47 @@ import {
RadioGroupItem,
} from "@openstatus/ui/src/components/radio-group";
+import { useCookieState } from "@/hooks/use-cookie-state";
import { cn } from "@/lib/utils";
import type { WorkspacePlan } from "@openstatus/db/src/schema";
+import { getPriceConfig } from "@openstatus/db/src/schema/plan/utils";
export function PricingPlanRadio({
onChange,
}: {
onChange(value: WorkspacePlan): void;
}) {
+ const [currency] = useCookieState("x-currency", "USD");
return (
<RadioGroup
defaultValue="team"
className="grid grid-cols-3 gap-4"
onValueChange={onChange}
>
- {workspacePlans.map((key) => (
- <div key={key}>
- <RadioGroupItem value={key} id={key} className="peer sr-only" />
- <Label
- htmlFor={key}
- className={cn(
- "flex flex-col items-center justify-between rounded-md border-2 border-muted bg-popover p-4 hover:bg-accent hover:text-accent-foreground peer-data-[state=checked]:border-primary [&:has([data-state=checked])]:border-primary",
- key === "team" && "bg-muted/50",
- )}
- >
- <span className="text-sm capitalize">{allPlans[key].title}</span>
- <span className="mt-1 font-light text-muted-foreground text-xs">
- {allPlans[key].price}€/month
- </span>
- </Label>
- </div>
- ))}
+ {workspacePlans.map((key) => {
+ const price = getPriceConfig(key, currency);
+ return (
+ <div key={key}>
+ <RadioGroupItem value={key} id={key} className="peer sr-only" />
+ <Label
+ htmlFor={key}
+ className={cn(
+ "flex flex-col items-center justify-between rounded-md border-2 border-muted bg-popover p-4 hover:bg-accent hover:text-accent-foreground peer-data-[state=checked]:border-primary [&:has([data-state=checked])]:border-primary",
+ key === "team" && "bg-muted/50",
+ )}
+ >
+ <span className="text-sm capitalize">{allPlans[key].title}</span>
+ <span className="mt-1 font-light text-muted-foreground text-xs">
+ {new Intl.NumberFormat(price.locale, {
+ style: "currency",
+ currency: price.currency,
+ }).format(price.value)}
+ /month
+ </span>
+ </Label>
+ </div>
+ );
+ })}
</RadioGroup>
);
}
diff --git a/apps/web/src/components/marketing/pricing/pricing-table.tsx b/apps/web/src/components/marketing/pricing/pricing-table.tsx
index 7d56729a2..3558175a1 100644
--- a/apps/web/src/components/marketing/pricing/pricing-table.tsx
+++ b/apps/web/src/components/marketing/pricing/pricing-table.tsx
@@ -20,8 +20,10 @@ import {
import { pricingTableConfig } from "../../../config/pricing-table";
import { LoadingAnimation } from "@/components/loading-animation";
+import { useCookieState } from "@/hooks/use-cookie-state";
import { cn } from "@/lib/utils";
import { allPlans } from "@openstatus/db/src/schema/plan/config";
+import { getPriceConfig } from "@openstatus/db/src/schema/plan/utils";
import {
Tooltip,
TooltipContent,
@@ -41,6 +43,7 @@ export function PricingTable({
isLoading?: boolean;
}) {
const router = useRouter();
+ const [currency] = useCookieState("x-currency", "USD");
const selectedPlans = Object.entries(allPlans)
.filter(([key, _]) => plans.includes(key as keyof typeof allPlans))
.map(([key, value]) => ({ key: key as keyof typeof allPlans, ...value }));
@@ -56,6 +59,8 @@ export function PricingTable({
</TableHead>
{selectedPlans.map(({ key, ...plan }) => {
const isCurrentPlan = key === currentPlan;
+ const price = getPriceConfig(key, currency);
+ console.log(price);
return (
<TableHead
key={key}
@@ -71,7 +76,12 @@ export function PricingTable({
{plan.description}
</p>
<p className="mb-2 text-right">
- <span className="font-cal text-xl">{plan.price}€</span>{" "}
+ <span className="font-cal text-xl">
+ {new Intl.NumberFormat(price.locale, {
+ style: "currency",
+ currency: price.currency,
+ }).format(price.value)}
+ </span>
<span className="font-light text-muted-foreground text-sm">
/month
</span>
diff --git a/apps/web/src/middleware.ts b/apps/web/src/middleware.ts
index 26513a852..eed5a8d2d 100644
--- a/apps/web/src/middleware.ts
+++ b/apps/web/src/middleware.ts
@@ -2,6 +2,7 @@ import { NextResponse } from "next/server";
import { db } from "@openstatus/db/src/db";
import { user, usersToWorkspaces, workspace } from "@openstatus/db/src/schema";
+import { getCurrency } from "@openstatus/db/src/schema/plan/utils";
import { auth } from "@/lib/auth";
import { eq } from "@openstatus/db";
@@ -48,6 +49,9 @@ const publicAppPaths = [
export default auth(async (req) => {
const url = req.nextUrl.clone();
+ const continent = req.headers.get("x-vercel-ip-continent") || "NA";
+ const country = req.headers.get("x-vercel-ip-country") || "US";
+ const currency = getCurrency({ continent, country });
if (url.pathname.includes("api/trpc")) {
return NextResponse.next();
@@ -129,9 +133,14 @@ export default auth(async (req) => {
// reset workspace slug cookie if no auth
if (!req.auth && req.cookies.has("workspace-slug")) {
const response = NextResponse.next();
+ response.cookies.set("x-currency", currency);
response.cookies.delete("workspace-slug");
return response;
}
+
+ const response = NextResponse.next();
+ response.cookies.set("x-currency", currency);
+ return response;
});
export const config = {
diff --git a/packages/api/src/router/stripe/utils.ts b/packages/api/src/router/stripe/utils.ts
index 1aa4101fc..ba456c554 100644
--- a/packages/api/src/router/stripe/utils.ts
+++ b/packages/api/src/router/stripe/utils.ts
@@ -20,7 +20,7 @@ export const PLANS = [
monthly: {
priceIds: {
test: "price_1OVHQDBXJcTfzsyJjfiXl10Y",
- production: "price_1PdJ5MBXJcTfzsyJqWXeMUEQ",
+ production: "price_1RxsLNBXJcTfzsyJ7La5Jn5y",
},
},
},
@@ -31,7 +31,7 @@ export const PLANS = [
monthly: {
priceIds: {
test: "price_1OVHPlBXJcTfzsyJvPlB1kNb",
- production: "price_1PiBNnBXJcTfzsyJu65D9wfN",
+ production: "price_1RxsJzBXJcTfzsyJBOztaKlR",
},
},
},
diff --git a/packages/db/src/schema/plan/config.ts b/packages/db/src/schema/plan/config.ts
index 79272a24f..ced68df9d 100644
--- a/packages/db/src/schema/plan/config.ts
+++ b/packages/db/src/schema/plan/config.ts
@@ -8,7 +8,11 @@ export const allPlans: Record<
title: "Hobby" | "Starter" | "Pro";
id: WorkspacePlan;
description: string;
- price: number;
+ price: {
+ USD: number;
+ EUR: number;
+ INR: number;
+ };
limits: Limits;
}
> = {
@@ -16,7 +20,11 @@ export const allPlans: Record<
title: "Hobby",
id: "free",
description: "Perfect for personal projects",
- price: 0,
+ price: {
+ USD: 0,
+ EUR: 0,
+ INR: 0,
+ },
limits: {
monitors: 1,
"synthetic-checks": 30,
@@ -49,7 +57,11 @@ export const allPlans: Record<
title: "Starter",
id: "starter",
description: "Perfect for uptime monitoring",
- price: 30,
+ price: {
+ USD: 30,
+ EUR: 30,
+ INR: 3000,
+ },
limits: {
monitors: 20,
"synthetic-checks": 100,
@@ -118,7 +130,11 @@ export const allPlans: Record<
title: "Pro",
id: "team",
description: "Perfect for global synthetic monitoring",
- price: 100,
+ price: {
+ USD: 100,
+ EUR: 100,
+ INR: 10000,
+ },
limits: {
monitors: 50,
"synthetic-checks": 300,
diff --git a/packages/db/src/schema/plan/utils.ts b/packages/db/src/schema/plan/utils.ts
index e542cef87..85dcb115a 100644
--- a/packages/db/src/schema/plan/utils.ts
+++ b/packages/db/src/schema/plan/utils.ts
@@ -13,3 +13,32 @@ export function getLimits(plan: WorkspacePlan | null) {
export function getPlanConfig(plan: WorkspacePlan | null) {
return allPlans[plan || "free"];
}
+
+export function getCurrency({
+ continent,
+ country,
+}: {
+ continent: string;
+ country: string;
+}) {
+ if (country === "IN") {
+ return "INR";
+ }
+ if (continent === "EU") {
+ return "EUR";
+ }
+ return "USD";
+}
+
+export function getPriceConfig(plan: WorkspacePlan, currency?: string) {
+ const planConfig = allPlans[plan];
+ if (!currency) {
+ return { value: planConfig.price.USD, locale: "en-US", currency: "USD" };
+ }
+ if (currency in planConfig.price) {
+ const value = planConfig.price[currency as keyof typeof planConfig.price];
+ const locale = currency === "EUR" ? "fr-FR" : "en-US";
+ return { value, locale, currency };
+ }
+ return { value: planConfig.price.USD, locale: "en-US", currency: "USD" };
+}