Skip to content

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

Fix Avalara crash with shipping methods webhook (#9121)

Rafał Pitoń0ce75a6e06ce
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0940f985cc..d8d362b495 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -9,6 +9,8 @@ All notable, unreleased changes to this project will be documented in this file.
- Do no allow using `id` for updating checkout and order metadata - #8906 by @IKarbowiak
- Use `token` instead
- Remove `graphene-django` dependency - #9170 by @rafalp
+- Don't run plugins when calculating checkout's total price for available shipping methods resolution - #9121 by @rafalp
+ - Use either net or gross price depending on store configuration.
## Other
@@ -52,6 +54,7 @@ All notable, unreleased changes to this project will be documented in this file.
`CheckoutAddPromoCode`, `CheckoutPaymentCreate` will raise a ValidationError when product in the checkout is
unavailable - #8978 by @IKarbowiak
- Fix disabled warehouses appearing as valid click and collect points when checkout contains only preorders - #9052 by @rafalp
+- Fix crash when Avalara plugin was used together with Webhooks plugin for shipping methods - #9121 by @rafalp
# 3.0.0
diff --git a/saleor/checkout/base_calculations.py b/saleor/checkout/base_calculations.py
index 6b316d7540..5ef9f2561d 100644
--- a/saleor/checkout/base_calculations.py
+++ b/saleor/checkout/base_calculations.py
@@ -197,6 +197,24 @@ def base_checkout_total(
return total
+def base_checkout_lines_total(
+ checkout_lines: Iterable["CheckoutLineInfo"],
+ channel: "Channel",
+ currency: str,
+ discounts: Optional[Iterable[DiscountInfo]] = None,
+) -> TaxedMoney:
+ line_totals = [
+ calculate_base_line_total_price(
+ line,
+ channel,
+ discounts,
+ ).price_with_sale
+ for line in checkout_lines
+ ]
+
+ return sum(line_totals, zero_taxed_money(currency))
+
+
def base_checkout_line_total(
checkout_line_info: "CheckoutLineInfo",
channel: "Channel",
diff --git a/saleor/checkout/fetch.py b/saleor/checkout/fetch.py
index 6ce4e8ad1e..17ed7e262f 100644
--- a/saleor/checkout/fetch.py
+++ b/saleor/checkout/fetch.py
@@ -466,15 +466,20 @@ def get_valid_internal_shipping_method_list_for_checkout_info(
shipping_address: Optional["Address"],
lines: Iterable[CheckoutLineInfo],
discounts: Iterable["DiscountInfo"],
- manager: "PluginsManager",
shipping_channel_listings: Iterable[ShippingMethodChannelListing],
) -> List["ShippingMethodData"]:
+ from . import base_calculations
from .utils import get_valid_internal_shipping_methods_for_checkout
country_code = shipping_address.country.code if shipping_address else None
- subtotal = manager.calculate_checkout_subtotal(
- checkout_info, lines, checkout_info.shipping_address, discounts
+
+ subtotal = base_calculations.base_checkout_lines_total(
+ lines,
+ checkout_info.channel,
+ checkout_info.checkout.currency,
+ discounts,
)
+
# if a voucher is applied to shipping, we don't want to subtract the discount amount
# as some methods based on shipping price may become unavailable,
# for example, method on which the discount was applied
@@ -483,6 +488,7 @@ def get_valid_internal_shipping_method_list_for_checkout_info(
)
if not is_shipping_voucher:
subtotal -= checkout_info.checkout.discount
+
valid_shipping_methods = get_valid_internal_shipping_methods_for_checkout(
checkout_info,
lines,
@@ -535,7 +541,6 @@ def update_delivery_method_lists_for_checkout_info(
shipping_address,
lines,
discounts,
- manager,
shipping_channel_listings,
),
get_valid_external_shipping_method_list_for_checkout_info(
diff --git a/saleor/graphql/checkout/tests/test_checkout.py b/saleor/graphql/checkout/tests/test_checkout.py
index dc48693131..0aaf870f31 100644
--- a/saleor/graphql/checkout/tests/test_checkout.py
+++ b/saleor/graphql/checkout/tests/test_checkout.py
@@ -3605,6 +3605,59 @@ def test_checkout_shipping_method_update_external_shipping_method(
assert PRIVATE_META_APP_SHIPPING_ID in checkout.private_metadata
[email protected]("saleor.plugins.webhook.tasks.send_webhook_request_sync")
+def test_checkout_shipping_method_update_external_shipping_method_with_tax_plugin(
+ mock_send_request,
+ staff_api_client,
+ address,
+ checkout_with_item,
+ shipping_app,
+ channel_USD,
+ settings,
+):
+ settings.PLUGINS = [
+ "saleor.plugins.tests.sample_plugins.PluginSample",
+ "saleor.plugins.webhook.plugin.WebhookPlugin",
+ ]
+ response_method_id = "abcd"
+ mock_json_response = [
+ {
+ "id": response_method_id,
+ "name": "Provider - Economy",
+ "amount": "10",
+ "currency": "USD",
+ "maximum_delivery_days": "7",
+ }
+ ]
+ mock_send_request.return_value = mock_json_response
+
+ checkout = checkout_with_item
+ checkout.shipping_address = address
+ checkout.save()
+
+ method_id = graphene.Node.to_global_id(
+ "app", f"{shipping_app.id}:{response_method_id}"
+ )
+
+ # Set external shipping method for first time
+ response = staff_api_client.post_graphql(
+ MUTATION_UPDATE_SHIPPING_METHOD,
+ {"token": checkout_with_item.token, "shippingMethodId": method_id},
+ )
+ data = get_graphql_content(response)["data"]["checkoutShippingMethodUpdate"]
+ assert not data["errors"]
+
+ # Set external shipping for second time
+ # Without a fix this request results in infinite recursion
+ # between Avalara and Webhooks plugins
+ response = staff_api_client.post_graphql(
+ MUTATION_UPDATE_SHIPPING_METHOD,
+ {"token": checkout_with_item.token, "shippingMethodId": method_id},
+ )
+ data = get_graphql_content(response)["data"]["checkoutShippingMethodUpdate"]
+ assert not data["errors"]
+
+
@pytest.mark.parametrize("is_valid_delivery_method", (True, False))
@pytest.mark.parametrize(
"delivery_method, node_name, attribute_name",
diff --git a/saleor/plugins/tests/sample_plugins.py b/saleor/plugins/tests/sample_plugins.py
index 9383fe64b3..d52119784d 100644
--- a/saleor/plugins/tests/sample_plugins.py
+++ b/saleor/plugins/tests/sample_plugins.py
@@ -98,6 +98,9 @@ class PluginSample(BasePlugin):
discounts: Iterable["DiscountInfo"],
previous_value: CheckoutTaxedPricesData,
):
+ # See if delivery method doesn't trigger infinite recursion
+ bool(checkout_info.delivery_method_info.delivery_method)
+
price = Money("1.0", currency=checkout_info.checkout.currency)
return CheckoutTaxedPricesData(
price_with_sale=TaxedMoney(price, price),