Return part of an order line from the support screen

C# · ASP.NET Core · intermediate · modification

Support staff can now return part of an order line instead of only the whole line. The new `PartialRefund.Cents` works out the refund in cents as the returned units' share of what the customer paid for the line. `ReturnsController.Create` reads the new `quantity`, checks it against the line's quantity after the line lookup, and passes the quantity and the computed cents to the existing `RefundService`. `ReturnDtos.cs` adds `Quantity` to `ReturnRequest`. `PartialRefundTests.cs` (new) tests `PartialRefund.Cents`.

ASP.NET Core 10 Web API on .NET 10 (C# 14, nullable reference types enabled, implicit usings), controllers with `[ApiController]`, EF Core 10 with Npgsql, xUnit for tests (template global usings in the existing `Storefront.Api.Tests` project). Existing and unchanged: `Program.cs` registers `ShopDbContext`, `RefundService` (scoped), JWT bearer authentication, the `Support` policy (staff with the support role) and an exception handler that logs and answers a generic problem. One store: support staff may return lines of any order. `ShopDbContext.OrderLines` is the `OrderLine` entity: `int Id`, `int OrderId`, `string Sku`, `int Quantity` from 1 to 999, and `decimal PaidAmount`, what the customer paid for the whole line after discounts, in euros, a Postgres `numeric(10,2)` column from 0.01 to 99,999.99 (so always whole cents). Every order in the database is paid: the checkout webhook creates it only after the payment succeeds. `RefundService.RefundLineAsync(OrderLine line, int quantity, long amountCents, CancellationToken ct)` inserts a `LineReturn` row (`Id`, `OrderLineId` under a unique index, `Quantity`, `AmountCents`) and returns `null` instead when the line already has one (the unique index also stops two concurrent returns); it then asks the payment provider to refund `amountCents` on the order's payment, in integer cents. An `amountCents` of 0 is recorded without calling the provider; if the provider refuses, the service removes the row and throws `PaymentProviderException`, which the exception handler answers with 502. The back-office return dialog ships in the same release and always sends `quantity`.

Requirements

Files touched

Review this PR

C# practice