Skip to content

Fix refund total ignoring order-level custom totals (e.g. payment discounts...)#2

Open
norgeindian wants to merge 1 commit into
mageme:mainfrom
norgeindian:fix/order-level-discount-refund
Open

Fix refund total ignoring order-level custom totals (e.g. payment discounts...)#2
norgeindian wants to merge 1 commit into
mageme:mainfrom
norgeindian:fix/order-level-discount-refund

Conversation

@norgeindian
Copy link
Copy Markdown
Contributor

RefundCalculator computed the refund by summing per-item row_total minus item-level discount_amount. This missed any order-level adjustment stored outside item fields — e.g. a payment-method discount (Fooman Surcharge) that is recorded as a custom total and reduces grand_total without distributing to item discount_amount.

For such orders the calculator produced a total higher than grand_total, causing the post-condition guard to throw a LogicException and rolling back the entire withdrawal submission. The JS sidebar preview also displayed the inflated amount.

Fix: after computing the item-based total, derive the order-level gap as
gap = grand_total - (sum(row_total - discount_amount) + sum(tax) + shipping + shipping_tax)
and distribute it proportionally to the fraction of the order's item value being returned (ratio = itemsSubtotal / orderItemsBase). A full return produces ratio 1.0 and always yields grand_total; a partial return gets a fair proportional share. The gap is zero for standard orders, so there is no behavioural change for orders without custom totals.

The same computation is added to form.phtml (passed to JS as orderItemsBase + orderLevelGap) and withdrawal-summary.js so the live sidebar preview stays in sync with the server calculation.

…counts)

RefundCalculator computed the refund by summing per-item row_total minus
item-level discount_amount. This missed any order-level adjustment stored
outside item fields — e.g. a payment-method discount (Fooman Surcharge
"Vorkassenrabatt") that is recorded as a custom total and reduces
grand_total without distributing to item discount_amount.

For such orders the calculator produced a total higher than grand_total,
causing the post-condition guard to throw a LogicException and rolling
back the entire withdrawal submission. The JS sidebar preview also
displayed the inflated amount.

Fix: after computing the item-based total, derive the order-level gap as
    gap = grand_total - (sum(row_total - discount_amount) + sum(tax) + shipping + shipping_tax)
and distribute it proportionally to the fraction of the order's item value
being returned (ratio = itemsSubtotal / orderItemsBase). A full return
produces ratio 1.0 and always yields grand_total; a partial return gets a
fair proportional share. The gap is zero for standard orders, so there is
no behavioural change for orders without custom totals.

The same computation is added to form.phtml (passed to JS as
orderItemsBase + orderLevelGap) and withdrawal-summary.js so the live
sidebar preview stays in sync with the server calculation.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant