5.1 KiB
5.1 KiB
Tasks: enterprise-payment
Change ID: enterprise-payment
Package: @suaveplan/payment
Target path: universal/commerce/payment
Prerequisite packages on Verdaccio: @suaveplan/error, @suaveplan/types, @suaveplan/events, @suaveplan/idempotency, @suaveplan/crypto, @suaveplan/fsm
0. Pre-flight (non-negotiable)
- 0.1 Confirm clean working tree (
git statusreports nothing) and no in-flight worktrees with uncommitted work (git worktree listthengit statusin each) - 0.2 If any worktrees exist under
.claude/worktrees/, account for each one: still-active agent, work-pending-merge, or stale-and-prune. Stale worktrees MUST be removed (git worktree remove) before opening a new one — code that has been lost twice on main is the failure mode this guards against. - 0.3 Confirm the package set in
MANIFEST.mdmatchesfind packages -maxdepth 3 -name package.jsoncount. Drift here means a prior change archived without regenerating the manifest.
1. Scaffold
- Create
universal/commerce/payment/withsrc/,tests/,src/__benchmarks__/and all feature subdirectories per the layout inproposal.md. - Write
package.jsonat version0.1.0with"type": "module","sideEffects": false, correctexportsmap,dependencies, anddevDependencies. - Write
tsconfig.jsonextending@suaveplan/typescript-config/library.json. - Write
vitest.config.tsusingcreateTestConfig()from@suaveplan/testing/config. - Add
lint,typecheck,test,test:coverage,build,docs:checkscripts.
2. Types + errors
src/types/common.ts—PaymentStatus,PaymentIntent,PaymentTransition,Receipt,CreatePaymentOptions,PaymentWebhookEvent,PaymentAdapter(interface),PaymentStore(interface),PaymentServiceOptions.src/types/common.md(≥200 words, both examples, API reference).src/error-codes.ts—PaymentErrorCode(8 codes), metadata, register, eager call.src/constants/index.ts—ALLOWED_TRANSITIONSmap,TERMINAL_STATUSESset,DEFAULT_CLOCK_SKEW_MS = 300_000.src/types/schemas.ts+schemas.md— Zod schemas. Note:PaymentIntentschema must NOT includepan,cvv, orexpiryfields.
3. Core modules
src/core/payment-fsm/—PaymentFsm: wraps@suaveplan/fsm; defines all valid transitions perALLOWED_TRANSITIONS;transition(intent, to, key)checks legality, throwsPAYMENT_INVALID_TRANSITIONif illegal, returns newPaymentIntentwith updated status and appendedPaymentTransition. Tests: all legal transitions, two illegal transitions, transition adds history entry.src/webhook/webhook-verifier/—WebhookVerifier.verify(body, secret, timestamp, signature): constant-time HMAC via@suaveplan/crypto; checks|now - timestamp| <= skewMs; throwsPAYMENT_SIGNATURE_FAILEDon mismatch,PAYMENT_SIGNATURE_FAILEDon expired. Tests: valid, tampered, expired, missing timestamp.src/receipt/receipt-generator/—generateReceipt(intent): Receipt: maps intent fields;formattedAmountusesIntl.NumberFormatwith currency. Tests: GBP formatting, USD formatting, partial refund status, all terminal statuses.src/utils/amount.ts—formatAmount,toMinorUnits,fromMinorUnits,addAmounts,subtractAmounts.subtractAmountsthrowsPAYMENT_AMOUNT_INVALIDif result < 0. Tests: all functions, negative subtraction throws.src/core/payment-service/—PaymentService:create,authorise,capture,refund,cancel— each wraps state transition with idempotency guard (fromoptions.idempotency); callsoptions.adapterfor provider interaction; stores result inoptions.store; emits event onoptions.bus.getReceipt(intentId)fetches from store and callsgenerateReceipt. Tests: happy path create→authorise→capture, duplicate idempotency key returns cached result, refund > captured amount throws,cancelfrom terminal state throws.
4. OTel instrumentation
- Span
payment.transitionwith attributespayment.from_status,payment.to_status,payment.provider; counterpayment.transition_total; histogrampayment.transition_duration_ms.
5. Property-based tests (tests/property.test.ts)
- State machine reachability: every valid create→terminal path through
ALLOWED_TRANSITIONSterminates incaptured,refunded,failed, orcancelled. - Amount arithmetic:
addAmounts(a, b) === addAmounts(b, a)for non-negative integers;fromMinorUnits(toMinorUnits(x, 'GBP'), 'GBP') === xfor 0–1 000 000.
6. Benchmarks (src/__benchmarks__/payment.bench.ts)
PaymentFsm.transition100 000 times.generateReceipt100 000 times.
7. Docs + verify
- One
.mdper implementation file (≥200 words, both examples, API reference). RootREADME.md. bunx turbo lint typecheck test --filter=@suaveplan/payment— green.bunx turbo test:coverage --filter=@suaveplan/payment— 100%.bun scripts/publish.ts --package @suaveplan/payment.- Archive:
openspec/archive/2026-04-24-enterprise-payment/.