Skip to content

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

Handle empty request to avatax

Maciej Korycinskid3b3fdcd09b1
diff --git a/saleor/plugins/avatax/__init__.py b/saleor/plugins/avatax/__init__.py
index 0350e1aa7d..f3ac1ff646 100644
--- a/saleor/plugins/avatax/__init__.py
+++ b/saleor/plugins/avatax/__init__.py
@@ -124,8 +124,8 @@ def api_get_request(
def _validate_adddress_details(
shipping_address, is_shipping_required, address, shipping_method
):
- if not is_shipping_required and not address:
- return False
+ if not is_shipping_required and address:
+ return True
if not shipping_address:
return False
if not shipping_method:
@@ -151,7 +151,7 @@ def _validate_checkout(checkout: "Checkout", lines: Iterable["CheckoutLine"]) ->
return False
shipping_address = checkout.shipping_address
- is_shipping_required = checkout.is_shipping_required
+ is_shipping_required = checkout.is_shipping_required()
address = shipping_address or checkout.billing_address
return _validate_adddress_details(
shipping_address, is_shipping_required, address, checkout.shipping_method
diff --git a/saleor/plugins/avatax/plugin.py b/saleor/plugins/avatax/plugin.py
index 4841f83552..fcb5b174a1 100644
--- a/saleor/plugins/avatax/plugin.py
+++ b/saleor/plugins/avatax/plugin.py
@@ -266,6 +266,8 @@ class AvataxPlugin(BasePlugin):
transaction_type=TransactionType.ORDER,
discounts=discounts,
)
+ if not data.get("createTransactionModel", {}).get("lines"):
+ return previous_value
transaction_url = urljoin(
get_api_url(self.config.use_sandbox), "transactions/createoradjust"
)
diff --git a/saleor/plugins/avatax/tasks.py b/saleor/plugins/avatax/tasks.py
index bf348ca870..0e58c1e8e0 100644
--- a/saleor/plugins/avatax/tasks.py
+++ b/saleor/plugins/avatax/tasks.py
@@ -20,12 +20,22 @@ def api_post_request_task(transaction_url, data, config, order_id):
"Unable to send the order %s to Avatax. Order doesn't exist.", order_id
)
return
- response = api_post_request(transaction_url, data, config)
+ if not data.get("createTransactionModel", {}).get("lines"):
+ msg = "The order doesn't have any line which should be sent to Avatax."
+ external_notification_event(
+ order=order, user=None, message=msg, parameters=None
+ )
+ return
+ response = api_post_request(transaction_url, data, config)
msg = f"Order sent to Avatax. Order ID: {order.token}"
if not response or "error" in response:
- msg = "Unable to send order to Avatax."
+ avatax_msg = response.get("error", {}).get("message", "")
+ msg = f"Unable to send order to Avatax. {avatax_msg}"
+ logger.warning(
+ "Unable to send order %s to Avatax. Response %s", order.token, response
+ )
external_notification_event(order=order, user=None, message=msg, parameters=None)
- if not response:
+ if not response or "error" in response:
raise TaxError
diff --git a/saleor/plugins/avatax/tests/test_avatax.py b/saleor/plugins/avatax/tests/test_avatax.py
index 62118d976e..ae1bf36981 100644
--- a/saleor/plugins/avatax/tests/test_avatax.py
+++ b/saleor/plugins/avatax/tests/test_avatax.py
@@ -17,6 +17,7 @@ from .. import (
META_DESCRIPTION_KEY,
AvataxConfiguration,
TransactionType,
+ _validate_adddress_details,
api_get_request,
api_post_request,
generate_request_data_from_checkout,
@@ -841,3 +842,35 @@ def test_get_order_tax_data_raised_error(
# then
assert e._excinfo[1].args[0] == return_value["error"]
+
+
+ "shipping_address_none, shipping_method_none, billing_address_none, "
+ "is_shipping_required, expected_is_valid",
+ [
+ (False, False, False, True, True),
+ (True, True, False, True, False),
+ (True, True, False, False, True),
+ (False, True, False, True, False),
+ (True, False, False, True, False),
+ ],
+)
+def test_validate_adddress_details(
+ shipping_address_none,
+ shipping_method_none,
+ billing_address_none,
+ is_shipping_required,
+ expected_is_valid,
+ checkout_ready_to_complete,
+):
+ shipping_address = checkout_ready_to_complete.shipping_address
+ shipping_address = None if shipping_address_none else shipping_address
+ billing_address = checkout_ready_to_complete.billing_address
+ billing_address = None if billing_address_none else billing_address
+ address = shipping_address or billing_address
+ shipping_method = checkout_ready_to_complete.shipping_method
+ shipping_method = None if shipping_method_none else shipping_method
+ is_valid = _validate_adddress_details(
+ shipping_address, is_shipping_required, address, shipping_method
+ )
+ assert is_valid is expected_is_valid
diff --git a/saleor/plugins/avatax/tests/test_tasks.py b/saleor/plugins/avatax/tests/test_tasks.py
index b613694392..8eea49ea68 100644
--- a/saleor/plugins/avatax/tests/test_tasks.py
+++ b/saleor/plugins/avatax/tests/test_tasks.py
@@ -70,12 +70,15 @@ def test_api_post_request_task_creates_order_event(
def test_api_post_request_task_missing_response(
order_with_lines, shipping_zone, monkeypatch
):
- monkeypatch.setattr("saleor.plugins.avatax.tasks.api_post_request", lambda *_: {})
+ mock_api_post_request = {"error": {"message": "Wrong credentials"}}
+ monkeypatch.setattr(
+ "saleor.plugins.avatax.tasks.api_post_request", lambda *_: mock_api_post_request
+ )
config = AvataxConfiguration(
username_or_account="test", password_or_license="test", use_sandbox=False,
)
- request_data = {}
+ request_data = get_order_request_data(order_with_lines, config)
transaction_url = urljoin(
get_api_url(config.use_sandbox), "transactions/createoradjust"
@@ -88,4 +91,33 @@ def test_api_post_request_task_missing_response(
assert order_with_lines.events.count() == 1
event = order_with_lines.events.get()
assert event.type == OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
- assert event.parameters["message"] == "Unable to send order to Avatax."
+ expected_msg = "Unable to send order to Avatax. Wrong credentials"
+ assert event.parameters["message"] == expected_msg
+
+
+def test_api_post_request_task_order_doesnt_have_any_lines_with_taxes_to_calculate(
+ order_with_lines, shipping_zone, monkeypatch
+):
+ mock_api_post_request = {"error": {"message": "Wrong credentials"}}
+ monkeypatch.setattr(
+ "saleor.plugins.avatax.tasks.api_post_request", lambda *_: mock_api_post_request
+ )
+
+ config = AvataxConfiguration(
+ username_or_account="test", password_or_license="test", use_sandbox=False,
+ )
+ request_data = {}
+
+ transaction_url = urljoin(
+ get_api_url(config.use_sandbox), "transactions/createoradjust"
+ )
+
+ api_post_request_task(
+ transaction_url, request_data, asdict(config), order_with_lines.pk
+ )
+
+ assert order_with_lines.events.count() == 1
+ event = order_with_lines.events.get()
+ assert event.type == OrderEvents.EXTERNAL_SERVICE_NOTIFICATION
+ expected_msg = "The order doesn't have any line which should be sent to Avatax."
+ assert event.parameters["message"] == expected_msg