Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Make invoice webhook payload contain order instead of order lines
Piotr Blaszczykf34a82ac1cea
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 7af11c5c5f..746d98cfeb 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -76,6 +76,8 @@ All notable, unreleased changes to this project will be documented in this file.- Fix doubling price in checkout for products without tax - #7056 by @IKarbowiak- Introduce changes in plugins method:- `calculate_checkout_subtotal` has been dropped from plugins, for correct subtotal calculation, `calculate_checkout_line_total` must be set (manager method for calculating checkout subtotal uses `calculate_checkout_line_total` method)+- Make `order` property of invoice webhook payload contain order instead of order lines - #7081 by @pdblaszczyk+ - Affected webhook events: `INVOICE_REQUESTED`, `INVOICE_SENT`, `INVOICE_DELETED`### Otherdiff --git a/saleor/core/analytics.py b/saleor/core/analytics.pyindex 354345ecb5..6abd553af3 100644--- a/saleor/core/analytics.py+++ b/saleor/core/analytics.py@@ -54,7 +54,7 @@ def get_order_payloads(order):quantity=order_line.quantity,item_id=order_line.product_sku,)- for order_line in order+ for order_line in order.lines.all()]return ga.transaction(order.id,diff --git a/saleor/core/tests/test_analytics.py b/saleor/core/tests/test_analytics.pyindex 65fb01e16f..97ebd33186 100644--- a/saleor/core/tests/test_analytics.py+++ b/saleor/core/tests/test_analytics.py@@ -18,7 +18,7 @@ def test_get_order_payloads(order_with_lines):assert Decimal(transaction["tt"]) == order.total.tax.amountassert Decimal(transaction["ts"]) == order.shipping_price.net.amount- for i, line in enumerate(order):+ for i, line in enumerate(order.lines.all()):item = data[i + 1]assert item["ti"] == order.pkassert item["in"] == line.variant.display_product()diff --git a/saleor/core/utils/random_data.py b/saleor/core/utils/random_data.pyindex 56ccbcd183..c5dc76144d 100644--- a/saleor/core/utils/random_data.py+++ b/saleor/core/utils/random_data.py@@ -627,7 +627,7 @@ def create_order_lines(order, discounts, how_many=10):def create_fulfillments(order):- for line in order:+ for line in order.lines.all():if random.choice([False, True]):fulfillment, _ = Fulfillment.objects.get_or_create(order=order)quantity = random.randrange(0, line.quantity) + 1@@ -695,7 +695,7 @@ def create_fake_order(discounts, max_order_lines=5):lines = create_order_lines(order, discounts, random.randrange(1, max_order_lines))order.total = sum([line.total_price for line in lines], shipping_price)weight = Weight(kg=0)- for line in order:+ for line in order.lines.all():weight += line.variant.get_weight()order.weight = weightorder.save()diff --git a/saleor/graphql/order/mutations/draft_orders.py b/saleor/graphql/order/mutations/draft_orders.pyindex ca7c59d9cb..3b144df6aa 100644--- a/saleor/graphql/order/mutations/draft_orders.py+++ b/saleor/graphql/order/mutations/draft_orders.py@@ -374,7 +374,7 @@ class DraftOrderComplete(BaseMutation):order.save()- for line in order:+ for line in order.lines.all():if line.variant.track_inventory:line_data = OrderLineData(line=line, quantity=line.quantity, variant=line.variantdiff --git a/saleor/graphql/order/tests/test_bulk_delete.py b/saleor/graphql/order/tests/test_bulk_delete.pyindex f83f448a5f..5e6e9083dc 100644--- a/saleor/graphql/order/tests/test_bulk_delete.py+++ b/saleor/graphql/order/tests/test_bulk_delete.py@@ -53,7 +53,7 @@ def test_fail_to_delete_non_draft_order_lines(staff_api_client, order_with_lines, permission_manage_orders):order = order_with_lines- order_lines = [line for line in order]+ order_lines = [line for line in order.lines.all()]# Ensure we cannot delete a non-draft orderorder.status = OrderStatus.CANCELEDorder.save()@@ -77,7 +77,7 @@ def test_delete_draft_order_lines(staff_api_client, order_with_lines, permission_manage_orders):order = order_with_lines- order_lines = [line for line in order]+ order_lines = [line for line in order.lines.all()]# Only lines in draft order can be deletedorder.status = OrderStatus.DRAFTorder.save()diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.pyindex 5e77cae996..818d022a59 100644--- a/saleor/graphql/order/tests/test_order.py+++ b/saleor/graphql/order/tests/test_order.py@@ -2358,7 +2358,7 @@ def test_draft_order_complete(order.refresh_from_db()assert data["status"] == order.status.upper()- for line in order:+ for line in order.lines.all():allocation = line.allocations.get()assert allocation.quantity_allocated == line.quantity_unfulfilleddiff --git a/saleor/graphql/order/utils.py b/saleor/graphql/order/utils.pyindex e7eee159a8..845161c8f6 100644--- a/saleor/graphql/order/utils.py+++ b/saleor/graphql/order/utils.py@@ -68,7 +68,7 @@ def validate_shipping_address(order):def validate_order_lines(order, country):- for line in order:+ for line in order.lines.all():if line.variant is None:raise ValidationError({@@ -88,7 +88,7 @@ def validate_order_lines(order, country):def validate_product_is_published(order):variant_ids = []- for line in order:+ for line in order.lines.all():variant_ids.append(line.variant_id)unpublished_product = Product.objects.filter(variants__id__in=variant_ids@@ -161,7 +161,7 @@ def validate_variant_channel_listings(variants, channel):def validate_product_is_available_for_purchase(order):- for line in order:+ for line in order.lines.all():product_channel_listing = line.variant.product.channel_listings.filter(channel_id=order.channel_id).first()diff --git a/saleor/order/models.py b/saleor/order/models.pyindex f4b897462c..16feb9f27c 100644--- a/saleor/order/models.py+++ b/saleor/order/models.py@@ -250,9 +250,6 @@ class Order(ModelWithMetadata):def _index_shipping_phone(self):return self.shipping_address.phone- def __iter__(self):- return iter(self.lines.all())-def __repr__(self):return "<Order #%r>" % (self.id,)@@ -297,14 +294,14 @@ class Order(ModelWithMetadata):)def is_shipping_required(self):- return any(line.is_shipping_required for line in self)+ return any(line.is_shipping_required for line in self.lines.all())def get_subtotal(self):- subtotal_iterator = (line.total_price for line in self)+ subtotal_iterator = (line.total_price for line in self.lines.all())return sum(subtotal_iterator, zero_taxed_money(currency=self.currency))def get_total_quantity(self):- return sum([line.quantity for line in self])+ return sum([line.quantity for line in self.lines.all()])def is_draft(self):return self.status == OrderStatus.DRAFT@@ -559,7 +556,7 @@ class Fulfillment(ModelWithMetadata):return self.status != FulfillmentStatus.CANCELEDdef get_total_quantity(self):- return sum([line.quantity for line in self])+ return sum([line.quantity for line in self.lines.all()])@propertydef is_tracking_number_url(self):diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.pyindex dff31cd609..93b3eed9cb 100644--- a/saleor/order/tests/test_order.py+++ b/saleor/order/tests/test_order.py@@ -568,7 +568,7 @@ def test_update_order_prices_tax_included(order_with_lines, vatlayer, site_settidef _calculate_order_weight_from_lines(order):weight = zero_weight()- for line in order:+ for line in order.lines.all():weight += line.variant.get_weight() * line.quantityreturn weightdiff --git a/saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.py b/saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.pyindex 2a48b2053a..c1419febf6 100644--- a/saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.py+++ b/saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.py@@ -20,7 +20,7 @@ def test_create_return_fulfillment_only_order_lines(order_lines_to_return = order_with_lines.lines.all()original_quantity = {- line.id: line.quantity_unfulfilled for line in order_with_lines+ line.id: line.quantity_unfulfilled for line in order_with_lines.lines.all()}order_line_ids = order_lines_to_return.values_list("id", flat=True)original_allocations = list(@@ -86,7 +86,7 @@ def test_create_return_fulfillment_only_order_lines_with_refund(order_lines_to_return = order_with_lines.lines.all()original_quantity = {- line.id: line.quantity_unfulfilled for line in order_with_lines+ line.id: line.quantity_unfulfilled for line in order_with_lines.lines.all()}order_line_ids = order_lines_to_return.values_list("id", flat=True)original_allocations = list(@@ -141,7 +141,7 @@ def test_create_return_fulfillment_only_order_lines_included_shipping_costs(order_lines_to_return = order_with_lines.lines.all()original_quantity = {- line.id: line.quantity_unfulfilled for line in order_with_lines+ line.id: line.quantity_unfulfilled for line in order_with_lines.lines.all()}order_line_ids = order_lines_to_return.values_list("id", flat=True)original_allocations = list(@@ -198,7 +198,7 @@ def test_create_return_fulfillment_only_order_lines_with_replace_request(order_lines_to_return = order_with_lines.lines.all()original_quantity = {- line.id: line.quantity_unfulfilled for line in order_with_lines+ line.id: line.quantity_unfulfilled for line in order_with_lines.lines.all()}order_line_ids = order_lines_to_return.values_list("id", flat=True)original_allocations = list(@@ -309,7 +309,7 @@ def test_create_return_fulfillment_multiple_order_line_returns(payment = order_with_lines.get_last_payment()order_lines_to_return = order_with_lines.lines.all()original_quantity = {- line.id: line.quantity_unfulfilled for line in order_with_lines+ line.id: line.quantity_unfulfilled for line in order_with_lines.lines.all()}order_line_ids = order_lines_to_return.values_list("id", flat=True)lines_count = order_lines_to_return.count()diff --git a/saleor/order/tests/test_order_actions_refund_products.py b/saleor/order/tests/test_order_actions_refund_products.pyindex 280774082d..168cd429eb 100644--- a/saleor/order/tests/test_order_actions_refund_products.py+++ b/saleor/order/tests/test_order_actions_refund_products.py@@ -23,7 +23,7 @@ def test_create_refund_fulfillment_only_order_lines(order_lines_to_refund = order_with_lines.lines.all()original_quantity = {- line.id: line.quantity_unfulfilled for line in order_with_lines+ line.id: line.quantity_unfulfilled for line in order_with_lines.lines.all()}order_line_ids = order_lines_to_refund.values_list("id", flat=True)original_allocations = list(@@ -75,7 +75,7 @@ def test_create_refund_fulfillment_multiple_order_line_refunds(payment = order_with_lines.get_last_payment()order_lines_to_refund = order_with_lines.lines.all()original_quantity = {- line.id: line.quantity_unfulfilled for line in order_with_lines+ line.id: line.quantity_unfulfilled for line in order_with_lines.lines.all()}order_line_ids = order_lines_to_refund.values_list("id", flat=True)lines_count = order_lines_to_refund.count()@@ -119,7 +119,7 @@ def test_create_refund_fulfillment_included_shipping_costs(payment = order_with_lines.get_last_payment()order_lines_to_refund = order_with_lines.lines.all()original_quantity = {- line.id: line.quantity_unfulfilled for line in order_with_lines+ line.id: line.quantity_unfulfilled for line in order_with_lines.lines.all()}order_line_ids = order_lines_to_refund.values_list("id", flat=True)lines_count = order_with_lines.lines.count()diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex e8bf90fd9d..4aa5afea57 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -171,7 +171,7 @@ def recalculate_order(order: Order, **kwargs):def recalculate_order_weight(order):"""Recalculate order weights."""weight = zero_weight()- for line in order:+ for line in order.lines.all():if line.variant:weight += line.variant.get_weight() * line.quantityorder.weight = weight@@ -410,7 +410,7 @@ def restock_order_lines(order):).first()dellocating_stock_lines: List[OrderLineData] = []- for line in order:+ for line in order.lines.all():if line.variant and line.variant.track_inventory:if line.quantity_unfulfilled > 0:dellocating_stock_lines.append(diff --git a/saleor/webhook/tests/test_webhook_payloads.py b/saleor/webhook/tests/test_webhook_payloads.pyindex f771f3d7c5..5ac53f8610 100644--- a/saleor/webhook/tests/test_webhook_payloads.py+++ b/saleor/webhook/tests/test_webhook_payloads.py@@ -10,6 +10,7 @@ from ...product.models import ProductVariantfrom ..payloads import (ORDER_FIELDS,PRODUCT_VARIANT_FIELDS,+ generate_invoice_payload,generate_order_payload,generate_product_variant_payload,)@@ -44,7 +45,6 @@ def test_generate_order_payload(assert payload.get("shipping_method")assert payload.get("shipping_tax_rate")assert payload.get("lines")- assert payload.get("payments")assert payload.get("shipping_address")assert payload.get("billing_address")assert payload.get("fulfillments")@@ -183,3 +183,32 @@ def test_generate_product_variant_deleted_payload(assert len(payload["attributes"]) == 2assert len(payload["channel_listings"]) == 1assert len(payload.keys()) == len(payload_fields)+++def test_generate_invoice_payload(fulfilled_order):+ invoice = fulfilled_order.invoices.first()+ payload = json.loads(generate_invoice_payload(invoice))[0]++ assert payload == {+ "type": "Invoice",+ "id": graphene.Node.to_global_id("Invoice", invoice.id),+ "order": {+ "type": "Order",+ "id": graphene.Node.to_global_id("Order", invoice.order.id),+ "private_metadata": {},+ "metadata": {},+ "created": ANY,+ "status": "fulfilled",+ "user_email": "[email protected]",+ "shipping_method_name": "DHL",+ "shipping_price_net_amount": "10.000",+ "shipping_price_gross_amount": "12.300",+ "shipping_tax_rate": "0.0000",+ "total_net_amount": "80.000",+ "total_gross_amount": "98.400",+ "weight": "0.0:kg",+ },+ "number": "01/12/2020/TEST",+ "created": ANY,+ "external_url": "http://www.example.com/invoice.pdf",+ }