Fix point of sale inventory undercount on multi-unit orders
What changed, and why it matters
This commit fixes a bug in BTCPay Server's point-of-sale inventory tracking. When a customer bought multiple units of an item in a single order, the system could mistakenly count the purchase as only one unit sold, causing the inventory count to stay too high. The fix ensures the full shopping cart is used to update inventory instead of a single-item fallback taking over.
Review whether any deployed merchants rely on accurate POS inventory limits; consider whether the bug could have caused overselling and whether refunds or stock corrections are needed. Apply the patch and verify multi-unit orders now deduct the correct quantity.
Security signals we found
Inventory undercount / oversell risk from incorrect quantity handling
Logic branch ordering bug causing fallback to override richer data source
Point-of-sale inventory integrity issue
Evidence from the diff
In AppInventoryUpdaterHostedService.cs, the original logic first checked whether invoiceEvent.Invoice.Metadata.ItemCode was non-empty, and only parsed the PosData cart if ItemCode was empty. Because a single-line cart also sets ItemCode, the single-item branch ran and added only one AppCartItem, ignoring the cart’s actual quantity. The patch reverses the order so the PosData cart is parsed first, and the single-item fallback only runs when the cart is empty. This corrects inventory deduction for multi-unit orders.
Changed components
BTCPayServer/HostedServices/AppInventoryUpdaterHostedService.csPoint of sale inventory updaterAppCartItem inventory deductionInspect captured patch +4 / −3
### BTCPayServer/HostedServices/AppInventoryUpdaterHostedService.cs
@@ -44,13 +44,14 @@ protected override async Task ProcessEvent(object evt, CancellationToken cancell
return;
}
- if (!string.IsNullOrEmpty(invoiceEvent.Invoice.Metadata.ItemCode) ||
- AppService.TryParsePosCartItems(invoiceEvent.Invoice.Metadata.PosData, out cartItems))
+ // A single line cart also sets ItemCode, so the cart must win or only one unit is deducted
+ if (AppService.TryParsePosCartItems(invoiceEvent.Invoice.Metadata.PosData, out cartItems) ||
+ !string.IsNullOrEmpty(invoiceEvent.Invoice.Metadata.ItemCode))
{
var appIds = AppService.GetAppInternalTags(invoiceEvent.Invoice);
var items = cartItems?.ToList() ?? new List<AppCartItem>();
- if (!string.IsNullOrEmpty(invoiceEvent.Invoice.Metadata.ItemCode))
+ if (items.Count == 0 && !string.IsNullOrEmpty(invoiceEvent.Invoice.Metadata.ItemCode))
{
items.Add(new AppCartItem
{Why this scored 49/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.