Checkout total in the shopper's currency
C# · ASP.NET Core · intermediate · modification
The checkout page can now show the order total in the shopper's currency next to the euro total. `GET /api/checkout/summary` accepts an optional `currency` and, for one of the six supported currencies, adds a `display` block with the converted total and the FX provider's quote id, which payment uses later. The provider call lives in the new `Fx/FxQuoteClient.cs` behind `IFxQuoteClient`: it posts the euro total to `/v1/quotes` with the API key and a 3-second timeout, and turns error statuses, timeouts, network failures and unreadable bodies into `FxUnavailableException`, which `CheckoutEndpoints.cs` logs before falling back to the euro-only summary. `Program.cs` binds the `Fx` options and registers the client. `CheckoutSummaryCurrencyTests.cs` covers the endpoint with a fake quote client.
ASP.NET Core 10 minimal API on .NET 10 (C# 14, nullable reference types enabled, implicit usings), xUnit for tests. Existing and unchanged: `CheckoutSummaryService.ForUserAsync(userId, ct)` returns the shopper's open cart as `CheckoutSummary(SubtotalCents, ShippingCents, TotalCents)` (`long` euro cents, the total from 0 to 10,000,000) or `null` when there is no open cart; it prices shipping through `ShippingRatesClient`, the typed client registered in `Program.cs`. `User.GetUserId()` (`Shop.Api.Auth`) reads the signed-in shopper's id. Configuration always sets `Fx:BaseUrl` (the provider's origin, `https://api.fxquotes.example`) and `Fx:ApiKey` (from the secret store). The FX provider is an HTTPS API at about a 20 ms round trip (TLS 1.3); a successful `POST /v1/quotes` answers 201 with `{ "quoteId", "to", "amountMinor", "expiresAt" }`, where `to` echoes the request, `amountMinor` is a non-negative integer and `expiresAt` an ISO 8601 timestamp; failures answer 4xx or 5xx. The API runs as three Linux containers (Kestrel) behind a load balancer and calls the provider directly, with no proxy. JSON uses the ASP.NET Core web defaults (camelCase names). Tests use the existing `ShopApiFactory` (`WebApplicationFactory<Program>` with a test authentication scheme: a request with an `X-Test-User` header is signed in as that user; `u-1` has an open cart of 3,650 cents plus 500 cents shipping). The checkout page change ships separately.
Requirements
- `GET /api/checkout/summary` (existing) takes an optional `currency` query parameter so the checkout page can show the order total in the shopper's currency next to the euro total. For `USD`, `GBP`, `CHF`, `SEK`, `DKK` or `PLN` (uppercase ISO 4217 codes) the response gains `display: { currency, amountMinor, quoteId, expiresAt }`, the order total converted by the FX provider, in that currency's minor units. Without the parameter, or with `EUR`, `display` is `null` and no quote is requested. Any other value, an empty one included, answers 400 `{ "error": "unsupported currency" }`.
- Each summary with a display currency gets its own quote from the FX provider: `POST /v1/quotes` with `{ "from": "EUR", "to", "amountMinor" }` (the order total in euro cents) and the API key as a bearer token. Payment later charges the quoted amount by `quoteId`, so quotes are never cached or shared between shoppers.
- If the provider answers an error status, sends a success body that cannot be read as a quote (not JSON, or JSON `null`), cannot be reached or does not answer within 3 seconds, the summary still answers 200 in euros with `display: null`, and the failure is logged as a warning. A request the shopper aborts is not reported as a provider failure.
- The quote is on the checkout page's critical path. At the evening peak one API instance requests up to 150 quotes per second. The provider keeps an idle connection open for 90 seconds, and outbound calls in this API reuse open connections across requests, as the shipping rates client does.
- Unchanged: the endpoint requires a signed-in shopper (401 otherwise) and answers 404 when the shopper has no open cart; the euro amounts and `currency: "EUR"` keep their meaning.
Files touched
- src/Shop.Api/Program.cs
- src/Shop.Api/Checkout/CheckoutEndpoints.cs
- src/Shop.Api/Fx/FxQuoteClient.cs
- tests/Shop.Api.Tests/CheckoutSummaryCurrencyTests.cs