Skip to content

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

Log checkout details when unknown discount reason occur during checkout complete (#16836)

Piotr Zabieglik42cd7ef8d949
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index 33db9bcc01..d932b425eb 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -87,6 +87,7 @@ from .utils import (
get_checkout_metadata,
get_or_create_checkout_metadata,
get_voucher_for_checkout_info,
+ log_unknown_discount_reason,
)
if TYPE_CHECKING:
@@ -477,7 +478,7 @@ def _create_lines_for_order(
replace=True,
check_reservations=True,
)
- return [
+ order_lines_info = [
_create_line_for_order(
manager,
checkout_info,
@@ -490,6 +491,12 @@ def _create_lines_for_order(
for checkout_line_info in lines
]
+ log_unknown_discount_reason(
+ [line_info.line for line_info in order_lines_info], checkout_info, lines, logger
+ )
+
+ return order_lines_info
+
def _prepare_order_data(
*,
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 125cf6ceac..b7af3dac5e 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -68,7 +68,7 @@ if TYPE_CHECKING:
from ..account.models import Address
from ..core.pricing.interface import LineInfo
- from ..order.models import Order
+ from ..order.models import Order, OrderLine
from .fetch import CheckoutInfo, CheckoutLineInfo
@@ -1131,3 +1131,24 @@ def checkout_info_for_logs(
for line_info in checkout_lines_info
],
}
+
+
+def log_unknown_discount_reason(
+ order_lines: Iterable["OrderLine"],
+ checkout_info: "CheckoutInfo",
+ checkout_lines_info: Iterable["CheckoutLineInfo"],
+ logger,
+):
+ prices_entered_with_tax = checkout_info.tax_configuration.prices_entered_with_tax
+ for line in order_lines:
+ discount_price = line.undiscounted_unit_price - line.unit_price
+ if prices_entered_with_tax:
+ discount_amount = discount_price.gross
+ else:
+ discount_amount = discount_price.net
+ if discount_amount.amount > 0 and not line.unit_discount_reason:
+ logger.warning(
+ "Unknown discount reason",
+ extra=checkout_info_for_logs(checkout_info, checkout_lines_info),
+ )
+ return
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
index 15b41b90f9..e9eea36359 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_payment.py
@@ -4961,3 +4961,61 @@ def test_checkout_complete_with_invalid_address(
order = Order.objects.get(checkout_token=checkout.token)
assert order.shipping_address.postal_code == invalid_postal_code
assert order.billing_address.postal_code == invalid_postal_code
+
+
+@patch("saleor.checkout.complete_checkout._get_unit_discount_reason")
+def test_checkout_complete_log_unknown_discount_reason(
+ mocked_discount_reason,
+ user_api_client,
+ checkout_with_item_and_voucher_specific_products,
+ voucher_specific_product_type,
+ payment_dummy,
+ address,
+ shipping_method,
+ caplog,
+):
+ # given
+ mocked_discount_reason.return_value = None
+
+ checkout = checkout_with_item_and_voucher_specific_products
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.save(
+ update_fields=["shipping_address", "shipping_method", "billing_address"]
+ )
+
+ manager = get_plugins_manager(allow_replica=False)
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, manager)
+
+ total = calculations.checkout_total(
+ manager=manager, checkout_info=checkout_info, lines=lines, address=address
+ )
+ payment = payment_dummy
+ payment.is_active = True
+ payment.order = None
+ payment.total = total.gross.amount
+ payment.currency = total.gross.currency
+ payment.checkout = checkout
+ payment.save()
+ assert not payment.transactions.exists()
+
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "redirectUrl": "https://www.example.com",
+ }
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["errors"]
+
+ order = Order.objects.first()
+ order_line = order.lines.first()
+ assert not order_line.unit_discount_reason
+ assert "Unknown discount reason" in caplog.text
+ assert caplog.records[0].checkout_id == to_global_id_or_none(checkout)
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
index 6b22febda2..72bdc044e5 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete_with_transactions.py
@@ -4487,3 +4487,47 @@ def test_checkout_complete_with_invalid_address(
assert not data["errors"]
assert order.shipping_address.postal_code == invalid_postal_code
assert order.billing_address.postal_code == invalid_postal_code
+
+
+@patch("saleor.checkout.complete_checkout._get_unit_discount_reason")
+def test_checkout_complete_log_unknown_discount_reason(
+ mocked_discount_reason,
+ user_api_client,
+ checkout_with_item_and_voucher_specific_products,
+ voucher_specific_product_type,
+ address,
+ shipping_method,
+ transaction_events_generator,
+ transaction_item_generator,
+ caplog,
+):
+ # given
+ mocked_discount_reason.return_value = None
+
+ checkout = prepare_checkout_for_test(
+ checkout_with_item_and_voucher_specific_products,
+ address,
+ address,
+ shipping_method,
+ transaction_item_generator,
+ transaction_events_generator,
+ )
+
+ variables = {
+ "id": to_global_id_or_none(checkout),
+ "redirectUrl": "https://www.example.com",
+ }
+
+ # when
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ # then
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["errors"]
+
+ order = Order.objects.first()
+ order_line = order.lines.first()
+ assert not order_line.unit_discount_reason
+ assert "Unknown discount reason" in caplog.text
+ assert caplog.records[0].checkout_id == to_global_id_or_none(checkout)