Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fixes incorrect gift card balances after covering the full order total (#17566)
Maciej Korycinski8d0c9b969276
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fd4c56ae40..d6318acbd9 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -106,3 +106,4 @@ All notable, unreleased changes to this project will be documented in this file.certain types of data. - #17506 by @NyanKiyoshi- Improve status calculation for orders with waiting-for-approval fulfillments - #17471 by @delemeator- Allow to change Admin email plugin custom templates back to default - #17563 by @wcislo-saleor+- Fixes incorrect gift card balances after covering the full order total - #17566 by @korycinsdiff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.pyindex 0cef6d0fab..15379029d9 100644--- a/saleor/checkout/complete_checkout.py+++ b/saleor/checkout/complete_checkout.py@@ -623,15 +623,11 @@ def _prepare_order_data(order_data.update(_process_voucher_data_for_order(checkout_info))- order_data["total_price_left"] = (- calculations.checkout_subtotal(- manager=manager,- checkout_info=checkout_info,- lines=lines,- address=address,- )- + undiscounted_base_shipping_price- - checkout.discount+ order_data["total_price_left"] = calculations.checkout_total(+ manager=manager,+ checkout_info=checkout_info,+ lines=lines,+ address=address,).grosstry:@@ -1419,14 +1415,13 @@ def _create_order_from_checkout()# giftcards- total_without_giftcard = (- order.subtotal- + undiscounted_base_shipping_price- - checkout_info.checkout.discount- )- add_gift_cards_to_order(- checkout_info, order, total_without_giftcard.gross, user, app- )+ total_without_giftcard = calculations.checkout_total(+ manager=manager,+ checkout_info=checkout_info,+ lines=checkout_lines_info,+ address=address,+ ).gross+ add_gift_cards_to_order(checkout_info, order, total_without_giftcard, user, app)# paymentscheckout_info.checkout.payments.update(order=order, checkout_id=None)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.pyindex f38b7582f4..83ec567aee 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@@ -1488,7 +1488,7 @@ def test_checkout_complete_with_voucher_single_use(@pytest.mark.integration-def test_checkout_complete_with_voucher_and_gift_card(+def test_checkout_complete_with_voucher_paid_with_gift_card_and_payment(user_api_client,checkout_with_voucher_percentage,voucher_percentage,@@ -1610,6 +1610,114 @@ def test_checkout_complete_with_voucher_and_gift_card()+def test_checkout_complete_with_voucher_paid_by_gift_card(+ user_api_client,+ checkout_with_voucher_percentage,+ voucher_percentage,+ gift_card,+ address,+ shipping_method,+):+ # given+ code = voucher_percentage.codes.first()+ voucher_used_count = code.used+ voucher_percentage.usage_limit = voucher_used_count + 1+ voucher_percentage.save(update_fields=["usage_limit"])++ checkout = checkout_with_voucher_percentage+ checkout.shipping_address = address+ checkout.shipping_method = shipping_method+ checkout.billing_address = address+ checkout.save()++ checkout.gift_cards.add(gift_card)++ expected_voucher_discount = checkout.discount++ checkout_line = checkout.lines.first()+ checkout_line_quantity = checkout_line.quantity+ checkout_line_variant = checkout_line.variant++ manager = get_plugins_manager(allow_replica=False)+ lines, _ = fetch_checkout_lines(checkout)+ checkout_info = fetch_checkout_info(checkout, lines, manager)++ total_without_gc = calculations.checkout_total(+ manager=manager, checkout_info=checkout_info, lines=lines, address=address+ )++ gift_card.initial_balance_amount = total_without_gc.gross.amount + Decimal("1")+ gift_card.current_balance_amount = total_without_gc.gross.amount + Decimal("1")+ gift_card.save()++ expected_gc_balance_amount = (+ gift_card.initial_balance_amount - total_without_gc.gross.amount+ )++ total = calculations.calculate_checkout_total_with_gift_cards(+ manager, checkout_info, lines, address+ )++ shipping_price = shipping_method.channel_listings.get(+ channel=checkout.channel+ ).price++ 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.get()++ subtotal = get_subtotal(order.lines.all(), order.currency)+ assert order.subtotal == subtotal+ assert data["order"]["subtotal"]["gross"]["amount"] == subtotal.gross.amount+ assert order.total == total+ assert (+ order.undiscounted_total+ == subtotal + shipping_price + expected_voucher_discount+ )++ order_line = order.lines.first()+ assert checkout_line_quantity == order_line.quantity+ assert checkout_line_variant == order_line.variant+ assert order.shipping_address == address+ assert order.shipping_method == checkout.shipping_method++ assert (+ order_line.unit_discount_amount+ == (expected_voucher_discount / checkout_line_quantity).amount+ )+ assert order_line.unit_discount_reason++ code.refresh_from_db()+ assert code.used == voucher_used_count + 1+ order_discount = order.discounts.filter(type=DiscountType.VOUCHER).get()+ assert order_discount.amount_value == expected_voucher_discount.amount+ assert order.voucher == voucher_percentage+ assert order.voucher.code == code.code++ assert not Checkout.objects.filter(pk=checkout.pk).exists(), (+ "Checkout should have been deleted"+ )++ gift_card.refresh_from_db()+ assert gift_card.current_balance.amount == expected_gc_balance_amount+ assert gift_card.last_used_on+ assert GiftCardEvent.objects.filter(+ gift_card=gift_card, type=GiftCardEvents.USED_IN_ORDER+ )++@pytest.mark.integrationdef test_checkout_complete_free_shipping_voucher_and_gift_card(user_api_client,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.pyindex 54f8443b8e..625ba54a95 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@@ -2028,7 +2028,7 @@ def test_checkout_with_voucher_complete(@pytest.mark.integration-def test_checkout_complete_with_entire_order_voucher_and_gift_card(+def test_checkout_complete_with_entire_order_voucher_paid_with_gift_card_and_transaction(user_api_client,checkout_with_voucher_percentage,voucher_percentage,@@ -2138,6 +2138,108 @@ def test_checkout_complete_with_entire_order_voucher_and_gift_card()+def test_checkout_complete_with_voucher_paid_with_gift_card(+ user_api_client,+ checkout_with_voucher_percentage,+ voucher_percentage,+ gift_card,+ address,+ shipping_method,+ transaction_events_generator,+ transaction_item_generator,+):+ # given+ checkout = prepare_checkout_for_test(+ checkout_with_voucher_percentage,+ address,+ address,+ shipping_method,+ transaction_item_generator,+ transaction_events_generator,+ )+ checkout.gift_cards.add(gift_card)++ code = voucher_percentage.codes.first()+ voucher_used_count = code.used+ voucher_percentage.usage_limit = voucher_used_count + 1+ voucher_percentage.save(update_fields=["usage_limit"])++ expected_voucher_discount = checkout.discount++ manager = get_plugins_manager(allow_replica=False)+ lines, _ = fetch_checkout_lines(checkout)+ checkout_info = fetch_checkout_info(checkout, lines, manager)++ total_without_gc = calculations.checkout_total(+ manager=manager, checkout_info=checkout_info, lines=lines, address=address+ )++ gift_card.initial_balance_amount = total_without_gc.gross.amount + Decimal("1")+ gift_card.current_balance_amount = total_without_gc.gross.amount + Decimal("1")+ gift_card.save()++ expected_gc_balance_amount = (+ gift_card.initial_balance_amount - total_without_gc.gross.amount+ )++ total = calculations.calculate_checkout_total_with_gift_cards(+ manager, checkout_info, lines, address+ )++ shipping_price = shipping_method.channel_listings.get(+ channel=checkout.channel+ ).price++ 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.get()+ assert order.total == total+ subtotal = get_subtotal(order.lines.all(), order.currency)+ assert order.subtotal.gross == subtotal.gross+ assert (+ order.undiscounted_total+ == subtotal + shipping_price + expected_voucher_discount+ )++ code.refresh_from_db()+ assert code.used == voucher_used_count + 1+ order_discount = order.discounts.filter(type=DiscountType.VOUCHER).first()+ assert order_discount+ assert order_discount.amount_value == expected_voucher_discount.amount+ assert order.voucher == voucher_percentage+ assert order.voucher.code == code.code++ assert not Checkout.objects.filter(pk=checkout.pk).exists(), (+ "Checkout should have been deleted"+ )++ order_line = order.lines.first()+ assert (+ order_line.unit_discount_amount+ == (expected_voucher_discount / order_line.quantity).amount+ )+ assert order_line.unit_discount_reason++ gift_card.refresh_from_db()+ assert gift_card.current_balance.amount == expected_gc_balance_amount+ assert gift_card.last_used_on+ assert GiftCardEvent.objects.filter(+ gift_card=gift_card, type=GiftCardEvents.USED_IN_ORDER+ )++@pytest.mark.integrationdef test_checkout_complete_with_voucher_apply_once_per_order(user_api_client,