Skip to content

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

Refactor voucher and shipping amount for payment lines data

Mateusz Grzybf365f77eb905
diff --git a/saleor/payment/gateway.py b/saleor/payment/gateway.py
index 6e7e4266d9..a4e03c20c4 100644
--- a/saleor/payment/gateway.py
+++ b/saleor/payment/gateway.py
@@ -1,6 +1,6 @@
import logging
from decimal import Decimal
-from typing import TYPE_CHECKING, Callable, Dict, List, Optional
+from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple
from ..core.tracing import traced_atomic_transaction
from . import GatewayError, PaymentError, TransactionKind
@@ -18,7 +18,7 @@ from .utils import (
if TYPE_CHECKING:
# flake8: noqa
- from ..payment.interface import CustomerSource, PaymentGateway
+ from ..payment.interface import CustomerSource, PaymentGateway, RefundData
from ..plugins.manager import PluginsManager
@@ -194,7 +194,7 @@ def refund(
manager: "PluginsManager",
channel_slug: str,
amount: Decimal = None,
- refund_data: Optional[Dict[int, int]] = None,
+ refund_data: Optional["RefundData"] = None,
) -> Transaction:
if amount is None:
amount = payment.captured_amount
diff --git a/saleor/payment/gateways/np_atobarai/api.py b/saleor/payment/gateways/np_atobarai/api.py
index ee700dbeae..133e5c45db 100644
--- a/saleor/payment/gateways/np_atobarai/api.py
+++ b/saleor/payment/gateways/np_atobarai/api.py
@@ -1,8 +1,8 @@
import logging
-from typing import Dict, List, Optional, Tuple
+from typing import List, Optional, Tuple
from ....order.models import Fulfillment
-from ...interface import PaymentData
+from ...interface import PaymentData, RefundData
from ...models import Payment
from .api_helpers import (
cancel,
@@ -95,7 +95,7 @@ def change_transaction(
config: ApiConfig,
payment: Payment,
payment_information: PaymentData,
- refund_data: Optional[Dict[int, int]],
+ refund_data: Optional[RefundData],
) -> Optional[PaymentResult]:
"""Change transaction.
@@ -157,7 +157,7 @@ def reregister_transaction_for_partial_return(
payment_information: PaymentData,
shipping_company_code: Optional[str],
tracking_number: Optional[str],
- refund_data: Optional[Dict[int, int]],
+ refund_data: Optional[RefundData],
) -> PaymentResult:
"""Change transaction.
diff --git a/saleor/payment/gateways/np_atobarai/api_helpers.py b/saleor/payment/gateways/np_atobarai/api_helpers.py
index 85269be39e..79150af9e9 100644
--- a/saleor/payment/gateways/np_atobarai/api_helpers.py
+++ b/saleor/payment/gateways/np_atobarai/api_helpers.py
@@ -1,6 +1,6 @@
import logging
from decimal import Decimal
-from typing import TYPE_CHECKING, Dict, Iterable, List, Optional
+from typing import TYPE_CHECKING, Iterable, List, Optional
import requests
from django.utils import timezone
@@ -8,7 +8,7 @@ from posuto import Posuto
from requests.auth import HTTPBasicAuth
from ....order.models import Order
-from ...interface import AddressData, PaymentData, PaymentLineData
+from ...interface import AddressData, PaymentData, PaymentLineData, RefundData
from ...utils import price_to_minor_unit
from .api_types import NPResponse, error_np_response
from .const import NP_ATOBARAI, REQUEST_TIMEOUT
@@ -132,20 +132,55 @@ def _get_goods_name(line: PaymentLineData, config: "ApiConfig") -> str:
return str(line.variant_id)
+def _get_voucher_and_shipping_goods(
+ config: "ApiConfig",
+ payment_information: PaymentData,
+ refund_shipping: bool = False,
+) -> List[dict]:
+ """Convert voucher and shipping amount into goods lines."""
+ goods_lines = []
+ voucher_amount = payment_information.lines_data.voucher_amount
+ if voucher_amount:
+ goods_lines.append(
+ {
+ "goods_name": "Voucher",
+ "goods_price": format_price(
+ voucher_amount, payment_information.currency
+ ),
+ "quantity": 1,
+ }
+ )
+ shipping_amount = payment_information.lines_data.shipping_amount
+ if shipping_amount and not refund_shipping:
+ goods_lines.append(
+ {
+ "goods_name": "Shipping",
+ "goods_price": format_price(
+ shipping_amount, payment_information.currency
+ ),
+ "quantity": 1,
+ }
+ )
+
+ return goods_lines
+
+
def get_refunded_goods(
config: "ApiConfig",
- refund_data: Dict[int, int],
+ refund_data: RefundData,
payment_information: PaymentData,
) -> List[dict]:
return [
{
"goods_name": _get_goods_name(line, config),
- "goods_price": format_price(line.gross, payment_information.currency),
+ "goods_price": format_price(line.amount, payment_information.currency),
"quantity": quantity,
}
- for line in payment_information.lines
- if (quantity := refund_data.get(line.variant_id, line.quantity))
- ]
+ for line in payment_information.lines_data.lines
+ if (quantity := refund_data.lines.get(line.variant_id, line.quantity))
+ ] + _get_voucher_and_shipping_goods(
+ config, payment_information, refund_data.shipping
+ )
def get_goods(config: "ApiConfig", payment_information: PaymentData) -> List[dict]:
@@ -153,10 +188,10 @@ def get_goods(config: "ApiConfig", payment_information: PaymentData) -> List[dic
{
"quantity": line.quantity,
"goods_name": _get_goods_name(line, config),
- "goods_price": format_price(line.gross, payment_information.currency),
+ "goods_price": format_price(line.amount, payment_information.currency),
}
- for line in payment_information.lines
- ]
+ for line in payment_information.lines_data.lines
+ ] + _get_voucher_and_shipping_goods(config, payment_information)
def get_goods_with_discount(
diff --git a/saleor/payment/gateways/np_atobarai/tests/conftest.py b/saleor/payment/gateways/np_atobarai/tests/conftest.py
index e754b693d1..aba02930c0 100644
--- a/saleor/payment/gateways/np_atobarai/tests/conftest.py
+++ b/saleor/payment/gateways/np_atobarai/tests/conftest.py
@@ -5,7 +5,7 @@ import pytest
from .....plugins.manager import get_plugins_manager
from .....plugins.models import PluginConfiguration
-from ....interface import AddressData, PaymentLineData
+from ....interface import AddressData, PaymentLineData, PaymentLinesData
from ..api_types import get_api_config
from ..const import (
FILL_MISSING_ADDRESS,
@@ -57,16 +57,20 @@ def np_atobarai_plugin(settings, monkeypatch, channel_USD):
def _resolve_lines():
- return [
- PaymentLineData(
- gross=Decimal("100.00"),
- product_name=f"Product Name {i}",
- product_sku=f"PRODUCT_SKU_{i}",
- variant_id=i,
- quantity=5,
- )
- for i in range(3)
- ]
+ return PaymentLinesData(
+ lines=[
+ PaymentLineData(
+ amount=Decimal("100.00"),
+ product_name=f"Product Name {i}",
+ product_sku=f"PRODUCT_SKU_{i}",
+ variant_id=i,
+ quantity=5,
+ )
+ for i in range(3)
+ ],
+ shipping_amount=Decimal("120.00"),
+ voucher_amount=Decimal("-10.00"),
+ )
@pytest.fixture
@@ -92,7 +96,7 @@ def np_payment_data(np_address_data, dummy_payment_data):
dummy_payment_data,
billing=np_address_data,
shipping=np_address_data,
- _resolve_lines=_resolve_lines,
+ _resolve_lines_data=_resolve_lines,
)
diff --git a/saleor/payment/gateways/np_atobarai/tests/test_api.py b/saleor/payment/gateways/np_atobarai/tests/test_api.py
index 55788cc391..a3b513ed94 100644
--- a/saleor/payment/gateways/np_atobarai/tests/test_api.py
+++ b/saleor/payment/gateways/np_atobarai/tests/test_api.py
@@ -7,6 +7,7 @@ import requests
from graphene import Node
from .... import PaymentError
+from ....interface import RefundData
from .. import PaymentStatus, api, const
from ..api_helpers import format_price, get_goods_with_discount, get_refunded_goods
from ..api_types import NPResponse
@@ -605,7 +606,7 @@ def test_change_transaction_refunded_goods(
mocked_request, config, payment_dummy, np_payment_data
):
# given
- refund_data = {i: i for i in range(1, 4)}
+ refund_data = RefundData(lines={i: i for i in range(1, 4)}, shipping=False)
mocked_request.return_value = NPResponse(
result={
"authori_result": "00",
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 0d0eb58df8..fb1e22ab93 100644
--- a/saleor/payment/gateways/np_atobarai/tests/test_api_helpers.py
+++ b/saleor/payment/gateways/np_atobarai/tests/test_api_helpers.py
@@ -4,7 +4,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_refunded_goods
@@ -165,36 +165,90 @@ def test_get_goods(
{
"goods_name": line.product_sku if sku_as_name else line.product_name,
"goods_price": int(
- price_to_minor_unit(line.gross, np_payment_data.currency)
+ price_to_minor_unit(line.amount, np_payment_data.currency)
),
"quantity": line.quantity,
}
- for line in np_payment_data.lines
+ for line in np_payment_data.lines_data.lines
+ ] + [
+ {
+ "goods_name": "Voucher",
+ "goods_price": int(
+ price_to_minor_unit(
+ np_payment_data.lines_data.voucher_amount, np_payment_data.currency
+ )
+ ),
+ "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,
+ },
]
+ "refund_shipping, shipping_goods",
+ [
+ (True, []),
+ (
+ False,
+ [
+ {
+ "goods_name": "Shipping",
+ "goods_price": 12000,
+ "quantity": 1,
+ }
+ ],
+ ),
+ ],
+)
@pytest.mark.parametrize("sku_as_name", [True, False])
def test_get_refunded_goods(
config,
np_payment_data,
sku_as_name,
+ refund_shipping,
+ shipping_goods,
):
# given
config.sku_as_name = sku_as_name
- refund_data = {1: 23, 2: 0, 3: 13}
+ refund_data_lines = {1: 23, 2: 0, 3: 13}
+ refund_data = RefundData(lines=refund_data_lines, shipping=refund_shipping)
# when
goods = get_refunded_goods(config, refund_data, np_payment_data)
# then
- assert goods == [
- {
- "goods_name": line.product_sku if sku_as_name else line.product_name,
- "goods_price": int(
- price_to_minor_unit(line.gross, np_payment_data.currency)
- ),
- "quantity": quantity,
- }
- for line in np_payment_data.lines
- if (quantity := refund_data.get(line.variant_id, line.quantity))
- ]
+ assert (
+ goods
+ == [
+ {
+ "goods_name": line.product_sku if sku_as_name else line.product_name,
+ "goods_price": int(
+ price_to_minor_unit(line.amount, np_payment_data.currency)
+ ),
+ "quantity": quantity,
+ }
+ for line in np_payment_data.lines_data.lines
+ if (quantity := refund_data_lines.get(line.variant_id, line.quantity))
+ ]
+ + [
+ {
+ "goods_name": "Voucher",
+ "goods_price": int(
+ price_to_minor_unit(
+ np_payment_data.lines_data.voucher_amount,
+ np_payment_data.currency,
+ )
+ ),
+ "quantity": 1,
+ },
+ ]
+ + shipping_goods
+ )
diff --git a/saleor/payment/interface.py b/saleor/payment/interface.py
index 3cfec19640..987eebe0be 100644
--- a/saleor/payment/interface.py
+++ b/saleor/payment/interface.py
@@ -69,13 +69,26 @@ class StorePaymentMethodEnum(str, Enum):
@dataclass
class PaymentLineData:
- gross: Decimal
+ amount: Decimal
variant_id: int
product_name: str
product_sku: Optional[str]
quantity: int
+@dataclass
+class PaymentLinesData:
+ shipping_amount: Decimal
+ voucher_amount: Decimal
+ lines: List[PaymentLineData]
+
+
+@dataclass
+class RefundData:
+ shipping: bool
+ lines: Dict[int, int]
+
+
@dataclass
class PaymentData:
"""Dataclass for storing all payment information.
@@ -104,18 +117,18 @@ class PaymentData:
store_payment_method: StorePaymentMethodEnum = StorePaymentMethodEnum.NONE
payment_metadata: Dict[str, str] = field(default_factory=dict)
psp_reference: Optional[str] = None
- refund_data: Optional[Dict[int, int]] = None
+ refund_data: Optional[RefundData] = None
# Optional, lazy-evaluated gateway arguments
- _resolve_lines: InitVar[Callable] = None
+ _resolve_lines_data: InitVar[Callable[[], PaymentLinesData]] = None
- def __post_init__(self, _resolve_lines: Callable):
- self.__resolve_lines = _resolve_lines
+ def __post_init__(self, _resolve_lines_data: Callable[[], PaymentLinesData]):
+ self.__resolve_lines_data = _resolve_lines_data
# Note: this field does not appear in webhook payloads,
# because it's not visible to dataclasses.asdict
@cached_property
- def lines(self) -> List[PaymentLineData]:
- return self.__resolve_lines()
+ def lines_data(self) -> PaymentLinesData:
+ return self.__resolve_lines_data()
@dataclass
diff --git a/saleor/payment/tests/test_utils.py b/saleor/payment/tests/test_utils.py
index 9868fa6294..daaff9bb95 100644
--- a/saleor/payment/tests/test_utils.py
+++ b/saleor/payment/tests/test_utils.py
@@ -11,10 +11,8 @@ 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 ..interface import PaymentLineData, PaymentLinesData
from ..utils import (
- SHIPPING_PAYMENT_LINE_ID,
- VOUCHER_PAYMENT_LINE_ID,
create_payment_lines_information,
create_refund_data,
get_channel_slug_from_payment,
@@ -45,12 +43,8 @@ def create_refund_fulfillment_helper(payment_dummy):
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
-):
[email protected]("refund_shipping_costs", [True, False])
+def test_create_refund_data_order_lines(order_with_lines, refund_shipping_costs):
# given
order_lines = order_with_lines.lines.all()
order_refund_lines = [
@@ -68,21 +62,15 @@ def test_create_refund_data_order_lines(
)
# 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,
+ assert refund_data.lines == {
+ line.variant_id: line.quantity - refund_line.quantity
+ for line, refund_line in zip(order_lines, order_refund_lines)
}
+ assert refund_data.shipping == refund_shipping_costs
- ["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
-):
[email protected]("refund_shipping_costs", [True, False])
+def test_create_refund_data_fulfillment_lines(fulfilled_order, refund_shipping_costs):
# given
fulfillment_lines = fulfilled_order.fulfillments.first().lines.all()
order_refund_lines = []
@@ -106,21 +94,15 @@ def test_create_refund_data_fulfillment_lines(
)
# 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,
+ assert refund_data.lines == {
+ line.order_line.variant_id: line.quantity - refund_line.quantity
+ for line, refund_line in zip(fulfillment_lines, fulfillment_refund_lines)
}
+ assert refund_data.shipping == refund_shipping_costs
- ["refund_shipping_costs", "shipping_line_quantity"], [(True, 0), (False, 1)]
-)
-def test_create_refund_data_shipping_only(
- order, refund_shipping_costs, shipping_line_quantity
-):
[email protected]("refund_shipping_costs", [True, False])
+def test_create_refund_data_shipping_only(order, refund_shipping_costs):
# given
order_refund_lines = []
fulfillment_refund_lines = []
@@ -131,7 +113,8 @@ def test_create_refund_data_shipping_only(
)
# then
- assert refund_data == {SHIPPING_PAYMENT_LINE_ID: shipping_line_quantity}
+ assert not refund_data.lines
+ assert refund_data.shipping == refund_shipping_costs
@patch("saleor.order.actions.gateway.refund")
@@ -139,13 +122,13 @@ def test_create_refund_data_shipping_only(
[
"previous_refund_shipping_costs",
"current_refund_shipping_costs",
- "shipping_line_quantity",
+ "refund_shipping_costs",
],
[
- (True, True, 0),
- (True, False, 0),
- (False, True, 0),
- (False, False, 1),
+ (True, True, True),
+ (True, False, True),
+ (False, True, True),
+ (False, False, False),
],
)
def test_create_refund_data_previously_refunded_order_lines(
@@ -154,7 +137,7 @@ def test_create_refund_data_previously_refunded_order_lines(
create_refund_fulfillment_helper,
previous_refund_shipping_costs,
current_refund_shipping_costs,
- shipping_line_quantity,
+ refund_shipping_costs,
):
# given
order_lines = order_with_lines.lines.all()
@@ -189,13 +172,11 @@ def test_create_refund_data_previously_refunded_order_lines(
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,
+ assert refund_data.lines == {
+ line.variant_id: line.quantity - refund_line.quantity
+ for line, refund_line in zip(order_lines, order_refund_lines)
}
+ assert refund_data.shipping == refund_shipping_costs
@patch("saleor.order.actions.gateway.refund")
@@ -203,13 +184,13 @@ def test_create_refund_data_previously_refunded_order_lines(
[
"previous_refund_shipping_costs",
"current_refund_shipping_costs",
- "shipping_line_quantity",
+ "refund_shipping_costs",
],
[
- (True, True, 0),
- (True, False, 0),
- (False, True, 0),
- (False, False, 1),
+ (True, True, True),
+ (True, False, True),
+ (False, True, True),
+ (False, False, False),
],
)
def test_create_refund_data_previously_refunded_fulfillment_lines(
@@ -218,7 +199,7 @@ def test_create_refund_data_previously_refunded_fulfillment_lines(
create_refund_fulfillment_helper,
previous_refund_shipping_costs,
current_refund_shipping_costs,
- shipping_line_quantity,
+ refund_shipping_costs,
):
# given
fulfillment_lines = list(
@@ -261,15 +242,13 @@ def test_create_refund_data_previously_refunded_fulfillment_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,
+ assert refund_data.lines == {
+ line.variant_id: line.quantity - refund_line.quantity
+ for line, refund_line in zip(
+ fulfilled_order.lines.all(), fulfillment_refund_lines
+ )
}
+ assert refund_data.shipping == refund_shipping_costs
@patch("saleor.order.actions.gateway.refund")
@@ -277,13 +256,13 @@ def test_create_refund_data_previously_refunded_fulfillment_lines(
[
"previous_refund_shipping_costs",
"current_refund_shipping_costs",
- "shipping_line_quantity",
+ "refund_shipping_costs",
],
[
- (True, True, 0),
- (True, False, 0),
- (False, True, 0),
- (False, False, 1),
+ (True, True, True),
+ (True, False, True),
+ (False, True, True),
+ (False, False, False),
],
)
def test_create_refund_data_previously_refunded_shipping_only(
@@ -292,7 +271,7 @@ def test_create_refund_data_previously_refunded_shipping_only(
create_refund_fulfillment_helper,
previous_refund_shipping_costs,
current_refund_shipping_costs,
- shipping_line_quantity,
+ refund_shipping_costs,
):
# given
create_refund_fulfillment_helper(
@@ -310,7 +289,8 @@ def test_create_refund_data_previously_refunded_shipping_only(
)
# then
- assert refund_data == {SHIPPING_PAYMENT_LINE_ID: shipping_line_quantity}
+ assert not refund_data.lines
+ assert refund_data.shipping == refund_shipping_costs
def test_create_payment_lines_information_order(payment_dummy):
@@ -318,28 +298,22 @@ def test_create_payment_lines_information_order(payment_dummy):
manager = get_plugins_manager()
# when
- payment_lines = create_payment_lines_information(payment_dummy, manager)
+ payment_lines_data = create_payment_lines_information(payment_dummy, manager)
# then
order = payment_dummy.order
- assert payment_lines == [
+ assert payment_lines_data.lines == [
PaymentLineData(
- gross=line.unit_price_gross_amount,
+ amount=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,
- )
]
+ assert payment_lines_data.shipping_amount == order.shipping_price_gross_amount
+ assert payment_lines_data.voucher_amount == Decimal("0.00")
def test_create_payment_lines_information_order_with_voucher(payment_dummy):
@@ -350,35 +324,21 @@ def test_create_payment_lines_information_order_with_voucher(payment_dummy):
manager = get_plugins_manager()
# when
- payment_lines = create_payment_lines_information(payment_dummy, manager)
+ payment_lines_data = create_payment_lines_information(payment_dummy, manager)
# then
-
- assert payment_lines == [
+ assert payment_lines_data.lines == [
PaymentLineData(
- gross=line.unit_price_gross_amount,
+ amount=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,
- ),
]
+ assert payment_lines_data.shipping_amount == order.shipping_price_gross_amount
+ assert payment_lines_data.voucher_amount == -voucher_amount
def get_expected_checkout_payment_lines(
@@ -400,7 +360,7 @@ def get_expected_checkout_payment_lines(
product_sku = line_info.variant.sku
expected_payment_lines.append(
PaymentLineData(
- gross=unit_gross,
+ amount=unit_gross,
variant_id=variant_id,
product_name=product_name,
product_sku=product_sku,
@@ -414,17 +374,12 @@ def get_expected_checkout_payment_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
+ return PaymentLinesData(
+ lines=expected_payment_lines,
+ shipping_amount=shipping_gross,
+ voucher_amount=Decimal("0.00"),
+ )
def test_create_payment_lines_information_checkout(payment_dummy, checkout_with_items):
@@ -466,20 +421,13 @@ def test_create_payment_lines_information_checkout_with_voucher(
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(
+ expected_payment_lines_data = 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
+ expected_payment_lines_data.voucher_amount = -voucher_amount
+
+ assert payment_lines == expected_payment_lines_data
def test_create_payment_lines_information_invalid_payment(payment_dummy):
@@ -488,10 +436,12 @@ def test_create_payment_lines_information_invalid_payment(payment_dummy):
payment_dummy.order = None
# when
- payment_lines = create_payment_lines_information(payment_dummy, manager)
+ payment_lines_data = create_payment_lines_information(payment_dummy, manager)
# then
- assert not payment_lines
+ assert not payment_lines_data.lines
+ assert not payment_lines_data.shipping_amount
+ assert not payment_lines_data.voucher_amount
def test_get_channel_slug_from_payment_with_order(payment_dummy):
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index c674fdcde0..4b24a0835d 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -34,7 +34,9 @@ from .interface import (
GatewayResponse,
PaymentData,
PaymentLineData,
+ PaymentLinesData,
PaymentMethodInfo,
+ RefundData,
StorePaymentMethodEnum,
)
from .models import Payment, Transaction
@@ -48,7 +50,7 @@ ALLOWED_GATEWAY_KINDS = {choices[0] for choices in TransactionKind.CHOICES}
def create_payment_lines_information(
payment: Payment,
manager: PluginsManager,
-) -> List[PaymentLineData]:
+) -> PaymentLinesData:
checkout = payment.checkout
order = payment.order
@@ -57,12 +59,16 @@ def create_payment_lines_information(
elif order:
return create_order_payment_lines_information(order)
- return []
+ return PaymentLinesData(
+ shipping_amount=Decimal("0.00"),
+ voucher_amount=Decimal("0.00"),
+ lines=[],
+ )
def create_checkout_payment_lines_information(
checkout: Checkout, manager: PluginsManager
-) -> List[PaymentLineData]:
+) -> PaymentLinesData:
line_items = []
lines, _ = fetch_checkout_lines(checkout)
discounts = fetch_active_discounts()
@@ -88,7 +94,7 @@ def create_checkout_payment_lines_information(
product_name=product_name,
product_sku=product_sku,
variant_id=line_info.variant.id,
- gross=unit_gross,
+ amount=unit_gross,
)
)
shipping_amount = manager.calculate_checkout_shipping(
@@ -98,16 +104,16 @@ def create_checkout_payment_lines_information(
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)
+ voucher_amount = -checkout.discount_amount
- return line_items
+ return PaymentLinesData(
+ shipping_amount=shipping_amount,
+ voucher_amount=voucher_amount,
+ lines=line_items,
+ )
-def create_order_payment_lines_information(order: Order) -> List[PaymentLineData]:
+def create_order_payment_lines_information(order: Order) -> PaymentLinesData:
line_items = []
for order_line in order.lines.all():
product_name = f"{order_line.product_name}, {order_line.variant_name}"
@@ -122,60 +128,17 @@ def create_order_payment_lines_information(order: Order) -> List[PaymentLineData
product_name=product_name,
product_sku=order_line.product_sku,
variant_id=variant_id,
- gross=order_line.unit_price_gross_amount,
+ amount=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
+ shipping_amount = order.shipping_price_gross_amount
+ voucher_amount = order.total_gross_amount - order.undiscounted_total_gross_amount
-
-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,
+ return PaymentLinesData(
+ shipping_amount=shipping_amount,
+ voucher_amount=voucher_amount,
+ lines=line_items,
)
@@ -185,7 +148,7 @@ def create_payment_information(
amount: Decimal = None,
customer_id: str = None,
store_source: bool = False,
- refund_data: Optional[Dict[int, int]] = None,
+ refund_data: Optional[RefundData] = None,
additional_data: Optional[dict] = None,
manager: Optional[PluginsManager] = None,
) -> PaymentData:
@@ -250,7 +213,7 @@ def create_payment_information(
payment_metadata=payment.metadata,
psp_reference=payment.psp_reference,
refund_data=refund_data,
- _resolve_lines=lambda: create_payment_lines_information(
+ _resolve_lines_data=lambda: create_payment_lines_information(
payment, manager or get_plugins_manager()
),
)
@@ -305,7 +268,7 @@ def create_refund_data(
order_lines_to_refund: List[OrderLineInfo],
fulfillment_lines_to_refund: List[FulfillmentLineData],
refund_shipping_costs: bool,
-) -> Dict[int, int]:
+) -> RefundData:
order_lines = {line.variant_id: line.quantity for line in order.lines.all()}
refund_lines = _prepare_refund_lines(
@@ -326,12 +289,10 @@ def create_refund_data(
shipping_refund_amount__isnull=True
).exists()
- lines[SHIPPING_PAYMENT_LINE_ID] = (
- 0 if shipping_previously_refunded or refund_shipping_costs else 1
+ return RefundData(
+ shipping=shipping_previously_refunded or refund_shipping_costs, lines=lines
)
- return lines
-
def create_payment(
gateway: str,