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; }