Skip to content

Commit

Permalink
Add missing shipping method data in order and checkout events payload…
Browse files Browse the repository at this point in the history
…s. (#9692)

* Add missing shipping method data in order and checkout events payloads.

* quantize_price function used to get price_amount
  • Loading branch information
szdrasiak committed May 9, 2022
1 parent 992258d commit dabd1a2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
36 changes: 32 additions & 4 deletions saleor/webhook/payloads.py
Expand Up @@ -208,6 +208,32 @@ def _generate_collection_point_payload(warehouse: "Warehouse"):
return collection_point_data


def _generate_shipping_method_payload(shipping_method, channel):
if not shipping_method:
return None

serializer = PayloadSerializer()
shipping_method_fields = ("name", "type")

shipping_method_channel_listing = shipping_method.channel_listings.filter(
channel=channel,
).first()

payload = serializer.serialize(
[shipping_method],
fields=shipping_method_fields,
extra_dict_data={
"currency": shipping_method_channel_listing.currency,
"price_amount": quantize_price(
shipping_method_channel_listing.price_amount,
shipping_method_channel_listing.currency,
),
},
)

return json.loads(payload)[0]


@traced_payload_generator
def generate_order_payload(
order: "Order",
Expand Down Expand Up @@ -235,7 +261,6 @@ def generate_order_payload(
discount_price_fields = ("amount_value",)

channel_fields = ("slug", "currency_code")
shipping_method_fields = ("name", "type", "currency", "price_amount")

lines = order.lines.all()
fulfillments = order.fulfillments.all()
Expand Down Expand Up @@ -276,6 +301,9 @@ def generate_order_payload(
if order.collection_point
else None,
"payments": json.loads(_generate_order_payment_payload(payments)),
"shipping_method": _generate_shipping_method_payload(
order.shipping_method, order.channel
),
}
if with_meta:
extra_dict_data["meta"] = generate_meta(
Expand All @@ -287,7 +315,6 @@ def generate_order_payload(
fields=ORDER_FIELDS,
additional_fields={
"channel": (lambda o: o.channel, channel_fields),
"shipping_method": (lambda o: o.shipping_method, shipping_method_fields),
"shipping_address": (lambda o: o.shipping_address, ADDRESS_FIELDS),
"billing_address": (lambda o: o.billing_address, ADDRESS_FIELDS),
"discounts": (lambda _: discounts, discount_fields),
Expand Down Expand Up @@ -458,7 +485,6 @@ def generate_checkout_payload(
quantize_price_fields(checkout, checkout_price_fields, checkout.currency)
user_fields = ("email", "first_name", "last_name")
channel_fields = ("slug", "currency_code")
shipping_method_fields = ("name", "type", "currency", "price_amount")

discounts = fetch_active_discounts()
lines_dict_data = serialize_checkout_lines(checkout, discounts)
Expand All @@ -479,14 +505,16 @@ def generate_checkout_payload(
"user": (lambda c: c.user, user_fields),
"billing_address": (lambda c: c.billing_address, ADDRESS_FIELDS),
"shipping_address": (lambda c: c.shipping_address, ADDRESS_FIELDS),
"shipping_method": (lambda c: c.shipping_method, shipping_method_fields),
"warehouse_address": (
lambda c: warehouse.address if warehouse else None,
ADDRESS_FIELDS,
),
},
extra_dict_data={
# Casting to list to make it json-serializable
"shipping_method": _generate_shipping_method_payload(
checkout.shipping_method, checkout.channel
),
"lines": list(lines_dict_data),
"collection_point": json.loads(
_generate_collection_point_payload(checkout.collection_point)
Expand Down
21 changes: 21 additions & 0 deletions saleor/webhook/tests/test_webhook_payloads.py
Expand Up @@ -111,6 +111,9 @@ def test_generate_order_payload(
assert order.fulfillments.count() == 1

fulfillment = order.fulfillments.first()
shipping_method_channel_listing = order.shipping_method.channel_listings.filter(
channel=order.channel,
).first()

# when
payload = json.loads(generate_order_payload(order, customer_user))[0]
Expand Down Expand Up @@ -143,6 +146,13 @@ def test_generate_order_payload(
),
"name": order.shipping_method.name,
"type": order.shipping_method.type,
"currency": shipping_method_channel_listing.currency,
"price_amount": str(
quantize_price(
shipping_method_channel_listing.price_amount,
shipping_method_channel_listing.currency,
)
),
},
"payments": [
{
Expand Down Expand Up @@ -1115,6 +1125,10 @@ def test_generate_checkout_payload(
# when
payload = json.loads(generate_checkout_payload(checkout, customer_user))[0]

shipping_method_channel_listing = checkout.shipping_method.channel_listings.filter(
channel=checkout.channel,
).first()

# then
assert payload == {
"type": "Checkout",
Expand Down Expand Up @@ -1179,6 +1193,13 @@ def test_generate_checkout_payload(
),
"name": checkout.shipping_method.name,
"type": checkout.shipping_method.type,
"currency": shipping_method_channel_listing.currency,
"price_amount": str(
quantize_price(
shipping_method_channel_listing.price_amount,
shipping_method_channel_listing.currency,
)
),
},
"lines": serialize_checkout_lines(checkout),
"collection_point": json.loads(
Expand Down

0 comments on commit dabd1a2

Please sign in to comment.