Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Handle calculate checkout total zero and negative (#9047) (#9106)
Jakub27b65eae2a9a
diff --git a/saleor/checkout/base_calculations.py b/saleor/checkout/base_calculations.pyindex 7b34a2a4c7..6b316d7540 100644--- a/saleor/checkout/base_calculations.py+++ b/saleor/checkout/base_calculations.py@@ -7,7 +7,7 @@ manager.from decimal import Decimalfrom typing import TYPE_CHECKING, Iterable, Optional-from prices import TaxedMoney+from prices import Money, TaxedMoneyfrom ..core.prices import quantize_pricefrom ..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 totaldef base_checkout_line_total(diff --git a/saleor/checkout/tests/test_base_calculations.py b/saleor/checkout/tests/test_base_calculations.pyindex 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, TaxedMoneyfrom ...discount import DiscountValueType, VoucherTypefrom ...discount.utils import get_product_discount_on_salefrom ..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_moneydiff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.pyindex 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.currencydefault_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.pyindex 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, TaxedMoneyfrom ...checkout.fetch import fetch_checkout_info, fetch_checkout_linesfrom ...core.prices import quantize_price-from ...core.taxes import TaxType+from ...core.taxes import TaxType, zero_taxed_moneyfrom ...discount.utils import fetch_catalogue_infofrom ...graphql.discount.mutations import convert_catalogue_info_to_global_idsfrom ...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)