From 73e6fc6a4846480ba2f98968486f55b271fc208f Mon Sep 17 00:00:00 2001 From: Alexander Asomba <3323537+alexasomba@users.noreply.github.com> Date: Wed, 25 Nov 2026 10:02:27 +0100 Subject: [PATCH] fix(core): refuse stripe_tax with a non-Stripe provider --- .changeset/provider-tax-guard.md | 7 +++ packages/core/src/routes/checkout.ts | 13 +++++ packages/core/test/provider-tax-guard.test.ts | 56 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 .changeset/provider-tax-guard.md create mode 100644 packages/core/test/provider-tax-guard.test.ts diff --git a/.changeset/provider-tax-guard.md b/.changeset/provider-tax-guard.md new file mode 100644 index 0000000..92b4bf4 --- /dev/null +++ b/.changeset/provider-tax-guard.md @@ -0,0 +1,7 @@ +--- +"@dashcommerce/core": patch +--- + +Refuse `stripe_tax` checkout with a non-Stripe provider instead of charging the pre-tax total. + +Stripe Tax is calculated by Stripe at checkout, so core returns zero tax for `taxMode: "stripe_tax"`. A provider that ignores `automatic_tax` would silently undercharge. `checkout/create-session` now returns `409 stripe_tax_requires_stripe` and asks the merchant to switch to flat/table tax or to Stripe. diff --git a/packages/core/src/routes/checkout.ts b/packages/core/src/routes/checkout.ts index a80bce0..0337848 100644 --- a/packages/core/src/routes/checkout.ts +++ b/packages/core/src/routes/checkout.ts @@ -512,6 +512,19 @@ export const checkoutRoutes = { } const policy = await readPricingPolicy(ctx); + // Stripe Tax is calculated by Stripe at checkout; core deliberately + // returns zero tax for this mode. A provider that does not implement + // Stripe's automatic_tax would charge the pre-tax total, so fail + // loudly instead of silently undercharging. + if (policy.taxMode === "stripe_tax" && provider.id !== "stripe") { + return new Response( + JSON.stringify({ + error: `Stripe Tax is only available with Stripe. Set the tax mode to flat or table in Settings → Tax, or switch to Stripe.`, + code: "stripe_tax_requires_stripe", + }), + { status: 409, headers: { "Content-Type": "application/json" } }, + ); + } const recalculated = await finalizeCheckoutCart(ctx, withContact, items, policy); if (totalsChanged(withContact, recalculated)) { await save(ctx, recalculated); diff --git a/packages/core/test/provider-tax-guard.test.ts b/packages/core/test/provider-tax-guard.test.ts new file mode 100644 index 0000000..f7c5dca --- /dev/null +++ b/packages/core/test/provider-tax-guard.test.ts @@ -0,0 +1,56 @@ +/** + * Stripe Tax is calculated by Stripe at checkout. Core returns zero tax for + * that mode, so a provider that ignores automatic_tax would charge the + * pre-tax total. Checkout must refuse instead of undercharging. + */ + +import { afterEach, describe, expect, it } from "bun:test"; +import type { RouteContext } from "emdash"; +import { + createMockPaymentProvider, + registerPaymentProvider, + resetPaymentProviders, +} from "../src/payment-provider"; +import sandbox from "../src/sandbox-entry"; +import { makeRouteContext, ticketCart } from "./helpers/route-harness"; + +afterEach(() => resetPaymentProviders()); + +async function createSession(settings: Record) { + const { ctx } = makeRouteContext({ + "settings:paymentProvider": "mock", + "settings:checkoutMode": "hosted", + "settings:mockSecretKey": "k", + "settings:mockWebhookSecret": "test-secret", + ...settings, + }); + const sessionId = `sess_${Math.random()}`; + await ctx.kv.set(`cart:${sessionId}`, ticketCart(sessionId)); + const routeCtx = { + ...ctx, + request: new Request("http://test/checkout/create-session", { + method: "POST", + headers: { Cookie: `dashcommerce_sid=${sessionId}` }, + }), + input: {}, + } as unknown as RouteContext; + const res = (await sandbox.routes["checkout/create-session"].handler(routeCtx, ctx)) as Response; + return { status: res.status, body: (await res.json()) as Record }; +} + +describe("Stripe Tax with a non-Stripe provider", () => { + it("refuses stripe_tax instead of charging the pre-tax total", async () => { + registerPaymentProvider(createMockPaymentProvider()); + const res = await createSession({ "settings:taxMode": "stripe_tax" }); + expect(res.status).toBe(409); + expect(res.body.code).toBe("stripe_tax_requires_stripe"); + expect(String(res.body.error)).toMatch(/flat or table/i); + }); + + it("still starts checkout with flat tax", async () => { + registerPaymentProvider(createMockPaymentProvider()); + const res = await createSession({ "settings:taxMode": "flat" }); + expect(res.status).toBe(200); + expect(res.body.orderDraftId).toBeTruthy(); + }); +});