-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Description
This issue is automatically created based on existing pull request: #40467: Fix DivisionByZeroError in creditmemo tax for zero base shipping amount
Description
The collect() method in Magento\Sales\Model\Order\Creditmemo\Total\Tax has a partial guard at line 137 that checks $orderShippingAmount > 0 before dividing at line 139, but line 140 divides by $baseOrderShippingAmount without any zero-check.
When an order has free shipping (base_shipping_amount = 0), this causes a DivisionByZeroError at line 140:
$basePart = $creditmemo->getBaseShippingAmount() / $baseOrderShippingAmount;This is inconsistent with line 109 in the same method, which correctly guards $baseOrderShippingAmount before dividing:
if ($baseOrderShippingAmount && $creditmemo->getBaseShippingAmount() !== null) {
$taxFactor = $creditmemo->getBaseShippingAmount() / $baseOrderShippingAmount;Steps to reproduce
- Place an order with free shipping (
shipping_amount = 0,base_shipping_amount = 0) - Create an invoice for the order
- Trigger credit memo creation via
CreditmemoFactory::createByOrder() DivisionByZeroErroris thrown atTax.php:140
In production, this is triggered by the Stripe webhook retry cron (stripe_webhook_events_retry) when it attempts to cancel/refund an order with free shipping that has an existing invoice.
Expected result
Credit memo is created without error. When shipping amounts are zero, the shipping tax proportion block is skipped entirely (no shipping tax to apportion).
Actual result
DivisionByZeroError: Division by zero in vendor/magento/module-sales/Model/Order/Creditmemo/Total/Tax.php:140
This error repeats on every cron cycle, blocking webhook retry processing.
Fix
Add $baseOrderShippingAmount > 0 to the existing guard clause at line 137, so both divisors ($orderShippingAmount at line 139 and $baseOrderShippingAmount at line 140) are protected.
Related
The invoice path (line 109) already has the correct guard. This fix makes the non-invoice path consistent.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status