Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Handle calculate checkout total zero and negative (#9047) (#9106)

Jakub27b65eae2a9a
diff --git a/saleor/checkout/base_calculations.py b/saleor/checkout/base_calculations.py
index 7b34a2a4c7..6b316d7540 100644
--- a/saleor/checkout/base_calculations.py
+++ b/saleor/checkout/base_calculations.py
@@ -7,7 +7,7 @@ manager.
from decimal import Decimal
from typing import TYPE_CHECKING, Iterable, Optional
-from prices import TaxedMoney
+from prices import Money, TaxedMoney
from ..core.prices import quantize_price
from ..core.taxes import zero_money, zero_taxed_money
@@ -182,12 +182,19 @@ def calculate_base_price_for_shipping_method(
def base_checkout_total(
subtotal: TaxedMoney,
shipping_price: TaxedMoney,
- discount: TaxedMoney,
+ discount: Money,
currency: str,
) -> TaxedMoney:
"""Return the total cost of the checkout."""
+ zero = zero_taxed_money(currency)
total = subtotal + shipping_price - discount
- return max(total, zero_taxed_money(currency))
+ # Discount is subtracted from both gross and net values, which may cause negative
+ # net value if we are having a discount that covers whole price.
+ # Comparing TaxedMoney objects works only on gross values. That is why we are
+ # explicitly returning zero_taxed_money if total.gross is less or equal zero.
+ if total.gross <= zero.gross:
+ return zero
+ return total
def base_checkout_line_total(
diff --git a/saleor/checkout/tests/test_base_calculations.py b/saleor/checkout/tests/test_base_calculations.py
index 66d54553d7..cf3dd3c015 100644
--- a/saleor/checkout/tests/test_base_calculations.py
+++ b/saleor/checkout/tests/test_base_calculations.py
@@ -5,6 +5,7 @@ from prices import Money, TaxedMoney
from ...discount import DiscountValueType, VoucherType
from ...discount.utils import get_product_discount_on_sale
from ..base_calculations import (
+ base_checkout_total,
base_tax_rate,
calculate_base_line_total_price,
calculate_base_line_unit_price,
@@ -612,3 +613,34 @@ def test_base_tax_rate_net_price_zero():
def test_base_tax_rate_gross_price_zero():
price = TaxedMoney(net=Money(3, "USD"), gross=Money(0, "USD"))
assert base_tax_rate(price) == Decimal("0.0")
+
+
+def test_base_checkout_total():
+ # given
+ currency = "USD"
+ taxed_money = TaxedMoney(net=Money(10, currency), gross=Money(10, currency))
+ subtotal = taxed_money
+ shipping_price = taxed_money
+ discount = Money(5, currency)
+
+ # when
+ total = base_checkout_total(subtotal, shipping_price, discount, currency)
+ expected = subtotal + shipping_price - discount
+
+ # then
+ assert total == expected
+
+
+def test_base_checkout_total_high_discount():
+ # given
+ currency = "USD"
+ zero_taxed_money = TaxedMoney(net=Money(0, currency), gross=Money(0, currency))
+ subtotal = TaxedMoney(net=Money(10, currency), gross=Money(12, currency))
+ shipping_price = zero_taxed_money
+ discount = Money(20, currency)
+
+ # when
+ total = base_checkout_total(subtotal, shipping_price, discount, currency)
+
+ # then
+ assert total == zero_taxed_money
diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.py
index 4e96997389..3d190fc544 100644
--- a/saleor/plugins/manager.py
+++ b/saleor/plugins/manager.py
@@ -203,6 +203,7 @@ class PluginsManager(PaymentInterface):
address: Optional["Address"],
discounts: Iterable[DiscountInfo],
) -> TaxedMoney:
+ currency = checkout_info.checkout.currency
default_value = base_calculations.base_checkout_total(
subtotal=self.calculate_checkout_subtotal(
checkout_info, lines, address, discounts
@@ -211,8 +212,15 @@ class PluginsManager(PaymentInterface):
checkout_info, lines, address, discounts
),
discount=checkout_info.checkout.discount,
- currency=checkout_info.checkout.currency,
+ currency=currency,
)
+
+ if default_value <= zero_taxed_money(currency):
+ return quantize_price(
+ default_value,
+ currency,
+ )
+
return quantize_price(
self.__run_method_on_plugins(
"calculate_checkout_total",
@@ -223,7 +231,7 @@ class PluginsManager(PaymentInterface):
discounts,
channel_slug=checkout_info.channel.slug,
),
- checkout_info.checkout.currency,
+ currency,
)
def calculate_checkout_subtotal(
diff --git a/saleor/plugins/tests/test_manager.py b/saleor/plugins/tests/test_manager.py
index fba8ae1c54..08da567291 100644
--- a/saleor/plugins/tests/test_manager.py
+++ b/saleor/plugins/tests/test_manager.py
@@ -10,7 +10,7 @@ from prices import Money, TaxedMoney
from ...checkout.fetch import fetch_checkout_info, fetch_checkout_lines
from ...core.prices import quantize_price
-from ...core.taxes import TaxType
+from ...core.taxes import TaxType, zero_taxed_money
from ...discount.utils import fetch_catalogue_info
from ...graphql.discount.mutations import convert_catalogue_info_to_global_ids
from ...payment.interface import PaymentGateway
@@ -1066,3 +1066,33 @@ def test_manager_delivery_retry(event_delivery):
manager = PluginsManager(plugins=plugins)
delivery_retry = manager.event_delivery_retry(event_delivery=event_delivery)
assert delivery_retry
+
+
+ "saleor.plugins.manager.PluginsManager._PluginsManager__run_method_on_single_plugin"
+)
[email protected]("saleor.plugins.manager.base_calculations.base_checkout_total")
+def test_calculate_checkout_total_zero_default_value(
+ mocked_base_checkout_total,
+ mocked_run_method,
+ checkout_with_item,
+ discount_info,
+):
+ # given
+ plugins = ["saleor.plugins.tests.sample_plugins.PluginSample"]
+ currency = checkout_with_item.currency
+ mocked_base_checkout_total.return_value = zero_taxed_money(currency)
+ manager = PluginsManager(plugins=plugins)
+ lines, _ = fetch_checkout_lines(checkout_with_item)
+ checkout_info = fetch_checkout_info(
+ checkout_with_item, lines, [discount_info], manager
+ )
+
+ # when
+ taxed_total = manager.calculate_checkout_total(
+ checkout_info, lines, None, [discount_info]
+ )
+
+ # then
+ assert "calculate_checkout_total" not in mocked_run_method.call_args_list
+ assert taxed_total == zero_taxed_money(currency)