Skip to content

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

Fix error when discount_amount is 0 for taxes webhook payload

Ania Warzechae65000bc56ec
diff --git a/saleor/core/prices.py b/saleor/core/prices.py
index b8be0d03d5..08d7c83aa1 100644
--- a/saleor/core/prices.py
+++ b/saleor/core/prices.py
@@ -14,6 +14,8 @@ PriceType = TypeVar("PriceType", TaxedMoney, Money, Decimal, TaxedMoneyRange)
def quantize_price(price: PriceType, currency: str) -> PriceType:
precision = get_currency_precision(currency)
number_places = Decimal(10) ** -precision
+ if not price:
+ return Decimal(0) # type: ignore[return-value]
return price.quantize(number_places)
diff --git a/saleor/core/tests/test_prices.py b/saleor/core/tests/test_prices.py
new file mode 100644
index 0000000000..873204c4af
--- /dev/null
+++ b/saleor/core/tests/test_prices.py
@@ -0,0 +1,14 @@
+from decimal import Decimal
+
+import pytest
+
+from ..prices import quantize_price
+
+
+ ("price", "currency", "expected_value"),
+ [(0, "USD", 0), (Decimal(5), "USD", Decimal(5.00))],
+)
+def test_quantize_price(price, currency, expected_value):
+ price = quantize_price(price, currency)
+ assert price == expected_value
diff --git a/saleor/webhook/tests/test_webhook_payloads.py b/saleor/webhook/tests/test_webhook_payloads.py
index a10ef884fa..dd73ec2743 100644
--- a/saleor/webhook/tests/test_webhook_payloads.py
+++ b/saleor/webhook/tests/test_webhook_payloads.py
@@ -2191,6 +2191,104 @@ def test_generate_checkout_payload_for_tax_calculation_digital_checkout(
)
+@patch("saleor.webhook.payloads.serialize_checkout_lines_for_tax_calculation")
[email protected]("prices_entered_with_tax", [True, False])
+def test_generate_checkout_payload_for_tax_calculation_no_discount(
+ mocked_serialize_checkout_lines_for_tax_calculation,
+ mocked_fetch_checkout,
+ checkout_with_prices,
+ prices_entered_with_tax,
+ order_promotion_rule,
+):
+ checkout = checkout_with_prices
+ rule = order_promotion_rule
+ currency = checkout.currency
+
+ discount_amount = 0
+ checkout.discount_amount = discount_amount
+ checkout.discount_name = rule.name
+ checkout.save(update_fields=["discount_amount", "discount_name"])
+
+ tax_configuration = checkout.channel.tax_configuration
+ tax_configuration.prices_entered_with_tax = prices_entered_with_tax
+ tax_configuration.save(update_fields=["prices_entered_with_tax"])
+ tax_configuration.country_exceptions.all().delete()
+
+ mocked_serialized_checkout_lines = {"data": "checkout_lines_data"}
+ mocked_serialize_checkout_lines_for_tax_calculation.return_value = (
+ mocked_serialized_checkout_lines
+ )
+
+ # when
+ lines, _ = fetch_checkout_lines(checkout_with_prices)
+ manager = get_plugins_manager(allow_replica=False)
+ checkout_info = fetch_checkout_info(checkout_with_prices, lines, manager)
+ payload = json.loads(
+ generate_checkout_payload_for_tax_calculation(checkout_info, lines)
+ )[0]
+ address = checkout.shipping_address
+
+ # then
+ subtotal_price = Money(0, currency)
+ for line_info in lines:
+ variant = line_info.variant
+ variant_listing = line_info.channel_listing
+ unit_price = variant.get_price(variant_listing)
+ subtotal_price += unit_price * line_info.line.quantity
+ shipping_price = quantize_price(
+ checkout.shipping_method.channel_listings.get(
+ channel_id=checkout.channel_id
+ ).price.amount,
+ currency,
+ )
+ total_price_amount = subtotal_price.amount + shipping_price
+ assert payload == {
+ "type": "Checkout",
+ "id": graphene.Node.to_global_id("Checkout", checkout.pk),
+ "address": {
+ "type": "Address",
+ "id": graphene.Node.to_global_id("Address", address.pk),
+ "first_name": address.first_name,
+ "last_name": address.last_name,
+ "company_name": address.company_name,
+ "street_address_1": address.street_address_1,
+ "street_address_2": address.street_address_2,
+ "city": address.city,
+ "city_area": address.city_area,
+ "postal_code": address.postal_code,
+ "country": address.country.code,
+ "country_area": address.country_area,
+ "phone": str(address.phone),
+ },
+ "channel": {
+ "type": "Channel",
+ "id": graphene.Node.to_global_id("Channel", checkout.channel_id),
+ "currency_code": checkout.channel.currency_code,
+ "slug": checkout.channel.slug,
+ },
+ "currency": currency,
+ "discounts": [],
+ "included_taxes_in_prices": prices_entered_with_tax,
+ "lines": mocked_serialized_checkout_lines,
+ "metadata": {"meta_key": "meta_value"},
+ "shipping_name": checkout.shipping_method.name,
+ "user_id": graphene.Node.to_global_id("User", checkout.user.pk),
+ "user_public_metadata": {"user_public_meta_key": "user_public_meta_value"},
+ "total_amount": str(
+ quantize_price(
+ total_price_amount,
+ currency,
+ )
+ ),
+ "shipping_amount": str(shipping_price),
+ }
+ mocked_fetch_checkout.assert_not_called()
+ mocked_serialize_checkout_lines_for_tax_calculation.assert_called_once_with(
+ checkout_info,
+ lines,
+ )
+
+
@freeze_time()
@pytest.mark.parametrize("prices_entered_with_tax", [True, False])
def test_generate_checkout_payload(