bidstage/test/payment-idempotency.test.ts
caelum0x c709b0d3ec feat: harden money-path idempotency across checkout, settlement, and reversal
- Send stable per-checkout Idempotency-Key headers to Creem and Dodo so
  concurrent retries converge on one payable provider session
- State-guarded provider-session persistence: a late provider response can
  no longer clobber a checkout a webhook already settled/reversed
- Key refund/dispute idempotency on the provider adjustment id (not the
  webhook event id) with ON CONFLICT claim, preventing double-subtraction
  when the same refund replays under a new event id
- Surface a conflicting second capture on an already-fulfilled checkout as
  an incident instead of silently swallowing it
- Clamp reversals to the settled contribution so post-dispute refunds are
  zero-delta no-ops instead of jamming the webhook forever
- Unique indexes: (provider, provider_adjustment_id) on payment_adjustments,
  partial (provider, provider_transaction_id) on bids (migration 0024)
- 4 new unit tests + 5 new Postgres integration scenarios (all green vs
  real Postgres 16)
2026-09-05 17:09:23 +03:00

115 lines
3.9 KiB
TypeScript

import { test } from "node:test";
import assert from "node:assert/strict";
import { creemCumulativeReversedCents } from "../lib/creem-refund";
import { createDodoCheckout } from "../lib/dodo";
import { createCreemCheckout } from "../lib/creem";
import { resetServerEnvForTests } from "../lib/env";
test("creemCumulativeReversedCents scales external refund amounts into contribution space", () => {
// Equal paid/credited amounts pass through unchanged.
assert.equal(
creemCumulativeReversedCents({ transactionAmount: 700, amountPaid: 700, refundedAmount: 300 }),
300,
);
// A different paid amount is scaled proportionally.
assert.equal(
creemCumulativeReversedCents({ transactionAmount: 700, amountPaid: 1000, refundedAmount: 500 }),
350,
);
// A full refund is clamped to the contribution.
assert.equal(
creemCumulativeReversedCents({ transactionAmount: 700, amountPaid: 700, refundedAmount: 700 }),
700,
);
});
test("creemCumulativeReversedCents rejects inconsistent external amounts", () => {
assert.throws(
() => creemCumulativeReversedCents({ transactionAmount: 700, amountPaid: 700, refundedAmount: 800 }),
/exceeds the amount paid/,
);
assert.throws(
() => creemCumulativeReversedCents({ transactionAmount: 700, amountPaid: 0, refundedAmount: 100 }),
/positive integer/,
);
assert.throws(
() => creemCumulativeReversedCents({ transactionAmount: 700, amountPaid: 700, refundedAmount: 0 }),
/positive integer/,
);
});
function setEnv(name: string, value: string): void {
(process.env as Record<string, string | undefined>)[name] = value;
}
type CapturedRequest = { url: string; headers: Record<string, string> };
async function withMockedFetch(
body: unknown,
run: () => Promise<void>,
): Promise<CapturedRequest> {
const captured: CapturedRequest = { url: "", headers: {} };
const original = globalThis.fetch;
globalThis.fetch = (async (url: string | URL, init?: RequestInit) => {
captured.url = String(url);
captured.headers = { ...(init?.headers as Record<string, string> | undefined) };
return {
ok: true,
status: 200,
json: async () => body,
} as Response;
}) as unknown as typeof fetch;
try {
await run();
} finally {
globalThis.fetch = original;
}
return captured;
}
test("createDodoCheckout sends a stable per-checkout idempotency key", async () => {
setEnv("DODO_PAYMENTS_API_KEY", "dodo_test_key");
setEnv("DODO_PAYMENTS_WEBHOOK_KEY", "whsec_dodo");
setEnv("DODO_PAYMENTS_PRODUCT_ID", "prod_dodo");
setEnv("DODO_PAYMENTS_BUSINESS_ID", "biz_dodo");
setEnv("DODO_PAYMENTS_TEST_MODE", "true");
resetServerEnvForTests();
const captured = await withMockedFetch(
{ session_id: "sess_dodo_1", checkout_url: "https://checkout.dodopayments.com/pay/1" },
async () => {
const checkout = await createDodoCheckout({
requestId: "checkout-uuid-1",
amountCents: 500,
successUrl: "https://bidstage.example/receipt/abc",
metadata: { product: "bidstage" },
});
assert.equal(checkout.id, "sess_dodo_1");
},
);
assert.equal(captured.headers["Idempotency-Key"], "checkout-uuid-1");
});
test("createCreemCheckout sends a stable per-checkout idempotency key", async () => {
setEnv("CREEM_API_KEY", "creem_test_key");
setEnv("CREEM_WEBHOOK_SECRET", "creem_secret");
setEnv("CREEM_PRODUCT_ID", "prod_creem");
setEnv("CREEM_TEST_MODE", "true");
resetServerEnvForTests();
const captured = await withMockedFetch(
{ id: "sess_creem_1", checkout_url: "https://checkout.creem.io/pay/1", status: "pending" },
async () => {
const checkout = await createCreemCheckout({
requestId: "checkout-uuid-2",
amountCents: 500,
successUrl: "https://bidstage.example/receipt/abc",
metadata: { product: "bidstage" },
});
assert.equal(checkout.id, "sess_creem_1");
},
);
assert.equal(captured.headers["Idempotency-Key"], "checkout-uuid-2");
});