Skip to content

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

Include line total to webhook payload (#6354)

Maciej Korycinski62d29b33373c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 794d0fbaa0..56f38fc85d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -76,6 +76,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add attributes validation while creating/updating a product's variant - #6269 by @GrzegorzDerdak
- Add metadata to page model - #6292 by @dominik-zeglen
- Fix for unnecesary attributes validation while updating simple product - #6300 by @GrzegorzDerdak
+- Include order line total price to webhook payload - #6354 by @korycins
## 2.10.2
diff --git a/saleor/tests/fixtures.py b/saleor/tests/fixtures.py
index df05651836..6e252de3c6 100644
--- a/saleor/tests/fixtures.py
+++ b/saleor/tests/fixtures.py
@@ -1312,7 +1312,7 @@ def order_line_with_one_allocation(customer_user, variant_with_many_stocks):
quantity=2,
variant=variant,
unit_price=TaxedMoney(net=net, gross=gross),
- tax_rate=23,
+ tax_rate=Decimal(23),
)
Allocation.objects.create(
diff --git a/saleor/webhook/payloads.py b/saleor/webhook/payloads.py
index 0a94adfee4..85f5d02532 100644
--- a/saleor/webhook/payloads.py
+++ b/saleor/webhook/payloads.py
@@ -1,5 +1,5 @@
import json
-from typing import Optional
+from typing import Iterable, Optional
from django.db.models import QuerySet
@@ -12,7 +12,7 @@ from ..core.utils.anonymization import (
)
from ..invoice.models import Invoice
from ..order import FulfillmentStatus, OrderStatus
-from ..order.models import Fulfillment, FulfillmentLine, Order
+from ..order.models import Fulfillment, FulfillmentLine, Order, OrderLine
from ..order.utils import get_order_country
from ..payment import ChargeStatus
from ..product.models import Product
@@ -55,6 +55,30 @@ ORDER_FIELDS = (
)
+def generate_order_lines_payload(lines: Iterable[OrderLine]):
+ line_fields = (
+ "product_name",
+ "variant_name",
+ "translated_product_name",
+ "translated_variant_name",
+ "product_sku",
+ "quantity",
+ "currency",
+ "unit_price_net_amount",
+ "unit_price_gross_amount",
+ "tax_rate",
+ )
+ serializer = PayloadSerializer()
+ return serializer.serialize(
+ lines,
+ fields=line_fields,
+ extra_dict_data={
+ "total_price_net_amount": (lambda l: l.get_total().net.amount),
+ "total_price_gross_amount": (lambda l: l.get_total().gross.amount),
+ },
+ )
+
+
def generate_order_payload(order: "Order"):
serializer = PayloadSerializer()
fulfillment_fields = ("status", "tracking_number", "created")
@@ -81,30 +105,20 @@ def generate_order_payload(order: "Order"):
"billing_country_code",
"billing_country_area",
)
- line_fields = (
- "product_name",
- "variant_name",
- "translated_product_name",
- "translated_variant_name",
- "product_sku",
- "quantity",
- "currency",
- "unit_price_net_amount",
- "unit_price_gross_amount",
- "tax_rate",
- )
+
shipping_method_fields = ("name", "type", "currency", "price_amount")
+ lines = order.lines.all()
order_data = serializer.serialize(
[order],
fields=ORDER_FIELDS,
additional_fields={
"shipping_method": (lambda o: o.shipping_method, shipping_method_fields),
- "lines": (lambda o: o.lines.all(), line_fields),
"payments": (lambda o: o.payments.all(), payment_fields),
"shipping_address": (lambda o: o.shipping_address, ADDRESS_FIELDS),
"billing_address": (lambda o: o.billing_address, ADDRESS_FIELDS),
"fulfillments": (lambda o: o.fulfillments.all(), fulfillment_fields),
},
+ extra_dict_data={"lines": json.loads(generate_order_lines_payload(lines))},
)
return order_data
diff --git a/saleor/webhook/tests/test_webhook_payloads.py b/saleor/webhook/tests/test_webhook_payloads.py
index 9f0bb54003..0ce862d58e 100644
--- a/saleor/webhook/tests/test_webhook_payloads.py
+++ b/saleor/webhook/tests/test_webhook_payloads.py
@@ -1,4 +1,5 @@
import json
+from decimal import Decimal
import graphene
@@ -24,3 +25,35 @@ def test_generate_order_payload(
assert payload.get("shipping_address")
assert payload.get("billing_address")
assert payload.get("fulfillments")
+
+
+def test_order_lines_have_all_required_fields(order, order_line_with_one_allocation):
+ order.lines.add(order_line_with_one_allocation)
+ line = order_line_with_one_allocation
+ payload = json.loads(generate_order_payload(order))[0]
+ lines_payload = payload.get("lines")
+
+ assert len(lines_payload) == 1
+ line_id = graphene.Node.to_global_id("OrderLine", line.id)
+ line_payload = lines_payload[0]
+ unit_net_amount = line.unit_price_net_amount.quantize(Decimal("0.001"))
+ unit_gross_amount = line.unit_price_gross_amount.quantize(Decimal("0.001"))
+ total_line = line.get_total()
+ assert line_payload == {
+ "id": line_id,
+ "type": "OrderLine",
+ "product_name": line.product_name,
+ "variant_name": line.variant_name,
+ "translated_product_name": line.translated_product_name,
+ "translated_variant_name": line.translated_variant_name,
+ "product_sku": line.product_sku,
+ "quantity": line.quantity,
+ "currency": line.currency,
+ "unit_price_net_amount": str(unit_net_amount),
+ "unit_price_gross_amount": str(unit_gross_amount),
+ "total_price_net_amount": str(total_line.net.amount.quantize(Decimal("0.001"))),
+ "total_price_gross_amount": str(
+ total_line.gross.amount.quantize(Decimal("0.001"))
+ ),
+ "tax_rate": str(line.tax_rate.quantize(Decimal("0.01"))),
+ }