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:
parent
e8b70ec372
commit
4ae3b8b3d8
1 changed files with 29 additions and 22 deletions
|
|
@ -293,13 +293,34 @@
|
||||||
<span>#dollarFormat(cart_grand_total)#</span>
|
<span>#dollarFormat(cart_grand_total)#</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<!--- Tax --->
|
<!--- Calculate all fees WITHOUT rounding intermediate values (matches createPaymentIntent.cfm) --->
|
||||||
<cfif get_order_info.TaxRate GT 0>
|
<cfset tax_amount_raw = cart_grand_total * get_order_info.TaxRate>
|
||||||
<cfset tax_amount = round(cart_grand_total * get_order_info.TaxRate * 100) / 100>
|
<cfset payfrit_fee_raw = PaymentPayfritsCut>
|
||||||
<cfelse>
|
|
||||||
<cfset tax_amount = 0>
|
<!--- 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>
|
</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>
|
<cfif tax_amount GT 0>
|
||||||
<div class="total-row">
|
<div class="total-row">
|
||||||
<span>Tax</span>
|
<span>Tax</span>
|
||||||
|
|
@ -307,39 +328,25 @@
|
||||||
</div>
|
</div>
|
||||||
</cfif>
|
</cfif>
|
||||||
|
|
||||||
<!--- Payfrit Service Fee --->
|
<cfif payfrit_fee_raw GT 0>
|
||||||
<cfif PaymentPayfritsCut GT 0>
|
|
||||||
<div class="total-row">
|
<div class="total-row">
|
||||||
<span>Service fee</span>
|
<span>Service fee</span>
|
||||||
<span>#dollarFormat(PaymentPayfritsCut)#</span>
|
<span>#dollarFormat(round(payfrit_fee_raw * 100) / 100)#</span>
|
||||||
</div>
|
</div>
|
||||||
</cfif>
|
</cfif>
|
||||||
|
|
||||||
<!--- Delivery Fee --->
|
<cfif PaymentDeliveryFee GT 0>
|
||||||
<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>
|
|
||||||
<div class="total-row">
|
<div class="total-row">
|
||||||
<span>Delivery fee</span>
|
<span>Delivery fee</span>
|
||||||
<span>#dollarFormat(PaymentDeliveryFee)#</span>
|
<span>#dollarFormat(PaymentDeliveryFee)#</span>
|
||||||
</div>
|
</div>
|
||||||
</cfif>
|
</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">
|
<div class="total-row">
|
||||||
<span>Processing fee</span>
|
<span>Processing fee</span>
|
||||||
<span>#dollarFormat(cardFee)#</span>
|
<span>#dollarFormat(cardFee)#</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<cfset order_grand_total = round(totalCustomerPays * 100) / 100>
|
|
||||||
|
|
||||||
<div class="total-row grand-total">
|
<div class="total-row grand-total">
|
||||||
<span>Total</span>
|
<span>Total</span>
|
||||||
<span>#dollarFormat(order_grand_total)#</span>
|
<span>#dollarFormat(order_grand_total)#</span>
|
||||||
|
|
|
||||||
Reference in a new issue