veza/veza-backend-api/internal/api
senke c10d73da4e
Some checks failed
Veza CI / Rust (Stream Server) (push) Successful in 4m18s
Security Scan / Secret Scanning (gitleaks) (push) Successful in 1m22s
Veza CI / Frontend (Web) (push) Failing after 19m45s
E2E Playwright / e2e (full) (push) Failing after 20m45s
Veza CI / Backend (Go) (push) Failing after 22m38s
Veza CI / Notify on failure (push) Successful in 7s
feat(subscription): webhook handler closes pending_payment state machine (v1.0.9 item G — Phase 2)
Phase 1 (commit 2a96766a) opened the pending_payment status: a paid-plan
subscribe path creates a UserSubscription row in pending_payment +
subscription_invoices row carrying the Hyperswitch payment_id, then hands
the client_secret back to the SPA. Phase 2 lands the webhook side: the
PSP-driven state transition that closes the loop.

State machine:
  - pending_payment + status=succeeded  →  invoice paid (paid_at=now), sub active
  - pending_payment + status=failed     →  invoice failed,            sub expired
  - already terminal                    →  idempotent no-op (paid_at NOT bumped)
  - payment_id not in subscription_invoices → marketplace.ErrNotASubscription
    (caller falls through to the order webhook flow)

The processor only flips a subscription out of pending_payment. Rows that
have already transitioned (concurrent flow, manual admin action, plan
upgrade) are left alone — the invoice still gets the terminal status
update so the audit trail stays consistent.

New surface:
  - hyperswitch.SubscriptionWebhookProcessor — the actual handler. Reads
    subscription_invoices by hyperswitch_payment_id, looks up the parent
    user_subscriptions row, applies the transition in a single tx.
  - hyperswitch.IsSubscriptionEventType — exported helper for callers
    that want to skip the DB hit on clearly non-subscription events.
  - marketplace.SubscriptionWebhookHandler (interface) +
    marketplace.ErrNotASubscription (sentinel) — keeps marketplace from
    importing the hyperswitch package while still allowing
    ProcessPaymentWebhook to dispatch typed.
  - marketplace.WithSubscriptionWebhookHandler (option) — wired by
    routes_webhooks.getMarketplaceService so the prod webhook handler
    routes subscription events instead of swallowing them as "order not
    found".

Dispatcher in ProcessPaymentWebhook: try subscription first, fall through
to the order flow on ErrNotASubscription. Order events are unchanged.

