Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Do not append the shipping data when price is zero and do not send the avatax request when there is no lines (#10601)
Iga Karbowiak94dbe9e96cd9
diff --git a/saleor/plugins/avatax/__init__.py b/saleor/plugins/avatax/__init__.pyindex f4c9546886..1174eac3b5 100644--- a/saleor/plugins/avatax/__init__.py+++ b/saleor/plugins/avatax/__init__.py@@ -418,7 +418,7 @@ def get_order_lines_data().exists()append_shipping_to_data(data,- shipping_price,+ shipping_price if shipping_price else None,config.shipping_tax_code,tax_included,shipping_discounted,@@ -484,6 +484,8 @@ def generate_request_data_from_checkout(lines = get_checkout_lines_data(checkout_info, lines_info, config, tax_included, discounts)+ if not lines:+ return {}voucher = checkout_info.voucher# for apply_once_per_order vouchers the discount is already applied on linesdiscount_amount = (@@ -540,6 +542,9 @@ def get_cached_response_or_fetch(Return cached response if requests data are the same. Fetch new data in other cases."""+ # if the data is empty it means there is nothing to send to avalara+ if not data:+ return Nonedata_cache_key = CACHE_KEY + token_in_cachecached_data = cache.get(data_cache_key)if taxes_need_new_fetch(data, cached_data) or force_refresh:@@ -576,6 +581,9 @@ def get_order_request_data(lines = get_order_lines_data(order, config, tax_included, discounted=discounted_lines)+ # if there is no lines to sent we do not want to send the request to avalara+ if not lines:+ return {}data = generate_request_data(transaction_type=transaction,lines=lines,@@ -596,9 +604,8 @@ def get_order_tax_data(response = get_cached_response_or_fetch(data, "order_%s" % order.id, config, force_refresh)- error = response.get("error")- if error:- raise TaxError(error)+ if response and "error" in response:+ raise TaxError(response.get("error"))return responsediff --git a/saleor/plugins/avatax/plugin.py b/saleor/plugins/avatax/plugin.pyindex 0b7881fdc7..a673e7beca 100644--- a/saleor/plugins/avatax/plugin.py+++ b/saleor/plugins/avatax/plugin.py@@ -203,17 +203,11 @@ class AvataxPlugin(BasePlugin):discounts: Iterable[DiscountInfo],previous_value: TaxedMoney,) -> TaxedMoney:- if self._skip_plugin(previous_value):- return previous_value- checkout_total = previous_value-- if not _validate_checkout(checkout_info, lines):- return checkout_total- response = get_checkout_tax_data(- checkout_info, lines, self.config.tax_included, discounts, self.config+ response = self._get_checkout_tax_data(+ checkout_info, lines, discounts, previous_value)- if not response or "error" in response:- return checkout_total+ if response is None:+ return previous_valuecurrency = checkout_info.checkout.currencytaxed_total = zero_taxed_money(currency)@@ -289,17 +283,11 @@ class AvataxPlugin(BasePlugin):if not charge_taxes_on_shipping():return base_shipping_price- if self._skip_plugin(previous_value):- return base_shipping_price-- if not _validate_checkout(checkout_info, lines):- return base_shipping_price-- response = get_checkout_tax_data(- checkout_info, lines, self.config.tax_included, discounts, self.config+ response = self._get_checkout_tax_data(+ checkout_info, lines, discounts, base_shipping_price)- if not response or "error" in response:- return base_shipping_price+ if response is None:+ return previous_valuecurrency = str(response.get("currencyCode"))return self._calculate_checkout_shipping(@@ -369,6 +357,8 @@ class AvataxPlugin(BasePlugin):request_data = get_order_request_data(order, self.config, self.config.tax_included)+ if not request_data:+ return previous_valuetransaction_url = urljoin(get_api_url(self.config.use_sandbox), "transactions/createoradjust"@@ -390,18 +380,12 @@ class AvataxPlugin(BasePlugin):discounts: Iterable["DiscountInfo"],previous_value: TaxedMoney,) -> TaxedMoney:- if self._skip_plugin(previous_value):- return previous_value-base_total = previous_valueif not checkout_line_info.product.charge_taxes:return base_total- if not _validate_checkout(checkout_info, lines):- return base_total-- taxes_data = get_checkout_tax_data(- checkout_info, lines, self.config.tax_included, discounts, self.config+ taxes_data = self._get_checkout_tax_data(+ checkout_info, lines, discounts, base_total)variant = checkout_line_info.variant@@ -453,15 +437,8 @@ class AvataxPlugin(BasePlugin):product: "Product",previous_value: OrderTaxedPricesData,) -> OrderTaxedPricesData:- if self._skip_plugin(previous_value):- return previous_value-if not product.charge_taxes:return previous_value-- if not _validate_order(order):- return previous_value-taxes_data = self._get_order_tax_data(order, previous_value)return self._calculate_order_line_total_price(taxes_data,@@ -523,22 +500,16 @@ class AvataxPlugin(BasePlugin):discounts: Iterable["DiscountInfo"],previous_value: TaxedMoney,) -> TaxedMoney:- if self._skip_plugin(previous_value):- return previous_value-base_total = previous_valueif not checkout_line_info.product.charge_taxes:return base_total- if not _validate_checkout(checkout_info, lines):- return base_total-+ taxes_data = self._get_checkout_tax_data(+ checkout_info, lines, discounts, base_total+ )variant = checkout_line_info.variantquantity = checkout_line_info.line.quantity- taxes_data = get_checkout_tax_data(- checkout_info, lines, self.config.tax_included, discounts, self.config- )default_total = previous_value * quantitytaxed_total_price = self._calculate_checkout_line_total_price(taxes_data,@@ -580,6 +551,9 @@ class AvataxPlugin(BasePlugin):def _calculate_order_shipping(self, order, taxes_data, previous_value) -> TaxedMoney:+ if taxes_data is None:+ return previous_value+currency = taxes_data.get("currencyCode")for line in taxes_data.get("lines", []):if line["itemCode"] == "Shipping":@@ -610,19 +584,9 @@ class AvataxPlugin(BasePlugin):def calculate_order_shipping(self, order: "Order", previous_value: TaxedMoney) -> TaxedMoney:- if self._skip_plugin(previous_value):- return previous_value-if not charge_taxes_on_shipping():return previous_value-- if not _validate_order(order):- return previous_value-- taxes_data = get_order_tax_data(- order, self.config, self.config.tax_included, False- )-+ taxes_data = self._get_order_tax_data(order, previous_value)return self._calculate_order_shipping(order, taxes_data, previous_value)def calculate_order_total(@@ -631,16 +595,7 @@ class AvataxPlugin(BasePlugin):lines: Iterable["OrderLine"],previous_value: TaxedMoney,) -> TaxedMoney:- if self._skip_plugin(previous_value):- return previous_value- order_total = previous_value-- if not _validate_order(order):- return order_total-- taxes_data = get_order_tax_data(- order, self.config, self.config.tax_included, False- )+ taxes_data = self._get_order_tax_data(order, previous_value)currency = order.currencytaxed_subtotal = zero_taxed_money(currency)diff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.pyindex 241b924fc5..3604ae908f 100644--- a/saleor/plugins/avatax/tests/test_avatax.py+++ b/saleor/plugins/avatax/tests/test_avatax.py@@ -3372,7 +3372,9 @@ def test_show_taxes_on_storefront(plugin_configuration):@patch("saleor.plugins.avatax.plugin.api_post_request_task.delay")@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])-def test_order_created(api_post_request_task_mock, order, plugin_configuration):+def test_order_created(+ api_post_request_task_mock, order, order_line, plugin_configuration+):# givenplugin_conf = plugin_configuration(from_street_address="Tęczowa 7",@@ -3394,7 +3396,17 @@ def test_order_created(api_post_request_task_mock, order, plugin_configuration):"createTransactionModel": {"companyCode": conf["Company name"],"type": TransactionType.INVOICE,- "lines": [],+ "lines": [+ {+ "amount": str(round(order_line.total_price.gross.amount, 3)),+ "description": order_line.variant.product.name,+ "discounted": False,+ "itemCode": order_line.variant.sku,+ "quantity": order_line.quantity,+ "taxCode": DEFAULT_TAX_CODE,+ "taxIncluded": True,+ }+ ],"code": str(order.id),"date": datetime.date.today().strftime("%Y-%m-%d"),"customerCode": 0,@@ -3447,6 +3459,22 @@ def test_order_created(api_post_request_task_mock, order, plugin_configuration):)+@patch("saleor.plugins.avatax.plugin.api_post_request_task.delay")+@override_settings(PLUGINS=["saleor.plugins.avatax.plugin.AvataxPlugin"])+def test_order_created_no_lines(+ api_post_request_task_mock, order, plugin_configuration+):+ """Ensure that when order has no lines, the request to avatax api is not sent."""+ # given+ manager = get_plugins_manager()++ # when+ manager.order_created(order)++ # then+ api_post_request_task_mock.assert_not_called()++@pytest.mark.vcrdef test_plugin_uses_configuration_from_db(plugin_configuration,@@ -3874,13 +3902,10 @@ def test_get_order_request_data_draft_order_with_shipping_voucher(# thenlines_data = request_data["createTransactionModel"]["lines"]- # lines + shipping- assert len(lines_data) == order_with_lines.lines.count() + 1- for line_data in lines_data[:-1]:+ # only lines, shipping is not added as after discount shipping price is 0+ assert len(lines_data) == order_with_lines.lines.count()+ for line_data in lines_data:assert line_data["discounted"] is False- # shipping line shouldn't be discounted- assert lines_data[-1]["discounted"] is False- assert Decimal(lines_data[-1]["amount"]) == Decimal("0")def test_get_order_request_data_draft_order_shipping_voucher_amount_too_high(@@ -3939,13 +3964,10 @@ def test_get_order_request_data_draft_order_shipping_voucher_amount_too_high(# thenlines_data = request_data["createTransactionModel"]["lines"]- # lines + shipping- assert len(lines_data) == order_with_lines.lines.count() + 1+ # only lines, shipping is not added as after discount shipping price is 0+ assert len(lines_data) == order_with_lines.lines.count()for line_data in lines_data[:-1]:assert line_data["discounted"] is False- # shipping line shouldn't be discounted- assert lines_data[-1]["discounted"] is False- assert Decimal(lines_data[-1]["amount"]) == Decimal("0")def test_get_order_request_data_draft_order_with_sale(@@ -4019,6 +4041,20 @@ def test_get_order_tax_data(assert response == return_value+def test_get_order_tax_data_empty_data(+ order,+ plugin_configuration,+):+ # given+ conf = plugin_configuration()++ # when+ response = get_order_tax_data(order, conf, tax_included=True)++ # then+ assert response is None++@patch("saleor.plugins.avatax.get_order_request_data")@patch("saleor.plugins.avatax.get_cached_response_or_fetch")def test_get_order_tax_data_raised_error(