Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Handle legacy SSO checkout sessions

Richie McIlroy94bd7b46f8cd
diff --git a/apps/web/__tests__/unit/sso-billing.test.ts b/apps/web/__tests__/unit/sso-billing.test.ts
index 4a287878b..511b38cb0 100644
--- a/apps/web/__tests__/unit/sso-billing.test.ts
+++ b/apps/web/__tests__/unit/sso-billing.test.ts
@@ -370,6 +370,7 @@ beforeEach(() => {
metadata: params.metadata as Stripe.Metadata,
customer: params.customer,
currency: params.currency ?? null,
+ allow_promotion_codes: params.allow_promotion_codes ?? false,
});
state.sessions.set(session.id, session);
return session;
@@ -854,6 +855,7 @@ describe("SSO checkout ownership and duplicate prevention", () => {
currency: "gbp",
customer: "cus_owner",
mode: "subscription",
+ allow_promotion_codes: true,
line_items: [{ price: STRIPE_SAML_SSO_PRICE_ID, quantity: 1 }],
metadata: expect.objectContaining({
type: "saml_sso",
@@ -913,6 +915,77 @@ describe("SSO checkout ownership and duplicate prevention", () => {
expect(state.billing.get(organizationId)?.checkoutCurrency).toBe("eur");
});
+ it("replaces an open checkout that cannot accept a promotion code", async () => {
+ await createSsoCheckout(checkoutInput());
+ const previous = state.billing.get(organizationId);
+ const previousSessionId = String(previous?.checkoutSessionId);
+ const previousSession = state.sessions.get(previousSessionId);
+ if (!previousSession) throw new Error("Missing checkout fixture");
+ state.sessions.set(previousSessionId, {
+ ...previousSession,
+ allow_promotion_codes: false,
+ });
+
+ await createSsoCheckout(checkoutInput());
+
+ expect(state.sessions.get(previousSessionId)?.status).toBe("expired");
+ expect(mocks.stripe.checkout.sessions.create).toHaveBeenCalledTimes(2);
+ const currentSessionId = String(
+ state.billing.get(organizationId)?.checkoutSessionId,
+ );
+ expect(currentSessionId).not.toBe(previousSessionId);
+ expect(state.sessions.get(currentSessionId)?.allow_promotion_codes).toBe(
+ true,
+ );
+ });
+
+ it("recovers an unsaved legacy checkout before replacing its idempotency key", async () => {
+ const oldAttemptId = "attempt_old";
+ state.billing.set(
+ organizationId,
+ billingRow({
+ checkoutAttemptId: oldAttemptId,
+ checkoutCurrency: "usd",
+ checkoutPriceId: STRIPE_SAML_SSO_PRICE_ID,
+ checkoutStartedAt: new Date("2026-09-02T00:00:00Z"),
+ }),
+ );
+ state.sessions.set(
+ "cs_old_unsaved",
+ checkoutSession({
+ id: "cs_old_unsaved",
+ status: "open",
+ payment_status: "unpaid",
+ url: "https://checkout.stripe.test/old-sso",
+ subscription: null,
+ allow_promotion_codes: false,
+ metadata: {
+ type: "saml_sso",
+ organizationId,
+ userId,
+ checkoutAttemptId: oldAttemptId,
+ },
+ }),
+ );
+ mocks.stripe.checkout.sessions.create.mockImplementationOnce(async () => {
+ throw Object.assign(new Error("Idempotency parameters changed"), {
+ type: "StripeIdempotencyError",
+ });
+ });
+
+ await createSsoCheckout(checkoutInput());
+
+ const calls = mocks.stripe.checkout.sessions.create.mock.calls;
+ expect(calls).toHaveLength(3);
+ expect(calls[0]?.[0].allow_promotion_codes).toBe(true);
+ expect(calls[1]?.[0].allow_promotion_codes).toBeUndefined();
+ expect(calls[1]?.[1].idempotencyKey).toBe(calls[0]?.[1].idempotencyKey);
+ expect(state.sessions.get("cs_old_unsaved")?.status).toBe("expired");
+ expect(state.billing.get(organizationId)?.checkoutAttemptId).not.toBe(
+ oldAttemptId,
+ );
+ });
+
it("confirms expiration before replacing an open checkout with the selected currency", async () => {
await createSsoCheckout(checkoutInput("usd"));
const previous = state.billing.get(organizationId);
diff --git a/apps/web/lib/sso/billing.ts b/apps/web/lib/sso/billing.ts
index 528c54808..8616a940a 100644
--- a/apps/web/lib/sso/billing.ts
+++ b/apps/web/lib/sso/billing.ts
@@ -730,20 +730,42 @@ export async function createSsoCheckout(
checkoutAttemptId: reserved.checkoutAttemptId,
};
const returnUrl = billingUrl(reserved.organizationId);
- session = await stripe().checkout.sessions.create(
- {
- mode: "subscription",
- customer: input.stripeCustomerId,
- currency: reserved.checkoutCurrency,
- line_items: [{ price: reserved.checkoutPriceId, quantity: 1 }],
- client_reference_id: reserved.organizationId,
- success_url: `${returnUrl}&sso_checkout={CHECKOUT_SESSION_ID}`,
- cancel_url: returnUrl,
- metadata,
- subscription_data: { metadata },
- },
- { idempotencyKey: `saml-sso-checkout-${reserved.checkoutAttemptId}` },
- );
+ const createParams: Stripe.Checkout.SessionCreateParams = {
+ mode: "subscription",
+ customer: input.stripeCustomerId,
+ currency: reserved.checkoutCurrency,
+ line_items: [{ price: reserved.checkoutPriceId, quantity: 1 }],
+ allow_promotion_codes: true,
+ client_reference_id: reserved.organizationId,
+ success_url: `${returnUrl}&sso_checkout={CHECKOUT_SESSION_ID}`,
+ cancel_url: returnUrl,
+ metadata,
+ subscription_data: { metadata },
+ };
+ const createOptions = {
+ idempotencyKey: `saml-sso-checkout-${reserved.checkoutAttemptId}`,
+ };
+ try {
+ session = await stripe().checkout.sessions.create(
+ createParams,
+ createOptions,
+ );
+ } catch (error) {
+ if (
+ !(
+ error instanceof Error &&
+ "type" in error &&
+ error.type === "StripeIdempotencyError"
+ )
+ )
+ throw error;
+ const legacyParams = { ...createParams };
+ delete legacyParams.allow_promotion_codes;
+ session = await stripe().checkout.sessions.create(
+ legacyParams,
+ createOptions,
+ );
+ }
await saveCheckoutSession(reserved, session.id);
}
if (
@@ -759,7 +781,8 @@ export async function createSsoCheckout(
}
if (
session.status === "open" &&
- reserved.checkoutCurrency !== input.currency
+ (reserved.checkoutCurrency !== input.currency ||
+ session.allow_promotion_codes !== true)
) {
const expired = await stripe().checkout.sessions.expire(
session.id,
@@ -768,7 +791,7 @@ export async function createSsoCheckout(
);
if (expired.id !== session.id || expired.status !== "expired") {
throw new Error(
- "The previous currency checkout could not be closed. Refresh before retrying.",
+ "The previous SAML SSO checkout could not be closed. Refresh before retrying.",
);
}
session = expired;