Skip to content

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

Add logs when checkout is paid by gift cards. (#16325)

Piotr Zabieglikc85de14db1d6
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 fea7af4ef9..a5c48387f9 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
@@ -170,7 +170,9 @@ def test_checkout_complete(
payment_dummy,
address,
shipping_method,
+ caplog,
):
+ # given
assert not gift_card.last_used_on
checkout = checkout_with_gift_card
@@ -210,8 +212,11 @@ def test_checkout_complete(
orders_count = Order.objects.count()
redirect_url = "https://www.example.com"
variables = {"id": to_global_id_or_none(checkout), "redirectUrl": redirect_url}
+
+ # 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"]
@@ -275,6 +280,14 @@ def test_checkout_complete(
assert not len(Reservation.objects.all())
+ assert str(checkout_info.checkout.pk) == caplog.records[0].checkout_id
+ assert gift_card.initial_balance_amount == Decimal(
+ caplog.records[0].gift_card_compensation
+ )
+ assert total.gross.amount == Decimal(
+ caplog.records[0].total_after_gift_card_compensation
+ )
+
@pytest.mark.integration
@patch("saleor.plugins.manager.PluginsManager.order_confirmed")
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 c48d7a3503..93cdf40b33 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
@@ -1175,6 +1175,7 @@ def test_checkout_complete(
shipping_method,
transaction_events_generator,
transaction_item_generator,
+ caplog,
):
# given
checkout = prepare_checkout_for_test(
@@ -1276,6 +1277,14 @@ def test_checkout_complete(
assert not len(Reservation.objects.all())
+ assert str(checkout_info.checkout.pk) == caplog.records[0].checkout_id
+ assert gift_card.initial_balance_amount == Decimal(
+ caplog.records[0].gift_card_compensation
+ )
+ assert total.gross.amount == Decimal(
+ caplog.records[0].total_after_gift_card_compensation
+ )
+
@pytest.mark.integration
@patch("saleor.plugins.manager.PluginsManager.order_confirmed")
diff --git a/saleor/order/utils.py b/saleor/order/utils.py
index 77497c2166..d089c315a3 100644
--- a/saleor/order/utils.py
+++ b/saleor/order/utils.py
@@ -1,3 +1,4 @@
+import logging
from collections.abc import Iterable
from decimal import Decimal
from typing import TYPE_CHECKING, Optional, cast
@@ -69,6 +70,8 @@ if TYPE_CHECKING:
from ..payment.models import Payment, TransactionItem
from ..plugins.manager import PluginsManager
+logger = logging.getLogger(__name__)
+
def get_order_country(order: Order) -> str:
"""Return country to which order will be shipped."""
@@ -447,6 +450,7 @@ def add_gift_cards_to_order(
user: Optional[User],
app: Optional["App"],
):
+ total_before_gift_card_compensation = total_price_left
order_gift_cards = []
gift_cards_to_update = []
balance_data: list[tuple[GiftCard, float]] = []
@@ -475,6 +479,15 @@ def add_gift_cards_to_order(
GiftCard.objects.bulk_update(gift_cards_to_update, update_fields)
gift_card_events.gift_cards_used_in_order_event(balance_data, order, user, app)
+ gift_card_compensation = total_before_gift_card_compensation - total_price_left
+ if gift_card_compensation.amount > 0:
+ details = {
+ "checkout_id": str(checkout_info.checkout.pk),
+ "gift_card_compensation": str(gift_card_compensation.amount),
+ "total_after_gift_card_compensation": str(total_price_left.amount),
+ }
+ logger.info("Gift card payment.", extra=details)
+
def update_gift_card_balance(
gift_card: GiftCard,