Send seller payouts through Paystream from the back office
C# · ASP.NET Core · advanced · modification
Finance pays sellers today by creating each transfer in Paystream's dashboard and pasting its id into Mark as sent. This PR gives the back office a Send button's endpoint instead: `POST /api/admin/payouts/{id}/send` creates the transfer through Paystream's API and records it on the payout. `PaystreamClient.cs` (new) is a typed `HttpClient` that posts the transfer and returns its id, with its options class. `PayoutsModule.cs` gains `AddPayouts`, which binds and validates the `Paystream` options at startup and registers the client with the standard resilience handler (5 second attempts, two retries) so a slow or briefly unavailable Paystream does not fail the press, and maps the new route in the existing finance group. `Program.cs` calls `AddPayouts`, and `Marketplace.Api.csproj` adds `Microsoft.Extensions.Http.Resilience`. `PaystreamClientTests.cs` (new) covers the client against a fake message handler.
Marketplace back-office API on ASP.NET Core 10 minimal APIs, .NET 10 (C# 14, nullable reference types and implicit usings enabled), EF Core 10 with Npgsql, xUnit for tests (the test project, unchanged, has a global `using Xunit`). `Program.cs` is shown in full. Existing and unchanged: `MarketDbContext.Payouts` maps the `Payout` entity: `Guid Id`; `int SellerId`; `string DestinationAccount`, the seller's Paystream account id, checked when the seller onboards; `long AmountCents`, from 1 to 100,000,000 in the currency's minor unit (what Paystream's `amount` takes); `string Currency`, a lowercase ISO code Paystream accepts; `PayoutStatus Status` (`Pending`, `Sent`, `Expired`); `string? TransferId`; and the UTC `DateTime CreatedAt` and `DateTime? SentAt`. The weekly statement job (unchanged) creates `Pending` payouts and moves a payout still `Pending` after 7 days to `Expired` for finance's manual review; a payout becomes `Sent` only through `mark-sent` or this endpoint. A 2xx answer from Paystream carries the transfer as JSON with a non-empty `id`. The `Paystream` configuration section (`BaseUrl`, the API root ending in `/`, and `ApiKey`) is set through environment variables in every environment. The JWT bearer setup gives finance staff the `finance` role. `UseExceptionHandler` with `AddProblemDetails` logs an unhandled exception and answers a generic 500 problem body. If saving the payout fails after Paystream answered (a browser abort, a database error), the payout also stays `Pending` for another press. Finance sends at most a few hundred payouts a week, one press at a time. Microsoft.Extensions.Http.Resilience 10.11: `AddStandardResilienceHandler` chains a rate limiter, a total request timeout (30 s by default), the retry, a circuit breaker and the per-attempt timeout; the PR overrides only the attempt timeout and the retry count.
Requirements
- New endpoint `POST /api/admin/payouts/{id}/send`, in the existing finance-only group, sends a `Pending` payout to the seller through Paystream's `POST /v1/transfers` and records it: status `Sent`, the Paystream transfer id and `SentAt`. It answers 200 with `{ "id", "transferId" }`. A payout that is not `Pending` (already `Sent`, or `Expired`) answers 409 and calls nothing; an unknown id answers 404.
- A Paystream call that looks transient is retried: an attempt with no answer within 5 seconds, a dropped connection, or a 408, 429 or 5xx answer is tried again, up to 3 attempts in all, with the standard resilience handler's backoff between them. If no attempt succeeds, or Paystream answers any other error status, the request fails with the app's generic 500, the payout stays `Pending`, and finance can press Send again.
- Paystream creates one transfer per `POST /v1/transfers` unless the request carries an `Idempotency-Key` header (any string up to 255 characters). A key Paystream has seen in the last 30 days returns that first response unchanged and creates nothing; while the first request with a key is still being processed, another request with the same key gets 409 `idempotency_key_in_use`; a key whose first request was answered with an error status is not remembered. An error status means Paystream created no transfer. The `description` is free text on the seller's statement and is not used to deduplicate. Paystream may create a transfer and still answer after our 5 seconds.
- Each payout reaches the seller at most once, however many attempts the retries or later Send presses make for it.
- Unchanged: `GET /api/admin/payouts` (the pending list) and `POST /api/admin/payouts/{id}/mark-sent`, which stays for payouts finance pays outside Paystream.
Files touched
- src/Marketplace.Api/Marketplace.Api.csproj
- src/Marketplace.Api/Program.cs
- src/Marketplace.Api/Payouts/PayoutsModule.cs
- src/Marketplace.Api/Payouts/PaystreamClient.cs
- tests/Marketplace.Api.Tests/PaystreamClientTests.cs