Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/provider-tax-guard.md
Original file line number Diff line number Diff line change
@@ -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.
13 changes: 13 additions & 0 deletions packages/core/src/routes/checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
56 changes: 56 additions & 0 deletions packages/core/test/provider-tax-guard.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, unknown>) {
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<string, unknown> };
}

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();
});
});