Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix multiple shipping refunds
Mateusz Grzyb0e0901a9fccd
diff --git a/saleor/graphql/order/tests/test_fulfillment_refund_products.py b/saleor/graphql/order/tests/test_fulfillment_refund_products.pyindex 36b2754c4c..1d44f033f9 100644--- a/saleor/graphql/order/tests/test_fulfillment_refund_products.py+++ b/saleor/graphql/order/tests/test_fulfillment_refund_products.py@@ -1,13 +1,16 @@from decimal import Decimal-from unittest.mock import ANY, Mock, patch, sentinel+from unittest.mock import ANY, patchimport graphenefrom prices import Money, TaxedMoneyfrom ....core.prices import quantize_price+from ....order import FulfillmentLineDatafrom ....order.error_codes import OrderErrorCode+from ....order.fetch import OrderLineInfofrom ....order.models import FulfillmentLine, FulfillmentStatusfrom ....payment import ChargeStatus, PaymentError+from ....payment.interface import RefundDatafrom ....warehouse.models import Allocation, Stockfrom ...tests.utils import get_graphql_content@@ -42,9 +45,6 @@ mutation OrderFulfillmentRefundProducts("""-REFUND_DATA = sentinel.REFUND_DATA--@patch("saleor.order.actions.gateway.refund")@patch("saleor.plugins.manager.PluginsManager.product_variant_back_in_stock")def test_fulfillment_refund_products_with_back_in_stock_webhook(@@ -137,7 +137,6 @@ def test_fulfillment_refund_products_order_without_payment(assert fulfillment is None-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_refund_products_amount_and_shipping_costs(mocked_refund,@@ -164,7 +163,10 @@ def test_fulfillment_refund_products_amount_and_shipping_costs(ANY,amount=quantize_price(amount_to_refund, fulfilled_order.currency),channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[],+ fulfillment_lines_to_refund=[],+ ),)@@ -238,7 +240,6 @@ def test_fulfillment_refund_products_refund_raising_payment_error(assert errors[0]["code"] == OrderErrorCode.CANNOT_REFUND.name-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_refund_products_order_lines(mocked_refund,@@ -275,7 +276,16 @@ def test_fulfillment_refund_products_order_lines(ANY,amount=line_to_refund.unit_price_gross_amount * 2,channel_slug=order_with_lines.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[+ OrderLineInfo(+ line=line_to_refund,+ quantity=2,+ variant=line_to_refund.variant,+ )+ ],+ fulfillment_lines_to_refund=[],+ ),)@@ -338,7 +348,6 @@ def test_fulfillment_refund_products_order_lines_quantity_bigger_than_unfulfilleassert refund_fulfillment is None-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_refund_products_fulfillment_lines(mocked_refund,@@ -385,11 +394,18 @@ def test_fulfillment_refund_products_fulfillment_lines(ANY,amount=fulfillment_line_to_refund.order_line.unit_price_gross_amount * 2,channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[],+ fulfillment_lines_to_refund=[+ FulfillmentLineData(+ line=fulfillment_line_to_refund,+ quantity=2,+ )+ ],+ ),)-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_refund_products_waiting_fulfillment_lines(mocked_refund,@@ -441,7 +457,15 @@ def test_fulfillment_refund_products_waiting_fulfillment_lines(ANY,amount=fulfillment_line_to_refund.order_line.unit_price_gross_amount * 3,channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[],+ fulfillment_lines_to_refund=[+ FulfillmentLineData(+ line=fulfillment_line_to_refund,+ quantity=3,+ )+ ],+ ),)@@ -556,7 +580,6 @@ def test_fulfillment_refund_products_amount_bigger_than_captured_amount(assert refund_fulfillment is None-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_refund_products_fulfillment_lines_include_shipping_costs(mocked_refund,@@ -606,11 +629,15 @@ def test_fulfillment_refund_products_fulfillment_lines_include_shipping_costs(ANY,amount=amount,channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[],+ fulfillment_lines_to_refund=[+ FulfillmentLineData(line=fulfillment_line_to_refund, quantity=2)+ ],+ ),)-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_refund_products_order_lines_include_shipping_costs(mocked_refund,@@ -652,11 +679,17 @@ def test_fulfillment_refund_products_order_lines_include_shipping_costs(ANY,amount=amount,channel_slug=order_with_lines.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[+ OrderLineInfo(+ line=line_to_refund, quantity=2, variant=line_to_refund.variant+ )+ ],+ fulfillment_lines_to_refund=[],+ ),)-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_refund_products_fulfillment_lines_custom_amount(mocked_refund,@@ -705,11 +738,18 @@ def test_fulfillment_refund_products_fulfillment_lines_custom_amount(ANY,amount=amount_to_refund,channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[],+ fulfillment_lines_to_refund=[+ FulfillmentLineData(+ line=fulfillment_line_to_refund,+ quantity=2,+ )+ ],+ ),)-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_refund_products_order_lines_custom_amount(mocked_refund,@@ -750,11 +790,17 @@ def test_fulfillment_refund_products_order_lines_custom_amount(ANY,amount=amount_to_refund,channel_slug=order_with_lines.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[+ OrderLineInfo(+ line=line_to_refund, quantity=2, variant=line_to_refund.variant+ )+ ],+ fulfillment_lines_to_refund=[],+ ),)-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_refund_products_fulfillment_lines_and_order_lines(mocked_refund,@@ -846,5 +892,15 @@ def test_fulfillment_refund_products_fulfillment_lines_and_order_lines(ANY,amount=amount,channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[+ OrderLineInfo(line=order_line, quantity=2, variant=order_line.variant)+ ],+ fulfillment_lines_to_refund=[+ FulfillmentLineData(+ line=fulfillment_line_to_refund,+ quantity=2,+ )+ ],+ ),)diff --git a/saleor/graphql/order/tests/test_fulfillment_return_products.py b/saleor/graphql/order/tests/test_fulfillment_return_products.pyindex 0d6fdb806a..a7c9fe49d6 100644--- a/saleor/graphql/order/tests/test_fulfillment_return_products.py+++ b/saleor/graphql/order/tests/test_fulfillment_return_products.py@@ -1,14 +1,16 @@from decimal import Decimal-from unittest.mock import ANY, Mock, patch, sentinel+from unittest.mock import ANY, patchimport graphenefrom prices import Money, TaxedMoneyfrom ....core.prices import quantize_price-from ....order import OrderOrigin, OrderStatus+from ....order import FulfillmentLineData, OrderOrigin, OrderStatusfrom ....order.error_codes import OrderErrorCode+from ....order.fetch import OrderLineInfofrom ....order.models import FulfillmentStatus, Orderfrom ....payment import ChargeStatus, PaymentError+from ....payment.interface import RefundDatafrom ....warehouse.models import Stockfrom ...tests.utils import get_graphql_content@@ -63,8 +65,6 @@ mutation OrderFulfillmentReturnProducts(}"""-REFUND_DATA = sentinel.REFUND_DATA-def test_fulfillment_return_products_order_without_payment(staff_api_client, permission_manage_orders, fulfilled_order@@ -86,7 +86,6 @@ def test_fulfillment_return_products_order_without_payment(assert fulfillment is None-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_return_products_amount_and_shipping_costs(mocked_refund,@@ -117,7 +116,10 @@ def test_fulfillment_return_products_amount_and_shipping_costs(ANY,amount=quantize_price(amount_to_refund, fulfilled_order.currency),channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[],+ fulfillment_lines_to_refund=[],+ ),)@@ -197,7 +199,6 @@ def test_fulfillment_return_products_refund_raising_payment_error(assert errors[0]["code"] == OrderErrorCode.CANNOT_REFUND.name-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_return_products_order_lines(mocked_refund,@@ -308,7 +309,16 @@ def test_fulfillment_return_products_order_lines(ANY,amount=amount,channel_slug=order_with_lines.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[+ OrderLineInfo(+ line=line_to_return,+ quantity=line_quantity_to_return,+ variant=line_to_return.variant,+ ),+ ],+ fulfillment_lines_to_refund=[],+ ),)@@ -423,7 +433,6 @@ def test_fulfillment_return_products_order_lines_quantity_bigger_than_unfulfilleassert return_fulfillment is None-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_return_products_order_lines_custom_amount(mocked_refund,@@ -467,11 +476,19 @@ def test_fulfillment_return_products_order_lines_custom_amount(ANY,amount=amount_to_refund,channel_slug=order_with_lines.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[+ OrderLineInfo(+ line=line_to_return,+ quantity=2,+ variant=line_to_return.variant,+ )+ ],+ fulfillment_lines_to_refund=[],+ ),)-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_return_products_fulfillment_lines(mocked_refund,@@ -594,7 +611,15 @@ def test_fulfillment_return_products_fulfillment_lines(ANY,amount=amount,channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[],+ fulfillment_lines_to_refund=[+ FulfillmentLineData(+ line=fulfillment_line_to_return,+ quantity=quantity_to_return,+ ),+ ],+ ),)@@ -765,7 +790,6 @@ def test_fulfillment_return_products_lines_with_incorrect_status(assert return_fulfillment is None-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_return_products_fulfillment_lines_include_shipping_costs(mocked_refund,@@ -818,11 +842,15 @@ def test_fulfillment_return_products_fulfillment_lines_include_shipping_costs(ANY,amount=amount,channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[],+ fulfillment_lines_to_refund=[+ FulfillmentLineData(line=fulfillment_line_to_return, quantity=2)+ ],+ ),)-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_fulfillment_return_products_fulfillment_lines_and_order_lines(mocked_refund,@@ -931,5 +959,14 @@ def test_fulfillment_return_products_fulfillment_lines_and_order_lines(ANY,amount=amount,channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[+ OrderLineInfo(+ line=order_line,+ quantity=2,+ variant=variant,+ )+ ],+ fulfillment_lines_to_refund=[],+ ),)diff --git a/saleor/order/actions.py b/saleor/order/actions.pyindex 2988b48930..ec29fcdb3f 100644--- a/saleor/order/actions.py+++ b/saleor/order/actions.py@@ -1003,7 +1003,6 @@ def create_refund_fulfillment(refund_shipping_costs=refund_shipping_costs,manager=manager,)-refunded_fulfillment = Fulfillment.objects.create(status=FulfillmentStatus.REFUNDED,order=order,@@ -1449,8 +1448,6 @@ def _process_refund(refund_data = RefundData(order_lines_to_refund=order_lines_to_refund,fulfillment_lines_to_refund=fulfillment_lines_to_refund,- refund_shipping_costs=refund_shipping_costs,- amount=amount,)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.pyindex 569fc80130..a6e63e933f 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@@ -1,8 +1,9 @@from decimal import Decimal-from unittest.mock import ANY, Mock, patch, sentinel+from unittest.mock import ANY, patchfrom prices import Money, TaxedMoney+from ...payment.interface import RefundDatafrom ...plugins.manager import get_plugins_managerfrom ...tests.utils import flush_post_commit_hooksfrom ...warehouse.models import Allocation, Stock@@ -11,8 +12,6 @@ from ..actions import create_fulfillments_for_returned_productsfrom ..fetch import OrderLineInfofrom ..models import Fulfillment, FulfillmentLine-REFUND_DATA = sentinel.REFUND_DATA-@patch("saleor.plugins.manager.PluginsManager.order_updated")@patch("saleor.order.actions.gateway.refund")@@ -89,7 +88,6 @@ def test_create_return_fulfillment_only_order_lines(@patch("saleor.plugins.manager.PluginsManager.order_updated")-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_create_return_fulfillment_only_order_lines_with_refund(mocked_refund,@@ -111,15 +109,16 @@ def test_create_return_fulfillment_only_order_lines_with_refund()lines_count = order_with_lines.lines.count()+ order_lines_to_refund = [+ OrderLineInfo(line=line, quantity=2, replace=False)+ for line in order_lines_to_return+ ]response = create_fulfillments_for_returned_products(user=staff_user,app=None,order=order_with_lines,payment=payment,- order_lines=[- OrderLineInfo(line=line, quantity=2, replace=False)- for line in order_lines_to_return- ],+ order_lines=order_lines_to_refund,fulfillment_lines=[],manager=get_plugins_manager(),refund=True,@@ -152,7 +151,9 @@ def test_create_return_fulfillment_only_order_lines_with_refund(ANY,amount=amount,channel_slug=order_with_lines.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=order_lines_to_refund, fulfillment_lines_to_refund=[]+ ),)assert not replace_order@@ -163,7 +164,6 @@ def test_create_return_fulfillment_only_order_lines_with_refund(@patch("saleor.plugins.manager.PluginsManager.order_updated")-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_create_return_fulfillment_only_order_lines_included_shipping_costs(mocked_refund,@@ -185,15 +185,16 @@ def test_create_return_fulfillment_only_order_lines_included_shipping_costs()lines_count = order_with_lines.lines.count()+ order_lines_to_refund = [+ OrderLineInfo(line=line, quantity=2, replace=False)+ for line in order_lines_to_return+ ]response = create_fulfillments_for_returned_products(user=staff_user,app=None,order=order_with_lines,payment=payment,- order_lines=[- OrderLineInfo(line=line, quantity=2, replace=False)- for line in order_lines_to_return- ],+ order_lines=order_lines_to_refund,fulfillment_lines=[],manager=get_plugins_manager(),refund=True,@@ -228,7 +229,10 @@ def test_create_return_fulfillment_only_order_lines_included_shipping_costs(ANY,amount=amount,channel_slug=order_with_lines.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=order_lines_to_refund,+ fulfillment_lines_to_refund=[],+ ),)assert not replace_order@@ -528,7 +532,6 @@ def test_create_return_fulfillment_only_fulfillment_lines_replace_order(@patch("saleor.plugins.manager.PluginsManager.order_updated")-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_create_return_fulfillment_with_lines_already_refunded(mocked_refund,@@ -625,7 +628,10 @@ def test_create_return_fulfillment_with_lines_already_refunded(ANY,amount=amount,channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[],+ fulfillment_lines_to_refund=fulfillment_lines_to_process,+ ),)assert returned_and_refunded_fulfillment.total_refund_amount == amountdiff --git a/saleor/order/tests/test_order_actions_refund_products.py b/saleor/order/tests/test_order_actions_refund_products.pyindex 62a7c7be8c..4e7631093a 100644--- a/saleor/order/tests/test_order_actions_refund_products.py+++ b/saleor/order/tests/test_order_actions_refund_products.py@@ -1,7 +1,8 @@from decimal import Decimal-from unittest.mock import ANY, Mock, patch, sentinel+from unittest.mock import ANY, patchfrom ...payment import ChargeStatus+from ...payment.interface import RefundDatafrom ...plugins.manager import get_plugins_managerfrom ...tests.utils import flush_post_commit_hooksfrom ...warehouse.models import Allocation@@ -10,11 +11,8 @@ from ..actions import create_refund_fulfillmentfrom ..fetch import OrderLineInfofrom ..models import FulfillmentLine-REFUND_DATA = sentinel.REFUND_DATA-@patch("saleor.plugins.manager.PluginsManager.order_updated")-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_create_refund_fulfillment_only_order_lines(mocked_refund, mocked_order_updated, order_with_lines, payment_dummy@@ -35,14 +33,15 @@ def test_create_refund_fulfillment_only_order_lines()lines_count = order_with_lines.lines.count()+ order_refund_lines = [+ OrderLineInfo(line=line, quantity=2) for line in order_lines_to_refund+ ]returned_fulfillemnt = create_refund_fulfillment(user=None,app=None,order=order_with_lines,payment=payment,- order_lines_to_refund=[- OrderLineInfo(line=line, quantity=2) for line in order_lines_to_refund- ],+ order_lines_to_refund=order_refund_lines,fulfillment_lines_to_refund=[],manager=get_plugins_manager(),)@@ -76,13 +75,14 @@ def test_create_refund_fulfillment_only_order_lines(ANY,amount=amount,channel_slug=order_with_lines.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=order_refund_lines, fulfillment_lines_to_refund=[]+ ),)mocked_order_updated.assert_called_once_with(order_with_lines)@patch("saleor.plugins.manager.PluginsManager.order_updated")-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_create_refund_fulfillment_included_shipping_costs(mocked_refund, mocked_order_updated, order_with_lines, payment_dummy@@ -99,14 +99,15 @@ def test_create_refund_fulfillment_included_shipping_costs(order_line_ids = order_lines_to_refund.values_list("id", flat=True)lines_count = order_with_lines.lines.count()+ order_refund_lines = [+ OrderLineInfo(line=line, quantity=2) for line in order_lines_to_refund+ ]returned_fulfillemnt = create_refund_fulfillment(user=None,app=None,order=order_with_lines,payment=payment,- order_lines_to_refund=[- OrderLineInfo(line=line, quantity=2) for line in order_lines_to_refund- ],+ order_lines_to_refund=order_refund_lines,fulfillment_lines_to_refund=[],manager=get_plugins_manager(),refund_shipping_costs=True,@@ -135,13 +136,14 @@ def test_create_refund_fulfillment_included_shipping_costs(ANY,amount=amount,channel_slug=order_with_lines.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=order_refund_lines, fulfillment_lines_to_refund=[]+ ),)mocked_order_updated.assert_called_once_with(order_with_lines)@patch("saleor.plugins.manager.PluginsManager.order_updated")-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_create_refund_fulfillment_only_fulfillment_lines(mocked_refund, mocked_order_updated, fulfilled_order, payment_dummy@@ -156,16 +158,17 @@ def test_create_refund_fulfillment_only_fulfillment_lines(original_quantity = {line.id: line.quantity for line in fulfillment_lines}fulfillment_lines_to_refund = fulfillment_lines+ fulfillment_refund_lines = [+ FulfillmentLineData(line=line, quantity=2)+ for line in fulfillment_lines_to_refund+ ]returned_fulfillemnt = create_refund_fulfillment(user=None,app=None,order=fulfilled_order,payment=payment,order_lines_to_refund=[],- fulfillment_lines_to_refund=[- FulfillmentLineData(line=line, quantity=2)- for line in fulfillment_lines_to_refund- ],+ fulfillment_lines_to_refund=fulfillment_refund_lines,manager=get_plugins_manager(),)@@ -187,7 +190,10 @@ def test_create_refund_fulfillment_only_fulfillment_lines(ANY,amount=amount,channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[],+ fulfillment_lines_to_refund=fulfillment_refund_lines,+ ),)mocked_order_updated.assert_called_once_with(fulfilled_order)@@ -196,7 +202,6 @@ def test_create_refund_fulfillment_only_fulfillment_lines(@patch("saleor.plugins.manager.PluginsManager.order_updated")-@patch("saleor.order.actions.create_refund_data", new=Mock(return_value=REFUND_DATA))@patch("saleor.order.actions.gateway.refund")def test_create_refund_fulfillment_custom_amount(mocked_refund, mocked_order_updated, fulfilled_order, payment_dummy@@ -212,16 +217,17 @@ def test_create_refund_fulfillment_custom_amount(fulfillment_lines_to_refund = fulfillment_linesamount = Decimal("10.00")+ fulfillment_refund_lines = [+ FulfillmentLineData(line=line, quantity=2)+ for line in fulfillment_lines_to_refund+ ]returned_fulfillemnt = create_refund_fulfillment(user=None,app=None,order=fulfilled_order,payment=payment,order_lines_to_refund=[],- fulfillment_lines_to_refund=[- FulfillmentLineData(line=line, quantity=2)- for line in fulfillment_lines_to_refund- ],+ fulfillment_lines_to_refund=fulfillment_refund_lines,manager=get_plugins_manager(),amount=amount,)@@ -241,7 +247,10 @@ def test_create_refund_fulfillment_custom_amount(ANY,amount=amount,channel_slug=fulfilled_order.channel.slug,- refund_data=REFUND_DATA,+ refund_data=RefundData(+ order_lines_to_refund=[],+ fulfillment_lines_to_refund=fulfillment_refund_lines,+ ),)mocked_order_updated.assert_called_once_with(fulfilled_order)diff --git a/saleor/payment/gateways/np_atobarai/api.py b/saleor/payment/gateways/np_atobarai/api.pyindex 0340e65c78..e81e2405b0 100644--- a/saleor/payment/gateways/np_atobarai/api.py+++ b/saleor/payment/gateways/np_atobarai/api.py@@ -100,14 +100,14 @@ def change_transaction(If the fulfillment was reported prior to changing given transaction,then no change is applied and payment status is set to FOR_REREGISTRATION."""- goods, billed_amount = get_goods_with_refunds(config, payment, payment_information)+ goods = get_goods_with_refunds(config, payment, payment_information)data = {"transactions": [{"np_transaction_id": payment.psp_reference,"billed_amount": format_price(- billed_amount,+ payment.captured_amount - payment_information.amount,payment_information.currency,),"goods": goods,@@ -175,7 +175,12 @@ def reregister_transaction_for_partial_return()return errors_payment_result(error_messages)- goods, billed_amount = get_goods_with_refunds(config, payment, payment_information)+ goods = get_goods_with_refunds(config, payment, payment_information)++ billed_amount = format_price(+ payment.captured_amount - payment_information.amount,+ payment_information.currency,+ )result, error_codes = register(config,diff --git a/saleor/payment/gateways/np_atobarai/api_helpers.py b/saleor/payment/gateways/np_atobarai/api_helpers.pyindex fe1041ab2c..242b09974c 100644--- a/saleor/payment/gateways/np_atobarai/api_helpers.py+++ b/saleor/payment/gateways/np_atobarai/api_helpers.py@@ -1,6 +1,6 @@import loggingfrom decimal import Decimal-from typing import TYPE_CHECKING, Iterable, List, Optional, Tuple+from typing import TYPE_CHECKING, Iterable, List, Optionalimport requestsfrom django.utils import timezone@@ -26,7 +26,6 @@ from .errors import ()from .utils import (calculate_manual_refund_amount,- calculate_refunded_shipping,create_refunded_lines,notify_dashboard,np_atobarai_opentracing_trace,@@ -143,7 +142,6 @@ def _get_goods_name(line: PaymentLineData, config: "ApiConfig") -> str:def _get_voucher_and_shipping_goods(config: "ApiConfig",payment_information: PaymentData,- refund_shipping: bool = False,) -> List[dict]:"""Convert voucher and shipping amount into NP Atobarai goods lines."""goods_lines = []@@ -159,7 +157,7 @@ def _get_voucher_and_shipping_goods(})shipping_amount = payment_information.lines_data.shipping_amount- if shipping_amount and not refund_shipping:+ if shipping_amount:goods_lines.append({"goods_name": "Shipping",@@ -177,7 +175,7 @@ def get_goods_with_refunds(config: "ApiConfig",payment: Payment,payment_information: PaymentData,-) -> Tuple[List[dict], Decimal]:+) -> List[dict]:"""Combine PaymentLinesData and RefundData into NP Atobarai's goods list.Used for payment updates.@@ -187,8 +185,6 @@ def get_goods_with_refunds(refund_data = payment_information.refund_data or RefundData(order_lines_to_refund=[],fulfillment_lines_to_refund=[],- refund_shipping_costs=False,- amount=None,)order = payment.order@@ -196,12 +192,7 @@ def get_goods_with_refunds(raise PaymentError("Cannot refund payment without order.")refunded_lines = create_refunded_lines(order, refund_data)- refunded_shipping = calculate_refunded_shipping(order, refund_data)- refunded_manual_amount = calculate_manual_refund_amount(- order, refund_data, payment_information- )-- total_order_amount = Decimal("0.00")+ refunded_manual_amount = calculate_manual_refund_amount(order, payment_information)for line in payment_information.lines_data.lines:quantity = line.quantity@@ -217,7 +208,7 @@ def get_goods_with_refunds(})- total_order_amount += line.amount * quantity+ goods_lines.extend(_get_voucher_and_shipping_goods(config, payment_information))if refunded_manual_amount:goods_lines.append(@@ -229,17 +220,8 @@ def get_goods_with_refunds("quantity": 1,})- total_order_amount -= refunded_manual_amount-- goods_lines.extend(- _get_voucher_and_shipping_goods(config, payment_information, refunded_shipping)- )- total_order_amount += payment_information.lines_data.voucher_amount- if not refunded_shipping:- total_order_amount += payment_information.lines_data.shipping_amount-- return goods_lines, total_order_amount+ return goods_linesdef get_goods(config: "ApiConfig", payment_information: PaymentData) -> List[dict]:@@ -265,11 +247,13 @@ def cancel(config: "ApiConfig", transaction_id: str) -> NPResponse:def register(config: "ApiConfig",payment_information: "PaymentData",- billed_amount: Optional[Decimal] = None,+ billed_amount: Optional[int] = None,goods: Optional[List[dict]] = None,) -> NPResponse:if billed_amount is None:- billed_amount = payment_information.amount+ billed_amount = format_price(+ payment_information.amount, payment_information.currency+ )if goods is None:goods = get_goods(config, payment_information)@@ -298,9 +282,7 @@ def register("shop_transaction_id": payment_information.payment_id,"shop_order_date": order_date,"settlement_type": NP_ATOBARAI,- "billed_amount": format_price(- billed_amount, payment_information.currency- ),+ "billed_amount": billed_amount,"customer": {"customer_name": format_name(billing),"company_name": billing.company_name,diff --git a/saleor/payment/gateways/np_atobarai/tests/test_api.py b/saleor/payment/gateways/np_atobarai/tests/test_api.pyindex a77894edc2..5795cb9e90 100644--- a/saleor/payment/gateways/np_atobarai/tests/test_api.py+++ b/saleor/payment/gateways/np_atobarai/tests/test_api.py@@ -7,7 +7,6 @@ import requestsfrom graphene import Nodefrom .... import PaymentError-from ....interface import RefundDatafrom .. import PaymentStatus, api, constfrom ..api_helpers import format_price, get_goods_with_refundsfrom ..api_types import NPResponse, PaymentResult@@ -591,40 +590,6 @@ def test_change_transaction_failed(assert payment_response.errors[email protected]("lines", [{}, {i: i for i in range(1, 4)}])[email protected]("shipping", [False, True])[email protected]("manual_amount", [Decimal("0.00"), Decimal("12.34")])-@patch("saleor.payment.gateways.np_atobarai.api.np_request")-def test_change_transaction_refunded_goods(- mocked_request,- config,- payment_dummy,- np_payment_data,- lines,- shipping,- manual_amount,-):- # given- np_payment_data.refund_data = RefundData(- lines=lines, shipping=shipping, manual_amount=manual_amount- )- mocked_request.return_value = NPResponse(- result={- "authori_result": "00",- "np_transaction_id": "123123123",- },- error_codes=[],- )-- # when- api.change_transaction(config, payment_dummy, np_payment_data)-- # then- data = mocked_request.call_args[1]["json"]- goods = data["transactions"][0]["goods"]- assert goods == get_goods_with_refunds(config, np_payment_data)--@patch("saleor.payment.gateways.np_atobarai.api.report")@patch("saleor.payment.gateways.np_atobarai.api.register")@patch("saleor.payment.gateways.np_atobarai.api.cancel")@@ -664,7 +629,7 @@ def test_reregister_transaction_success(billed_amount = format_price(payment_dummy.captured_amount - np_payment_data.amount, np_payment_data.currency)- goods = get_goods_with_refunds(config, np_payment_data)+ goods = get_goods_with_refunds(config, payment_dummy, np_payment_data)mocked_register.assert_called_once_with(config, np_payment_data, billed_amount, goods)diff --git a/saleor/payment/gateways/np_atobarai/tests/test_api_helpers.py b/saleor/payment/gateways/np_atobarai/tests/test_api_helpers.pyindex da473ddf7d..d8c218be9b 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, sentinelimport pytestfrom posuto import Posuto-from ....interface import AddressData, RefundData+from ....interface import AddressDatafrom ....utils import price_to_minor_unitfrom .. import api_helpers, errorsfrom ..api_helpers import get_goods, get_goods_with_refunds@@ -194,61 +194,30 @@ def test_get_goods(@pytest.mark.parametrize(- "manual_refund, discount_goods",+ "refund_amount, discount_goods",[- (Decimal("0.00"), []),+ (None, []),(- Decimal("12.34"),- [- {- "goods_name": "Discount",- "goods_price": -1234,- "quantity": 1,- }- ],- ),- ],-)- "refund_shipping, shipping_goods",- [- (True, []),- (- False,- [- {- "goods_name": "Shipping",- "goods_price": 12000,- "quantity": 1,- }- ],+ Decimal("5.00"),+ [{"goods_name": "Discount", "goods_price": -500, "quantity": 1}],),],)@pytest.mark.parametrize("sku_as_name", [True, False])- "refund_lines, quantities",- [({0: 1, 1: 2, 2: 3}, {0: 4, 1: 3, 2: 2}), ({}, {i: 5 for i in range(3)})],-)def test_get_goods_with_refunds(config,+ payment_dummy,np_payment_data,sku_as_name,- refund_shipping,- shipping_goods,- manual_refund,+ refund_amount,discount_goods,- refund_lines,- quantities,):# givenconfig.sku_as_name = sku_as_name- np_payment_data.refund_data = RefundData(- lines=refund_lines, shipping=refund_shipping, manual_amount=manual_refund- )+ np_payment_data.amount = refund_amount# when- goods = get_goods_with_refunds(config, np_payment_data)+ goods = get_goods_with_refunds(config, payment_dummy, np_payment_data)# thenassert (@@ -259,11 +228,10 @@ def test_get_goods_with_refunds("goods_price": int(price_to_minor_unit(line.amount, np_payment_data.currency)),- "quantity": quantities[line.variant_id],+ "quantity": line.quantity,}for line in np_payment_data.lines_data.lines]- + discount_goods+ [{"goods_name": "Voucher",@@ -275,6 +243,16 @@ def test_get_goods_with_refunds(),"quantity": 1,},+ {+ "goods_name": "Shipping",+ "goods_price": int(+ price_to_minor_unit(+ np_payment_data.lines_data.shipping_amount,+ np_payment_data.currency,+ )+ ),+ "quantity": 1,+ },]- + shipping_goods+ + discount_goods)diff --git a/saleor/payment/gateways/np_atobarai/tests/test_utils.py b/saleor/payment/gateways/np_atobarai/tests/test_utils.pyindex 6e4f4f35a9..c044b1812e 100644--- a/saleor/payment/gateways/np_atobarai/tests/test_utils.py+++ b/saleor/payment/gateways/np_atobarai/tests/test_utils.py@@ -1,9 +1,18 @@+from decimal import Decimal+from itertools import chain, zip_longest+from unittest.mock import Mock, patch+import pytest-from .....order import FulfillmentStatus, OrderEvents+from .....order import FulfillmentLineData, FulfillmentStatus, OrderEvents+from .....order.actions import create_refund_fulfillment+from .....order.fetch import OrderLineInfo+from .....plugins.manager import get_plugins_managerfrom .... import PaymentError+from ....interface import RefundDatafrom .. import get_fulfillment_for_order, get_shipping_company_code, notify_dashboardfrom ..const import SHIPPING_COMPANY_CODE_METADATA_KEY, SHIPPING_COMPANY_CODES+from ..utils import calculate_manual_refund_amount, create_refunded_linesdef test_notify_dashboard(order):@@ -138,3 +147,258 @@ def test_get_shipping_company_code_invalid_metadata(# thenassert shipping_company_code == config_shipping_company_code+++def create_refund_fulfillment_helper(payment_dummy):+ def factory(+ order,+ order_lines=None,+ fulfillment_lines=None,+ manual_refund_amount=None,+ refund_shipping_costs=False,+ ):+ if manual_refund_amount:+ payment_dummy.captured_amount = manual_refund_amount+ payment_dummy.save(update_fields=["captured_amount"])+ if refund_shipping_costs:+ payment_dummy.captured_amount = order.shipping_price_gross_amount+ payment_dummy.save(update_fields=["captured_amount"])++ with patch("saleor.order.actions.gateway.refund"):+ return create_refund_fulfillment(+ user=None,+ app=None,+ order=order,+ payment=payment_dummy,+ order_lines_to_refund=order_lines or [],+ fulfillment_lines_to_refund=fulfillment_lines or [],+ manager=get_plugins_manager(),+ amount=manual_refund_amount,+ refund_shipping_costs=refund_shipping_costs,+ )++ return factory+++def test_create_refund_lines_order_lines(order_with_lines):+ # given+ order_lines = order_with_lines.lines.all()+ order_refund_lines = [+ OrderLineInfo(line=(line := order_lines[0]), quantity=2, variant=line.variant),+ OrderLineInfo(line=(line := order_lines[1]), quantity=1, variant=line.variant),+ ]+ fulfillment_refund_lines = []+ refund_data = RefundData(+ order_lines_to_refund=order_refund_lines,+ fulfillment_lines_to_refund=fulfillment_refund_lines,+ )++ # when+ lines = create_refunded_lines(+ order_with_lines,+ refund_data,+ )++ # then+ assert lines == {line.line.variant_id: line.quantity for line in order_refund_lines}+++def test_create_refund_lines_fulfillment_lines(fulfilled_order):+ # given+ fulfillment_lines = fulfilled_order.fulfillments.first().lines.all()+ order_refund_lines = []+ fulfillment_refund_lines = [+ FulfillmentLineData(+ line=fulfillment_lines[0],+ quantity=2,+ ),+ FulfillmentLineData(+ line=fulfillment_lines[1],+ quantity=1,+ ),+ ]+ refund_data = RefundData(+ order_lines_to_refund=order_refund_lines,+ fulfillment_lines_to_refund=fulfillment_refund_lines,+ )++ # when+ lines = create_refunded_lines(+ fulfilled_order,+ refund_data,+ )++ # then+ assert lines == {+ line.line.order_line.variant_id: line.quantity+ for line in fulfillment_refund_lines+ }+++@patch("saleor.order.actions.gateway.refund")[email protected]("previous_refund_shipping_costs", [True, False])+def test_create_refund_data_previously_refunded_order_lines(+ _mocked_refund,+ order_with_lines,+ create_refund_fulfillment_helper,+ previous_refund_shipping_costs,+):+ # given+ order_lines = order_with_lines.lines.all()+ previous_order_refund_lines = [+ OrderLineInfo(line=(line := order_lines[0]), quantity=1, variant=line.variant)+ ]+ create_refund_fulfillment_helper(+ order_with_lines,+ order_lines=previous_order_refund_lines,+ refund_shipping_costs=previous_refund_shipping_costs,+ )+ current_order_refund_lines = [+ OrderLineInfo(line=(line := order_lines[0]), quantity=1, variant=line.variant),+ OrderLineInfo(line=(line := order_lines[1]), quantity=1, variant=line.variant),+ ]+ fulfillment_refund_lines = []+ refund_data = RefundData(+ order_lines_to_refund=current_order_refund_lines,+ fulfillment_lines_to_refund=fulfillment_refund_lines,+ )++ # when+ lines = create_refunded_lines(+ order_with_lines,+ refund_data,+ )++ # then+ order_refund_lines = [+ OrderLineInfo(line=cl.line, quantity=pl.quantity + cl.quantity)+ for pl, cl in zip_longest(+ previous_order_refund_lines,+ current_order_refund_lines,+ fillvalue=Mock(spec=OrderLineInfo, quantity=0),+ )+ ]+ assert lines == {line.line.variant_id: line.quantity for line in order_refund_lines}+++@patch("saleor.order.actions.gateway.refund")[email protected]("previous_refund_shipping_costs", [True, False])+def test_create_refund_data_previously_refunded_fulfillment_lines(+ _mocked_refund,+ fulfilled_order,+ create_refund_fulfillment_helper,+ previous_refund_shipping_costs,+):+ # given+ fulfillment_lines = list(+ chain.from_iterable(f.lines.all() for f in fulfilled_order.fulfillments.all())+ )+ previous_fulfillment_refund_lines = [+ FulfillmentLineData(line=fulfillment_lines[0], quantity=1)+ ]+ create_refund_fulfillment_helper(+ fulfilled_order,+ fulfillment_lines=previous_fulfillment_refund_lines,+ refund_shipping_costs=previous_refund_shipping_costs,+ )+ order_refund_lines = []+ current_fulfillment_refund_lines = [+ FulfillmentLineData(+ line=fulfillment_lines[0],+ quantity=1,+ ),+ FulfillmentLineData(+ line=fulfillment_lines[1],+ quantity=1,+ ),+ ]+ refund_data = RefundData(+ order_lines_to_refund=order_refund_lines,+ fulfillment_lines_to_refund=current_fulfillment_refund_lines,+ )++ # when+ lines = create_refunded_lines(+ fulfilled_order,+ refund_data,+ )++ # then+ fulfillment_refund_lines = [+ FulfillmentLineData(line=cl.line, quantity=pl.quantity + cl.quantity)+ for pl, cl in zip_longest(+ previous_fulfillment_refund_lines,+ current_fulfillment_refund_lines,+ fillvalue=Mock(spec=FulfillmentLineData, quantity=0),+ )+ ]+ assert lines == {+ line.line.order_line.variant_id: line.quantity+ for line in fulfillment_refund_lines+ }+++ "current_manual_refund_amount, expected_manual_amount",+ [+ (None, Decimal("0.00")),+ (Decimal("0.00"), Decimal("0.00")),+ (Decimal("12.34"), Decimal("12.34")),+ ],+)+def test_calculate_refund_amount(+ order,+ current_manual_refund_amount,+ expected_manual_amount,+):+ assert (+ calculate_manual_refund_amount(order, Mock(amount=current_manual_refund_amount))+ == expected_manual_amount+ )+++ [+ "previous_manual_refund_amount",+ "current_refund_amount",+ "refund_shipping",+ "expected_refund_amount",+ ],+ [+ (None, None, False, Decimal("0.00")),+ (None, None, True, Decimal("12.30")),+ (None, Decimal("8.00"), False, Decimal("8.00")),+ (None, Decimal("8.00"), True, Decimal("20.30")),+ (Decimal("5.00"), None, False, Decimal("5.00")),+ (Decimal("5.00"), None, True, Decimal("17.30")),+ (Decimal("5.00"), Decimal("8.00"), False, Decimal("13.00")),+ (Decimal("5.00"), Decimal("8.00"), True, Decimal("25.30")),+ ],+)+def test_calculate_manual_refund_amount_previously_refunded(+ order,+ create_refund_fulfillment_helper,+ previous_manual_refund_amount,+ current_refund_amount,+ refund_shipping,+ expected_refund_amount,+):+ # given+ if previous_manual_refund_amount:+ create_refund_fulfillment_helper(+ order, manual_refund_amount=previous_manual_refund_amount+ )+ if refund_shipping:+ create_refund_fulfillment_helper(+ order,+ refund_shipping_costs=refund_shipping,+ )++ # when+ refund_amount = calculate_manual_refund_amount(+ order, Mock(amount=current_refund_amount)+ )+ print(refund_amount, order.shipping_price_gross_amount)+ # then+ assert refund_amount == expected_refund_amountdiff --git a/saleor/payment/gateways/np_atobarai/utils.py b/saleor/payment/gateways/np_atobarai/utils.pyindex 0b2b09c682..c04cbf5f21 100644--- a/saleor/payment/gateways/np_atobarai/utils.py+++ b/saleor/payment/gateways/np_atobarai/utils.py@@ -119,50 +119,23 @@ def create_refunded_lines(def calculate_manual_refund_amount(order: Order,- refund_data: RefundData,payment_information: PaymentData,) -> Decimal:"""Return sum of all manual refunds for specified order.- Takes into account previous refunds and current refund mutation parameters.+ Takes into account previous refunds and current refund mutation amount."""- if (- not refund_data.order_lines_to_refund- and not refund_data.fulfillment_lines_to_refund- and not refund_data.refund_shipping_costs- and not refund_data.amount- ):- manual_amount_to_refund = payment_information.amount- else:- manual_amount_to_refund = refund_data.amount or Decimal("0.00")-- previous_manual_amount_to_refund = order.fulfillments.filter(- status__in=[- FulfillmentStatus.REFUNDED,- FulfillmentStatus.REFUNDED_AND_RETURNED,- ],- lines__order_line__variant_id__isnull=True,- shipping_refund_amount__isnull=True,- ).aggregate(manual_amount=Sum("total_refund_amount"))["manual_amount"]-- if previous_manual_amount_to_refund is None:- previous_manual_amount_to_refund = Decimal("0.00")- if manual_amount_to_refund is None:- manual_amount_to_refund = Decimal("0.00")+ manual_amount_to_refund = payment_information.amount or Decimal("0.00")++ previous_manual_amount_to_refund = (+ order.fulfillments.filter(+ status__in=[+ FulfillmentStatus.REFUNDED,+ FulfillmentStatus.REFUNDED_AND_RETURNED,+ ],+ lines__order_line__variant_id__isnull=True,+ ).aggregate(manual_amount=Sum("total_refund_amount"))["manual_amount"]+ or Decimal("0.00")+ )return previous_manual_amount_to_refund + manual_amount_to_refund---def calculate_refunded_shipping(- order: Order,- refund_data: RefundData,-) -> bool:- """Determine whether shipping is refunded for specified order.-- Takes into account previous refunds and current refund mutation parameters.- """- shipping_previously_refunded = order.fulfillments.exclude(- shipping_refund_amount__isnull=True- ).exists()-- return shipping_previously_refunded or refund_data.refund_shipping_costsdiff --git a/saleor/payment/interface.py b/saleor/payment/interface.pyindex 115747cd67..4aa9de8953 100644--- a/saleor/payment/interface.py+++ b/saleor/payment/interface.py@@ -90,8 +90,6 @@ class PaymentLinesData:class RefundData:order_lines_to_refund: List[OrderLineInfo]fulfillment_lines_to_refund: List[FulfillmentLineData]- refund_shipping_costs: bool- amount: Optional[Decimal]@dataclassdiff --git a/saleor/payment/tests/test_utils.py b/saleor/payment/tests/test_utils.pyindex fc8ae4a222..50cab7d7ca 100644--- a/saleor/payment/tests/test_utils.py+++ b/saleor/payment/tests/test_utils.py@@ -1,21 +1,8 @@from decimal import Decimal-from itertools import chain, zip_longest-from typing import List, Optional-from unittest.mock import Mock, patch--import pytest+from unittest.mock import patchfrom ...checkout.fetch import fetch_checkout_info, fetch_checkout_lines-from ...order import FulfillmentLineData-from ...order.actions import create_refund_fulfillment-from ...order.fetch import OrderLineInfo-from ...order.models import Orderfrom ...plugins.manager import get_plugins_manager-from ..gateways.np_atobarai.utils import (- calculate_manual_refund_amount,- calculate_refunded_shipping,- create_refunded_lines,-)from ..interface import PaymentLineData, PaymentLinesDatafrom ..utils import (create_payment_lines_information,@@ -24,268 +11,6 @@ from ..utils import ()-def create_refund_fulfillment_helper(payment_dummy):- def factory(- order: Order,- order_lines: List[OrderLineInfo] = None,- fulfillment_lines: List[FulfillmentLineData] = None,- manual_refund_amount: Optional[Decimal] = None,- refund_shipping_costs: bool = False,- ):- if manual_refund_amount:- payment_dummy.captured_amount = manual_refund_amount- payment_dummy.save(update_fields=["captured_amount"])- with patch("saleor.order.actions.gateway.refund"):- return create_refund_fulfillment(- user=None,- app=None,- order=order,- payment=payment_dummy,- order_lines_to_refund=order_lines or [],- fulfillment_lines_to_refund=fulfillment_lines or [],- manager=get_plugins_manager(),- amount=manual_refund_amount,- refund_shipping_costs=refund_shipping_costs,- )-- return factory---def test_create_refund_lines_order_lines(order_with_lines):- # given- order_lines = order_with_lines.lines.all()- order_refund_lines = [- OrderLineInfo(line=(line := order_lines[0]), quantity=2, variant=line.variant),- OrderLineInfo(line=(line := order_lines[1]), quantity=1, variant=line.variant),- ]- fulfillment_refund_lines = []-- # when- lines = create_refunded_lines(- order_with_lines,- order_refund_lines,- fulfillment_refund_lines,- )-- # then- assert lines == {line.line.variant_id: line.quantity for line in order_refund_lines}---def test_create_refund_lines_fulfillment_lines(fulfilled_order):- # given- fulfillment_lines = fulfilled_order.fulfillments.first().lines.all()- order_refund_lines = []- fulfillment_refund_lines = [- FulfillmentLineData(- line=fulfillment_lines[0],- quantity=2,- ),- FulfillmentLineData(- line=fulfillment_lines[1],- quantity=1,- ),- ]-- # when- lines = create_refunded_lines(- fulfilled_order,- order_refund_lines,- fulfillment_refund_lines,- )-- # then- assert lines == {- line.line.order_line.variant_id: line.quantity- for line in fulfillment_refund_lines- }---@patch("saleor.order.actions.gateway.refund")[email protected]("previous_refund_shipping_costs", [True, False])-def test_create_refund_data_previously_refunded_order_lines(- _mocked_refund,- order_with_lines,- create_refund_fulfillment_helper,- previous_refund_shipping_costs,-):- # given- order_lines = order_with_lines.lines.all()- previous_order_refund_lines = [- OrderLineInfo(line=(line := order_lines[0]), quantity=1, variant=line.variant)- ]- create_refund_fulfillment_helper(- order_with_lines,- order_lines=previous_order_refund_lines,- refund_shipping_costs=previous_refund_shipping_costs,- )- current_order_refund_lines = [- OrderLineInfo(line=(line := order_lines[0]), quantity=1, variant=line.variant),- OrderLineInfo(line=(line := order_lines[1]), quantity=1, variant=line.variant),- ]- fulfillment_refund_lines = []-- # when- lines = create_refunded_lines(- order_with_lines,- current_order_refund_lines,- fulfillment_refund_lines,- )-- # then- order_refund_lines = [- OrderLineInfo(line=cl.line, quantity=pl.quantity + cl.quantity)- for pl, cl in zip_longest(- previous_order_refund_lines,- current_order_refund_lines,- fillvalue=Mock(spec=OrderLineInfo, quantity=0),- )- ]- assert lines == {line.line.variant_id: line.quantity for line in order_refund_lines}---@patch("saleor.order.actions.gateway.refund")[email protected]("previous_refund_shipping_costs", [True, False])-def test_create_refund_data_previously_refunded_fulfillment_lines(- _mocked_refund,- fulfilled_order,- create_refund_fulfillment_helper,- previous_refund_shipping_costs,-):- # given- fulfillment_lines = list(- chain.from_iterable(f.lines.all() for f in fulfilled_order.fulfillments.all())- )- previous_fulfillment_refund_lines = [- FulfillmentLineData(line=fulfillment_lines[0], quantity=1)- ]- create_refund_fulfillment_helper(- fulfilled_order,- fulfillment_lines=previous_fulfillment_refund_lines,- refund_shipping_costs=previous_refund_shipping_costs,- )- order_refund_lines = []- current_fulfillment_refund_lines = [- FulfillmentLineData(- line=fulfillment_lines[0],- quantity=1,- ),- FulfillmentLineData(- line=fulfillment_lines[1],- quantity=1,- ),- ]-- # when- lines = create_refunded_lines(- fulfilled_order,- order_refund_lines,- current_fulfillment_refund_lines,- )-- # then- fulfillment_refund_lines = [- FulfillmentLineData(line=cl.line, quantity=pl.quantity + cl.quantity)- for pl, cl in zip_longest(- previous_fulfillment_refund_lines,- current_fulfillment_refund_lines,- fillvalue=Mock(spec=FulfillmentLineData, quantity=0),- )- ]- assert lines == {- line.line.order_line.variant_id: line.quantity- for line in fulfillment_refund_lines- }--- "current_manual_refund_amount, expected_manual_amount",- [- (None, Decimal("0.00")),- (Decimal("0.00"), Decimal("0.00")),- (Decimal("12.34"), Decimal("12.34")),- ],-)-def test_create_refund_manual_amount(- order,- current_manual_refund_amount,- expected_manual_amount,-):- assert (- calculate_manual_refund_amount(order, current_manual_refund_amount)- == expected_manual_amount- )--- "previous_manual_refund_amount", [None, Decimal("0.00"), Decimal("12.34")]-)- "current_manual_refund_amount", [None, Decimal("0.00"), Decimal("23.45")]-)-def test_create_refund_manual_amount_previously_refunded(- order,- create_refund_fulfillment_helper,- previous_manual_refund_amount,- current_manual_refund_amount,-):- # given- create_refund_fulfillment_helper(- order, manual_refund_amount=previous_manual_refund_amount- )-- # when- manual_amount = calculate_manual_refund_amount(order, current_manual_refund_amount)-- # then- expected_manual_amount = (previous_manual_refund_amount or Decimal("0.00")) + (- current_manual_refund_amount or Decimal("0.00")- )- assert manual_amount == expected_manual_amount--[email protected]("refund_shipping", [True, False])-def test_create_refund_shipping(order, refund_shipping):- assert calculate_refunded_shipping(order, refund_shipping) is refund_shipping---@patch("saleor.order.actions.gateway.refund")- [- "previous_refund_shipping_costs",- "current_refund_shipping_costs",- "refund_shipping_costs",- ],- [- (True, True, True),- (True, False, True),- (False, True, True),- (False, False, False),- ],-)-def test_create_refund_shipping_previously_refunded(- _mocked_refund,- order,- create_refund_fulfillment_helper,- previous_refund_shipping_costs,- current_refund_shipping_costs,- refund_shipping_costs,-):- # given- create_refund_fulfillment_helper(- order, refund_shipping_costs=previous_refund_shipping_costs- )-- # when- refund = calculate_refunded_shipping(- order,- current_refund_shipping_costs,- )-- # then- assert refund is refund_shipping_costs--def test_create_payment_lines_information_order(payment_dummy):# givenmanager = get_plugins_manager()diff --git a/saleor/payment/utils.py b/saleor/payment/utils.pyindex 897bff730f..1bdaf6c0e7 100644--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@ -79,7 +79,7 @@ def create_checkout_payment_lines_information(address,discounts,)- unit_gross = unit_price.undiscounted_price.gross.amount+ unit_gross = unit_price.price_with_sale.gross.amountquantity = line_info.line.quantityproduct_name = f"{line_info.variant.product.name}, {line_info.variant.name}"