Skip to content

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

Merge remote-tracking branch 'upstream/main' into feature/net-protections-3.1

Krzysztof Kulig494426c4d96a
diff --cc .isort.cfg
index dde7141ca1,6f5dbb7464..ba1f620a61
--- a/.isort.cfg
+++ b/.isort.cfg
@@@ -1,2 -1,2 +1,2 @@@
[settings]
- known_third_party = Adyen,PIL,authorizenet,babel,boto3,botocore,braintree,celery,dateutil,dj_database_url,dj_email_url,django,django_cache_url,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,google,google_measurement_protocol,graphene,graphene_django,graphene_federation,graphql,graphql_relay,html2text,html_to_draftjs,i18naddress,jaeger_client,jwt,kombu,lxml,markdown,measurement,micawber,mptt,oauthlib,openpyxl,opentracing,petl,phonenumber_field,phonenumbers,pkg_resources,posuto,prices,promise,pybars,pytest,pythonjsonlogger,pytimeparse,pytz,razorpay,requests,sendgrid,sentry_sdk,storages,stripe,urllib3,uvicorn,versatileimagefield,weasyprint
-known_third_party = Adyen,PIL,authorizenet,babel,boto3,botocore,braintree,celery,cryptography,dateutil,dj_database_url,dj_email_url,django,django_cache_url,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,google,google_measurement_protocol,graphene,graphene_django,graphene_federation,graphql,graphql_relay,html2text,html_to_draftjs,i18naddress,jaeger_client,jwt,kombu,lxml,markdown,measurement,micawber,mptt,oauthlib,openpyxl,opentracing,petl,phonenumber_field,phonenumbers,pkg_resources,prices,promise,pybars,pytest,pythonjsonlogger,pytimeparse,pytz,razorpay,requests,sendgrid,sentry_sdk,storages,stripe,urllib3,uvicorn,versatileimagefield,weasyprint
++known_third_party = Adyen,PIL,authorizenet,babel,boto3,botocore,braintree,celery,cryptography,dateutil,dj_database_url,dj_email_url,django,django_cache_url,django_countries,django_filters,django_measurement,django_prices,django_prices_openexchangerates,django_prices_vatlayer,draftjs_sanitizer,faker,freezegun,google,google_measurement_protocol,graphene,graphene_django,graphene_federation,graphql,graphql_relay,html2text,html_to_draftjs,i18naddress,jaeger_client,jwt,kombu,lxml,markdown,measurement,micawber,mptt,oauthlib,openpyxl,opentracing,petl,phonenumber_field,phonenumbers,pkg_resources,posuto,prices,promise,pybars,pytest,pythonjsonlogger,pytimeparse,pytz,razorpay,requests,sendgrid,sentry_sdk,storages,stripe,urllib3,uvicorn,versatileimagefield,weasyprint
diff --cc pyproject.toml
index 5ab934fb4e,a02d76b1a6..b2d4ac3017
--- a/pyproject.toml
+++ b/pyproject.toml
@@@ -66,7 -66,7 +66,8 @@@ documentation = "https://docs.saleor.io
sendgrid = "^6.7.1"
micawber = "^0.5.2"
django-celery-beat = "^2.2.1"
+ posuto = "^2021.10.0"
+ cryptography = "^36.0.0"
[tool.poetry.dependencies.celery]
version = ">=4.4.5,<6.0.0"
diff --cc requirements.txt
index fc31647eca,793d1259e9..1aa6fe5940
--- a/requirements.txt
+++ b/requirements.txt
@@@ -91,14 -92,13 +92,14 @@@ 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.41
- pillow==9.0.0; python_version >= "3.7"
+ phonenumberslite==8.12.42
+ pillow==9.0.1; python_version >= "3.7"
+posuto==2021.10.0
prices==1.1.0
promise==2.3
- 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.3
+ prompt-toolkit==3.0.26; python_full_version >= "3.6.2" and python_version >= "3.6"
+ proto-plus==1.19.9; python_version >= "3.6"
+ protobuf==3.19.4
psycopg2-binary==2.9.3; python_version >= "3.6"
pyasn1-modules==0.2.8; python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.6.0" and python_version >= "3.7"
pyasn1==0.4.8; python_version >= "3.7" and python_full_version < "3.0.0" and python_version < "4" and (python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.6.0" and python_version >= "3.7") or python_full_version >= "3.6.0" and python_version >= "3.7" and python_version < "4" and (python_version >= "3.7" and python_full_version < "3.0.0" or python_full_version >= "3.6.0" and python_version >= "3.7")
diff --cc requirements_dev.txt
index cbff234654,7fa6c321a8..07022e25b5
--- a/requirements_dev.txt
+++ b/requirements_dev.txt
@@@ -122,11 -123,10 +123,11 @@@ 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.41
- pillow==9.0.0; python_version >= "3.7"
+ phonenumberslite==8.12.42
+ pillow==9.0.1; 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.17.0; python_full_version >= "3.6.1"
prices==1.1.0
promise==2.3; python_version >= "3.6" and python_version < "4.0"
diff --cc saleor/payment/tests/test_utils.py
index e5ce136b60,9c2cf6f68e..9868fa6294
--- a/saleor/payment/tests/test_utils.py
+++ b/saleor/payment/tests/test_utils.py
@@@ -1,492 -1,61 +1,552 @@@
-from unittest.mock import patch
+from decimal import Decimal
+from itertools import chain, zip_longest
+from typing import List
+from unittest.mock import Mock, patch
-from ..utils import get_channel_slug_from_payment, try_void_or_refund_inactive_payment
+import pytest
+
+from ...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 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,
++ get_channel_slug_from_payment,
++ try_void_or_refund_inactive_payment,
+)
+
+
+def create_refund_fulfillment_helper(payment_dummy):
+ def factory(
+ order: Order,
+ 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 = [
+ 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 = [
+ 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
+ refund_data = create_refund_data(
+ order_with_lines,
+ current_order_refund_lines,
+ fulfillment_refund_lines,
+ current_refund_shipping_costs,
+ )
+
+ # 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 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:
+ unit_gross = manager.calculate_checkout_line_unit_price(
+ checkout_info,
+ lines,
+ line_info,
+ address,
+ discounts,
+ ).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)
++ 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)
++ 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_lines
+
+
+ def test_get_channel_slug_from_payment_with_order(payment_dummy):
+ expected = payment_dummy.order.channel.slug
+ assert get_channel_slug_from_payment(payment_dummy) == expected
+
+
+ def test_get_channel_slug_from_payment_with_checkout(checkout_with_payments):
+ payment = checkout_with_payments.payments.first()
+ expected = checkout_with_payments.channel.slug
+ assert get_channel_slug_from_payment(payment) == expected
+
+
+ def test_get_channel_slug_from_payment_without_checkout_and_order(
+ checkout_with_payments,
+ ):
+ payment = checkout_with_payments.payments.first()
+ payment.checkout.delete()
+ payment.refresh_from_db()
+ assert not get_channel_slug_from_payment(payment)
+
+
+ @patch("saleor.payment.utils.update_payment_charge_status")
+ @patch("saleor.payment.utils.get_channel_slug_from_payment")
+ @patch("saleor.payment.gateway.payment_refund_or_void")
+ def test_try_void_or_refund_inactive_payment_failed_transaction(
+ refund_or_void_mock,
+ get_channel_slug_from_payment_mock,
+ update_payment_charge_status_mock,
+ payment_txn_capture_failed,
+ ):
+ transaction = payment_txn_capture_failed.transactions.first()
+
+ assert not try_void_or_refund_inactive_payment(
+ payment_txn_capture_failed, transaction, None
+ )
+ assert not update_payment_charge_status_mock.called
+ assert not get_channel_slug_from_payment_mock.called
+ assert not refund_or_void_mock.called
+
+
+ @patch("saleor.payment.utils.update_payment_charge_status")
+ @patch("saleor.payment.utils.get_channel_slug_from_payment")
+ @patch("saleor.payment.gateway.payment_refund_or_void")
+ def test_try_void_or_refund_inactive_payment_transaction_success(
+ refund_or_void_mock,
+ get_channel_slug_from_payment_mock,
+ update_payment_charge_status_mock,
+ payment_txn_captured,
+ ):
+ transaction = payment_txn_captured.transactions.first()
+
+ assert not try_void_or_refund_inactive_payment(
+ payment_txn_captured, transaction, None
+ )
+ assert update_payment_charge_status_mock.called
+ assert get_channel_slug_from_payment_mock.called
+ assert refund_or_void_mock.called
diff --cc saleor/payment/utils.py
index 2ccf25e130,c6e168d0d9..c674fdcde0
--- a/saleor/payment/utils.py
+++ b/saleor/payment/utils.py
@@@ -44,140 -40,6 +45,140 @@@ GENERIC_TRANSACTION_ERROR = "Transactio
ALLOWED_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)
++ 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:
+ unit_price = manager.calculate_checkout_line_unit_price(
+ checkout_info,
+ lines,
+ line_info,
+ address,
+ discounts,
+ )
+ 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,