Skip to content

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

🐛 Retry checkout without the currency on any invalid request

Luke Vella7f469cbd25a0
diff --git a/apps/web/src/features/billing/actions.ts b/apps/web/src/features/billing/actions.ts
index b0e6b5e4c..98c3cad41 100644
--- a/apps/web/src/features/billing/actions.ts
+++ b/apps/web/src/features/billing/actions.ts
@@ -27,7 +27,7 @@ import { getStripe } from "@/features/billing/service";
import {
canChangeBillingInterval,
isEarlySupporter,
- isStripeCurrencyConflictError,
+ isStripeInvalidRequestError,
resolvePriceSet,
} from "@/features/billing/utils";
import { getActiveSpaceForUser } from "@/features/space/data";
@@ -166,11 +166,14 @@ export const upgradeToProAction = authActionClient
},
});
- // A customer with something still open in another currency (e.g. a
- // subscription for another space) must stay in that currency.
+ // Stripe refuses a currency that conflicts with anything the customer
+ // still has open in another one (e.g. a subscription for another space),
+ // with no code or param to match on.
+ // Retrying without the hint lets Stripe pick the customer's currency; an
+ // unrelated invalid request fails again and is thrown.
const checkoutSession = await createCheckoutSession(currency).catch(
(error: unknown) => {
- if (currency && isStripeCurrencyConflictError(error)) {
+ if (currency && isStripeInvalidRequestError(error)) {
return createCheckoutSession();
}
throw error;
diff --git a/apps/web/src/features/billing/utils.test.ts b/apps/web/src/features/billing/utils.test.ts
index 133b3b0f2..3bc6034af 100644
--- a/apps/web/src/features/billing/utils.test.ts
+++ b/apps/web/src/features/billing/utils.test.ts
@@ -2,7 +2,7 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import {
formatMinorUnitAmount,
isEarlySupporter,
- isStripeCurrencyConflictError,
+ isStripeInvalidRequestError,
resolvePriceSet,
} from "./utils";
@@ -123,30 +123,21 @@ describe("formatMinorUnitAmount", () => {
});
});
-describe("isStripeCurrencyConflictError", () => {
- it("matches an invalid request on the currency param", () => {
+describe("isStripeInvalidRequestError", () => {
+ it("matches the error Stripe returns for a currency conflict", () => {
expect(
- isStripeCurrencyConflictError({
+ isStripeInvalidRequestError({
rawType: "invalid_request_error",
- param: "currency",
+ code: null,
+ param: null,
+ message: "You cannot combine currencies on a single customer.",
}),
).toBe(true);
});
- it("ignores other invalid requests and other errors", () => {
- expect(
- isStripeCurrencyConflictError({
- rawType: "invalid_request_error",
- param: "line_items",
- }),
- ).toBe(false);
- expect(
- isStripeCurrencyConflictError({
- rawType: "api_error",
- param: "currency",
- }),
- ).toBe(false);
- expect(isStripeCurrencyConflictError(new Error("boom"))).toBe(false);
- expect(isStripeCurrencyConflictError(null)).toBe(false);
+ it("ignores other errors", () => {
+ expect(isStripeInvalidRequestError({ rawType: "api_error" })).toBe(false);
+ expect(isStripeInvalidRequestError(new Error("boom"))).toBe(false);
+ expect(isStripeInvalidRequestError(null)).toBe(false);
});
});
diff --git a/apps/web/src/features/billing/utils.ts b/apps/web/src/features/billing/utils.ts
index 9217b0653..09a069999 100644
--- a/apps/web/src/features/billing/utils.ts
+++ b/apps/web/src/features/billing/utils.ts
@@ -28,17 +28,11 @@ export function isStripeResourceMissingError(error: unknown) {
return isStripeErrorCode(error, "resource_missing");
}
-/**
- * Stripe refuses a checkout currency that conflicts with something the
- * customer still has open in another currency (an active subscription, a
- * balance, a customer discount).
- */
-export function isStripeCurrencyConflictError(error: unknown) {
+export function isStripeInvalidRequestError(error: unknown) {
return (
typeof error === "object" &&
error !== null &&
- (error as { rawType?: unknown }).rawType === "invalid_request_error" &&
- (error as { param?: unknown }).param === "currency"
+ (error as { rawType?: unknown }).rawType === "invalid_request_error"
);
}