Initial Commit
This commit is contained in:
98
openspec/changes/enterprise-api-plugin-payment/proposal.md
Normal file
98
openspec/changes/enterprise-api-plugin-payment/proposal.md
Normal file
@@ -0,0 +1,98 @@
|
||||
# OpenSpec Change: enterprise-api-plugin-payment
|
||||
|
||||
- **Change ID**: enterprise-api-plugin-payment
|
||||
- **Package**: `@suaveplan/api-plugin-payment`
|
||||
- **Category**: api/commerce
|
||||
- **Target path**: `api/commerce/api-plugin-payment`
|
||||
- **Status**: PROPOSED
|
||||
- **Date**: 2026-04-24
|
||||
- **Reference spec**: `/mnt/s/web/suaveplan/enterprise/api-plugin-payment.md`
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
Deliver `@suaveplan/api-plugin-payment` on the genesis branch from scratch at enterprise quality. HTTP surface over `@suaveplan/payment`: mounts `/payments` (intent lifecycle + refunds + list) and `/webhooks/payment` (HMAC-verified ingestion). Wraps a caller-supplied `PaymentProvider` in a `PaymentEngine`. Per-table SurrealQL migrations. Typed event bus — no `as any` casts. Full `onTenantCreate`/`onTenantDelete` lifecycle. Idempotency on mutations, rate limiting, and audit logging on all write paths. PCI-DSS SAQ-A posture documented.
|
||||
|
||||
---
|
||||
|
||||
## Available workspace deps on genesis
|
||||
|
||||
| Package | Role |
|
||||
|---|---|
|
||||
| `@suaveplan/api` (`workspace:^`) | `definePlugin` |
|
||||
| `@suaveplan/api-shared` (`workspace:^`) | Response helpers |
|
||||
| `@suaveplan/api-plugin-auth` (`workspace:^`) | `requireAuth` |
|
||||
| `@suaveplan/api-plugin-database` (`workspace:^`) | SurrealDB |
|
||||
| `@suaveplan/payment` (`workspace:^`) | `PaymentEngine`, `PaymentProvider` |
|
||||
| `@suaveplan/events` (`workspace:^`) | Typed event bus |
|
||||
| `@suaveplan/error` (`workspace:^`) | Typed errors |
|
||||
| `@suaveplan/idempotency` (`workspace:^`) | Mutation guard |
|
||||
| `@suaveplan/audit-logging` (`workspace:^`) | Write-path audit |
|
||||
| `@suaveplan/rate-limiter` (`workspace:^`) | Rate limiting |
|
||||
| `hono` + `@hono/zod-openapi` + `zod` | Routes |
|
||||
| `@opentelemetry/api` | OTel |
|
||||
|
||||
---
|
||||
|
||||
## Public API surface
|
||||
|
||||
```ts
|
||||
function createPaymentPlugin(provider: PaymentProvider, config?: PaymentPluginConfig): PluginDescriptor<PaymentPluginConfig>
|
||||
class PaymentService { createIntent, confirm, capture, cancel, refund, list }
|
||||
|
||||
// Schemas
|
||||
CreatePaymentIntentSchema, MoneySchema, PaymentIntentIdParamSchema
|
||||
PaymentIntentResponseSchema, PaymentListQuerySchema, RefundRequestSchema, RefundResponseSchema
|
||||
|
||||
// Types
|
||||
PaymentPluginConfig, PaymentDomainEventMap, SerializedIntent, SerializedRefund
|
||||
```
|
||||
|
||||
### Error codes
|
||||
|
||||
```ts
|
||||
type PaymentErrorCode =
|
||||
| "PAYMENT_INTENT_NOT_FOUND"
|
||||
| "PAYMENT_ALREADY_CAPTURED"
|
||||
| "PAYMENT_REFUND_EXCEEDS"
|
||||
| "PAYMENT_WEBHOOK_SIGNATURE"
|
||||
| "PAYMENT_WEBHOOK_STALE"
|
||||
| "PAYMENT_PROVIDER_ERROR"
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Genesis additions vs main-branch
|
||||
|
||||
1. **`PaymentBusEventMap`** declared via `src/events.ts` module augmentation — replaces all `(ctx.eventBus as any)?.emit?.()` calls.
|
||||
2. **Per-table SurrealQL** — `intent.surql`, `refund.surql`, `webhook_event.surql`.
|
||||
3. **Real `onTenantCreate`/`onTenantDelete`** — SurrealDB namespacing + provider configuration.
|
||||
4. **`onClose`** detaches all engine listeners registered in `onInit`.
|
||||
5. **`onInit` reads `ctx.getPluginConfig`** for `webhookSecret` / `webhookToleranceMs` overrides.
|
||||
6. **Idempotency** on `POST /payments` and `POST /payments/:id/refund`.
|
||||
7. **Rate limiting** on create-intent and refund.
|
||||
8. **Audit logging** on every state-changing operation and accepted webhook.
|
||||
|
||||
---
|
||||
|
||||
## File layout
|
||||
|
||||
```text
|
||||
api/commerce/api-plugin-payment/
|
||||
package.json tsconfig.json vitest.config.ts
|
||||
surql/intent.surql surql/refund.surql surql/webhook_event.surql
|
||||
src/
|
||||
index.ts events.ts
|
||||
types/ errors/
|
||||
services/payment/
|
||||
schemas/
|
||||
routes/payment/ routes/webhook/ routes/problem-json.ts
|
||||
tests/integration.test.ts
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Impact
|
||||
|
||||
Prerequisites: `@suaveplan/api`, `@suaveplan/api-plugin-auth`, `@suaveplan/api-plugin-database`, `@suaveplan/payment`, `@suaveplan/events`, `@suaveplan/idempotency`, `@suaveplan/audit-logging`, `@suaveplan/rate-limiter`, `@suaveplan/error`.
|
||||
74
openspec/changes/enterprise-api-plugin-payment/tasks.md
Normal file
74
openspec/changes/enterprise-api-plugin-payment/tasks.md
Normal file
@@ -0,0 +1,74 @@
|
||||
# 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/`.
|
||||
Reference in New Issue
Block a user