Skip to content

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

Refactor

Mateusz Grzyb3e0d6bbefc1a
diff --git a/saleor/graphql/order/tests/test_fulfillment_refund_products.py b/saleor/graphql/order/tests/test_fulfillment_refund_products.py
index 1d44f033f9..b4208bd477 100644
--- a/saleor/graphql/order/tests/test_fulfillment_refund_products.py
+++ b/saleor/graphql/order/tests/test_fulfillment_refund_products.py
@@ -164,8 +164,8 @@ def test_fulfillment_refund_products_amount_and_shipping_costs(
amount=quantize_price(amount_to_refund, fulfilled_order.currency),
channel_slug=fulfilled_order.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=[],
- fulfillment_lines_to_refund=[],
+ refund_shipping_costs=True,
+ refund_amount_is_automatically_calculated=False,
),
)
@@ -284,7 +284,6 @@ def test_fulfillment_refund_products_order_lines(
variant=line_to_refund.variant,
)
],
- fulfillment_lines_to_refund=[],
),
)
@@ -395,7 +394,6 @@ def test_fulfillment_refund_products_fulfillment_lines(
amount=fulfillment_line_to_refund.order_line.unit_price_gross_amount * 2,
channel_slug=fulfilled_order.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=[],
fulfillment_lines_to_refund=[
FulfillmentLineData(
line=fulfillment_line_to_refund,
@@ -458,7 +456,6 @@ def test_fulfillment_refund_products_waiting_fulfillment_lines(
amount=fulfillment_line_to_refund.order_line.unit_price_gross_amount * 3,
channel_slug=fulfilled_order.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=[],
fulfillment_lines_to_refund=[
FulfillmentLineData(
line=fulfillment_line_to_refund,
@@ -630,10 +627,10 @@ def test_fulfillment_refund_products_fulfillment_lines_include_shipping_costs(
amount=amount,
channel_slug=fulfilled_order.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=[],
fulfillment_lines_to_refund=[
FulfillmentLineData(line=fulfillment_line_to_refund, quantity=2)
],
+ refund_shipping_costs=True,
),
)
@@ -685,7 +682,7 @@ def test_fulfillment_refund_products_order_lines_include_shipping_costs(
line=line_to_refund, quantity=2, variant=line_to_refund.variant
)
],
- fulfillment_lines_to_refund=[],
+ refund_shipping_costs=True,
),
)
@@ -739,13 +736,13 @@ def test_fulfillment_refund_products_fulfillment_lines_custom_amount(
amount=amount_to_refund,
channel_slug=fulfilled_order.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=[],
fulfillment_lines_to_refund=[
FulfillmentLineData(
line=fulfillment_line_to_refund,
quantity=2,
)
],
+ refund_amount_is_automatically_calculated=False,
),
)
@@ -796,7 +793,7 @@ def test_fulfillment_refund_products_order_lines_custom_amount(
line=line_to_refund, quantity=2, variant=line_to_refund.variant
)
],
- fulfillment_lines_to_refund=[],
+ refund_amount_is_automatically_calculated=False,
),
)
diff --git a/saleor/graphql/order/tests/test_fulfillment_return_products.py b/saleor/graphql/order/tests/test_fulfillment_return_products.py
index a7c9fe49d6..b930d2cc25 100644
--- a/saleor/graphql/order/tests/test_fulfillment_return_products.py
+++ b/saleor/graphql/order/tests/test_fulfillment_return_products.py
@@ -117,8 +117,8 @@ def test_fulfillment_return_products_amount_and_shipping_costs(
amount=quantize_price(amount_to_refund, fulfilled_order.currency),
channel_slug=fulfilled_order.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=[],
- fulfillment_lines_to_refund=[],
+ refund_shipping_costs=True,
+ refund_amount_is_automatically_calculated=False,
),
)
@@ -317,7 +317,7 @@ def test_fulfillment_return_products_order_lines(
variant=line_to_return.variant,
),
],
- fulfillment_lines_to_refund=[],
+ refund_shipping_costs=True,
),
)
@@ -484,7 +484,7 @@ def test_fulfillment_return_products_order_lines_custom_amount(
variant=line_to_return.variant,
)
],
- fulfillment_lines_to_refund=[],
+ refund_amount_is_automatically_calculated=False,
),
)
@@ -612,13 +612,13 @@ def test_fulfillment_return_products_fulfillment_lines(
amount=amount,
channel_slug=fulfilled_order.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=[],
fulfillment_lines_to_refund=[
FulfillmentLineData(
line=fulfillment_line_to_return,
quantity=quantity_to_return,
),
],
+ refund_shipping_costs=True,
),
)
@@ -843,10 +843,10 @@ def test_fulfillment_return_products_fulfillment_lines_include_shipping_costs(
amount=amount,
channel_slug=fulfilled_order.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=[],
fulfillment_lines_to_refund=[
FulfillmentLineData(line=fulfillment_line_to_return, quantity=2)
],
+ refund_shipping_costs=True,
),
)
@@ -967,6 +967,5 @@ def test_fulfillment_return_products_fulfillment_lines_and_order_lines(
variant=variant,
)
],
- fulfillment_lines_to_refund=[],
),
)
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index 9c78a9b2e6..c6046d84f7 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -1449,7 +1449,7 @@ def _process_refund(
order_lines_to_refund=order_lines_to_refund,
fulfillment_lines_to_refund=fulfillment_lines_to_refund,
refund_shipping_costs=refund_shipping_costs,
- amount=amount,
+ refund_amount_is_automatically_calculated=amount is None,
)
if amount is None:
amount = _calculate_refund_amount(
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 a6e63e933f..dfb7ce58f2 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
@@ -152,7 +152,7 @@ def test_create_return_fulfillment_only_order_lines_with_refund(
amount=amount,
channel_slug=order_with_lines.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=order_lines_to_refund, fulfillment_lines_to_refund=[]
+ order_lines_to_refund=order_lines_to_refund,
),
)
assert not replace_order
@@ -231,7 +231,7 @@ def test_create_return_fulfillment_only_order_lines_included_shipping_costs(
channel_slug=order_with_lines.channel.slug,
refund_data=RefundData(
order_lines_to_refund=order_lines_to_refund,
- fulfillment_lines_to_refund=[],
+ refund_shipping_costs=True,
),
)
assert not replace_order
@@ -629,7 +629,6 @@ def test_create_return_fulfillment_with_lines_already_refunded(
amount=amount,
channel_slug=fulfilled_order.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=[],
fulfillment_lines_to_refund=fulfillment_lines_to_process,
),
)
diff --git a/saleor/order/tests/test_order_actions_refund_products.py b/saleor/order/tests/test_order_actions_refund_products.py
index 4e7631093a..29ceb947d6 100644
--- a/saleor/order/tests/test_order_actions_refund_products.py
+++ b/saleor/order/tests/test_order_actions_refund_products.py
@@ -76,7 +76,7 @@ def test_create_refund_fulfillment_only_order_lines(
amount=amount,
channel_slug=order_with_lines.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=order_refund_lines, fulfillment_lines_to_refund=[]
+ order_lines_to_refund=order_refund_lines,
),
)
mocked_order_updated.assert_called_once_with(order_with_lines)
@@ -137,7 +137,8 @@ def test_create_refund_fulfillment_included_shipping_costs(
amount=amount,
channel_slug=order_with_lines.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=order_refund_lines, fulfillment_lines_to_refund=[]
+ order_lines_to_refund=order_refund_lines,
+ refund_shipping_costs=True,
),
)
mocked_order_updated.assert_called_once_with(order_with_lines)
@@ -191,7 +192,6 @@ def test_create_refund_fulfillment_only_fulfillment_lines(
amount=amount,
channel_slug=fulfilled_order.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=[],
fulfillment_lines_to_refund=fulfillment_refund_lines,
),
)
@@ -248,8 +248,8 @@ def test_create_refund_fulfillment_custom_amount(
amount=amount,
channel_slug=fulfilled_order.channel.slug,
refund_data=RefundData(
- order_lines_to_refund=[],
fulfillment_lines_to_refund=fulfillment_refund_lines,
+ refund_amount_is_automatically_calculated=False,
),
)
mocked_order_updated.assert_called_once_with(fulfilled_order)
diff --git a/saleor/payment/gateways/np_atobarai/tests/test_api.py b/saleor/payment/gateways/np_atobarai/tests/test_api.py
index 5795cb9e90..ee8525b57f 100644
--- a/saleor/payment/gateways/np_atobarai/tests/test_api.py
+++ b/saleor/payment/gateways/np_atobarai/tests/test_api.py
@@ -51,7 +51,7 @@ def test_refund_payment_no_order(np_atobarai_plugin, np_payment_data, payment_du
payment_dummy.save(update_fields=["order", "captured_amount"])
# then
- with pytest.raises(PaymentError, match=rf"Order.*{payment_dummy.id}.*"):
+ with pytest.raises(PaymentError):
# when
plugin.refund_payment(payment_data, None)
@@ -510,7 +510,9 @@ def test_change_transaction_success(
mocked_request.return_value = response
# when
- payment_response = api.change_transaction(config, payment_dummy, np_payment_data)
+ payment_response = api.change_transaction(
+ config, payment_dummy, np_payment_data, Mock(), Decimal("12.34")
+ )
# then
assert payment_response.status == PaymentStatus.SUCCESS
@@ -544,7 +546,9 @@ def test_change_transaction_pending(
mocked_request.return_value = response
# when
- payment_response = api.change_transaction(config, payment_dummy, np_payment_data)
+ payment_response = api.change_transaction(
+ config, payment_dummy, np_payment_data, Mock(), Decimal("12.34")
+ )
# then
mocked_cancel.assert_called_once_with(config, transaction_id)
@@ -564,7 +568,9 @@ def test_change_transaction_post_fulfillment(
mocked_request.return_value = response
# when
- payment_response = api.change_transaction(config, payment_dummy, np_payment_data)
+ payment_response = api.change_transaction(
+ config, payment_dummy, np_payment_data, Mock(), Decimal("12.34")
+ )
# then
assert payment_response.status == PaymentStatus.FOR_REREGISTRATION
@@ -583,7 +589,9 @@ def test_change_transaction_failed(
mocked_request.return_value = response
# when
- payment_response = api.change_transaction(config, payment_dummy, np_payment_data)
+ payment_response = api.change_transaction(
+ config, payment_dummy, np_payment_data, Mock(), Decimal("12.34")
+ )
# then
assert payment_response.status == PaymentStatus.FAILED
@@ -616,22 +624,26 @@ def test_reregister_transaction_success(
mocked_report.return_value = NPResponse(result={}, error_codes=[])
# when
+ goods, billed_amount = get_goods_with_refunds(
+ config, payment_dummy, np_payment_data
+ )
payment_response = api.reregister_transaction_for_partial_return(
config,
payment_dummy,
np_payment_data,
shipping_company_code,
tracking_number,
+ goods,
+ billed_amount,
)
# then
mocked_cancel.assert_called_once_with(config, payment_dummy.psp_reference)
- billed_amount = format_price(
- payment_dummy.captured_amount - np_payment_data.amount, np_payment_data.currency
- )
- goods = get_goods_with_refunds(config, payment_dummy, np_payment_data)
mocked_register.assert_called_once_with(
- config, np_payment_data, billed_amount, goods
+ config,
+ np_payment_data,
+ int(format_price(billed_amount, np_payment_data.currency)),
+ goods,
)
mocked_report.assert_called_once_with(
config, shipping_company_code, new_psp_reference, tracking_number
@@ -643,7 +655,13 @@ def test_reregister_transaction_success(
def test_reregister_transaction_no_psp_reference(payment_dummy, np_payment_data):
# when
payment_response = api.reregister_transaction_for_partial_return(
- Mock(), payment_dummy, np_payment_data, Mock(), Mock()
+ Mock(),
+ payment_dummy,
+ np_payment_data,
+ Mock(),
+ Mock(),
+ Mock(),
+ Decimal("12.34"),
)
# then
@@ -662,7 +680,7 @@ def test_reregister_transaction_cancel_error(
# when
payment_response = api.reregister_transaction_for_partial_return(
- config, payment_dummy, np_payment_data, Mock(), Mock()
+ config, payment_dummy, np_payment_data, Mock(), Mock(), Mock(), Decimal("12.34")
)
# then
@@ -703,6 +721,8 @@ def test_reregister_transaction_report_error(
np_payment_data,
shipping_company_code,
tracking_number,
+ Mock(),
+ Decimal("12.34"),
)
# then
@@ -727,11 +747,7 @@ def test_reregister_transaction_general_error(
# when
payment_response = api.reregister_transaction_for_partial_return(
- config,
- payment_dummy,
- np_payment_data,
- Mock(),
- Mock(),
+ config, payment_dummy, np_payment_data, Mock(), Mock(), Mock(), Decimal("12.34")
)
# then
diff --git a/saleor/payment/gateways/np_atobarai/tests/test_api_helpers.py b/saleor/payment/gateways/np_atobarai/tests/test_api_helpers.py
index d8c218be9b..feb776f1a1 100644
--- a/saleor/payment/gateways/np_atobarai/tests/test_api_helpers.py
+++ b/saleor/payment/gateways/np_atobarai/tests/test_api_helpers.py
@@ -5,7 +5,7 @@ from unittest.mock import DEFAULT, Mock, patch, sentinel
import pytest
from posuto import Posuto
-from ....interface import AddressData
+from ....interface import AddressData, RefundData
from ....utils import price_to_minor_unit
from .. import api_helpers, errors
from ..api_helpers import get_goods, get_goods_with_refunds
@@ -215,9 +215,14 @@ def test_get_goods_with_refunds(
# given
config.sku_as_name = sku_as_name
np_payment_data.amount = refund_amount
+ np_payment_data.refund_data = RefundData(
+ refund_amount_is_automatically_calculated=False
+ )
# when
- goods = get_goods_with_refunds(config, payment_dummy, np_payment_data)
+ goods, billed_amount = get_goods_with_refunds(
+ config, payment_dummy, np_payment_data
+ )
# then
assert (
@@ -256,3 +261,11 @@ def test_get_goods_with_refunds(
]
+ discount_goods
)
+ manual_refund_amount = refund_amount or Decimal("0.00")
+ assert (
+ billed_amount
+ == sum(line.amount * line.quantity for line in np_payment_data.lines_data.lines)
+ + np_payment_data.lines_data.voucher_amount
+ + np_payment_data.lines_data.shipping_amount
+ - manual_refund_amount
+ )
diff --git a/saleor/payment/gateways/np_atobarai/tests/test_utils.py b/saleor/payment/gateways/np_atobarai/tests/test_utils.py
index ee3aa08361..eec4736263 100644
--- a/saleor/payment/gateways/np_atobarai/tests/test_utils.py
+++ b/saleor/payment/gateways/np_atobarai/tests/test_utils.py
@@ -340,23 +340,69 @@ def test_create_refund_data_previously_refunded_fulfillment_lines(
@pytest.mark.parametrize(
- "current_manual_refund_amount, expected_manual_amount",
[
- (None, Decimal("0.00")),
- (Decimal("0.00"), Decimal("0.00")),
- (Decimal("12.34"), Decimal("12.34")),
+ "current_refund_amount",
+ "shipping_amount",
+ "refund_shipping",
+ "is_manual",
+ "order_lines",
+ "fulfillment_lines",
+ "expected_manual_amount",
+ ],
+ [
+ (None, Decimal("40.00"), False, False, [], [], Decimal("0.00")),
+ (Decimal("12.34"), Decimal("40.00"), False, False, [], [], Decimal("12.34")),
+ (None, Decimal("40.00"), True, True, [], [], Decimal("40.00")),
+ (
+ Decimal("12.34"),
+ Decimal("40.00"),
+ False,
+ False,
+ [Mock()],
+ [],
+ Decimal("0.00"),
+ ),
+ (
+ Decimal("12.34"),
+ Decimal("40.00"),
+ False,
+ False,
+ [],
+ [Mock()],
+ Decimal("0.00"),
+ ),
],
)
def test_calculate_refund_amount(
order,
- current_manual_refund_amount,
+ current_refund_amount,
+ shipping_amount,
+ refund_shipping,
+ is_manual,
+ order_lines,
+ fulfillment_lines,
expected_manual_amount,
):
- assert (
- calculate_manual_refund_amount(order, Mock(amount=current_manual_refund_amount))
- == expected_manual_amount
+ # given
+ payment_information = Mock(
+ amount=current_refund_amount,
+ lines_data=Mock(
+ shipping_amount=shipping_amount,
+ ),
+ )
+ refund_data = Mock(
+ refund_shipping_costs=refund_shipping,
+ refund_is_automatically_calculated=not is_manual,
+ order_lines_to_refund=order_lines,
+ fulfillment_lines_to_refund=fulfillment_lines,
)
+ # when
+ amount = calculate_manual_refund_amount(order, payment_information, refund_data)
+
+ # then
+ assert amount == expected_manual_amount
+
@pytest.mark.parametrize(
[
@@ -395,9 +441,17 @@ def test_calculate_manual_refund_amount_previously_refunded(
refund_shipping_costs=refund_shipping,
)
+ refund_data = Mock(
+ refund_shipping_costs=False,
+ refund_is_automatically_calculated=False,
+ order_lines_to_refund=[],
+ fulfillment_lines_to_refund=[],
+ )
+ payment_information = Mock(amount=current_refund_amount)
+
# when
refund_amount = calculate_manual_refund_amount(
- order, Mock(amount=current_refund_amount)
+ order, payment_information, refund_data
)
# then
assert refund_amount == expected_refund_amount
diff --git a/saleor/payment/gateways/np_atobarai/utils.py b/saleor/payment/gateways/np_atobarai/utils.py
index 610591b41b..4ba5fd3060 100644
--- a/saleor/payment/gateways/np_atobarai/utils.py
+++ b/saleor/payment/gateways/np_atobarai/utils.py
@@ -77,7 +77,7 @@ def create_refunded_lines(
Takes into account previous refunds and current refund mutation parameters.
:return: Dictionary of variant ids and refunded quantities.
"""
- if refund_data.amount:
+ if not refund_data.refund_amount_is_automatically_calculated:
order_lines_to_refund = []
fulfillment_lines_to_refund = []
else:
@@ -133,10 +133,20 @@ def calculate_manual_refund_amount(
Takes into account previous refunds and current refund mutation amount.
"""
- if not refund_data.amount and refund_data.refund_shipping_costs:
+ if (
+ refund_data.order_lines_to_refund or refund_data.fulfillment_lines_to_refund
+ ) and refund_data.refund_amount_is_automatically_calculated:
+ # automatic line refund
+ manual_amount_to_refund = Decimal("0.00")
+ elif (
+ refund_data.refund_shipping_costs
+ and refund_data.refund_amount_is_automatically_calculated
+ ):
+ # automatic shipping refund
manual_amount_to_refund = payment_information.lines_data.shipping_amount
else:
- manual_amount_to_refund = refund_data.amount or Decimal("0.00")
+ # manual refund
+ manual_amount_to_refund = payment_information.amount or Decimal("0.00")
previous_manual_amount_to_refund = (
order.fulfillments.filter(
@@ -144,7 +154,7 @@ def calculate_manual_refund_amount(
FulfillmentStatus.REFUNDED,
FulfillmentStatus.REFUNDED_AND_RETURNED,
],
- lines__order_line__variant_id__isnull=True,
+ lines__isnull=True,
).aggregate(manual_amount=Sum("total_refund_amount"))["manual_amount"]
or Decimal("0.00")
)
diff --git a/saleor/payment/interface.py b/saleor/payment/interface.py
index e31d16436a..184e483085 100644
--- a/saleor/payment/interface.py
+++ b/saleor/payment/interface.py
@@ -91,7 +91,7 @@ class RefundData:
order_lines_to_refund: List[OrderLineInfo] = field(default_factory=list)
fulfillment_lines_to_refund: List[FulfillmentLineData] = field(default_factory=list)
refund_shipping_costs: bool = False
- amount: Optional[Decimal] = None
+ refund_amount_is_automatically_calculated: bool = True
@dataclass