Skip to content

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

Fix failed request handler in AvataxPlugin (#19123)

Maciej Korycinskie8733aee630a
diff --git a/saleor/plugins/avatax/__init__.py b/saleor/plugins/avatax/__init__.py
index 7f8db05304..5657b43b95 100644
--- a/saleor/plugins/avatax/__init__.py
+++ b/saleor/plugins/avatax/__init__.py
@@ -654,7 +654,7 @@ def get_checkout_tax_data(
data, str(checkout_info.checkout.token), config
)
- if response is None:
+ if not response:
return response
convert_response_lines_list_to_dict(response, iter_checkout_lines(lines_info))
diff --git a/saleor/plugins/avatax/plugin.py b/saleor/plugins/avatax/plugin.py
index e861e60793..a2ca37dd5c 100644
--- a/saleor/plugins/avatax/plugin.py
+++ b/saleor/plugins/avatax/plugin.py
@@ -301,7 +301,7 @@ class DeprecatedAvataxPlugin(BasePlugin):
if response is None:
return previous_value
- currency = str(response.get("currencyCode"))
+ currency = response.get("currencyCode") or checkout_info.checkout.currency
return self._calculate_checkout_shipping(
checkout_info, currency, response.get("lines", {}), base_shipping_price.net
)
@@ -502,7 +502,9 @@ class DeprecatedAvataxPlugin(BasePlugin):
if not taxes_data or "error" in taxes_data:
return base_value
- currency = taxes_data.get("currencyCode")
+ currency = (
+ taxes_data.get("currencyCode") or base_value.price_with_discounts.currency
+ )
line_price_with_discounts = None
line = taxes_data.get("lines", {}).get(order_line_id)
@@ -601,7 +603,7 @@ class DeprecatedAvataxPlugin(BasePlugin):
def _calculate_order_shipping(self, order, taxes_data) -> TaxedMoney:
prices_entered_with_tax = partial(_get_prices_entered_with_tax_for_order, order)
- currency = taxes_data.get("currencyCode")
+ currency = taxes_data.get("currencyCode") or order.currency
line = taxes_data.get("lines", {}).get(SHIPPING_ITEM_CODE)
if line:
tax = Decimal(line.get("tax", 0.0))
diff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.py
index 30e03dc5fb..bd318a46d1 100644
--- a/saleor/plugins/avatax/tests/test_avatax.py
+++ b/saleor/plugins/avatax/tests/test_avatax.py
@@ -2127,6 +2127,45 @@ def test_calculate_checkout_shipping(
)
+@patch("saleor.plugins.avatax.get_cached_response_or_fetch")
+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.DeprecatedAvataxPlugin"])
+def test_calculate_checkout_shipping_when_failed_to_deliver_request(
+ mocked_get_cached_response_or_fetch,
+ checkout_with_item_on_promotion,
+ checkout_delivery,
+ address,
+ ship_to_pl_address,
+ site_settings,
+ monkeypatch,
+ plugin_configuration,
+):
+ # given
+ plugin_configuration()
+ monkeypatch.setattr(
+ "saleor.plugins.avatax.plugin.get_cached_tax_codes_or_fetch",
+ lambda _: {"PC040156": "desc"},
+ )
+ manager = get_plugins_manager(allow_replica=False)
+ site_settings.company_address = address
+ site_settings.save()
+
+ mocked_get_cached_response_or_fetch.return_value = {}
+
+ checkout = checkout_with_item_on_promotion
+ checkout.shipping_address = ship_to_pl_address
+ checkout.assigned_delivery = checkout_delivery(checkout)
+ checkout.save()
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+
+ # when
+ shipping_price = manager.calculate_checkout_shipping(checkout_info, lines, address)
+
+ # then
+ shipping_price = quantize_price(shipping_price, shipping_price.currency)
+ assert shipping_price == TaxedMoney(net=Money(10, "USD"), gross=Money(10, "USD"))
+
+
@pytest.mark.vcr
@pytest.mark.parametrize(
("expected_net", "expected_gross", "prices_entered_with_tax"),