Skip to content

[Issue] Fix DivisionByZeroError in creditmemo tax for zero base shipping amount #40492

@m2-assistant

Description

@m2-assistant

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

  1. Place an order with free shipping (shipping_amount = 0, base_shipping_amount = 0)
  2. Create an invoice for the order
  3. Trigger credit memo creation via CreditmemoFactory::createByOrder()
  4. DivisionByZeroError is thrown at Tax.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

No one assigned

    Labels

    Area: TaxComponent: SalesIssue: ConfirmedGate 3 Passed. Manual verification of the issue completed. Issue is confirmedPriority: P2A defect with this priority could have functionality issues which are not to expectations.Reproduced on 2.4.xThe issue has been reproduced on latest 2.4-develop branch

    Type

    No type

    Projects

    Status

    Ready for Development

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions