Fix receipt rounding to match Stripe charge exactly

Calculate total first without intermediate rounding, then work backwards for display values.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
John Mizerek 2026-02-15 17:11:00 -08:00
parent e8b70ec372
commit 4ae3b8b3d8

View file

@ -293,13 +293,34 @@
<span>#dollarFormat(cart_grand_total)#</span>
</div>
<!--- Tax --->
<cfif get_order_info.TaxRate GT 0>
<cfset tax_amount = round(cart_grand_total * get_order_info.TaxRate * 100) / 100>
<cfelse>
<cfset tax_amount = 0>
<!--- Calculate all fees WITHOUT rounding intermediate values (matches createPaymentIntent.cfm) --->
<cfset tax_amount_raw = cart_grand_total * get_order_info.TaxRate>
<cfset payfrit_fee_raw = PaymentPayfritsCut>
<!--- Delivery Fee --->
<cfset PaymentDeliveryFee = 0>
<cfif get_order_info.OrderTypeID EQ 3>
<cfmodule template="../modules/get_delivery_fee.cfm" OrderID="#get_order_info.ID#">
<cfset PaymentDeliveryFee = calculated_delivery_fee>
</cfif>
<!--- Calculate total BEFORE card fee (no rounding) --->
<cfset totalBeforeCardFee = cart_grand_total + tax_amount_raw + payfrit_fee_raw + PaymentDeliveryFee>
<!--- Calculate final total with Stripe fees: (amount + $0.30) / (1 - 2.9%) --->
<cfset cardFeePercent = 0.029>
<cfset cardFeeFixed = 0.30>
<cfset totalCustomerPays_raw = (totalBeforeCardFee + cardFeeFixed) / (1 - cardFeePercent)>
<!--- Round ONLY the final total to cents (this is what Stripe charges) --->
<cfset order_grand_total = round(totalCustomerPays_raw * 100) / 100>
<!--- Work backwards to get displayed values that add up correctly --->
<cfset tax_amount = round(tax_amount_raw * 100) / 100>
<cfset cardFee = order_grand_total - cart_grand_total - tax_amount - round(payfrit_fee_raw * 100) / 100 - PaymentDeliveryFee>
<cfset cardFee = round(cardFee * 100) / 100>
<!--- Display fees --->
<cfif tax_amount GT 0>
<div class="total-row">
<span>Tax</span>
@ -307,39 +328,25 @@
</div>
</cfif>
<!--- Payfrit Service Fee --->
<cfif PaymentPayfritsCut GT 0>
<cfif payfrit_fee_raw GT 0>
<div class="total-row">
<span>Service fee</span>
<span>#dollarFormat(PaymentPayfritsCut)#</span>
<span>#dollarFormat(round(payfrit_fee_raw * 100) / 100)#</span>
</div>
</cfif>
<!--- Delivery Fee --->
<cfset PaymentDeliveryFee = 0>
<cfif get_order_info.OrderTypeID EQ 3>
<cfmodule template="../modules/get_delivery_fee.cfm" OrderID="#get_order_info.ID#">
<cfset PaymentDeliveryFee = calculated_delivery_fee>
<cfif PaymentDeliveryFee GT 0>
<div class="total-row">
<span>Delivery fee</span>
<span>#dollarFormat(PaymentDeliveryFee)#</span>
</div>
</cfif>
<!--- Calculate Processing Fee (Stripe 2.9% + $0.30) --->
<cfset totalBeforeCardFee = cart_grand_total + tax_amount + PaymentPayfritsCut + PaymentDeliveryFee>
<cfset cardFeePercent = 0.029>
<cfset cardFeeFixed = 0.30>
<cfset totalCustomerPays = (totalBeforeCardFee + cardFeeFixed) / (1 - cardFeePercent)>
<cfset cardFee = round((totalCustomerPays - totalBeforeCardFee) * 100) / 100>
<div class="total-row">
<span>Processing fee</span>
<span>#dollarFormat(cardFee)#</span>
</div>
<cfset order_grand_total = round(totalCustomerPays * 100) / 100>
<div class="total-row grand-total">
<span>Total</span>
<span>#dollarFormat(order_grand_total)#</span>