Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix a bug where Checkout having Transaction and Gift Card couldn't be completed due to incorrectly raised `CHECKOUT_NOT_FULLY_PAID` error (#18103)
Marcin Wcisło940b8d83ec5c
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 514eeaf5cb..0e7dc29220 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -136,6 +136,8 @@ Like `reference`, the `single-reference` type can target entities defined in the- Fixed a bug that could prevent rich text attributes written in scripts using combining diacritical marks (for example, Arabic) from being saved properly.+- Fixed a bug where a Checkout partially paid by Transaction(s) and partially paid by Gift Card(s) could not be completed due to `CHECKOUT_NOT_FULLY_PAID` error. Checkout authorize and charge statuses are now recalculcated more reliably. Status calculcation is now taking into account available gift cards balance.+### DeprecationsFollowing plugins are now marked as deprecated:diff --git a/saleor/checkout/actions.py b/saleor/checkout/actions.pyindex 2af0e92de2..ab178d7a3f 100644--- a/saleor/checkout/actions.py+++ b/saleor/checkout/actions.py@@ -4,6 +4,7 @@ from typing import TYPE_CHECKING, Optional, castfrom django.utils import timezone+from ..core.taxes import zero_moneyfrom ..core.utils.events import (call_event_including_protected_events,webhook_async_event_requires_sync_webhooks_to_trigger,@@ -241,7 +242,14 @@ def transaction_amounts_for_checkout_updated_without_price_recalculation(previous_charge_status = checkout_info.checkout.charge_statusprevious_authorize_status = checkout_info.checkout.authorize_status- current_total_gross = checkout_info.checkout.total.gross+ current_total_gross = (+ checkout_info.checkout.total.gross+ - checkout_info.checkout.get_total_gift_cards_balance()+ )+ current_total_gross = max(+ current_total_gross, zero_money(current_total_gross.currency)+ )+update_checkout_payment_statuses(checkout=checkout_info.checkout,checkout_total_gross=current_total_gross,diff --git a/saleor/checkout/calculations.py b/saleor/checkout/calculations.pyindex f4653344cc..7a3c902e31 100644--- a/saleor/checkout/calculations.py+++ b/saleor/checkout/calculations.py@@ -802,7 +802,7 @@ def fetch_checkout_data("""if pregenerated_subscription_payloads is None:pregenerated_subscription_payloads = {}- previous_total_gross = checkout_info.checkout.total.gross+ previous_checkout_price_expiration = checkout_info.checkout.price_expirationcheckout_info, lines = _fetch_checkout_prices_if_expired(checkout_info=checkout_info,manager=manager,@@ -815,7 +815,7 @@ def fetch_checkout_data()current_total_gross = checkout_info.checkout.total.grossif (- current_total_gross != previous_total_gross+ checkout_info.checkout.price_expiration != previous_checkout_price_expirationor force_status_updateor (# Checkout with total being zero is fully authorized therefore@@ -825,6 +825,15 @@ def fetch_checkout_data(and bool(lines))):+ current_total_gross = (+ checkout_info.checkout.total.gross+ - checkout_info.checkout.get_total_gift_cards_balance(+ database_connection_name+ )+ )+ current_total_gross = max(+ current_total_gross, zero_money(current_total_gross.currency)+ )update_checkout_payment_statuses(checkout=checkout_info.checkout,checkout_total_gross=current_total_gross,diff --git a/saleor/checkout/tests/test_actions.py b/saleor/checkout/tests/test_actions.pyindex 82e99ce779..9bf3d9f8fe 100644--- a/saleor/checkout/tests/test_actions.py+++ b/saleor/checkout/tests/test_actions.py@@ -18,8 +18,9 @@ from ..actions import (call_checkout_events,call_checkout_info_event,transaction_amounts_for_checkout_updated,+ transaction_amounts_for_checkout_updated_without_price_recalculation,)-from ..calculations import fetch_checkout_data+from ..calculations import calculate_checkout_total, fetch_checkout_datafrom ..fetch import fetch_checkout_info, fetch_checkout_lines@@ -1680,3 +1681,55 @@ def test_call_checkout_events_only_async_when_sync_missing(call(plugins_manager.checkout_updated, checkout_with_items, webhooks=set()),])+++ ("gift_card_balance", "expected_authorize_status", "expected_charge_status"),+ [+ (0, CheckoutAuthorizeStatus.PARTIAL, CheckoutChargeStatus.PARTIAL),+ (10, CheckoutAuthorizeStatus.PARTIAL, CheckoutChargeStatus.PARTIAL),+ (20, CheckoutAuthorizeStatus.FULL, CheckoutChargeStatus.FULL),+ (40, CheckoutAuthorizeStatus.FULL, CheckoutChargeStatus.OVERCHARGED),+ ],+)+def test_transaction_amounts_for_checkout_updated_without_price_recalculation_considers_gift_cards_balance_when_updating_checkout_payment_status(+ checkout_with_gift_card,+ gift_card_balance,+ expected_authorize_status,+ expected_charge_status,+ transaction_item_generator,+):+ # given+ checkout = checkout_with_gift_card+ gift_card = checkout.gift_cards.first()+ gift_card.initial_balance_amount = Decimal(gift_card_balance)+ gift_card.current_balance_amount = Decimal(gift_card_balance)+ gift_card.save()++ lines, _ = fetch_checkout_lines(checkout)+ manager = get_plugins_manager(allow_replica=False)+ checkout_info = fetch_checkout_info(checkout, lines, manager)+ address = checkout.shipping_address or checkout.billing_address++ assert checkout.authorize_status == CheckoutAuthorizeStatus.NONE+ assert checkout.charge_status == CheckoutChargeStatus.NONE++ transaction = transaction_item_generator(+ checkout_id=checkout.pk,+ charged_value=Decimal(10),+ )++ total = calculate_checkout_total(+ manager=manager, checkout_info=checkout_info, lines=lines, address=address+ )+ assert total.gross.amount == Decimal(30)++ # when+ transaction_amounts_for_checkout_updated_without_price_recalculation(+ transaction, checkout, manager, None, None+ )++ # then+ checkout.refresh_from_db()+ assert checkout.authorize_status == expected_authorize_status+ assert checkout.charge_status == expected_charge_statusdiff --git a/saleor/checkout/tests/test_calculations.py b/saleor/checkout/tests/test_calculations.pyindex e7f60c3d1e..01fedf6b8e 100644--- a/saleor/checkout/tests/test_calculations.py+++ b/saleor/checkout/tests/test_calculations.py@@ -39,6 +39,7 @@ from ..calculations import (_apply_tax_data,_calculate_and_add_tax,_set_checkout_base_prices,+ calculate_checkout_total,fetch_checkout_data,logger,)@@ -1305,6 +1306,61 @@ def test_fetch_checkout_data_updates_status_for_zero_amount_checkout_with_lines(assert checkout_with_item_total_0.charge_status == CheckoutChargeStatus.FULL+ ("gift_card_balance", "expected_authorize_status", "expected_charge_status"),+ [+ (0, CheckoutAuthorizeStatus.PARTIAL, CheckoutChargeStatus.PARTIAL),+ (10, CheckoutAuthorizeStatus.PARTIAL, CheckoutChargeStatus.PARTIAL),+ (20, CheckoutAuthorizeStatus.FULL, CheckoutChargeStatus.FULL),+ (40, CheckoutAuthorizeStatus.FULL, CheckoutChargeStatus.OVERCHARGED),+ ],+)+def test_fetch_checkout_data_considers_gift_cards_balance_when_updating_checkout_payment_status(+ checkout_with_gift_card,+ gift_card_balance,+ expected_authorize_status,+ expected_charge_status,+ transaction_item_generator,+):+ # given+ checkout = checkout_with_gift_card+ gift_card = checkout.gift_cards.first()+ gift_card.initial_balance_amount = Decimal(gift_card_balance)+ gift_card.current_balance_amount = Decimal(gift_card_balance)+ gift_card.save()++ lines, _ = fetch_checkout_lines(checkout)+ manager = get_plugins_manager(allow_replica=False)+ checkout_info = fetch_checkout_info(checkout, lines, manager)+ address = checkout.shipping_address or checkout.billing_address++ assert checkout.authorize_status == CheckoutAuthorizeStatus.NONE+ assert checkout.charge_status == CheckoutChargeStatus.NONE++ transaction_item_generator(+ checkout_id=checkout.pk,+ charged_value=Decimal(10),+ )++ total = calculate_checkout_total(+ manager=manager, checkout_info=checkout_info, lines=lines, address=address+ )+ assert total.gross.amount == Decimal(30)++ # when+ fetch_checkout_data(+ checkout_info=checkout_info,+ manager=manager,+ lines=lines,+ address=address,+ )++ # then+ checkout.refresh_from_db()+ assert checkout.authorize_status == expected_authorize_status+ assert checkout.charge_status == expected_charge_status++def test_fetch_checkout_data_checkout_removed_before_save(checkout_with_prices,):diff --git a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.pyindex f5c9e1274a..78a0c0ba55 100644--- a/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py+++ b/saleor/graphql/checkout/tests/benchmark/test_checkout_mutations.py@@ -422,7 +422,7 @@ def test_create_checkout_with_reservations(}}- with django_assert_num_queries(84):+ with django_assert_num_queries(85):response = api_client.post_graphql(query, variables)assert get_graphql_content(response)["data"]["checkoutCreate"]assert Checkout.objects.first().lines.count() == 1@@ -440,7 +440,7 @@ def test_create_checkout_with_reservations(}}- with django_assert_num_queries(84):+ with django_assert_num_queries(85):response = api_client.post_graphql(query, variables)assert get_graphql_content(response)["data"]["checkoutCreate"]assert Checkout.objects.first().lines.count() == 10@@ -572,7 +572,7 @@ def test_create_checkout_with_order_promotion(# whenuser_api_client.ensure_access_token()- with django_assert_num_queries(90):+ with django_assert_num_queries(91):response = user_api_client.post_graphql(MUTATION_CHECKOUT_CREATE, variables)# then@@ -828,7 +828,7 @@ def test_update_checkout_lines_with_reservations()user_api_client.ensure_access_token()- with django_assert_num_queries(106):+ with django_assert_num_queries(107):variant_id = graphene.Node.to_global_id("ProductVariant", variants[0].pk)variables = {"id": to_global_id_or_none(checkout),@@ -842,7 +842,7 @@ def test_update_checkout_lines_with_reservations(assert not data["errors"]# Updating multiple lines in checkout has same query count as updating one- with django_assert_num_queries(106):+ with django_assert_num_queries(107):variables = {"id": to_global_id_or_none(checkout),"lines": [],@@ -1103,7 +1103,7 @@ def test_add_checkout_lines_with_reservations(user_api_client.ensure_access_token()# Adding multiple lines to checkout has same query count as adding one- with django_assert_num_queries(103):+ with django_assert_num_queries(104):variables = {"id": Node.to_global_id("Checkout", checkout.pk),"lines": [new_lines[0]],@@ -1116,7 +1116,7 @@ def test_add_checkout_lines_with_reservations(checkout.lines.exclude(id=line.id).delete()- with django_assert_num_queries(103):+ with django_assert_num_queries(104):variables = {"id": Node.to_global_id("Checkout", checkout.pk),"lines": new_lines,@@ -1167,7 +1167,7 @@ def test_add_checkout_lines_catalogue_discount_applies(# whenuser_api_client.ensure_access_token()- with django_assert_num_queries(94):+ with django_assert_num_queries(95):response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)# then@@ -1253,7 +1253,7 @@ def test_add_checkout_lines_multiple_catalogue_discount_applies(# whenuser_api_client.ensure_access_token()- with django_assert_num_queries(94):+ with django_assert_num_queries(95):response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)# then@@ -1289,7 +1289,7 @@ def test_add_checkout_lines_order_discount_applies(# whenuser_api_client.ensure_access_token()- with django_assert_num_queries(100):+ with django_assert_num_queries(101):response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)# then@@ -1324,7 +1324,7 @@ def test_add_checkout_lines_gift_discount_applies(# whenuser_api_client.ensure_access_token()- with django_assert_num_queries(127):+ with django_assert_num_queries(128):response = user_api_client.post_graphql(MUTATION_CHECKOUT_LINES_ADD, variables)# thendiff --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 8b3e418af2..f45bf12633 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@@ -156,11 +156,12 @@ def prepare_checkout_for_test(],)recalculate_transaction_amounts(transaction)- update_checkout_payment_statuses(- checkout=checkout_info.checkout,- checkout_total_gross=total.gross,- checkout_has_lines=bool(lines),- )++ # Set price expiration to force payment status recalculcation upon fetching checkout+ # data.+ checkout.price_expiration = timezone.now()+ checkout.save()+return checkout@@ -1023,6 +1024,16 @@ def test_checkout_with_voucher_not_applicable(transaction_item_generator,transaction_events_generator,)++ manager = get_plugins_manager(allow_replica=False)+ lines, _ = fetch_checkout_lines(checkout)+ checkout_info = fetch_checkout_info(checkout, lines, manager)+ calculations.fetch_checkout_data(+ checkout_info,+ manager,+ lines,+ )+Voucher.objects.all().delete()redirect_url = "https://www.example.com"@@ -1058,6 +1069,16 @@ def test_checkout_with_voucher_inactive_code(transaction_item_generator,transaction_events_generator,)++ manager = get_plugins_manager(allow_replica=False)+ lines, _ = fetch_checkout_lines(checkout)+ checkout_info = fetch_checkout_info(checkout, lines, manager)+ calculations.fetch_checkout_data(+ checkout_info,+ manager,+ lines,+ )+code.is_active = Falsecode.save(update_fields=["is_active"])@@ -1092,11 +1113,11 @@ def test_checkout_with_insufficient_stock(transaction_item_generator,transaction_events_generator,)+checkout_line = checkout.lines.first()stock = Stock.objects.get(product_variant=checkout_line.variant)- quantity_available = get_available_quantity_for_stock(stock)- checkout_line.quantity = quantity_available + 1- checkout_line.save()+ stock.quantity = 0+ stock.save(update_fields=["quantity"])redirect_url = "https://www.example.com"variables = {"id": to_global_id_or_none(checkout), "redirectUrl": redirect_url}@@ -3734,6 +3755,14 @@ def test_checkout_complete_own_reservation(transaction_item_generator,):# given+ checkout = checkout_with_item+ checkout_line = checkout.lines.first()+ stock = Stock.objects.get(product_variant=checkout_line.variant)+ quantity_available = get_available_quantity_for_stock(stock)++ checkout_line.quantity = quantity_available+ checkout_line.save()+checkout = prepare_checkout_for_test(checkout_with_item,address,@@ -3742,12 +3771,6 @@ def test_checkout_complete_own_reservation(transaction_item_generator,transaction_events_generator,)- checkout_line = checkout.lines.first()- stock = Stock.objects.get(product_variant=checkout_line.variant)- quantity_available = get_available_quantity_for_stock(stock)-- checkout_line.quantity = quantity_available- checkout_line.save()reservation = Reservation.objects.create(checkout_line=checkout_line,@@ -4089,6 +4112,13 @@ def test_complete_checkout_raises_error_for_local_stock(transaction_item_generator,):# given+ checkout = checkout_with_item_for_cc+ checkout_line = checkout.lines.first()+ stock = Stock.objects.get(product_variant=checkout_line.variant)+ quantity_available = get_available_quantity_for_stock(stock)+ checkout_line.quantity = quantity_available + 1+ checkout_line.save()+checkout = prepare_checkout_for_test(checkout_with_item_for_cc,None,@@ -4097,15 +4127,10 @@ def test_complete_checkout_raises_error_for_local_stock(transaction_item_generator,transaction_events_generator,)- checkout_line = checkout.lines.first()- stock = Stock.objects.get(product_variant=checkout_line.variant)- quantity_available = get_available_quantity_for_stock(stock)- checkout_line.quantity = quantity_available + 1- checkout_line.save()variables = {"id": to_global_id_or_none(checkout),- "rediirectUrl": "https://www.example.com",+ "redirectUrl": "https://www.example.com",}checkout.collection_point = warehouse_for_cc@@ -4134,6 +4159,14 @@ def test_comp_checkout_builds_order_for_all_warehouse_even_if_not_available_locatransaction_item_generator,):# given+ checkout = checkout_with_item_for_cc+ checkout_line = checkout.lines.first()+ stock = Stock.objects.get(+ product_variant=checkout_line.variant, warehouse=warehouse_for_cc+ )+ quantity_available = get_available_quantity_for_stock(stock)+ checkout_line.quantity = quantity_available + 1+ checkout_line.save()checkout = prepare_checkout_for_test(checkout_with_item_for_cc,None,@@ -4142,13 +4175,6 @@ def test_comp_checkout_builds_order_for_all_warehouse_even_if_not_available_locatransaction_item_generator,transaction_events_generator,)- checkout_line = checkout.lines.first()- stock = Stock.objects.get(- product_variant=checkout_line.variant, warehouse=warehouse_for_cc- )- quantity_available = get_available_quantity_for_stock(stock)- checkout_line.quantity = quantity_available + 1- checkout_line.save()warehouse_for_cc.click_and_collect_option = (WarehouseClickAndCollectOption.ALL_WAREHOUSES@@ -4182,14 +4208,7 @@ def test_checkout_complete_raises_InsufficientStock_when_quantity_above_stock_sutransaction_item_generator,):# given- checkout = prepare_checkout_for_test(- checkout_with_item_for_cc,- None,- address,- None,- transaction_item_generator,- transaction_events_generator,- )+ checkout = checkout_with_item_for_cccheckout_line = checkout.lines.first()overall_stock_quantity = (Stock.objects.filter(product_variant=checkout_line.variant).aggregate(@@ -4198,6 +4217,14 @@ def test_checkout_complete_raises_InsufficientStock_when_quantity_above_stock_su).pop("quantity__sum")checkout_line.quantity = overall_stock_quantity + 1checkout_line.save()+ checkout = prepare_checkout_for_test(+ checkout_with_item_for_cc,+ None,+ address,+ None,+ transaction_item_generator,+ transaction_events_generator,+ )warehouse_for_cc.click_and_collect_option = (WarehouseClickAndCollectOption.ALL_WAREHOUSES)diff --git a/saleor/order/tests/test_order_utils.py b/saleor/order/tests/test_order_utils.pyindex 414d35d451..9d2c320b02 100644--- a/saleor/order/tests/test_order_utils.py+++ b/saleor/order/tests/test_order_utils.py@@ -1,10 +1,13 @@+import datetimefrom decimal import Decimalimport grapheneimport pytest+from django.utils import timezonefrom prices import Money, TaxedMoneyfrom ...checkout.fetch import fetch_checkout_info, fetch_checkout_lines+from ...checkout.models import Checkoutfrom ...discount import DiscountType, DiscountValueTypefrom ...giftcard import GiftCardEventsfrom ...giftcard.models import GiftCardEvent@@ -454,6 +457,38 @@ def test_add_gift_cards_to_order_no_checkout_user(}+def test_add_gift_cards_to_order_invalidates_prices_for_other_checkout_attached_to_the_same_gift_card(+ checkout_with_gift_card,+ gift_card,+ order,+ staff_user,+ channel_USD,+):+ # given+ checkout = checkout_with_gift_card+ assert gift_card in checkout.gift_cards.all()+ manager = get_plugins_manager(allow_replica=False)+ lines, _ = fetch_checkout_lines(checkout)+ checkout_info = fetch_checkout_info(checkout, lines, manager)++ other_checkout = Checkout.objects.create(+ currency=channel_USD.currency_code, channel=channel_USD+ )+ other_checkout.gift_cards.add(gift_card)+ initial_price_expiration = timezone.now() + datetime.timedelta(minutes=30)+ other_checkout.price_expiration = initial_price_expiration+ other_checkout.save(update_fields=["price_expiration"])++ # when+ add_gift_cards_to_order(+ checkout_info, order, Money(30, gift_card.currency), staff_user, None+ )++ # then+ other_checkout.refresh_from_db()+ assert other_checkout.price_expiration != initial_price_expiration++def test_get_total_order_discount_excluding_shipping(order, voucher_shipping_type):# givenorder.discounts.create(diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 8a579babd3..fad7533072 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -16,6 +16,7 @@ from prices import Money, TaxedMoneyfrom ..account.models import Userfrom ..account.utils import store_user_addressfrom ..checkout import AddressType+from ..checkout.models import Checkoutfrom ..core.prices import quantize_pricefrom ..core.taxes import zero_moneyfrom ..core.tracing import traced_atomic_transaction@@ -496,6 +497,13 @@ 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)+ # Invalidate prices for checkouts attached to gift cards used by this order.+ # This will ensure the checkout status gets recalculcated to accomodate gift cards+ # balance changes.+ Checkout.objects.filter(gift_cards__in=gift_cards_to_update).exclude(+ token=checkout_info.checkout.token+ ).update(price_expiration=timezone.now())+gift_card_compensation = total_before_gift_card_compensation - total_price_leftif gift_card_compensation.amount > 0:details = {diff --git a/saleor/tests/e2e/checkout/test_checkout_complete_with_gift_card_recalculcates_status_for_other_checkout.py b/saleor/tests/e2e/checkout/test_checkout_complete_with_gift_card_recalculcates_status_for_other_checkout.pynew file mode 100644index 0000000000..a657fa6087--- /dev/null+++ b/saleor/tests/e2e/checkout/test_checkout_complete_with_gift_card_recalculcates_status_for_other_checkout.py@@ -0,0 +1,181 @@+import pytest++from ..gift_cards.utils import create_gift_card+from ..product.utils.preparing_product import prepare_product+from ..shop.utils import prepare_shop+from ..utils import assign_permissions+from .utils import (+ checkout_add_promo_code,+ checkout_complete,+ checkout_create,+ checkout_delivery_method_update,+ get_checkout,+ raw_checkout_complete,+)+++ "query_second_checkout_status_before_checkout_complete",+ [+ True,+ False,+ ],+)+def test_checkout_complete_with_gift_card_recalculcates_status_for_other_checkout(+ e2e_app_api_client,+ e2e_not_logged_api_client,+ permission_manage_product_types_and_attributes,+ permission_manage_orders,+ permission_manage_checkouts,+ permission_manage_gift_card,+ shop_permissions,+ query_second_checkout_status_before_checkout_complete,+):+ # Before+ permissions = [+ *shop_permissions,+ permission_manage_product_types_and_attributes,+ permission_manage_orders,+ permission_manage_checkouts,+ permission_manage_gift_card,+ ]+ assign_permissions(e2e_app_api_client, permissions)++ shop_data, _ = prepare_shop(+ e2e_app_api_client,+ channels=[+ {+ "shipping_zones": [+ {+ "shipping_methods": [{}],+ },+ ],+ "order_settings": {+ "allowUnpaidOrders": False,+ "markAsPaidStrategy": "TRANSACTION_FLOW",+ },+ }+ ],+ )+ channel_id = shop_data[0]["id"]+ channel_slug = shop_data[0]["slug"]+ warehouse_id = shop_data[0]["warehouse_id"]+ shipping_method_id = shop_data[0]["shipping_zones"][0]["shipping_methods"][0]["id"]++ variant_price = 10++ (+ _,+ product_variant_id,+ _,+ ) = prepare_product(+ e2e_app_api_client,+ warehouse_id,+ channel_id,+ variant_price,+ )++ assert shipping_method_id is not None++ gift_card = create_gift_card(e2e_app_api_client, 20, "USD", active=True)+ gift_card_code = gift_card["code"]+ gift_card_id = gift_card["id"]++ # Step 1 - Create first checkout+ lines = [+ {"variantId": product_variant_id, "quantity": 1},+ ]+ checkout_data = checkout_create(+ e2e_not_logged_api_client,+ lines,+ channel_slug,+ email="[email protected]",+ )+ first_checkout_id = checkout_data["id"]++ # Step 2 - Update delivery method for first checkout+ checkout_data = checkout_delivery_method_update(+ e2e_not_logged_api_client,+ first_checkout_id,+ shipping_method_id,+ )+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id++ # Step 3 - Add gift card to first checkout+ checkout_data = checkout_add_promo_code(+ e2e_not_logged_api_client,+ first_checkout_id,+ gift_card_code,+ )+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]+ assert total_gross_amount == 0+ assert checkout_data["giftCards"][0]["id"] == gift_card_id+ assert checkout_data["giftCards"][0]["last4CodeChars"] == gift_card_code[-4:]++ # Step 4 - Check first checkout status+ checkout_data = get_checkout(e2e_not_logged_api_client, first_checkout_id)+ assert checkout_data["authorizeStatus"] == "FULL"+ assert checkout_data["chargeStatus"] == "FULL"++ # Step 5 - Create second checkout+ lines = [+ {"variantId": product_variant_id, "quantity": 1},+ ]+ checkout_data = checkout_create(+ e2e_not_logged_api_client,+ lines,+ channel_slug,+ email="[email protected]",+ )+ second_checkout_id = checkout_data["id"]++ # Step 6 - Update delivery method for second checkout+ checkout_data = checkout_delivery_method_update(+ e2e_not_logged_api_client,+ second_checkout_id,+ shipping_method_id,+ )+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id++ # Step 7 - Add gift card to second checkout+ checkout_data = checkout_add_promo_code(+ e2e_not_logged_api_client,+ second_checkout_id,+ gift_card_code,+ )+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]+ assert total_gross_amount == 0+ assert checkout_data["giftCards"][0]["id"] == gift_card_id+ assert checkout_data["giftCards"][0]["last4CodeChars"] == gift_card_code[-4:]++ # Step 8 - Check second checkout status+ checkout_data = get_checkout(e2e_not_logged_api_client, second_checkout_id)+ assert checkout_data["authorizeStatus"] == "FULL"+ assert checkout_data["chargeStatus"] == "FULL"++ # Step 9 - Complete first checkout+ order_data = checkout_complete(+ e2e_not_logged_api_client,+ first_checkout_id,+ )+ assert order_data["id"] is not None++ # # Step 10 - Check second checkout status once again+ if query_second_checkout_status_before_checkout_complete:+ checkout_data = get_checkout(e2e_not_logged_api_client, second_checkout_id)+ assert checkout_data["authorizeStatus"] == "NONE"+ assert checkout_data["chargeStatus"] == "NONE"++ # Step 11 - Attempt to complete second checkout+ response = raw_checkout_complete(+ e2e_not_logged_api_client,+ second_checkout_id,+ )+ errors = response["errors"]+ assert len(errors) == 1+ assert errors[0] == {+ "code": "CHECKOUT_NOT_FULLY_PAID",+ "field": None,+ "message": "Provided payment methods can not cover the checkout's total amount",+ }diff --git a/saleor/tests/e2e/checkout/test_checkout_complete_with_transaction_and_gift_card.py b/saleor/tests/e2e/checkout/test_checkout_complete_with_transaction_and_gift_card.pynew file mode 100644index 0000000000..a4353132d1--- /dev/null+++ b/saleor/tests/e2e/checkout/test_checkout_complete_with_transaction_and_gift_card.py@@ -0,0 +1,371 @@+import pytest++from ..gift_cards.utils import create_gift_card+from ..orders.utils import order_query+from ..product.utils.preparing_product import prepare_product+from ..shop.utils import prepare_shop+from ..transactions.utils import create_transaction+from ..utils import assign_permissions+from .utils import (+ checkout_add_promo_code,+ checkout_complete,+ checkout_create,+ checkout_delivery_method_update,+)+++def test_checkout_complete_with_transaction_and_gift_card(+ e2e_app_api_client,+ e2e_not_logged_api_client,+ permission_manage_product_types_and_attributes,+ permission_manage_orders,+ permission_manage_checkouts,+ permission_manage_payments,+ permission_manage_gift_card,+ shop_permissions,+):+ # Before+ permissions = [+ *shop_permissions,+ permission_manage_product_types_and_attributes,+ permission_manage_orders,+ permission_manage_checkouts,+ permission_manage_payments,+ permission_manage_gift_card,+ ]+ assign_permissions(e2e_app_api_client, permissions)++ shop_data, _ = prepare_shop(+ e2e_app_api_client,+ channels=[+ {+ "shipping_zones": [+ {+ "shipping_methods": [{}],+ },+ ],+ "order_settings": {+ "allowUnpaidOrders": False,+ "markAsPaidStrategy": "TRANSACTION_FLOW",+ },+ }+ ],+ )+ channel_id = shop_data[0]["id"]+ channel_slug = shop_data[0]["slug"]+ warehouse_id = shop_data[0]["warehouse_id"]+ shipping_method_id = shop_data[0]["shipping_zones"][0]["shipping_methods"][0]["id"]++ variant_price = 10++ (+ _,+ product_variant_id,+ _,+ ) = prepare_product(+ e2e_app_api_client,+ warehouse_id,+ channel_id,+ variant_price,+ )++ assert shipping_method_id is not None++ gift_card = create_gift_card(e2e_app_api_client, 100, "USD", active=True)+ gift_card_code = gift_card["code"]+ gift_card_id = gift_card["id"]++ # Step 1 - Create checkout+ lines = [+ {"variantId": product_variant_id, "quantity": 1},+ ]+ checkout_data = checkout_create(+ e2e_not_logged_api_client,+ lines,+ channel_slug,+ email="[email protected]",+ )+ checkout_id = checkout_data["id"]++ subtotal_gross_amount = checkout_data["subtotalPrice"]["gross"]["amount"]+ assert subtotal_gross_amount == float(variant_price)++ # Step 2 - Update delivery method+ checkout_data = checkout_delivery_method_update(+ e2e_not_logged_api_client,+ checkout_id,+ shipping_method_id,+ )+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]+ shipping_price = checkout_data["deliveryMethod"]["price"]["amount"]+ assert shipping_price == 10+ assert total_gross_amount == subtotal_gross_amount + shipping_price+ assert checkout_data["chargeStatus"] == "NONE"+ assert checkout_data["authorizeStatus"] == "NONE"++ # Step 3 - Create transaction that partially authorize payment+ create_transaction(+ e2e_app_api_client,+ checkout_id,+ transaction_name="transaction",+ psp_reference="PSP-test",+ available_actions=["CHARGE", "CANCEL"],+ amount_authorized=subtotal_gross_amount,+ )++ # Step 4 - Add gift card to checkout+ checkout_data = checkout_add_promo_code(+ e2e_not_logged_api_client,+ checkout_id,+ gift_card_code,+ )+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]+ assert total_gross_amount == 0+ assert checkout_data["giftCards"][0]["id"] == gift_card_id+ assert checkout_data["giftCards"][0]["last4CodeChars"] == gift_card_code[-4:]++ # Step 5 - Complete checkout.+ order_data = checkout_complete(+ e2e_not_logged_api_client,+ checkout_id,+ )+ assert order_data["status"] == "UNFULFILLED"+ assert order_data["total"]["gross"]["amount"] == 0+++def test_checkout_complete_with_gift_card_and_transaction(+ e2e_app_api_client,+ e2e_not_logged_api_client,+ permission_manage_product_types_and_attributes,+ permission_manage_orders,+ permission_manage_checkouts,+ permission_manage_payments,+ permission_manage_gift_card,+ shop_permissions,+):+ # Before+ permissions = [+ *shop_permissions,+ permission_manage_product_types_and_attributes,+ permission_manage_orders,+ permission_manage_checkouts,+ permission_manage_payments,+ permission_manage_gift_card,+ ]+ assign_permissions(e2e_app_api_client, permissions)++ shop_data, _ = prepare_shop(+ e2e_app_api_client,+ channels=[+ {+ "shipping_zones": [+ {+ "shipping_methods": [{}],+ },+ ],+ "order_settings": {+ "allowUnpaidOrders": False,+ "markAsPaidStrategy": "TRANSACTION_FLOW",+ },+ }+ ],+ )+ channel_id = shop_data[0]["id"]+ channel_slug = shop_data[0]["slug"]+ warehouse_id = shop_data[0]["warehouse_id"]+ shipping_method_id = shop_data[0]["shipping_zones"][0]["shipping_methods"][0]["id"]++ variant_price = 10++ (+ _,+ product_variant_id,+ _,+ ) = prepare_product(+ e2e_app_api_client,+ warehouse_id,+ channel_id,+ variant_price,+ )++ assert shipping_method_id is not None++ gift_card = create_gift_card(e2e_app_api_client, 100, "USD", active=True)+ gift_card_code = gift_card["code"]+ gift_card_id = gift_card["id"]++ # Step 1 - Create checkout+ lines = [+ {"variantId": product_variant_id, "quantity": 1},+ ]+ checkout_data = checkout_create(+ e2e_not_logged_api_client,+ lines,+ channel_slug,+ email="[email protected]",+ )+ checkout_id = checkout_data["id"]++ subtotal_gross_amount = checkout_data["subtotalPrice"]["gross"]["amount"]+ assert subtotal_gross_amount == float(variant_price)++ # Step 2 - Update delivery method+ checkout_data = checkout_delivery_method_update(+ e2e_not_logged_api_client,+ checkout_id,+ shipping_method_id,+ )+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]+ shipping_price = checkout_data["deliveryMethod"]["price"]["amount"]+ assert shipping_price == 10+ assert total_gross_amount == subtotal_gross_amount + shipping_price+ assert checkout_data["chargeStatus"] == "NONE"+ assert checkout_data["authorizeStatus"] == "NONE"++ # Step 3 - Add gift card to checkout+ checkout_data = checkout_add_promo_code(+ e2e_not_logged_api_client,+ checkout_id,+ gift_card_code,+ )+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]+ assert total_gross_amount == 0+ assert checkout_data["giftCards"][0]["id"] == gift_card_id+ assert checkout_data["giftCards"][0]["last4CodeChars"] == gift_card_code[-4:]++ # Step 4 - Create transaction that partially authorize payment+ create_transaction(+ e2e_app_api_client,+ checkout_id,+ transaction_name="transaction",+ psp_reference="PSP-test",+ available_actions=["CHARGE", "CANCEL"],+ amount_authorized=subtotal_gross_amount,+ )++ # Step 5 - Complete checkout.+ order_data = checkout_complete(+ e2e_not_logged_api_client,+ checkout_id,+ )+ assert order_data["status"] == "UNFULFILLED"+ assert order_data["total"]["gross"]["amount"] == 0+++def test_checkout_complete_with_only_gift_card(+ e2e_app_api_client,+ e2e_not_logged_api_client,+ permission_manage_product_types_and_attributes,+ permission_manage_orders,+ permission_manage_checkouts,+ permission_manage_payments,+ permission_manage_gift_card,+ shop_permissions,+):+ # Before+ permissions = [+ *shop_permissions,+ permission_manage_product_types_and_attributes,+ permission_manage_orders,+ permission_manage_checkouts,+ permission_manage_payments,+ permission_manage_gift_card,+ ]+ assign_permissions(e2e_app_api_client, permissions)++ shop_data, _ = prepare_shop(+ e2e_app_api_client,+ channels=[+ {+ "shipping_zones": [+ {+ "shipping_methods": [{}],+ },+ ],+ "order_settings": {+ "allowUnpaidOrders": False,+ "markAsPaidStrategy": "TRANSACTION_FLOW",+ },+ }+ ],+ )+ channel_id = shop_data[0]["id"]+ channel_slug = shop_data[0]["slug"]+ warehouse_id = shop_data[0]["warehouse_id"]+ shipping_method_id = shop_data[0]["shipping_zones"][0]["shipping_methods"][0]["id"]++ variant_price = 10++ (+ _,+ product_variant_id,+ _,+ ) = prepare_product(+ e2e_app_api_client,+ warehouse_id,+ channel_id,+ variant_price,+ )++ assert shipping_method_id is not None++ gift_card = create_gift_card(e2e_app_api_client, 100, "USD", active=True)+ gift_card_code = gift_card["code"]+ gift_card_id = gift_card["id"]++ # Step 1 - Create checkout+ lines = [+ {"variantId": product_variant_id, "quantity": 1},+ ]+ checkout_data = checkout_create(+ e2e_not_logged_api_client,+ lines,+ channel_slug,+ email="[email protected]",+ )+ checkout_id = checkout_data["id"]++ subtotal_gross_amount = checkout_data["subtotalPrice"]["gross"]["amount"]+ assert subtotal_gross_amount == float(variant_price)++ # Step 2 - Update delivery method+ checkout_data = checkout_delivery_method_update(+ e2e_not_logged_api_client,+ checkout_id,+ shipping_method_id,+ )+ assert checkout_data["deliveryMethod"]["id"] == shipping_method_id+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]+ shipping_price = checkout_data["deliveryMethod"]["price"]["amount"]+ assert shipping_price == 10+ assert total_gross_amount == subtotal_gross_amount + shipping_price+ assert checkout_data["chargeStatus"] == "NONE"+ assert checkout_data["authorizeStatus"] == "NONE"++ # Step 3 - Add gift card to checkout+ checkout_data = checkout_add_promo_code(+ e2e_not_logged_api_client,+ checkout_id,+ gift_card_code,+ )+ total_gross_amount = checkout_data["totalPrice"]["gross"]["amount"]+ assert total_gross_amount == 0+ assert checkout_data["giftCards"][0]["id"] == gift_card_id+ assert checkout_data["giftCards"][0]["last4CodeChars"] == gift_card_code[-4:]++ # Step 4 - Complete checkout.+ order_data = checkout_complete(+ e2e_not_logged_api_client,+ checkout_id,+ )+ assert order_data["status"] == "UNCONFIRMED"+ assert order_data["total"]["gross"]["amount"] == 0++ # Step 5 - Check order status+ order_data = order_query(e2e_app_api_client, order_data["id"])+ assert order_data["status"] == "UNFULFILLED"diff --git a/saleor/tests/e2e/checkout/utils/query_checkout.py b/saleor/tests/e2e/checkout/utils/query_checkout.pyindex 4078d15bd6..acc3d6ffc4 100644--- a/saleor/tests/e2e/checkout/utils/query_checkout.py+++ b/saleor/tests/e2e/checkout/utils/query_checkout.py@@ -52,6 +52,8 @@ query Checkout($checkoutId: ID!){amount}}+ authorizeStatus+ chargeStatus}}"""diff --git a/saleor/tests/e2e/checkout/zero_total/test_pay_for_total_checkout_with_gift_card.py b/saleor/tests/e2e/checkout/zero_total/test_pay_for_total_checkout_with_gift_card.pyindex 9e408df386..9444c07bac 100644--- a/saleor/tests/e2e/checkout/zero_total/test_pay_for_total_checkout_with_gift_card.py+++ b/saleor/tests/e2e/checkout/zero_total/test_pay_for_total_checkout_with_gift_card.py@@ -2,6 +2,7 @@ import pytestfrom ...channel.utils import update_channelfrom ...gift_cards.utils import create_gift_card+from ...orders.utils.order_query import order_queryfrom ...product.utils.preparing_product import prepare_productsfrom ...shop.utils.preparing_shop import prepare_default_shopfrom ...utils import assign_permissions@@ -28,6 +29,7 @@ def test_gift_card_total_payment_for_checkout_core_1101(shop_permissions,permission_manage_product_types_and_attributes,permission_manage_gift_card,+ permission_manage_orders,mark_as_paid_strategy,):# Before@@ -35,6 +37,7 @@ def test_gift_card_total_payment_for_checkout_core_1101(*shop_permissions,permission_manage_product_types_and_attributes,permission_manage_gift_card,+ permission_manage_orders,]assign_permissions(e2e_staff_api_client, permissions)@@ -141,7 +144,11 @@ def test_gift_card_total_payment_for_checkout_core_1101(e2e_logged_api_client,checkout_id,)- assert order_data["status"] == "UNFULFILLED"+ assert order_data["status"] == "UNCONFIRMED"assert order_data["total"]["gross"]["amount"] == 0assert order_data["giftCards"][0]["id"] == gift_card_idassert order_data["giftCards"][0]["last4CodeChars"] == gift_card_code[-4:]++ # Step 5 - Check order status+ order_data = order_query(e2e_staff_api_client, order_data["id"])+ assert order_data["status"] == "UNFULFILLED"