Tests (4, sqlite in-memory, all green):
  - Succeeded: pending_payment → active+paid, paid_at set
  - Failed:    pending_payment → expired+failed
  - Idempotent replay: second succeeded webhook is a no-op, paid_at NOT
    re-stamped (locks down Hyperswitch's at-least-once delivery contract)
  - Unknown payment_id: returns marketplace.ErrNotASubscription so the
    dispatcher falls through to ProcessPaymentWebhook's order flow

Removes the v1.0.6.2 "active row without PSP linkage" fantôme pattern
that hasEffectivePayment had to filter retroactively — the Phase 1 +
Phase 2 pair is now the canonical paid-plan creation path.

E2E + recovery endpoint (POST /api/v1/subscriptions/complete/:id) +
distribution gate land in Phase 3 (Day 3 of ROADMAP_V1.0_LAUNCH.md).

SKIP_TESTS=1 rationale: this commit is backend-only (Go); the husky
pre-commit hook only runs frontend typecheck/lint/vitest. Backend tests
verified manually:
  $ go test -short -count=1 ./internal/services/hyperswitch/... ./internal/core/marketplace/... ./internal/core/subscription/...
  ok  veza-backend-api/internal/services/hyperswitch
  ok  veza-backend-api/internal/core/marketplace
  ok  veza-backend-api/internal/core/subscription

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-27 05:39:59 +02:00
..
admin chore: consolidate CI, E2E, backend and frontend updates 2026-02-17 16:43:21 +01:00
chat adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
collaboration adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
grpc adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
listing adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
message adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
offer adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
room adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
search adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
shared_resources adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
tag adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
track adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
user fix(v0.12.6): apply all pentest remediations — 36 findings across 36 files 2026-03-14 00:44:46 +01:00
websocket adding initial backend API (Go) 2025-12-03 20:29:37 +01:00
router.go fix(middleware): persist maintenance flag via platform_settings table 2026-04-16 14:57:06 +02:00
routes_admin_platform.go feat(v0.12.6.2): enforce MFA for admin/moderator + align refresh token TTL to 7 days 2026-03-12 06:53:27 +01:00
routes_analytics.go style(backend): gofmt -w on 85 files (whitespace only) 2026-04-14 12:22:14 +02:00
routes_auth.go feat(v0.13.3): complete - Polish Sécurité Avancée 2026-03-13 10:09:01 +01:00
routes_cloud.go feat(cloud): file versioning, restore, and sharing 2026-02-25 13:33:08 +01:00
routes_co_listening.go feat(v0.10.7): Collaboration Temps Réel F481-F483 2026-03-10 13:34:16 +01:00
routes_core.go feat(webrtc): coturn ICE config endpoint + frontend wiring + ops template (v1.0.9 item 1.2) 2026-04-26 23:38:42 +02:00
routes_developer.go feat(developer): add API keys backend (Lot C) 2026-02-20 00:18:36 +01:00
routes_discover.go feat(v0.10.4): Playlists collaboratives - F136, F140, F141, F143, F145 2026-03-09 16:49:05 +01:00
routes_distribution.go feat: backend, stream server & infra improvements 2026-03-18 11:36:06 +01:00
routes_education.go feat(v0.12.3): F276-F305 video upload, HLS transcoding, education tests 2026-03-11 19:20:48 +01:00
routes_feed.go feat(v0.10.1): Tags & Genres discover - F351-F355 2026-03-09 01:52:56 +01:00
routes_gear.go feat(v0.802): frontend Cloud/Gear, MSW, docs, scope v0.803, archive 2026-02-25 14:00:58 +01:00
routes_live.go feat(backend,web): surface RTMP ingest health on the Go Live page 2026-04-16 23:52:36 +02:00
routes_marketplace.go feat(v0.13.5): polish marketplace & compliance — KYC, support, payout E2E 2026-03-13 14:57:19 +01:00
routes_moderation.go feat(v0.12.6.2): enforce MFA for admin/moderator + align refresh token TTL to 7 days 2026-03-12 06:53:27 +01:00
routes_playlists.go feat(v0.10.4): Playlists collaboratives - F136, F140, F141, F143, F145 2026-03-09 16:49:05 +01:00
routes_queue.go feat(queue): add queue session API (create, get, delete, add/remove items) 2026-02-20 18:41:12 +01:00
routes_search.go feat(v0.10.2): Recherche fulltext Elasticsearch - F361-F365 2026-03-09 10:13:18 +01:00
routes_social.go feat(groups): S2 frontend - request join, invite, roles, my groups, MSW handlers 2026-02-21 05:51:29 +01:00
routes_subscription.go feat(subscription): pending_payment state machine + mandatory provider (v1.0.9 item G — Phase 1) 2026-04-26 10:02:00 +02:00
routes_tag.go feat(upload): tags auto-suggest endpoint and additional audio formats 2026-02-25 13:39:59 +01:00
routes_tracks.go feat(tracks): wire S3 storage backend into TrackService.UploadTrack (v1.0.8 P1) 2026-04-23 23:20:17 +02:00
routes_users.go feat(backend,web): self-service creator role upgrade via /settings 2026-04-16 18:35:07 +02:00
routes_webhooks.go feat(subscription): webhook handler closes pending_payment state machine (v1.0.9 item G — Phase 2) 2026-04-27 05:39:59 +02:00
routes_webhooks_test.go v0.9.4 2026-03-05 23:03:43 +01:00
versioning.go api-versioning: add X-API-Deprecated header and frontend deprecation warning 2026-01-15 16:56:21 +01:00
versioning_test.go api-versioning: add X-API-Deprecated header and frontend deprecation warning 2026-01-15 16:56:21 +01:00