Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Keep the trailing zero on partial Pro prices (#3594)
Luke Vella4b42e0e2a75c
diff --git a/apps/landing/src/app/[locale]/(main)/pricing/pricing-controls.tsx b/apps/landing/src/app/[locale]/(main)/pricing/pricing-controls.tsxindex b84dc756b..2a44cff77 100644--- a/apps/landing/src/app/[locale]/(main)/pricing/pricing-controls.tsx+++ b/apps/landing/src/app/[locale]/(main)/pricing/pricing-controls.tsx@@ -1,6 +1,7 @@"use client";import type { PricesByCurrency } from "@rallly/billing";+import { formatMinorUnitAmount } from "@rallly/billing/pricing";import { cn } from "@rallly/ui";import { NumberTicker } from "@rallly/ui/number-ticker";import {@@ -199,12 +200,7 @@ export function PlanPrice() {// price like 8.33 would render as 8.const format = React.useCallback((minorUnits: number) =>- new Intl.NumberFormat(locale, {- style: "currency",- currency: currency.toUpperCase(),- minimumFractionDigits: 0,- maximumFractionDigits: 2,- }).format(minorUnits / 100),+ formatMinorUnitAmount({ amount: minorUnits, currency, locale }),[locale, currency],);diff --git a/apps/web/src/app/[locale]/(space)/settings/billing/components/pro-plan-card.tsx b/apps/web/src/app/[locale]/(space)/settings/billing/components/pro-plan-card.tsxindex 2ed7665dc..c9fa47848 100644--- a/apps/web/src/app/[locale]/(space)/settings/billing/components/pro-plan-card.tsx+++ b/apps/web/src/app/[locale]/(space)/settings/billing/components/pro-plan-card.tsx@@ -1,6 +1,7 @@"use client";import { mutationOptions } from "@next-safe-action/adapter-tanstack-query";+import { formatMinorUnitAmount } from "@rallly/billing/pricing";import { posthog } from "@rallly/posthog/client";import { Badge } from "@rallly/ui/badge";import { Button } from "@rallly/ui/button";@@ -19,7 +20,6 @@ import type {BillingInterval,SubscriptionStatus,} from "@/features/billing/schema";-import { formatMinorUnitAmount } from "@/features/billing/utils";import { SpaceTierLabel } from "@/features/space/components/space-tier";import { Trans, useTranslation } from "@/i18n/client";import { useDateTime, useDateTimeConfig } from "@/lib/datetime/client";diff --git a/apps/web/src/features/billing/components/pay-wall-dialog.tsx b/apps/web/src/features/billing/components/pay-wall-dialog.tsxindex e212acb8a..7422d70e1 100644--- a/apps/web/src/features/billing/components/pay-wall-dialog.tsx+++ b/apps/web/src/features/billing/components/pay-wall-dialog.tsx@@ -1,6 +1,10 @@"use client";-import { isDisplayedCurrency, pricingData } from "@rallly/billing/pricing";+import {+ formatMinorUnitAmount,+ isDisplayedCurrency,+ pricingData,+} from "@rallly/billing/pricing";import { posthog } from "@rallly/posthog/client";import { Button } from "@rallly/ui/button";import {@@ -280,23 +284,12 @@ export function PayWallDialog({setSelectedPlan(spaceTierSchema.parse(value));};- const currencyFormatter = React.useMemo(- () =>- new Intl.NumberFormat(locale, {- style: "currency",- currency: currency.toUpperCase(),- minimumFractionDigits: 0,- maximumFractionDigits: 2,- }),- [locale, currency],- );-- const getProPrice = () => {- if (isAnnual) {- return currencyFormatter.format(amounts.yearly / 12 / 100);- }- return currencyFormatter.format(amounts.monthly / 100);- };+ const getProPrice = () =>+ formatMinorUnitAmount({+ amount: isAnnual ? Math.round(amounts.yearly / 12) : amounts.monthly,+ currency,+ locale,+ });return (<Dialog@@ -414,12 +407,16 @@ export function PayWallDialog({defaults="{yearlyPrice} per year, save {savings}."i18nKey="yearlyBillingDescription"values={{- yearlyPrice: currencyFormatter.format(- amounts.yearly / 100,- ),- savings: currencyFormatter.format(- (amounts.monthly * 12 - amounts.yearly) / 100,- ),+ yearlyPrice: formatMinorUnitAmount({+ amount: amounts.yearly,+ currency,+ locale,+ }),+ savings: formatMinorUnitAmount({+ amount: amounts.monthly * 12 - amounts.yearly,+ currency,+ locale,+ }),}}/></div>@@ -459,7 +456,12 @@ export function PayWallDialog({</TabsContent>)}{selectedPlan === "hobby" && (- <Button disabled={true} size="xl" className="w-full">+ <Button+ disabled={true}+ size="xl"+ shape="rounded"+ className="w-full"+ ><Trans i18nKey="currentPlan" defaults="Current plan" /></Button>)}diff --git a/apps/web/src/features/billing/components/upgrade-button.tsx b/apps/web/src/features/billing/components/upgrade-button.tsxindex 5f8ef1316..9cae8f6eb 100644--- a/apps/web/src/features/billing/components/upgrade-button.tsx+++ b/apps/web/src/features/billing/components/upgrade-button.tsx@@ -31,6 +31,7 @@ export const UpgradeButton = ({<Buttontype="button"size="xl"+ shape="rounded"className={className}variant="primary"loading={upgradeToPro.isPending}diff --git a/apps/web/src/features/billing/utils.test.ts b/apps/web/src/features/billing/utils.test.tsindex 3bc6034af..8851d61af 100644--- a/apps/web/src/features/billing/utils.test.ts+++ b/apps/web/src/features/billing/utils.test.ts@@ -1,6 +1,5 @@import { afterEach, describe, expect, it, vi } from "vitest";import {- formatMinorUnitAmount,isEarlySupporter,isStripeInvalidRequestError,resolvePriceSet,@@ -103,26 +102,6 @@ describe("resolvePriceSet", () => {});});-describe("formatMinorUnitAmount", () => {- it("formats minor units as a localized currency string", () => {- expect(- formatMinorUnitAmount({ amount: 5600, currency: "usd", locale: "en" }),- ).toBe("$56");- });-- it("keeps cents when the amount needs them", () => {- expect(- formatMinorUnitAmount({ amount: 467, currency: "usd", locale: "en" }),- ).toBe("$4.67");- });-- it("does not scale zero decimal currencies", () => {- expect(- formatMinorUnitAmount({ amount: 5600, currency: "jpy", locale: "en" }),- ).toBe("¥5,600");- });-});-describe("isStripeInvalidRequestError", () => {it("matches the error Stripe returns for a currency conflict", () => {expect(diff --git a/apps/web/src/features/billing/utils.ts b/apps/web/src/features/billing/utils.tsindex 09a069999..7ad7385f5 100644--- a/apps/web/src/features/billing/utils.ts+++ b/apps/web/src/features/billing/utils.ts@@ -70,32 +70,6 @@ export function resolvePriceSet({return { monthly: pricing.monthly, yearly: pricing.yearly };}-export function formatMinorUnitAmount({- amount,- currency,- locale,-}: {- amount: number;- currency: string;- locale: string;-}) {- const code = currency.toUpperCase();- // Stripe amounts are in the currency's minor unit, which Intl knows: two- // fraction digits for most currencies, none for JPY and friends.- const minorUnitDigits =- new Intl.NumberFormat(locale, {- style: "currency",- currency: code,- }).resolvedOptions().maximumFractionDigits ?? 2;- // Whole amounts render without trailing zeros ("$10", not "$10.00").- return new Intl.NumberFormat(locale, {- style: "currency",- currency: code,- minimumFractionDigits: 0,- maximumFractionDigits: minorUnitDigits,- }).format(amount / 10 ** minorUnitDigits);-}-/*** A subscription that renews and can therefore change billing interval.* `past_due` is stored as active (Stripe keeps retrying), but its nextdiff --git a/packages/billing/src/pricing.test.ts b/packages/billing/src/pricing.test.tsindex bd70fd3cb..c3bb20054 100644--- a/packages/billing/src/pricing.test.ts+++ b/packages/billing/src/pricing.test.ts@@ -1,5 +1,9 @@import { describe, expect, it } from "vitest";-import { getCountryCurrency, yearlySavingsPercent } from "./pricing";+import {+ formatMinorUnitAmount,+ getCountryCurrency,+ yearlySavingsPercent,+} from "./pricing";describe("yearlySavingsPercent", () => {it("rounds the saving of yearly against twelve months to a whole percent", () => {@@ -38,3 +42,29 @@ describe("getCountryCurrency", () => {expect(getCountryCurrency("US", ["eur", "gbp"])).toBe("eur");});});++describe("formatMinorUnitAmount", () => {+ it("formats minor units as a localized currency string", () => {+ expect(+ formatMinorUnitAmount({ amount: 5600, currency: "usd", locale: "en" }),+ ).toBe("$56");+ });++ it("keeps cents when the amount needs them", () => {+ expect(+ formatMinorUnitAmount({ amount: 467, currency: "usd", locale: "en" }),+ ).toBe("$4.67");+ });++ it("keeps the trailing zero of a partial amount", () => {+ expect(+ formatMinorUnitAmount({ amount: 550, currency: "eur", locale: "en" }),+ ).toBe("€5.50");+ });++ it("does not scale zero decimal currencies", () => {+ expect(+ formatMinorUnitAmount({ amount: 5600, currency: "jpy", locale: "en" }),+ ).toBe("¥5,600");+ });+});diff --git a/packages/billing/src/pricing.ts b/packages/billing/src/pricing.tsindex 6b8e80df9..f85de06fb 100644--- a/packages/billing/src/pricing.ts+++ b/packages/billing/src/pricing.ts@@ -98,3 +98,32 @@ export function getCountryCurrency(}return available.includes("usd") ? "usd" : available[0];}++export function formatMinorUnitAmount({+ amount,+ currency,+ locale,+}: {+ amount: number;+ currency: string;+ locale: string;+}) {+ const code = currency.toUpperCase();+ // Stripe amounts are in the currency's minor unit, which Intl knows: two+ // fraction digits for most currencies, none for JPY and friends.+ const minorUnitDigits =+ new Intl.NumberFormat(locale, {+ style: "currency",+ currency: code,+ }).resolvedOptions().maximumFractionDigits ?? 2;+ const scale = 10 ** minorUnitDigits;+ // Whole amounts drop the fraction ("$10", not "$10.00"); any other amount+ // shows every minor digit ("€5.50", not "€5.5").+ const fractionDigits = amount % scale === 0 ? 0 : minorUnitDigits;+ return new Intl.NumberFormat(locale, {+ style: "currency",+ currency: code,+ minimumFractionDigits: fractionDigits,+ maximumFractionDigits: fractionDigits,+ }).format(amount / scale);+}