Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix for shipping api doesn't apply taxes
Krzysztof Świątekd94ff8f0e923
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 4fb2656c7f..79ffa42fd2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -7,6 +7,7 @@ All notable, unreleased changes to this project will be documented in this file.- Fix problem with free shipping voucher - #4942 by @IKarbowiak- Add sub-categories to random data - #4949 by @IKarbowiak- Deprecate `localized` field in Money type - #4952 by @IKarbowiak+- Fix for shipping api doesn't apply taxes - #4913 by @kswiatek92## 2.9.0diff --git a/saleor/graphql/checkout/types.py b/saleor/graphql/checkout/types.pyindex 80e9b8b498..ce585da6f2 100644--- a/saleor/graphql/checkout/types.py+++ b/saleor/graphql/checkout/types.py@@ -3,7 +3,7 @@ import graphene_django_optimizer as gql_optimizerfrom ...checkout import modelsfrom ...checkout.utils import get_valid_shipping_methods_for_checkout-from ...core.taxes import zero_taxed_money+from ...core.taxes import display_gross_prices, zero_taxed_moneyfrom ...extensions.manager import get_extensions_managerfrom ..core.connection import CountableDjangoObjectTypefrom ..core.resolvers import resolve_meta, resolve_private_meta@@ -176,6 +176,17 @@ class Checkout(MetadataObjectType, CountableDjangoObjectType):)if available is None:return []++ manager = get_extensions_manager()+ display_gross = display_gross_prices()+ for shipping_method in available:+ taxed_price = manager.apply_taxes_to_shipping(+ shipping_method.price, root.shipping_address+ )+ if display_gross:+ shipping_method.price = taxed_price.gross+ else:+ shipping_method.price = taxed_price.netreturn available@staticmethoddiff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.pyindex aff71408eb..89deca4d41 100644--- a/saleor/graphql/order/types.py+++ b/saleor/graphql/order/types.py@@ -3,6 +3,8 @@ import graphene_django_optimizer as gql_optimizerfrom django.core.exceptions import ValidationErrorfrom graphene import relay+from ...core.taxes import display_gross_prices+from ...extensions.manager import get_extensions_managerfrom ...order import modelsfrom ...order.models import FulfillmentStatusfrom ...order.utils import get_valid_shipping_methods_for_order@@ -476,6 +478,17 @@ class Order(MetadataObjectType, CountableDjangoObjectType):available = get_valid_shipping_methods_for_order(root)if available is None:return []++ manager = get_extensions_manager()+ display_gross = display_gross_prices()+ for shipping_method in available:+ taxed_price = manager.apply_taxes_to_shipping(+ shipping_method.price, root.shipping_address+ )+ if display_gross:+ shipping_method.price = taxed_price.gross+ else:+ shipping_method.price = taxed_price.netreturn available@staticmethoddiff --git a/tests/api/test_checkout.py b/tests/api/test_checkout.pyindex 2a84d23b60..f0c2177fe0 100644--- a/tests/api/test_checkout.py+++ b/tests/api/test_checkout.py@@ -22,6 +22,7 @@ from saleor.payment.interface import GatewayResponsefrom saleor.shipping import ShippingMethodTypefrom saleor.shipping.models import ShippingMethodfrom tests.api.utils import get_graphql_content+from saleor.extensions.manager import ExtensionsManager@pytest.fixture@@ -434,6 +435,57 @@ def test_checkout_available_shipping_methods(assert data["availableShippingMethods"] == [{"name": shipping_method.name}]+ "expected_price_type, expected_price, display_gross_prices",+ (("gross", 13, True), ("net", 10, False)),+)+def test_checkout_available_shipping_methods_with_price_displayed(+ expected_price_type,+ expected_price,+ display_gross_prices,+ monkeypatch,+ api_client,+ checkout_with_item,+ address,+ shipping_zone,+ site_settings,+):+ shipping_method = shipping_zone.shipping_methods.first()+ taxed_price = TaxedMoney(net=Money(10, "USD"), gross=Money(13, "USD"))+ apply_taxes_to_shipping_mock = mock.Mock(return_value=taxed_price)+ monkeypatch.setattr(+ ExtensionsManager, "apply_taxes_to_shipping", apply_taxes_to_shipping_mock+ )+ site_settings.display_gross_prices = display_gross_prices+ site_settings.save()+ checkout_with_item.shipping_address = address+ checkout_with_item.save()++ query = """+ query getCheckout($token: UUID!) {+ checkout(token: $token) {+ availableShippingMethods {+ name+ price {+ amount+ }+ }+ }+ }+ """+ variables = {"token": checkout_with_item.token}+ response = api_client.post_graphql(query, variables)+ content = get_graphql_content(response)+ data = content["data"]["checkout"]++ apply_taxes_to_shipping_mock.assert_called_once_with(+ shipping_method.price, mock.ANY+ )+ assert data["availableShippingMethods"] == [+ {"name": "DHL", "price": {"amount": expected_price}}+ ]++def test_checkout_no_available_shipping_methods_without_address(api_client, checkout_with_item):diff --git a/tests/api/test_order.py b/tests/api/test_order.pyindex 276df201a1..60a4de1a52 100644--- a/tests/api/test_order.py+++ b/tests/api/test_order.py@@ -1,6 +1,6 @@import uuidfrom datetime import date, timedelta-from unittest.mock import MagicMock, Mock+from unittest.mock import ANY, MagicMock, Mockimport grapheneimport pytest@@ -10,6 +10,7 @@ from prices import Money, TaxedMoneyfrom saleor.account.models import CustomerEventfrom saleor.core.taxes import zero_taxed_money+from saleor.extensions.manager import ExtensionsManagerfrom saleor.graphql.core.enums import ReportingPeriodfrom saleor.graphql.order.mutations.orders import (clean_order_cancel,@@ -211,6 +212,57 @@ def test_order_query(assert expected_method.type.upper() == method["type"]+ "expected_price_type, expected_price, display_gross_prices",+ (("gross", 13, True), ("net", 10, False)),+)+def test_order_available_shipping_methods_query(+ expected_price_type,+ expected_price,+ display_gross_prices,+ monkeypatch,+ staff_api_client,+ permission_manage_orders,+ fulfilled_order,+ shipping_zone,+ site_settings,+):+ query = """+ query OrdersQuery {+ orders(first: 1) {+ edges {+ node {+ availableShippingMethods {+ id+ price {+ amount+ }+ type+ }+ }+ }+ }+ }+ """+ shipping_method = shipping_zone.shipping_methods.first()+ taxed_price = TaxedMoney(net=Money(10, "USD"), gross=Money(13, "USD"))+ apply_taxes_to_shipping_mock = Mock(return_value=taxed_price)+ monkeypatch.setattr(+ ExtensionsManager, "apply_taxes_to_shipping", apply_taxes_to_shipping_mock+ )+ site_settings.display_gross_prices = display_gross_prices+ site_settings.save()++ staff_api_client.user.user_permissions.add(permission_manage_orders)+ response = staff_api_client.post_graphql(query)+ content = get_graphql_content(response)+ order_data = content["data"]["orders"]["edges"][0]["node"]+ method = order_data["availableShippingMethods"][0]++ apply_taxes_to_shipping_mock.assert_called_once_with(shipping_method.price, ANY)+ assert expected_price == method["price"]["amount"]++def test_order_query_customer(api_client):query = """query OrdersQuery {