Files
2026-05-27 23:29:53 +01:00

3.8 KiB

Tasks: enterprise-api-plugin-payment

Change ID: enterprise-api-plugin-payment Package: @suaveplan/api-plugin-payment Target path: api/commerce/api-plugin-payment Prerequisite packages on Verdaccio: @suaveplan/api, @suaveplan/api-shared, @suaveplan/api-plugin-auth, @suaveplan/api-plugin-database, @suaveplan/payment, @suaveplan/events, @suaveplan/idempotency, @suaveplan/audit-logging, @suaveplan/rate-limiter, @suaveplan/error


0. Pre-flight (non-negotiable)

  • 0.1 Confirm clean working tree (git status reports nothing) and no in-flight worktrees with uncommitted work (git worktree list then git status in 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.md matches find packages -maxdepth 3 -name package.json count. Drift here means a prior change archived without regenerating the manifest.

1. Scaffold

  • Create api/commerce/api-plugin-payment/; package.json v0.0.0, "sideEffects": ["src/events.ts"]; standard tooling.

2. Types, schemas, errors, events

  • src/types/ — PaymentPluginConfig (Zod), SerializedIntent, SerializedRefund.
  • src/schemas/payment.schemas.ts — all Zod schemas from public API.
  • src/errors/ — 6-code PaymentErrorCode union, metadata, registration. Test + doc.
  • src/events.ts — PaymentDomainEventMap augmentation covering payment:intent.created|captured|failed, payment:refund.succeeded|failed, payment:dispute.opened.

3. SurrealQL

  • surql/intent.surql, surql/refund.surql, surql/webhook_event.surql — per-table schemas with tenantId indexes.

4. PaymentService

  • createIntent, confirm, capture, cancel, refund, list — all delegating to PaymentEngine wrapping caller's PaymentProvider.
  • BigInt Money amounts serialised to strings for JSON (SerializedIntent / SerializedRefund).
  • Each state change emits typed event via ctx.eventBus.emit(...) (no as any).
  • .test.ts (all methods, BigInt serialisation, event emission) + .md.

5. Routes

  • POST /payments — requireAuth + idempotency + rate-limit + audit.
  • GET /payments — requireAuth + pagination.
  • GET /payments/:id — requireAuth.
  • POST /payments/:id/confirm, capture, cancel — requireAuth + audit.
  • POST /payments/:id/refund — requireAuth + idempotency + rate-limit + audit.
  • POST /webhooks/payment — HMAC-SHA256 verify; replay protection (tolerance window); audit accepted events.
  • Route .test.ts (HMAC good/bad/replay, refund overage, cancel-after-capture) + .md.

6. Plugin factory

  • createPaymentPlugin(provider, config?): definePlugin, dependencies: ["database"], migrationsPath: "./surql".
  • onInit: read ctx.getPluginConfig; build PaymentEngine; capture engine listener disposers.
  • onClose: call all disposers; clear disposer array.
  • onTenantCreate: create SurrealDB namespace; register provider webhook endpoint.
  • onTenantDelete: purge tenant payment data (soft-delete + audit).

7. Integration tests + publish

  • Intent create → capture → refund round-trip (real SurrealDB; stub PaymentProvider).
  • HMAC replay protection integration test.
  • bunx turbo lint typecheck test --filter=@suaveplan/api-plugin-payment — green; 100% coverage.
  • bun scripts/publish.ts --package @suaveplan/api-plugin-payment.
  • Archive → openspec/archive/2026-04-24-enterprise-api-plugin-payment/.