From b25198b3f5689a595da78bb582224fbf927dc3c7 Mon Sep 17 00:00:00 2001 From: Mike Date: Sun, 22 Mar 2026 21:38:17 +0000 Subject: [PATCH] fix: receipt total now uses actual payment amount from DB Instead of recalculating the grand total from line items + rates (which can drift by a penny due to floating point), use the actual PaymentFromCreditCard or PaymentPaidInCash values from the Payments table. This ensures the receipt always matches what the customer was actually charged. --- receipt/index.php | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/receipt/index.php b/receipt/index.php index 53d97dc..14bc54a 100644 --- a/receipt/index.php +++ b/receipt/index.php @@ -152,17 +152,32 @@ $isCardOrder = (float) $order['PaymentFromCreditCard'] > 0; $receiptTip = (float) ($order['TipAmount'] ?? 0); $totalBeforeCardFee = $cartGrandTotal + $taxAmountRaw + $payfritFeeRaw + $deliveryFee + $receiptTip; -if ($isCardOrder) { +// Use ACTUAL payment amounts from the database as the source of truth, +// so the receipt total always matches what was really charged. +$actualCardPaid = (float) $order['PaymentFromCreditCard']; +$actualCashPaid = (float) $order['PaymentPaidInCash']; +$taxAmount = round($taxAmountRaw * 100) / 100; + +if ($isCardOrder && $actualCardPaid > 0) { + // Grand total = what was charged to card + any balance applied + $orderGrandTotal = round(($actualCardPaid + $balanceApplied) * 100) / 100; + // Back-calculate card/processing fee as the remainder + $cardFee = $orderGrandTotal - $cartGrandTotal - $taxAmount - round($payfritFeeRaw * 100) / 100 - $deliveryFee - $receiptTip; + $cardFee = round($cardFee * 100) / 100; + if ($cardFee < 0) $cardFee = 0; +} elseif ($isCashOrder && $actualCashPaid > 0) { + $orderGrandTotal = round(($actualCashPaid + $balanceApplied) * 100) / 100; + $cardFee = 0; +} elseif ($isCardOrder) { + // Fallback: recalculate if no payment record yet $cardFeePercent = 0.029; $cardFeeFixed = 0.30; $totalCustomerPaysRaw = ($totalBeforeCardFee + $cardFeeFixed) / (1 - $cardFeePercent); $orderGrandTotal = round($totalCustomerPaysRaw * 100) / 100; - $taxAmount = round($taxAmountRaw * 100) / 100; $cardFee = $orderGrandTotal - $cartGrandTotal - $taxAmount - round($payfritFeeRaw * 100) / 100 - $deliveryFee - $receiptTip; $cardFee = round($cardFee * 100) / 100; } else { $orderGrandTotal = round($totalBeforeCardFee * 100) / 100; - $taxAmount = round($taxAmountRaw * 100) / 100; $cardFee = 0; }