Skip to content

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

Fix issue with missing fulfillment in webhook order updated

Maciej Korycinskib4b49da70ba2
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index 290e1e32e4..58855bd9c6 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -11,6 +11,7 @@ from django.db import transaction
from ..account.models import User
from ..core import analytics
from ..core.exceptions import AllocationError, InsufficientStock, InsufficientStockData
+from ..core.transactions import transaction_with_commit_on_errors
from ..payment import (
ChargeStatus,
CustomPaymentChoices,
@@ -176,11 +177,9 @@ def order_returned(
order: "Order",
user: Optional["User"],
returned_lines: List[Tuple[QuantityType, OrderLine]],
- manager: "PluginsManager",
):
order_returned_event(order=order, user=user, returned_lines=returned_lines)
update_order_status(order)
- manager.order_updated(order)
@transaction.atomic
@@ -712,18 +711,18 @@ def create_refund_fulfillment(
unfulfilled lines will be deallocated.
"""
- _process_refund(
- requester=requester,
- order=order,
- payment=payment,
- order_lines_to_refund=order_lines_to_refund,
- fulfillment_lines_to_refund=fulfillment_lines_to_refund,
- amount=amount,
- refund_shipping_costs=refund_shipping_costs,
- manager=manager,
- )
+ with transaction_with_commit_on_errors():
+ _process_refund(
+ requester=requester,
+ order=order,
+ payment=payment,
+ order_lines_to_refund=order_lines_to_refund,
+ fulfillment_lines_to_refund=fulfillment_lines_to_refund,
+ amount=amount,
+ refund_shipping_costs=refund_shipping_costs,
+ manager=manager,
+ )
- with transaction.atomic():
refunded_fulfillment = Fulfillment.objects.create(
status=FulfillmentStatus.REFUNDED, order=order
)
@@ -739,6 +738,7 @@ def create_refund_fulfillment(
)
Fulfillment.objects.filter(order=order, lines=None).delete()
+ transaction.on_commit(lambda: manager.order_updated(order))
return refunded_fulfillment
@@ -908,7 +908,6 @@ def create_return_fulfillment(
order: "Order",
order_lines: List[OrderLineData],
fulfillment_lines: List[FulfillmentLineData],
- manager: "PluginsManager",
refund: bool = False,
) -> Fulfillment:
status = FulfillmentStatus.RETURNED
@@ -945,7 +944,6 @@ def create_return_fulfillment(
order,
user=requester,
returned_lines=returned_lines_list,
- manager=manager,
)
)
@@ -1025,19 +1023,20 @@ def create_fulfillments_for_returned_products(
"""
return_order_lines = [data for data in order_lines if not data.replace]
return_fulfillment_lines = [data for data in fulfillment_lines if not data.replace]
- if refund and payment:
- _process_refund(
- requester=requester,
- order=order,
- payment=payment,
- order_lines_to_refund=return_order_lines,
- fulfillment_lines_to_refund=return_fulfillment_lines,
- amount=amount,
- refund_shipping_costs=refund_shipping_costs,
- manager=manager,
- )
with transaction.atomic():
+ if refund and payment:
+ _process_refund(
+ requester=requester,
+ order=order,
+ payment=payment,
+ order_lines_to_refund=return_order_lines,
+ fulfillment_lines_to_refund=return_fulfillment_lines,
+ amount=amount,
+ refund_shipping_costs=refund_shipping_costs,
+ manager=manager,
+ )
+
replace_order_lines = [data for data in order_lines if data.replace]
replace_fulfillment_lines = [data for data in fulfillment_lines if data.replace]
@@ -1054,10 +1053,11 @@ def create_fulfillments_for_returned_products(
order=order,
order_lines=return_order_lines,
fulfillment_lines=return_fulfillment_lines,
- manager=manager,
refund=refund,
)
Fulfillment.objects.filter(order=order, lines=None).delete()
+
+ transaction.on_commit(lambda: manager.order_updated(order))
return return_fulfillment, replace_fulfillment, new_order
@@ -1094,6 +1094,7 @@ def _calculate_refund_amount(
return refund_amount
+@transaction_with_commit_on_errors()
def _process_refund(
requester: Optional["User"],
order: "Order",
@@ -1123,12 +1124,26 @@ def _process_refund(
"The refund operation is not available yet.",
code=OrderErrorCode.CANNOT_REFUND.value,
)
- order_refunded(order, requester, amount, payment, manager=manager)
+ transaction.on_commit(
+ lambda: events.payment_refunded_event(
+ order=order,
+ user=requester,
+ amount=amount, # type: ignore
+ payment=payment,
+ )
+ )
+ transaction.on_commit(
+ lambda: send_order_refunded_confirmation(
+ order, requester, amount, payment.currency, manager # type: ignore
+ )
+ )
- fulfillment_refunded_event(
- order=order,
- user=requester,
- refunded_lines=list(lines_to_refund.values()),
- amount=amount,
- shipping_costs_included=refund_shipping_costs,
+ transaction.on_commit(
+ lambda: fulfillment_refunded_event(
+ order=order,
+ user=requester,
+ refunded_lines=list(lines_to_refund.values()),
+ amount=amount, # type: ignore
+ shipping_costs_included=refund_shipping_costs,
+ )
)
diff --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.py
index c07233eebb..fb10d0de6a 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
@@ -11,9 +11,14 @@ from ..actions import create_fulfillments_for_returned_products
from ..models import Fulfillment, FulfillmentLine
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_return_fulfillment_only_order_lines(
- mocked_refund, order_with_lines, payment_dummy_fully_charged, staff_user
+ mocked_refund,
+ mocked_order_updated,
+ order_with_lines,
+ payment_dummy_fully_charged,
+ staff_user,
):
order_with_lines.payments.add(payment_dummy_fully_charged)
payment = order_with_lines.get_last_payment()
@@ -76,10 +81,17 @@ def test_create_return_fulfillment_only_order_lines(
assert order_lines_to_return.filter(id=event_lines[1]["line_pk"]).exists()
assert event_lines[1]["quantity"] == 2
+ mocked_order_updated.assert_called_once_with(order_with_lines)
+
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_return_fulfillment_only_order_lines_with_refund(
- mocked_refund, order_with_lines, payment_dummy_fully_charged, staff_user
+ mocked_refund,
+ mocked_order_updated,
+ order_with_lines,
+ payment_dummy_fully_charged,
+ staff_user,
):
order_with_lines.payments.add(payment_dummy_fully_charged)
payment = order_with_lines.get_last_payment()
@@ -108,6 +120,7 @@ def test_create_return_fulfillment_only_order_lines_with_refund(
)
returned_fulfillment, replaced_fulfillment, replace_order = response
+ flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillment.lines.all()
assert returned_fulfillment.status == FulfillmentStatus.REFUNDED_AND_RETURNED
assert len(returned_fulfillment_lines) == lines_count
@@ -131,10 +144,17 @@ def test_create_return_fulfillment_only_order_lines_with_refund(
mocked_refund.assert_called_once_with(payment_dummy_fully_charged, ANY, amount)
assert not replace_order
+ mocked_order_updated.assert_called_once_with(order_with_lines)
+
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_return_fulfillment_only_order_lines_included_shipping_costs(
- mocked_refund, order_with_lines, payment_dummy_fully_charged, staff_user
+ mocked_refund,
+ mocked_order_updated,
+ order_with_lines,
+ payment_dummy_fully_charged,
+ staff_user,
):
order_with_lines.payments.add(payment_dummy_fully_charged)
payment = order_with_lines.get_last_payment()
@@ -164,6 +184,7 @@ def test_create_return_fulfillment_only_order_lines_included_shipping_costs(
)
returned_fulfillment, replaced_fulfillment, replace_order = response
+ flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillment.lines.all()
assert returned_fulfillment.status == FulfillmentStatus.REFUNDED_AND_RETURNED
assert len(returned_fulfillment_lines) == lines_count
@@ -188,10 +209,17 @@ def test_create_return_fulfillment_only_order_lines_included_shipping_costs(
mocked_refund.assert_called_once_with(payment_dummy_fully_charged, ANY, amount)
assert not replace_order
+ mocked_order_updated.assert_called_once_with(order_with_lines)
+
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_return_fulfillment_only_order_lines_with_replace_request(
- mocked_refund, order_with_lines, payment_dummy_fully_charged, staff_user
+ mocked_refund,
+ mocked_order_updated,
+ order_with_lines,
+ payment_dummy_fully_charged,
+ staff_user,
):
order_with_lines.payments.add(payment_dummy_fully_charged)
payment = order_with_lines.get_last_payment()
@@ -225,6 +253,7 @@ def test_create_return_fulfillment_only_order_lines_with_replace_request(
)
returned_fulfillment, replaced_fulfillment, replace_order = response
+ flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillment.lines.all()
assert returned_fulfillment.status == FulfillmentStatus.RETURNED
# we replaced one line
@@ -300,10 +329,17 @@ def test_create_return_fulfillment_only_order_lines_with_replace_request(
)
assert replaced_line.tax_rate == expected_replaced_line.tax_rate
+ mocked_order_updated.assert_called_once_with(order_with_lines)
+
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_return_fulfillment_only_fulfillment_lines(
- mocked_refund, fulfilled_order, payment_dummy_fully_charged, staff_user
+ mocked_refund,
+ mocked_order_updated,
+ fulfilled_order,
+ payment_dummy_fully_charged,
+ staff_user,
):
fulfilled_order.payments.add(payment_dummy_fully_charged)
payment = fulfilled_order.get_last_payment()
@@ -325,6 +361,7 @@ def test_create_return_fulfillment_only_fulfillment_lines(
returned_fulfillment, replaced_fulfillment, replace_order = response
+ flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillment.lines.all()
assert returned_fulfillment.status == FulfillmentStatus.RETURNED
assert returned_fulfillment_lines.count() == len(order_line_ids)
@@ -339,10 +376,17 @@ def test_create_return_fulfillment_only_fulfillment_lines(
assert not mocked_refund.called
assert not replace_order
+ mocked_order_updated.assert_called_once_with(fulfilled_order)
+
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_return_fulfillment_only_fulfillment_lines_replace_order(
- mocked_refund, fulfilled_order, payment_dummy_fully_charged, staff_user
+ mocked_refund,
+ mocked_order_updated,
+ fulfilled_order,
+ payment_dummy_fully_charged,
+ staff_user,
):
fulfilled_order.payments.add(payment_dummy_fully_charged)
payment = fulfilled_order.get_last_payment()
@@ -371,6 +415,7 @@ def test_create_return_fulfillment_only_fulfillment_lines_replace_order(
returned_fulfillment, replaced_fulfillment, replace_order = response
+ flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillment.lines.all()
assert returned_fulfillment.status == FulfillmentStatus.RETURNED
# make sure that all order lines from refund are in expected fulfillment
@@ -436,10 +481,14 @@ def test_create_return_fulfillment_only_fulfillment_lines_replace_order(
)
assert replaced_line.tax_rate == expected_replaced_line.tax_rate
+ mocked_order_updated.assert_called_once_with(fulfilled_order)
+
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_return_fulfillment_with_lines_already_refunded(
mocked_refund,
+ mocked_order_updated,
fulfilled_order,
payment_dummy_fully_charged,
staff_user,
@@ -502,6 +551,7 @@ def test_create_return_fulfillment_with_lines_already_refunded(
refund=True,
)
+ flush_post_commit_hooks()
returned_and_refunded_fulfillment = Fulfillment.objects.get(
order=fulfilled_order, status=FulfillmentStatus.REFUNDED_AND_RETURNED
)
@@ -523,3 +573,5 @@ def test_create_return_fulfillment_with_lines_already_refunded(
]
)
mocked_refund.assert_called_once_with(payment_dummy_fully_charged, ANY, amount)
+
+ mocked_order_updated.assert_called_once_with(fulfilled_order)
diff --git a/saleor/order/tests/test_order_actions_refund_products.py b/saleor/order/tests/test_order_actions_refund_products.py
index a6eb47aa5a..0392976318 100644
--- a/saleor/order/tests/test_order_actions_refund_products.py
+++ b/saleor/order/tests/test_order_actions_refund_products.py
@@ -3,15 +3,17 @@ from unittest.mock import ANY, patch
from ...payment import ChargeStatus
from ...plugins.manager import get_plugins_manager
+from ...tests.utils import flush_post_commit_hooks
from ...warehouse.models import Allocation
from .. import FulfillmentLineData, FulfillmentStatus, OrderLineData
from ..actions import create_refund_fulfillment
from ..models import FulfillmentLine
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_refund_fulfillment_only_order_lines(
- mocked_refund, order_with_lines, payment_dummy
+ mocked_refund, mocked_order_updated, order_with_lines, payment_dummy
):
payment_dummy.captured_amount = payment_dummy.total
payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
@@ -40,6 +42,7 @@ def test_create_refund_fulfillment_only_order_lines(
manager=get_plugins_manager(),
)
+ flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillemnt.lines.all()
assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED
assert len(returned_fulfillment_lines) == lines_count
@@ -60,11 +63,13 @@ def test_create_refund_fulfillment_only_order_lines(
)
amount = sum([line.unit_price_gross_amount * 2 for line in order_lines_to_refund])
mocked_refund.assert_called_once_with(payment_dummy, ANY, amount)
+ mocked_order_updated.assert_called_once_with(order_with_lines)
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_refund_fulfillment_included_shipping_costs(
- mocked_refund, order_with_lines, payment_dummy
+ mocked_refund, mocked_order_updated, order_with_lines, payment_dummy
):
payment_dummy.captured_amount = payment_dummy.total
payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
@@ -89,6 +94,8 @@ def test_create_refund_fulfillment_included_shipping_costs(
manager=get_plugins_manager(),
refund_shipping_costs=True,
)
+
+ flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillemnt.lines.all()
assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED
assert len(returned_fulfillment_lines) == lines_count
@@ -100,11 +107,13 @@ def test_create_refund_fulfillment_included_shipping_costs(
amount = sum([line.unit_price_gross_amount * 2 for line in order_lines_to_refund])
amount += order_with_lines.shipping_price_gross_amount
mocked_refund.assert_called_once_with(payment_dummy, ANY, amount)
+ mocked_order_updated.assert_called_once_with(order_with_lines)
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_refund_fulfillment_only_fulfillment_lines(
- mocked_refund, fulfilled_order, payment_dummy
+ mocked_refund, mocked_order_updated, fulfilled_order, payment_dummy
):
payment_dummy.captured_amount = payment_dummy.total
payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
@@ -127,6 +136,8 @@ def test_create_refund_fulfillment_only_fulfillment_lines(
],
manager=get_plugins_manager(),
)
+
+ flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillemnt.lines.all()
assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED
assert len(returned_fulfillment_lines) == len(order_line_ids)
@@ -140,11 +151,13 @@ def test_create_refund_fulfillment_only_fulfillment_lines(
[line.order_line.unit_price_gross_amount * 2 for line in fulfillment_lines]
)
mocked_refund.assert_called_once_with(payment_dummy, ANY, amount)
+ mocked_order_updated.assert_called_once_with(fulfilled_order)
+@patch("saleor.plugins.manager.PluginsManager.order_updated")
@patch("saleor.order.actions.gateway.refund")
def test_create_refund_fulfillment_custom_amount(
- mocked_refund, fulfilled_order, payment_dummy
+ mocked_refund, mocked_order_updated, fulfilled_order, payment_dummy
):
payment_dummy.captured_amount = payment_dummy.total
payment_dummy.charge_status = ChargeStatus.FULLY_CHARGED
@@ -170,6 +183,7 @@ def test_create_refund_fulfillment_custom_amount(
amount=amount,
)
+ flush_post_commit_hooks()
returned_fulfillment_lines = returned_fulfillemnt.lines.all()
assert returned_fulfillemnt.status == FulfillmentStatus.REFUNDED
assert len(returned_fulfillment_lines) == len(order_line_ids)
@@ -180,3 +194,4 @@ def test_create_refund_fulfillment_custom_amount(
for line in fulfillment_lines:
assert line.quantity == original_quantity.get(line.pk) - 2
mocked_refund.assert_called_once_with(payment_dummy, ANY, amount)
+ mocked_order_updated.assert_called_once_with(fulfilled_order)