Skip to content

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

Use calculation module for building PaymentData object instead of direct plugin calls (#17728)

Maciej Korycinskid73ce407c2f1
diff --git a/saleor/payment/tests/test_utils.py b/saleor/payment/tests/test_utils.py
index 465c4ac176..e4f2cadcee 100644
--- a/saleor/payment/tests/test_utils.py
+++ b/saleor/payment/tests/test_utils.py
@@ -6,7 +6,7 @@ from unittest.mock import patch
import pytest
from freezegun import freeze_time
-from ...checkout import CheckoutAuthorizeStatus
+from ...checkout import CheckoutAuthorizeStatus, calculations
from ...checkout.fetch import fetch_checkout_info, fetch_checkout_lines
from ...order import OrderAuthorizeStatus, OrderChargeStatus, OrderGrantedRefundStatus
from ...plugins.manager import get_plugins_manager
@@ -68,7 +68,6 @@ def test_create_payment_lines_information_order_with_voucher(payment_dummy):
# when
payment_lines_data = create_payment_lines_information(payment_dummy, manager)
- # then
assert payment_lines_data.lines == [
PaymentLineData(
amount=line.unit_price_gross_amount,
@@ -87,11 +86,11 @@ def get_expected_checkout_payment_lines(manager, checkout_info, lines, address):
expected_payment_lines = []
for line_info in lines:
- unit_gross = manager.calculate_checkout_line_unit_price(
- checkout_info,
- lines,
- line_info,
- address,
+ unit_gross = calculations.checkout_line_unit_price(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ checkout_line_info=line_info,
).gross.amount
quantity = line_info.line.quantity
variant_id = line_info.variant.id
@@ -107,7 +106,8 @@ def get_expected_checkout_payment_lines(manager, checkout_info, lines, address):
)
)
- shipping_gross = manager.calculate_checkout_shipping(
+ shipping_gross = calculations.checkout_shipping_price(
+ manager=manager,
checkout_info=checkout_info,
lines=lines,
address=address,
@@ -120,8 +120,22 @@ def get_expected_checkout_payment_lines(manager, checkout_info, lines, address):
)
-def test_create_payment_lines_information_checkout(payment_dummy, checkout_with_items):
+def test_create_payment_lines_information_checkout_with_flat_rates(
+ payment_dummy,
+ checkout_with_items,
+ tax_configuration_flat_rates,
+ default_tax_class,
+ address,
+ shipping_method,
+):
# given
+ tax_configuration_flat_rates.prices_entered_with_tax = False
+ tax_configuration_flat_rates.save()
+
+ checkout_with_items.shipping_address = address
+ checkout_with_items.shipping_method = shipping_method
+ checkout_with_items.save()
+
manager = get_plugins_manager(allow_replica=False)
payment_dummy.order = None
payment_dummy.checkout = checkout_with_items
@@ -131,8 +145,10 @@ def test_create_payment_lines_information_checkout(payment_dummy, checkout_with_
# then
lines, _ = fetch_checkout_lines(checkout_with_items)
+
checkout_info = fetch_checkout_info(checkout_with_items, lines, manager)
address = checkout_with_items.shipping_address
+
expected_payment_lines = get_expected_checkout_payment_lines(
manager, checkout_info, lines, address
)
@@ -140,14 +156,10 @@ def test_create_payment_lines_information_checkout(payment_dummy, checkout_with_
assert payment_lines == expected_payment_lines
-def test_create_payment_lines_information_checkout_with_voucher(
- payment_dummy, checkout_with_items
-):
+def test_create_payment_lines_information_checkout(payment_dummy, checkout_with_items):
# given
manager = get_plugins_manager(allow_replica=False)
- voucher_amount = Decimal("12.30")
payment_dummy.order = None
- checkout_with_items.discount_amount = voucher_amount
payment_dummy.checkout = checkout_with_items
# when
@@ -157,6 +169,31 @@ def test_create_payment_lines_information_checkout_with_voucher(
lines, _ = fetch_checkout_lines(checkout_with_items)
checkout_info = fetch_checkout_info(checkout_with_items, lines, manager)
address = checkout_with_items.shipping_address
+ expected_payment_lines = get_expected_checkout_payment_lines(
+ manager, checkout_info, lines, address
+ )
+
+ assert payment_lines == expected_payment_lines
+
+
+def test_create_payment_lines_information_checkout_with_voucher(
+ payment_dummy, checkout_with_voucher
+):
+ # given
+ manager = get_plugins_manager(allow_replica=False)
+ voucher_amount = Decimal("12.30")
+ payment_dummy.order = None
+ checkout = checkout_with_voucher
+ checkout.discount_amount = voucher_amount
+ payment_dummy.checkout = checkout
+
+ # when
+ payment_lines = create_payment_lines_information(payment_dummy, manager)
+
+ # then
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+ address = checkout.shipping_address
expected_payment_lines_data = get_expected_checkout_payment_lines(
manager, checkout_info, lines, address
)
diff --git a/saleor/payment/utils.py b/saleor/payment/utils.py
index 4a588dab83..4c06deb3bc 100644
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@ -16,6 +16,7 @@ from pydantic import ValidationError
from ..account.models import User
from ..app.models import App
from ..channel import TransactionFlowStrategy
+from ..checkout import calculations
from ..checkout.actions import (
transaction_amounts_for_checkout_updated,
update_last_transaction_modified_at_for_checkout,
@@ -168,11 +169,11 @@ def create_checkout_payment_lines_information(
address = checkout_info.shipping_address or checkout_info.billing_address
for line_info in lines:
- unit_price = manager.calculate_checkout_line_unit_price(
- checkout_info,
- lines,
- line_info,
- address,
+ unit_price = calculations.checkout_line_unit_price(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ checkout_line_info=line_info,
)
unit_gross = unit_price.gross.amount
@@ -188,12 +189,12 @@ def create_checkout_payment_lines_information(
amount=unit_gross,
)
)
- shipping_amount = manager.calculate_checkout_shipping(
+ shipping_amount = calculations.checkout_shipping_price(
+ manager=manager,
checkout_info=checkout_info,
lines=lines,
address=address,
).gross.amount
-
voucher_amount = -checkout.discount_amount
return PaymentLinesData(