veza/veza-backend-api/internal/api
senke 92cf6d6f76 feat(backend,marketplace): refund reverse-charge with idempotent webhook
Fourth item of the v1.0.6 backlog, and the structuring one — the pre-
v1.0.6 RefundOrder wrote `status='refunded'` to the DB and called
Hyperswitch synchronously in the same transaction, treating the API
ack as terminal confirmation. In reality Hyperswitch returns `pending`
and only finalizes via webhook. Customers could see "refunded" in the
UI while their bank was still uncredited, and the seller balance
stayed credited even on successful refunds.

v1.0.6 flow
  Phase 1 — open a pending refund (short row-locked transaction):
    * validate permissions + 14-day window + double-submit guard
    * persist Refund{status=pending}
    * flip order to `refund_pending` (not `refunded` — that's the
      webhook's job)
  Phase 2 — call PSP outside the transaction:
    * Provider.CreateRefund returns (refund_id, status, err). The
      refund_id is the unique idempotency key for the webhook.
    * on PSP error: mark Refund{status=failed}, roll order back to
      `completed` so the buyer can retry.
    * on success: persist hyperswitch_refund_id, stay in `pending`
      even if the sync status is "succeeded". The webhook is the only
      authoritative signal. (Per customer guidance: "ne jamais flipper
      à succeeded sur la réponse synchrone du POST".)
  Phase 3 — webhook drives terminal state:
    * ProcessRefundWebhook looks up by hyperswitch_refund_id (UNIQUE
      constraint in the new `refunds` table guarantees idempotency).
    * terminal-state short-circuit: IsTerminal() returns 200 without
      mutating anything, so a Hyperswitch retry storm is safe.
    * on refund.succeeded: flip refund + order to succeeded/refunded,
      revoke licenses, debit seller balance, mark every SellerTransfer
      for the order as `reversed`. All within a row-locked tx.
    * on refund.failed: flip refund to failed, order back to
      `completed`.

Seller-side reconciliation
  * SellerBalance.DebitSellerBalance was using Postgres-only GREATEST,
    which silently failed on SQLite tests. Ported to a portable
    CASE WHEN that clamps at zero in both DBs.
  * SellerTransfer.Status = "reversed" captures the refund event in
    the ledger. The actual Stripe Connect Transfers:reversal call is
    flagged TODO(v1.0.7) — requires wiring through TransferService
    with connected-account context that the current transfer worker
    doesn't expose. The internal balance is corrected here so the
    buyer and seller views match as soon as the PSP confirms; the
    missing piece is purely the money-movement round-trip at Stripe.

Webhook routing
  * HyperswitchWebhookPayload extended with event_type + refund_id +
    error_message, with flat and nested (object.*) shapes supported
    (same tolerance as the existing payment fields).
  * New IsRefundEvent() discriminator: matches any event_type
    containing "refund" (case-insensitive) or presence of refund_id.
    routes_webhooks.go peeks the payload once and dispatches to
    ProcessRefundWebhook or ProcessPaymentWebhook.
  * No signature-verification changes — the same HMAC-SHA512 check
    protects both paths.

Handler response
  * POST /marketplace/orders/:id/refund now returns
    `{ refund: { id, status: "pending" }, message }` so the UI can
    surface the in-flight state. A new ErrRefundAlreadyRequested maps
    to 400 with a "already in progress" message instead of silently
    creating a duplicate row (the double-submit guard checks order
    status = `refund_pending` *before* the existing-row check so the
    error is explicit).

Schema
  * Migration 978_refunds_table.sql adds the `refunds` table with
    UNIQUE(hyperswitch_refund_id). The uniqueness constraint is the
    load-bearing idempotency guarantee — a duplicate PSP notification
    lands on the same DB row, and the webhook handler's
    FOR UPDATE + IsTerminal() check turns it into a no-op.
  * hyperswitch_refund_id is nullable (NULL between Phase 1 and
    Phase 2) so the UNIQUE index ignores rows that haven't been
    assigned a PSP id yet.

Partial refunds
  * The Provider.CreateRefund signature carries `amount *int64`
    already (nil = full), but the service call-site passes nil. Full
    refunds only for v1.0.6 — partial-refund UX needs a product
    decision and is deferred to v1.0.7. Flagged in the ErrRefund*
    section.

Tests (15 cases, all sqlite-in-memory + httptest-style mock provider)
  * RefundOrder phase 1
      - OpensPendingRefund: pending state, refund_id captured, order
        → refund_pending, licenses untouched
      - PSPErrorRollsBack: failed state, order reverts to completed
      - DoubleRequestRejected: second call returns
        ErrRefundAlreadyRequested, not a generic ErrOrderNotRefundable
      - NotCompleted / NoPaymentID / Forbidden / SellerCanRefund
      - ExpiredRefundWindow / FallbackExpiredNoDeadline
  * ProcessRefundWebhook
      - SucceededFinalizesState: refund + order + licenses + seller
        balance + seller transfer all reconciled in one tx
      - FailedRollsOrderBack: order returns to completed for retry
      - IsRefundEventIdempotentOnReplay: second webhook asserts
        succeeded_at timestamp is *unchanged*, proving the second
        invocation bailed out on IsTerminal (not re-ran)
      - UnknownRefundIDReturnsOK: never-issued refund_id → 200 silent
        (avoids a Hyperswitch retry storm on stale events)
      - MissingRefundID: explicit 400 error
      - NonTerminalStatusIgnored: pending/processing leave the row
        alone
  * HyperswitchWebhookPayload.IsRefundEvent: 6 dispatcher cases
    (flat event_type, mixed case, payment event, refund_id alone,
    empty, nested object.refund_id)

Backward compat
  * hyperswitch.Provider still exposes the old Refund(ctx,...) error
    method for any call-site that only cared about success/failure.
  * Old mockRefundPaymentProvider replaced; external mocks need to
    add CreateRefund — the interface is now (refundID, status, err).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 02:02:57 +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
handlers ci(cache): add save-always to persist cache on job failure 2026-04-14 18:01:40 +02: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 fix(middleware): persist maintenance flag via platform_settings table 2026-04-16 14:57:06 +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(v0.12.1): subscription plans service, handler, and routes 2026-03-10 19:36:57 +01: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 fix(backend,web): restore audio playback via /stream fallback 2026-04-16 14:52:26 +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(backend,marketplace): refund reverse-charge with idempotent webhook 2026-04-17 02:02:57 +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