Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix checkout payload (#9333)
Marcin Gębalaa3c478408852
diff --git a/saleor/plugins/webhook/tests/test_webhook.py b/saleor/plugins/webhook/tests/test_webhook.pyindex d01eb970ae..84a0c7360b 100644--- a/saleor/plugins/webhook/tests/test_webhook.py+++ b/saleor/plugins/webhook/tests/test_webhook.py@@ -1,5 +1,6 @@import jsonfrom datetime import datetime+from decimal import Decimalfrom unittest import mockfrom unittest.mock import ANY, MagicMockfrom urllib.parse import urlencode@@ -597,6 +598,28 @@ def test_checkout_created()+def test_checkout_payload_includes_sales(checkout_with_item, sale, discount_info):+ data = json.loads(generate_checkout_payload(checkout_with_item))+ variant = checkout_with_item.lines.first().variant+ channel_listing = variant.channel_listings.first()+ variant_price_with_sale = variant.get_price(+ product=variant.product,+ collections=[],+ channel=checkout_with_item.channel,+ channel_listing=channel_listing,+ discounts=[discount_info],+ )+ variant_price_without_sale = variant.get_price(+ product=variant.product,+ collections=[],+ channel=checkout_with_item.channel,+ channel_listing=channel_listing,+ discounts=[],+ )+ assert variant_price_without_sale > variant_price_with_sale+ assert Decimal(data[0]["lines"][0]["base_price"]) == variant_price_with_sale.amount++@freeze_time("1914-06-28 10:50")@mock.patch("saleor.plugins.webhook.plugin._get_webhooks_for_event")@mock.patch("saleor.plugins.webhook.plugin.trigger_webhooks_async")diff --git a/saleor/webhook/payloads.py b/saleor/webhook/payloads.pyindex 163976f9f4..55c9baf135 100644--- a/saleor/webhook/payloads.py+++ b/saleor/webhook/payloads.py@@ -21,6 +21,7 @@ from ..core.utils.anonymization import (generate_fake_user,)from ..core.utils.json_serializer import CustomJsonEncoder+from ..discount.utils import fetch_active_discountsfrom ..order import FulfillmentStatus, OrderStatusfrom ..order.models import Fulfillment, FulfillmentLine, Order, OrderLinefrom ..order.utils import get_order_country@@ -408,7 +409,6 @@ def generate_checkout_payload("discount_name","private_metadata","metadata",- "channel",)checkout_price_fields = ("discount_amount",)@@ -416,7 +416,9 @@ def generate_checkout_payload(user_fields = ("email", "first_name", "last_name")channel_fields = ("slug", "currency_code")shipping_method_fields = ("name", "type", "currency", "price_amount")- lines_dict_data = serialize_checkout_lines(checkout)++ discounts = fetch_active_discounts()+ lines_dict_data = serialize_checkout_lines(checkout, discounts)# todo use the most appropriate warehousewarehouse = Nonediff --git a/saleor/webhook/serializers.py b/saleor/webhook/serializers.pyindex ce892cbf63..98a0a9aad6 100644--- a/saleor/webhook/serializers.py+++ b/saleor/webhook/serializers.py@@ -1,11 +1,12 @@from datetime import date, datetime-from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union+from typing import TYPE_CHECKING, Any, Dict, Iterable, List, Optional, Unionimport graphenefrom ..attribute import AttributeEntityType, AttributeInputTypefrom ..checkout.fetch import fetch_checkout_linesfrom ..core.prices import quantize_price+from ..discount import DiscountInfofrom ..product.models import Productif TYPE_CHECKING:@@ -14,7 +15,9 @@ if TYPE_CHECKING:from ..product.models import ProductVariant-def serialize_checkout_lines(checkout: "Checkout") -> List[dict]:+def serialize_checkout_lines(+ checkout: "Checkout", discounts: Optional[Iterable[DiscountInfo]] = None+) -> List[dict]:data = []channel = checkout.channelcurrency = channel.currency_code@@ -24,7 +27,9 @@ def serialize_checkout_lines(checkout: "Checkout") -> List[dict]:channel_listing = line_info.channel_listingcollections = line_info.collectionsproduct = variant.product- base_price = variant.get_price(product, collections, channel, channel_listing)+ base_price = variant.get_price(+ product, collections, channel, channel_listing, discounts or []+ )data.append({"sku": variant.sku,diff --git a/saleor/webhook/tests/test_webhook_payloads.py b/saleor/webhook/tests/test_webhook_payloads.pyindex bc1c22ede2..6a962495ce 100644--- a/saleor/webhook/tests/test_webhook_payloads.py+++ b/saleor/webhook/tests/test_webhook_payloads.py@@ -578,6 +578,9 @@ def test_generate_list_gateways_payload(checkout):payload = generate_list_gateways_payload(currency, checkout)data = json.loads(payload)assert data["checkout"] == json.loads(generate_checkout_payload(checkout))[0]+ assert data["checkout"]["channel"]["id"] == graphene.Node.to_global_id(+ "Channel", checkout.channel.pk+ )assert data["currency"] == currency