Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Merge branch 'main' into feature/net-protections-3.1
Mateusz Grzyb3f3842c3282c
diff --cc requirements.txtindex 6147aa9f10,6c674c2dab..02162c7361--- a/requirements.txt+++ b/requirements.txt@@@ -89,16 -89,16 +89,17 @@@ mypy-extensions==0.4.3; python_versionoauthlib==3.1.1; python_version >= "3.6"openpyxl==3.0.9; python_version >= "3.6"opentracing==2.4.0+ packaging==21.3; python_version >= "3.6"petl==1.7.4; (python_version >= "2.7" and python_full_version < "3.0.0") or (python_full_version >= "3.4.0")- phonenumberslite==8.12.38- pillow==8.4.0; python_version >= "3.6"+ phonenumberslite==8.12.40+ pillow==9.0.0; python_version >= "3.7"+posuto==2021.10.0prices==1.1.0promise==2.3- prompt-toolkit==3.0.23; python_full_version >= "3.6.2" and python_version >= "3.6"+ prompt-toolkit==3.0.24; python_full_version >= "3.6.2" and python_version >= "3.6"proto-plus==1.19.8; python_version >= "3.6"protobuf==3.19.1; python_version >= "3.5"- psycopg2-binary==2.9.2; python_version >= "3.6"+ psycopg2-binary==2.9.3; python_version >= "3.6"pyasn1-modules==0.2.8; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.6.0" and python_version >= "3.6"pyasn1==0.4.8; python_version >= "3.6" and python_full_version < "3.0.0" and python_version < "4" and (python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.6.0" and python_version >= "3.6") or python_full_version >= "3.6.0" and python_version >= "3.6" and python_version < "4" and (python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.6.0" and python_version >= "3.6")pybars3==0.9.7diff --cc requirements_dev.txtindex 796451cff8,f471930f92..5c7f969b91--- a/requirements_dev.txt+++ b/requirements_dev.txt@@@ -123,18 -122,17 +122,18 @@@ opentracing==2.4.packaging==21.3; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.6"pathspec==0.9.0; python_full_version >= "3.6.2"petl==1.7.4; (python_version >= "2.7" and python_full_version < "3.0.0") or (python_full_version >= "3.4.0")- phonenumberslite==8.12.38- pillow==8.4.0; python_version >= "3.6"- platformdirs==2.4.0; python_version >= "3.6" and python_full_version >= "3.6.2"+ phonenumberslite==8.12.40+ pillow==9.0.0; python_version >= "3.7"+ platformdirs==2.4.1; python_version >= "3.7" and python_full_version >= "3.6.2"pluggy==1.0.0; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.6"+posuto==2021.10.0- pre-commit==2.15.0; python_full_version >= "3.6.1"+ pre-commit==2.16.0; python_full_version >= "3.6.1"prices==1.1.0promise==2.3; python_version >= "3.6" and python_version < "4.0"- prompt-toolkit==3.0.23; python_full_version >= "3.6.2" and python_version >= "3.6"+ prompt-toolkit==3.0.24; python_full_version >= "3.6.2" and python_version >= "3.6"proto-plus==1.19.8; python_version >= "3.6"protobuf==3.19.1; python_version >= "3.5"- psycopg2-binary==2.9.2; python_version >= "3.6"+ psycopg2-binary==2.9.3; python_version >= "3.6"py==1.11.0; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.5.0" and python_version >= "3.6"pyasn1-modules==0.2.8; python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.6.0" and python_version >= "3.6"pyasn1==0.4.8; python_version >= "3.6" and python_full_version < "3.0.0" and python_version < "4" and (python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.6.0" and python_version >= "3.6") or python_full_version >= "3.6.0" and python_version >= "3.6" and python_version < "4" and (python_version >= "3.6" and python_full_version < "3.0.0" or python_full_version >= "3.6.0" and python_version >= "3.6")diff --cc saleor/graphql/order/mutations/fulfillments.pyindex e86fea67f9,9935991a76..0fc92e53f2--- a/saleor/graphql/order/mutations/fulfillments.py+++ b/saleor/graphql/order/mutations/fulfillments.py@@@ -694,9 -694,7 +695,9 @@@ class FulfillmentRefundAndReturnProduct)cleaned_order_lines.append(- OrderLineData(- OrderLineInfo(line=line, quantity=quantity, replace=replace)++ OrderLineInfo(+ line=line, quantity=quantity, variant=variant, replace=replace+ ))cleaned_input["order_lines"] = cleaned_order_linesdiff --cc saleor/graphql/order/tests/test_fulfillment_refund_products.pyindex 2b476438a4,c6458595ae..36b2754c4c--- a/saleor/graphql/order/tests/test_fulfillment_refund_products.py+++ b/saleor/graphql/order/tests/test_fulfillment_refund_products.py@@@ -1,5 -1,5 +1,5 @@@from decimal import Decimal--from unittest.mock import ANY, patch++from unittest.mock import ANY, Mock, patch, sentinelimport graphenefrom prices import Money, TaxedMoney@@@ -42,6 -42,6 +42,9 @@@ 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(@@@ -134,6 -134,6 +137,7 @@@ def test_fulfillment_refund_products_orassert 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,@@@ -160,7 -160,6 +164,7 @@@ANY,amount=quantize_price(amount_to_refund, fulfilled_order.currency),channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)@@@ -234,6 -233,6 +238,7 @@@ def test_fulfillment_refund_products_reassert 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,@@@ -270,7 -269,6 +275,7 @@@ANY,amount=line_to_refund.unit_price_gross_amount * 2,channel_slug=order_with_lines.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)@@@ -333,6 -331,6 +338,7 @@@ def test_fulfillment_refund_products_orassert 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,@@@ -379,10 -377,9 +385,11 @@@ANY,amount=fulfillment_line_to_refund.order_line.unit_price_gross_amount * 2,channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)++@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,@@@ -434,7 -431,6 +441,7 @@@ANY,amount=fulfillment_line_to_refund.order_line.unit_price_gross_amount * 3,channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)@@@ -549,6 -545,6 +556,7 @@@ def test_fulfillment_refund_products_amassert 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,@@@ -594,14 -590,10 +602,15 @@@amount = fulfillment_line_to_refund.order_line.unit_price_gross_amount * 2amount += fulfilled_order.shipping_price_gross_amountmocked_refund.assert_called_with(- payment_dummy, ANY, amount=amount, channel_slug=fulfilled_order.channel.slug+ payment_dummy,+ ANY,+ amount=amount,+ channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)++@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,@@@ -639,14 -631,10 +648,15 @@@amount = line_to_refund.unit_price_gross_amount * 2amount += order_with_lines.shipping_price_gross_amountmocked_refund.assert_called_with(- payment_dummy, ANY, amount=amount, channel_slug=order_with_lines.channel.slug+ payment_dummy,+ ANY,+ amount=amount,+ channel_slug=order_with_lines.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)++@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,@@@ -695,10 -683,9 +705,11 @@@ANY,amount=amount_to_refund,channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)++@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,@@@ -739,10 -726,9 +750,11 @@@ANY,amount=amount_to_refund,channel_slug=order_with_lines.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)++@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,@@@ -830,9 -816,5 +842,9 @@@amount += order_line.unit_price_gross_amount * 2amount = quantize_price(amount, fulfilled_order.currency)mocked_refund.assert_called_with(- payment_dummy, ANY, amount=amount, channel_slug=fulfilled_order.channel.slug+ payment_dummy,+ ANY,+ amount=amount,+ channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)diff --cc saleor/graphql/order/tests/test_fulfillment_return_products.pyindex 5a2139b5a2,f9b5bbaf5c..0d6fdb806a--- a/saleor/graphql/order/tests/test_fulfillment_return_products.py+++ b/saleor/graphql/order/tests/test_fulfillment_return_products.py@@@ -1,5 -1,5 +1,5 @@@from decimal import Decimal--from unittest.mock import ANY, patch++from unittest.mock import ANY, Mock, patch, sentinelimport graphenefrom prices import Money, TaxedMoney@@@ -63,6 -63,6 +63,8 @@@ mutation OrderFulfillmentReturnProducts}"""++REFUND_DATA = sentinel.REFUND_DATA++def test_fulfillment_return_products_order_without_payment(staff_api_client, permission_manage_orders, fulfilled_order@@@ -84,6 -84,6 +86,7 @@@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,@@@ -114,7 -114,6 +117,7 @@@ANY,amount=quantize_price(amount_to_refund, fulfilled_order.currency),channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)@@@ -194,6 -193,6 +197,7 @@@ def test_fulfillment_return_products_reassert 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,@@@ -300,11 -299,7 +304,11 @@@amount = line_to_return.unit_price_gross_amount * line_quantity_to_returnamount += order_with_lines.shipping_price_gross_amountmocked_refund.assert_called_with(- payment_dummy, ANY, amount=amount, channel_slug=order_with_lines.channel.slug+ payment_dummy,+ ANY,+ amount=amount,+ channel_slug=order_with_lines.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)@@@ -419,6 -414,6 +423,7 @@@ def test_fulfillment_return_products_orassert 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,@@@ -462,10 -457,9 +467,11 @@@ANY,amount=amount_to_refund,channel_slug=order_with_lines.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)++@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,@@@ -584,11 -578,7 +590,11 @@@)amount += fulfilled_order.shipping_price_gross_amountmocked_refund.assert_called_with(- payment_dummy, ANY, amount=amount, channel_slug=fulfilled_order.channel.slug+ payment_dummy,+ ANY,+ amount=amount,+ channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)@@@ -759,6 -749,6 +765,7 @@@ def test_fulfillment_return_products_liassert 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,@@@ -807,14 -797,10 +814,15 @@@amount = fulfillment_line_to_return.order_line.unit_price_gross_amount * 2amount += fulfilled_order.shipping_price_gross_amountmocked_refund.assert_called_with(- payment_dummy, ANY, amount=amount, channel_slug=fulfilled_order.channel.slug+ payment_dummy,+ ANY,+ amount=amount,+ channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)++@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,@@@ -919,9 -905,5 +927,9 @@@amount = order_line.unit_price_gross_amount * 2amount = quantize_price(amount, fulfilled_order.currency)mocked_refund.assert_called_with(- payment_dummy, ANY, amount=amount, channel_slug=fulfilled_order.channel.slug+ payment_dummy,+ ANY,+ amount=amount,+ channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)diff --cc saleor/order/tests/test_order_actions_create_fulfillments_for_returned_products.pyindex 1370fd0fd2,c9615b9646..569fc80130--- 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,21 -1,16 +1,18 @@@from decimal import Decimal--from unittest.mock import ANY, patch++from unittest.mock import ANY, Mock, patch, sentinelfrom prices import Money, TaxedMoneyfrom ...plugins.manager import get_plugins_managerfrom ...tests.utils import flush_post_commit_hooksfrom ...warehouse.models import Allocation, Stock- from .. import (- FulfillmentLineData,- FulfillmentStatus,- OrderEvents,- OrderLineData,- OrderOrigin,- )+ from .. import FulfillmentLineData, FulfillmentStatus, OrderEvents, OrderOriginfrom ..actions import create_fulfillments_for_returned_products+ from ..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")@@@ -92,6 -87,6 +89,7 @@@ def test_create_return_fulfillment_only@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,@@@ -154,7 -149,6 +152,7 @@@ANY,amount=amount,channel_slug=order_with_lines.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)assert not replace_order@@@ -165,6 -159,6 +163,7 @@@@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,@@@ -229,7 -223,6 +228,7 @@@ANY,amount=amount,channel_slug=order_with_lines.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)assert not replace_order@@@ -529,6 -522,6 +528,7 @@@ def test_create_return_fulfillment_only@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 -617,6 +625,7 @@@ANY,amount=amount,channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)assert returned_and_refunded_fulfillment.total_refund_amount == amountdiff --cc saleor/order/tests/test_order_actions_refund_products.pyindex 281fdda5fc,f5ea1c0d20..62a7c7be8c--- a/saleor/order/tests/test_order_actions_refund_products.py+++ b/saleor/order/tests/test_order_actions_refund_products.py@@@ -1,16 -1,17 +1,20 @@@from decimal import Decimal--from unittest.mock import ANY, patch++from unittest.mock import ANY, Mock, patch, sentinelfrom ...payment import ChargeStatusfrom ...plugins.manager import get_plugins_managerfrom ...tests.utils import flush_post_commit_hooksfrom ...warehouse.models import Allocation- from .. import FulfillmentLineData, FulfillmentStatus, OrderLineData+ from .. import FulfillmentLineData, FulfillmentStatusfrom ..actions import create_refund_fulfillment+ from ..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@@@ -68,16 -69,12 +72,17 @@@assert returned_fulfillemnt.shipping_refund_amount is Nonemocked_refund.assert_called_once_with(- payment_dummy, ANY, amount=amount, channel_slug=order_with_lines.channel.slug+ payment_dummy,+ ANY,+ amount=amount,+ channel_slug=order_with_lines.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)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@@@ -126,16 -123,12 +131,17 @@@)mocked_refund.assert_called_once_with(- payment_dummy, ANY, amount=amount, channel_slug=order_with_lines.channel.slug+ payment_dummy,+ ANY,+ amount=amount,+ channel_slug=order_with_lines.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)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@@@ -177,11 -170,7 +183,11 @@@[line.order_line.unit_price_gross_amount * 2 for line in fulfillment_lines])mocked_refund.assert_called_once_with(- payment_dummy, ANY, amount=amount, channel_slug=fulfilled_order.channel.slug+ payment_dummy,+ ANY,+ amount=amount,+ channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)mocked_order_updated.assert_called_once_with(fulfilled_order)@@@ -190,6 -179,6 +196,7 @@@@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@@@ -230,11 -219,7 +237,11 @@@for line in fulfillment_lines:assert line.quantity == original_quantity.get(line.pk) - 2mocked_refund.assert_called_once_with(- payment_dummy, ANY, amount=amount, channel_slug=fulfilled_order.channel.slug+ payment_dummy,+ ANY,+ amount=amount,+ channel_slug=fulfilled_order.channel.slug,- refund_data=ANY,++ refund_data=REFUND_DATA,)mocked_order_updated.assert_called_once_with(fulfilled_order)diff --cc saleor/payment/tests/test_utils.pyindex eb3a2d6d40,0000000000..e5ce136b60mode 100644,000000..100644--- a/saleor/payment/tests/test_utils.py+++ b/saleor/payment/tests/test_utils.py@@@ -1,501 -1,0 +1,492 @@@+from decimal import Decimal+from itertools import chain, zip_longest+from typing import List+from unittest.mock import Mock, patch++import pytest+- from ...checkout.calculations import checkout_line_total+from ...checkout.fetch import fetch_checkout_info, fetch_checkout_lines- from ...order import FulfillmentLineData, OrderLineData++from ...order import FulfillmentLineData+from ...order.actions import create_refund_fulfillment++from ...order.fetch import OrderLineInfo+from ...order.models import Order+from ...plugins.manager import get_plugins_manager+from ..interface import PaymentLineData+from ..utils import (+ SHIPPING_PAYMENT_LINE_ID,+ VOUCHER_PAYMENT_LINE_ID,+ create_payment_lines_information,+ create_refund_data,+)+++def create_refund_fulfillment_helper(payment_dummy):+ def factory(+ order: Order,- order_lines: List[OrderLineData] = None,++ order_lines: List[OrderLineInfo] = None,+ fulfillment_lines: List[FulfillmentLineData] = None,+ refund_shipping_costs: bool = False,+ ):+ 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(),+ refund_shipping_costs=refund_shipping_costs,+ )++ return factory+++ ["refund_shipping_costs", "shipping_line_quantity"], [(True, 0), (False, 1)]+)+def test_create_refund_data_order_lines(+ order_with_lines, refund_shipping_costs, shipping_line_quantity+):+ # given+ order_lines = order_with_lines.lines.all()+ order_refund_lines = [- OrderLineData(line=(line := order_lines[0]), quantity=2, variant=line.variant),- OrderLineData(line=(line := order_lines[1]), quantity=1, variant=line.variant),++ 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+ refund_data = create_refund_data(+ order_with_lines,+ order_refund_lines,+ fulfillment_refund_lines,+ refund_shipping_costs,+ )++ # then+ assert refund_data == {+ **{+ line.variant_id: line.quantity - refund_line.quantity+ for line, refund_line in zip(order_lines, order_refund_lines)+ },+ SHIPPING_PAYMENT_LINE_ID: shipping_line_quantity,+ }+++ ["refund_shipping_costs", "shipping_line_quantity"], [(True, 0), (False, 1)]+)+def test_create_refund_data_fulfillment_lines(+ fulfilled_order, refund_shipping_costs, shipping_line_quantity+):+ # 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+ refund_data = create_refund_data(+ fulfilled_order,+ order_refund_lines,+ fulfillment_refund_lines,+ refund_shipping_costs,+ )++ # then+ assert refund_data == {+ **{+ line.order_line.variant_id: line.quantity - refund_line.quantity+ for line, refund_line in zip(fulfillment_lines, fulfillment_refund_lines)+ },+ SHIPPING_PAYMENT_LINE_ID: shipping_line_quantity,+ }+++ ["refund_shipping_costs", "shipping_line_quantity"], [(True, 0), (False, 1)]+)+def test_create_refund_data_shipping_only(+ order, refund_shipping_costs, shipping_line_quantity+):+ # given+ order_refund_lines = []+ fulfillment_refund_lines = []++ # when+ refund_data = create_refund_data(+ order, order_refund_lines, fulfillment_refund_lines, refund_shipping_costs+ )++ # then+ assert refund_data == {SHIPPING_PAYMENT_LINE_ID: shipping_line_quantity}+++@patch("saleor.order.actions.gateway.refund")+ [+ "previous_refund_shipping_costs",+ "current_refund_shipping_costs",+ "shipping_line_quantity",+ ],+ [+ (True, True, 0),+ (True, False, 0),+ (False, True, 0),+ (False, False, 1),+ ],+)+def test_create_refund_data_previously_refunded_order_lines(+ _mocked_refund,+ order_with_lines,+ create_refund_fulfillment_helper,+ previous_refund_shipping_costs,+ current_refund_shipping_costs,+ shipping_line_quantity,+):+ # given+ order_lines = order_with_lines.lines.all()+ previous_order_refund_lines = [- OrderLineData(line=(line := order_lines[0]), quantity=1, variant=line.variant)++ 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 = [- OrderLineData(line=(line := order_lines[0]), quantity=1, variant=line.variant),- OrderLineData(line=(line := order_lines[1]), quantity=1, variant=line.variant),++ 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+ refund_data = create_refund_data(+ order_with_lines,+ current_order_refund_lines,+ fulfillment_refund_lines,+ current_refund_shipping_costs,+ )++ # then+ order_refund_lines = [- OrderLineData(line=cl.line, quantity=pl.quantity + cl.quantity)++ 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=OrderLineData, quantity=0),++ fillvalue=Mock(spec=OrderLineInfo, quantity=0),+ )+ ]+ assert refund_data == {+ **{+ line.variant_id: line.quantity - refund_line.quantity+ for line, refund_line in zip(order_lines, order_refund_lines)+ },+ SHIPPING_PAYMENT_LINE_ID: shipping_line_quantity,+ }+++@patch("saleor.order.actions.gateway.refund")+ [+ "previous_refund_shipping_costs",+ "current_refund_shipping_costs",+ "shipping_line_quantity",+ ],+ [+ (True, True, 0),+ (True, False, 0),+ (False, True, 0),+ (False, False, 1),+ ],+)+def test_create_refund_data_previously_refunded_fulfillment_lines(+ _mocked_refund,+ fulfilled_order,+ create_refund_fulfillment_helper,+ previous_refund_shipping_costs,+ current_refund_shipping_costs,+ shipping_line_quantity,+):+ # 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+ refund_data = create_refund_data(+ fulfilled_order,+ order_refund_lines,+ current_fulfillment_refund_lines,+ current_refund_shipping_costs,+ )++ # 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 refund_data == {+ **{+ line.variant_id: line.quantity - refund_line.quantity+ for line, refund_line in zip(+ fulfilled_order.lines.all(), fulfillment_refund_lines+ )+ },+ SHIPPING_PAYMENT_LINE_ID: shipping_line_quantity,+ }+++@patch("saleor.order.actions.gateway.refund")+ [+ "previous_refund_shipping_costs",+ "current_refund_shipping_costs",+ "shipping_line_quantity",+ ],+ [+ (True, True, 0),+ (True, False, 0),+ (False, True, 0),+ (False, False, 1),+ ],+)+def test_create_refund_data_previously_refunded_shipping_only(+ _mocked_refund,+ order,+ create_refund_fulfillment_helper,+ previous_refund_shipping_costs,+ current_refund_shipping_costs,+ shipping_line_quantity,+):+ # given+ create_refund_fulfillment_helper(+ order, refund_shipping_costs=previous_refund_shipping_costs+ )+ order_refund_lines = []+ fulfillment_refund_lines = []++ # when+ refund_data = create_refund_data(+ order,+ order_refund_lines,+ fulfillment_refund_lines,+ current_refund_shipping_costs,+ )++ # then+ assert refund_data == {SHIPPING_PAYMENT_LINE_ID: shipping_line_quantity}+++def test_create_payment_lines_information_order(payment_dummy):+ # given+ manager = get_plugins_manager()++ # when+ payment_lines = create_payment_lines_information(payment_dummy, manager)++ # then+ order = payment_dummy.order+ assert payment_lines == [+ PaymentLineData(+ gross=line.unit_price_gross_amount,+ variant_id=line.variant_id,+ product_name=f"{line.product_name}, {line.variant_name}",+ product_sku=line.product_sku,+ quantity=line.quantity,+ )+ for line in order.lines.all()+ ] + [+ PaymentLineData(+ gross=order.shipping_price_gross_amount,+ variant_id=SHIPPING_PAYMENT_LINE_ID,+ product_name="Shipping",+ product_sku="Shipping",+ quantity=1,+ )+ ]+++def test_create_payment_lines_information_order_with_voucher(payment_dummy):+ # given+ voucher_amount = Decimal("12.30")+ order = payment_dummy.order+ order.undiscounted_total_gross_amount += voucher_amount+ manager = get_plugins_manager()++ # when+ payment_lines = create_payment_lines_information(payment_dummy, manager)++ # then++ assert payment_lines == [+ PaymentLineData(+ gross=line.unit_price_gross_amount,+ variant_id=line.variant_id,+ product_name=f"{line.product_name}, {line.variant_name}",+ product_sku=line.product_sku,+ quantity=line.quantity,+ )+ for line in order.lines.all()+ ] + [+ PaymentLineData(+ gross=order.shipping_price_gross_amount,+ variant_id=SHIPPING_PAYMENT_LINE_ID,+ product_name="Shipping",+ product_sku="Shipping",+ quantity=1,+ ),+ PaymentLineData(+ gross=-voucher_amount,+ variant_id=VOUCHER_PAYMENT_LINE_ID,+ product_name="Voucher",+ product_sku="Voucher",+ quantity=1,+ ),+ ]+++def get_expected_checkout_payment_lines(+ manager, checkout_info, lines, address, discounts+):+ expected_payment_lines = []++ for line_info in lines:- total_price = checkout_line_total(- manager=manager,- checkout_info=checkout_info,- lines=lines,- checkout_line_info=line_info,- discounts=discounts,- )+ unit_gross = manager.calculate_checkout_line_unit_price(- total_price,- line_info.line.quantity,+ checkout_info,+ lines,+ line_info,+ address,+ discounts,- ).gross.amount++ ).undiscounted_price.gross.amount+ quantity = line_info.line.quantity+ variant_id = line_info.variant.id+ product_name = f"{line_info.variant.product.name}, {line_info.variant.name}"+ product_sku = line_info.variant.sku+ expected_payment_lines.append(+ PaymentLineData(+ gross=unit_gross,+ variant_id=variant_id,+ product_name=product_name,+ product_sku=product_sku,+ quantity=quantity,+ )+ )++ shipping_gross = manager.calculate_checkout_shipping(+ checkout_info=checkout_info,+ lines=lines,+ address=address,+ discounts=discounts,+ ).gross.amount+ expected_payment_lines.append(+ PaymentLineData(+ gross=shipping_gross,+ variant_id=SHIPPING_PAYMENT_LINE_ID,+ product_name="Shipping",+ product_sku="Shipping",+ quantity=1,+ )+ )++ return expected_payment_lines+++def test_create_payment_lines_information_checkout(payment_dummy, checkout_with_items):+ # given+ manager = get_plugins_manager()+ payment_dummy.order = None+ payment_dummy.checkout = checkout_with_items++ # when+ payment_lines = create_payment_lines_information(payment_dummy, manager)++ # then+ lines = fetch_checkout_lines(checkout_with_items)+ discounts = []+ checkout_info = fetch_checkout_info(checkout_with_items, lines, discounts, manager)+ address = checkout_with_items.shipping_address+ expected_payment_lines = get_expected_checkout_payment_lines(+ manager, checkout_info, lines, address, discounts+ )++ assert payment_lines == expected_payment_lines+++def test_create_payment_lines_information_checkout_with_voucher(+ payment_dummy, checkout_with_items+):+ # given+ manager = get_plugins_manager()+ voucher_amount = Decimal("12.30")+ payment_dummy.order = None+ checkout_with_items.discount_amount = voucher_amount+ payment_dummy.checkout = checkout_with_items++ # when+ payment_lines = create_payment_lines_information(payment_dummy, manager)++ # then+ lines = fetch_checkout_lines(checkout_with_items)+ discounts = []+ checkout_info = fetch_checkout_info(checkout_with_items, lines, discounts, manager)+ address = checkout_with_items.shipping_address+ expected_payment_lines = get_expected_checkout_payment_lines(+ manager, checkout_info, lines, address, discounts+ )+ expected_payment_lines.append(+ PaymentLineData(+ gross=-voucher_amount,+ variant_id=VOUCHER_PAYMENT_LINE_ID,+ product_name="Voucher",+ product_sku="Voucher",+ quantity=1,+ )+ )++ assert payment_lines == expected_payment_lines+++def test_create_payment_lines_information_invalid_payment(payment_dummy):+ # given+ manager = get_plugins_manager()+ payment_dummy.order = None++ # when+ payment_lines = create_payment_lines_information(payment_dummy, manager)++ # then+ assert not payment_linesdiff --cc saleor/payment/utils.pyindex 3cd60a37b5,b42be75bd3..2ccf25e130--- a/saleor/payment/utils.py+++ b/saleor/payment/utils.py@@@ -1,9 -1,7 +1,9 @@@import jsonimport logging+from collections import defaultdictfrom decimal import Decimal-from typing import TYPE_CHECKING, Dict, List, Optional, cast+from itertools import chain- from typing import Any, Dict, Iterator, List, Optional, Tuple++from typing import Any, Dict, Iterator, List, Optional, Tuple, castimport graphenefrom babel.numbers import get_currency_precision@@@ -11,15 -9,10 +11,15 @@@ from django.core.serializers.json imporfrom django.db.models import Qfrom ..account.models import User- from ..checkout.calculations import checkout_line_total+from ..checkout.fetch import fetch_checkout_info, fetch_checkout_linesfrom ..checkout.models import Checkoutfrom ..core.prices import quantize_pricefrom ..core.tracing import traced_atomic_transaction-from ..order.models import Order+from ..discount.utils import fetch_active_discounts- from ..order import FulfillmentLineData, FulfillmentStatus, OrderLineData++from ..order import FulfillmentLineData, FulfillmentStatus++from ..order.fetch import OrderLineInfo+from ..order.models import FulfillmentLine, Order, OrderLine+from ..plugins.manager import PluginsManager, get_plugins_managerfrom . import (ChargeStatus,GatewayError,@@@ -44,149 -39,6 +44,140 @@@ GENERIC_TRANSACTION_ERROR = "TransactioALLOWED_GATEWAY_KINDS = {choices[0] for choices in TransactionKind.CHOICES}+def create_payment_lines_information(+ payment: Payment,+ manager: PluginsManager,+) -> List[PaymentLineData]:+ checkout = payment.checkout+ order = payment.order++ if checkout:+ return create_checkout_payment_lines_information(checkout, manager)+ elif order:+ return create_order_payment_lines_information(order)++ return []+++def create_checkout_payment_lines_information(+ checkout: Checkout, manager: PluginsManager+) -> List[PaymentLineData]:+ line_items = []+ lines = fetch_checkout_lines(checkout)+ discounts = fetch_active_discounts()+ checkout_info = fetch_checkout_info(checkout, lines, discounts, manager)+ address = checkout_info.shipping_address or checkout_info.billing_address++ for line_info in lines:- total = checkout_line_total(- manager=manager,- checkout_info=checkout_info,- lines=lines,- checkout_line_info=line_info,- discounts=discounts,- )+ unit_price = manager.calculate_checkout_line_unit_price(- total,- line_info.line.quantity,+ checkout_info,+ lines,+ line_info,+ address,+ discounts,+ )- unit_gross = unit_price.gross.amount++ unit_gross = unit_price.undiscounted_price.gross.amount++ quantity = line_info.line.quantity+ product_name = f"{line_info.variant.product.name}, {line_info.variant.name}"+ product_sku = line_info.variant.sku+ line_items.append(+ PaymentLineData(+ quantity=quantity,+ product_name=product_name,+ product_sku=product_sku,+ variant_id=line_info.variant.id,+ gross=unit_gross,+ )+ )+ shipping_amount = manager.calculate_checkout_shipping(+ checkout_info=checkout_info,+ lines=lines,+ address=address,+ discounts=discounts,+ ).gross.amount++ line_items.append(create_shipping_payment_line_data(amount=shipping_amount))++ voucher_line_item = create_checkout_voucher_payment_line_data(checkout)+ if voucher_line_item:+ line_items.append(voucher_line_item)++ return line_items+++def create_order_payment_lines_information(order: Order) -> List[PaymentLineData]:+ line_items = []+ for order_line in order.lines.all():+ product_name = f"{order_line.product_name}, {order_line.variant_name}"++ variant_id = order_line.variant_id+ if variant_id is None:+ continue++ line_items.append(+ PaymentLineData(+ quantity=order_line.quantity,+ product_name=product_name,+ product_sku=order_line.product_sku,+ variant_id=variant_id,+ gross=order_line.unit_price_gross_amount,+ )+ )++ line_items.append(+ create_shipping_payment_line_data(amount=order.shipping_price_gross_amount)+ )++ voucher_line_item = create_order_voucher_payment_line_data(order)+ if voucher_line_item:+ line_items.append(voucher_line_item)++ return line_items+++# Values are outside of model's pk range to resolve+# any collision with actual product variant pk+VOUCHER_PAYMENT_LINE_ID = 0+SHIPPING_PAYMENT_LINE_ID = -1+++def create_shipping_payment_line_data(amount: Decimal) -> PaymentLineData:+ return PaymentLineData(+ quantity=1,+ product_name="Shipping",+ product_sku="Shipping",+ variant_id=SHIPPING_PAYMENT_LINE_ID,+ gross=amount,+ )+++def create_checkout_voucher_payment_line_data(+ checkout: Checkout,+) -> Optional[PaymentLineData]:+ discount_amount = -checkout.discount_amount+ return create_voucher_payment_line_data(discount_amount)+++def create_order_voucher_payment_line_data(+ order: Order,+) -> Optional[PaymentLineData]:+ discount_amount = order.total_gross_amount - order.undiscounted_total_gross_amount+ return create_voucher_payment_line_data(discount_amount)+++def create_voucher_payment_line_data(amount: Decimal) -> Optional[PaymentLineData]:+ if not amount:+ return None+ return PaymentLineData(+ quantity=1,+ product_name="Voucher",+ product_sku="Voucher",+ variant_id=VOUCHER_PAYMENT_LINE_ID,+ gross=amount,+ )++def create_payment_information(payment: Payment,payment_token: str = None,@@@ -245,91 -103,12 +244,93 @@@store_payment_method=StorePaymentMethodEnum[payment.store_payment_method.upper()],+ checkout_token=checkout_token,+ checkout_metadata=checkout_metadata,payment_metadata=payment.metadata,psp_reference=payment.psp_reference,+ refund_data=refund_data,+ _resolve_lines=lambda: create_payment_lines_information(+ payment, manager or get_plugins_manager()+ ),+ )+++RefundLines = Iterator[Tuple[Any, OrderLine]]+++def _prepare_refund_lines(+ order: Order,- order_lines_to_refund: List[OrderLineData],++ order_lines_to_refund: List[OrderLineInfo],+ fulfillment_lines_to_refund: List[FulfillmentLineData],+) -> Iterator[Tuple[int, int]]:+ previous_fulfillment_lines = FulfillmentLine.objects.prefetch_related(+ "order_line"+ ).filter(+ fulfillment__order_id=order.pk,+ fulfillment__status__in=[+ FulfillmentStatus.REFUNDED,+ FulfillmentStatus.REFUNDED_AND_RETURNED,+ ],+ order_line__variant_id__isnull=False,+ )++ previous_refund_lines = (+ (p_variant_id, line.quantity)+ for line in previous_fulfillment_lines+ if (p_variant_id := line.order_line.variant_id)+ )++ current_order_refund_lines = (+ (variant.id, line.quantity)+ for line in order_lines_to_refund+ if (variant := line.variant)+ )++ current_fulfillment_refund_lines = (+ (f_variant_id, line.quantity)+ for line in fulfillment_lines_to_refund+ if (f_variant_id := line.line.order_line.variant_id)+ )++ return chain(+ previous_refund_lines,+ current_order_refund_lines,+ current_fulfillment_refund_lines,+ )+++def create_refund_data(+ order: Order,- order_lines_to_refund: List[OrderLineData],++ order_lines_to_refund: List[OrderLineInfo],+ fulfillment_lines_to_refund: List[FulfillmentLineData],+ refund_shipping_costs: bool,+) -> Dict[int, int]:+ order_lines = {line.variant_id: line.quantity for line in order.lines.all()}++ refund_lines = _prepare_refund_lines(+ order, order_lines_to_refund, fulfillment_lines_to_refund)+ summed_refund_lines: Dict[int, int] = defaultdict(int)++ for variant_id, quantity in refund_lines:+ summed_refund_lines[variant_id] += quantity++ lines = {+ variant_id: order_lines[variant_id] - summed_refund_lines[variant_id]+ for variant_id in summed_refund_lines+ }++ shipping_previously_refunded = order.fulfillments.exclude(+ shipping_refund_amount__isnull=True+ ).exists()++ lines[SHIPPING_PAYMENT_LINE_ID] = (+ 0 if shipping_previously_refunded or refund_shipping_costs else 1+ )++ return lines+def create_payment(gateway: str,