Skip to content

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

Fix gift cards automatic fulfillment (#10325)

Iga Karbowiak6a528259e3bd
diff --git a/saleor/checkout/complete_checkout.py b/saleor/checkout/complete_checkout.py
index 036f3c4639..e16288f8c0 100644
--- a/saleor/checkout/complete_checkout.py
+++ b/saleor/checkout/complete_checkout.py
@@ -35,7 +35,6 @@ from ..discount.utils import (
increase_voucher_usage,
release_voucher_usage,
)
-from ..giftcard.utils import fulfill_non_shippable_gift_cards
from ..graphql.checkout.utils import (
prepare_insufficient_stock_checkout_validation_error,
)
@@ -581,7 +580,11 @@ def _create_order(
transaction.on_commit(
lambda: order_created(
- order_info=order_info, user=user, app=app, manager=manager
+ order_info=order_info,
+ user=user,
+ app=app,
+ manager=manager,
+ site_settings=site_settings,
)
)
@@ -590,11 +593,6 @@ def _create_order(
lambda: send_order_confirmation(order_info, checkout.redirect_url, manager)
)
- if site_settings.automatically_fulfill_non_shippable_gift_card:
- fulfill_non_shippable_gift_cards(
- order, order_lines, site_settings, user, app, manager
- )
-
return order
@@ -977,7 +975,11 @@ def _post_create_order_actions(
transaction.on_commit(
lambda: order_created(
- order_info=order_info, user=user, app=app, manager=manager
+ order_info=order_info,
+ user=user,
+ app=app,
+ manager=manager,
+ site_settings=site_settings,
)
)
@@ -988,12 +990,6 @@ def _post_create_order_actions(
)
)
- order_lines = [line.line for line in order_lines_info]
- if site_settings.automatically_fulfill_non_shippable_gift_card:
- fulfill_non_shippable_gift_cards(
- order, order_lines, site_settings, user, app, manager
- )
-
def _create_order_from_checkout(
checkout_info: CheckoutInfo,
diff --git a/saleor/checkout/tests/test_checkout_complete.py b/saleor/checkout/tests/test_checkout_complete.py
index 594d93aac2..7d2fa4464c 100644
--- a/saleor/checkout/tests/test_checkout_complete.py
+++ b/saleor/checkout/tests/test_checkout_complete.py
@@ -796,11 +796,15 @@ def test_create_order_with_many_gift_cards(
def test_create_order_gift_card_bought(
send_notification_mock,
checkout_with_gift_card_items,
+ payment_txn_captured,
customer_user,
shipping_method,
is_anonymous_user,
non_shippable_gift_card_product,
):
+ """Ensure that the gift cards will be fulfilled, when newly created order
+ is captured."""
+ # given
checkout_user = None if is_anonymous_user else customer_user
checkout = checkout_with_gift_card_items
checkout.user = checkout_user
@@ -815,6 +819,22 @@ def test_create_order_gift_card_bought(
lines, _ = fetch_checkout_lines(checkout)
checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ amount = calculations.calculate_checkout_total_with_gift_cards(
+ manager, checkout_info, lines, customer_user.default_billing_address
+ ).gross.amount
+
+ payment_txn_captured.order = None
+ payment_txn_captured.checkout = checkout
+ payment_txn_captured.captured_amount = amount
+ payment_txn_captured.total = amount
+ payment_txn_captured.save(
+ update_fields=["order", "checkout", "total", "captured_amount"]
+ )
+
+ txn = payment_txn_captured.transactions.first()
+ txn.amount = amount
+ txn.save(update_fields=["amount"])
+
subtotal = calculations.checkout_subtotal(
manager=manager,
checkout_info=checkout_info,
@@ -829,6 +849,7 @@ def test_create_order_gift_card_bought(
)
total_gross = subtotal.gross + shipping_price.gross - checkout.discount
+ # when
order = _create_order(
checkout_info=checkout_info,
checkout_lines=lines,
@@ -844,6 +865,7 @@ def test_create_order_gift_card_bought(
manager=manager,
)
+ # then
flush_post_commit_hooks()
assert order.total.gross == total_gross
flush_post_commit_hooks()
@@ -855,6 +877,7 @@ def test_create_order_gift_card_bought(
).unit_price_gross
)
assert GiftCardEvent.objects.filter(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
+ flush_post_commit_hooks()
send_notification_mock.assert_called_once_with(
checkout_user,
None,
@@ -867,6 +890,70 @@ def test_create_order_gift_card_bought(
)
[email protected]("saleor.giftcard.utils.send_gift_card_notification")
[email protected]("is_anonymous_user", (True, False))
+def test_create_order_gift_card_bought_order_not_captured_gift_cards_not_sent(
+ send_notification_mock,
+ checkout_with_gift_card_items,
+ customer_user,
+ shipping_method,
+ is_anonymous_user,
+):
+ """Ensure that the gift cards will be not fulfilled, when newly created order
+ is not captured."""
+ # given
+ checkout_user = None if is_anonymous_user else customer_user
+ checkout = checkout_with_gift_card_items
+ checkout.user = checkout_user
+ checkout.billing_address = customer_user.default_billing_address
+ checkout.shipping_address = customer_user.default_billing_address
+ checkout.shipping_method = shipping_method
+ checkout.tracking_code = "tracking_code"
+ checkout.redirect_url = "https://www.example.com"
+ checkout.save()
+
+ manager = get_plugins_manager()
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+
+ subtotal = calculations.checkout_subtotal(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ address=checkout.shipping_address,
+ )
+ shipping_price = calculations.checkout_shipping_price(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ address=checkout.shipping_address,
+ )
+ total_gross = subtotal.gross + shipping_price.gross - checkout.discount
+
+ # when
+ order = _create_order(
+ checkout_info=checkout_info,
+ checkout_lines=lines,
+ order_data=_prepare_order_data(
+ manager=manager,
+ checkout_info=checkout_info,
+ lines=lines,
+ discounts=None,
+ taxes_included_in_prices=True,
+ ),
+ user=customer_user if not is_anonymous_user else AnonymousUser(),
+ app=None,
+ manager=manager,
+ )
+
+ # then
+ flush_post_commit_hooks()
+ flush_post_commit_hooks()
+ assert order.total.gross == total_gross
+ assert not GiftCard.objects.exists()
+ send_notification_mock.assert_not_called()
+
+
@mock.patch("saleor.giftcard.utils.send_gift_card_notification")
@pytest.mark.parametrize("is_anonymous_user", (True, False))
def test_create_order_gift_card_bought_only_shippable_gift_card(
diff --git a/saleor/checkout/tests/test_order_from_checkout.py b/saleor/checkout/tests/test_order_from_checkout.py
index c526603cb0..ee7b46d583 100644
--- a/saleor/checkout/tests/test_order_from_checkout.py
+++ b/saleor/checkout/tests/test_order_from_checkout.py
@@ -231,7 +231,9 @@ def test_create_order_gift_card_bought(
is_anonymous_user,
non_shippable_gift_card_product,
app,
+ payment_txn_captured,
):
+ # given
checkout_user = None if is_anonymous_user else customer_user
checkout = checkout_with_gift_card_items
checkout.user = checkout_user
@@ -260,6 +262,13 @@ def test_create_order_gift_card_bought(
)
total_gross = subtotal.gross + shipping_price.gross - checkout.discount
+ payment = payment_txn_captured
+ payment.checkout = checkout
+ payment.captured_amount = total_gross.amount
+ payment.total = total_gross.amount
+ payment.save(update_fields=["checkout", "captured_amount", "total"])
+
+ # when
order = create_order_from_checkout(
checkout_info=checkout_info,
checkout_lines=lines,
@@ -270,6 +279,7 @@ def test_create_order_gift_card_bought(
tracking_code="tracking_code",
)
+ # then
flush_post_commit_hooks()
assert order.total.gross == total_gross
flush_post_commit_hooks()
@@ -281,6 +291,7 @@ def test_create_order_gift_card_bought(
).unit_price_gross
)
assert GiftCardEvent.objects.filter(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
+ flush_post_commit_hooks()
send_notification_mock.assert_called_once_with(
None,
app,
diff --git a/saleor/graphql/checkout/tests/mutations/test_checkout_complete.py b/saleor/graphql/checkout/tests/mutations/test_checkout_complete.py
index c7a351017c..b4e1549ae8 100644
--- a/saleor/graphql/checkout/tests/mutations/test_checkout_complete.py
+++ b/saleor/graphql/checkout/tests/mutations/test_checkout_complete.py
@@ -426,10 +426,11 @@ def test_checkout_complete_gift_card_bought(
customer_user,
user_api_client,
checkout_with_gift_card_items,
- payment_dummy,
address,
shipping_method,
+ payment_txn_captured,
):
+ # given
checkout = checkout_with_gift_card_items
checkout.shipping_address = address
checkout.shipping_method = shipping_method
@@ -449,24 +450,28 @@ def test_checkout_complete_gift_card_bought(
site_settings.automatically_fulfill_non_shippable_gift_card = True
site_settings.save()
- payment = payment_dummy
+ payment = payment_txn_captured
payment.is_active = True
payment.order = None
+ payment.captured_amount = total.gross.amount
payment.total = total.gross.amount
payment.currency = total.gross.currency
payment.checkout = checkout
payment.save()
- assert not payment.transactions.exists()
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"]
+ flush_post_commit_hooks()
assert Order.objects.count() == orders_count + 1
order = Order.objects.first()
assert order.status == OrderStatus.PARTIALLY_FULFILLED
diff --git a/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py b/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py
index 4e46663523..4465dfd1a2 100644
--- a/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py
+++ b/saleor/graphql/checkout/tests/mutations/test_order_create_from_checkout.py
@@ -206,7 +206,9 @@ def test_order_from_checkout_gift_card_bought(
checkout_with_gift_card_items,
address,
shipping_method,
+ payment_txn_captured,
):
+ # given
checkout = checkout_with_gift_card_items
checkout.shipping_address = address
checkout.shipping_method = shipping_method
@@ -216,29 +218,53 @@ def test_order_from_checkout_gift_card_bought(
checkout.user = customer_user
checkout.save()
+ manager = get_plugins_manager()
+ lines, _ = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+
+ amount = calculations.calculate_checkout_total_with_gift_cards(
+ manager, checkout_info, lines, address
+ ).gross.amount
+
+ payment_txn_captured.order = None
+ payment_txn_captured.checkout = checkout
+ payment_txn_captured.captured_amount = amount
+ payment_txn_captured.total = amount
+ payment_txn_captured.save(
+ update_fields=["order", "checkout", "total", "captured_amount"]
+ )
+
+ txn = payment_txn_captured.transactions.first()
+ txn.amount = amount
+ txn.save(update_fields=["amount"])
+
site_settings.automatically_confirm_all_new_orders = True
site_settings.automatically_fulfill_non_shippable_gift_card = True
site_settings.save()
orders_count = Order.objects.count()
variables = {"id": graphene.Node.to_global_id("Checkout", checkout.pk)}
+
+ # when
response = app_api_client.post_graphql(
MUTATION_ORDER_CREATE_FROM_CHECKOUT,
variables,
permissions=[permission_handle_checkouts],
)
+ # then
content = get_graphql_content(response)
data = content["data"]["orderCreateFromCheckout"]
assert not data["errors"]
assert Order.objects.count() == orders_count + 1
+ flush_post_commit_hooks()
order = Order.objects.first()
assert order.status == OrderStatus.PARTIALLY_FULFILLED
- flush_post_commit_hooks()
gift_card = GiftCard.objects.get()
assert GiftCardEvent.objects.filter(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
+ flush_post_commit_hooks()
send_notification_mock.assert_called_once_with(
None,
app,
diff --git a/saleor/graphql/order/mutations/order_capture.py b/saleor/graphql/order/mutations/order_capture.py
index 6aceeebe48..9a1c35ead0 100644
--- a/saleor/graphql/order/mutations/order_capture.py
+++ b/saleor/graphql/order/mutations/order_capture.py
@@ -100,5 +100,6 @@ class OrderCapture(BaseMutation):
amount,
payment,
info.context.plugins,
+ info.context.site.settings,
)
return OrderCapture(order=order)
diff --git a/saleor/graphql/order/mutations/order_confirm.py b/saleor/graphql/order/mutations/order_confirm.py
index ea5ba405ad..f546c28cea 100644
--- a/saleor/graphql/order/mutations/order_confirm.py
+++ b/saleor/graphql/order/mutations/order_confirm.py
@@ -92,6 +92,7 @@ class OrderConfirm(ModelMutation):
payment.total,
payment,
manager,
+ info.context.site.settings,
)
)
transaction.on_commit(
diff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.py
index f09b54400b..18d3bcd404 100644
--- a/saleor/graphql/order/tests/test_order.py
+++ b/saleor/graphql/order/tests/test_order.py
@@ -3,7 +3,6 @@ from datetime import date, datetime, timedelta
from decimal import Decimal
from functools import reduce
from operator import getitem
-from unittest import mock
from unittest.mock import ANY, MagicMock, Mock, call, patch
import graphene
@@ -33,6 +32,7 @@ from ....order import FulfillmentStatus, OrderEvents, OrderOrigin, OrderStatus
from ....order import events as order_events
from ....order.error_codes import OrderErrorCode
from ....order.events import order_replacement_created
+from ....order.fetch import fetch_order_info
from ....order.interface import OrderTaxedPricesData
from ....order.models import Order, OrderEvent, OrderLine, get_order_number
from ....order.notifications import get_default_order_payload
@@ -1539,24 +1539,38 @@ ORDER_CONFIRM_MUTATION = """
"""
+@patch("saleor.order.actions.handle_fully_paid_order")
@patch("saleor.plugins.manager.PluginsManager.notify")
@patch("saleor.payment.gateway.capture")
def test_order_confirm(
capture_mock,
mocked_notify,
+ handle_fully_paid_order_mock,
staff_api_client,
order_unconfirmed,
permission_manage_orders,
payment_txn_preauth,
+ site_settings,
):
+ # given
payment_txn_preauth.order = order_unconfirmed
- payment_txn_preauth.save()
+ payment_txn_preauth.captured_amount = order_unconfirmed.total.gross.amount
+ payment_txn_preauth.total = order_unconfirmed.total.gross.amount
+ payment_txn_preauth.save(update_fields=["order", "captured_amount", "total"])
+
+ order_unconfirmed.total_charged = order_unconfirmed.total.gross
+ order_unconfirmed.save(update_fields=["total_charged_amount"])
+
staff_api_client.user.user_permissions.add(permission_manage_orders)
assert not OrderEvent.objects.exists()
+
+ # when
response = staff_api_client.post_graphql(
ORDER_CONFIRM_MUTATION,
{"id": graphene.Node.to_global_id("Order", order_unconfirmed.id)},
)
+
+ # then
order_data = get_graphql_content(response)["data"]["orderConfirm"]["order"]
assert order_data["status"] == OrderStatus.UNFULFILLED.upper()
@@ -1592,6 +1606,10 @@ def test_order_confirm(
expected_payload,
channel_slug=order_unconfirmed.channel.slug,
)
+ order_info = fetch_order_info(order_unconfirmed)
+ handle_fully_paid_order_mock.assert_called_once_with(
+ ANY, order_info, staff_api_client.user, None, site_settings
+ )
@patch("saleor.plugins.manager.PluginsManager.notify")
@@ -4660,7 +4678,7 @@ def test_draft_order_complete_not_available_shipping_method(
assert {error["field"] for error in data["errors"]} == {"shipping", "lines"}
[email protected]("saleor.plugins.manager.PluginsManager.excluded_shipping_methods_for_order")
+@patch("saleor.plugins.manager.PluginsManager.excluded_shipping_methods_for_order")
def test_draft_order_complete_with_excluded_shipping_method(
mocked_webhook,
draft_order,
@@ -4692,7 +4710,7 @@ def test_draft_order_complete_with_excluded_shipping_method(
assert data["errors"][0]["field"] == "shipping"
[email protected]("saleor.plugins.manager.PluginsManager.excluded_shipping_methods_for_order")
+@patch("saleor.plugins.manager.PluginsManager.excluded_shipping_methods_for_order")
def test_draft_order_complete_with_not_excluded_shipping_method(
mocked_webhook,
draft_order,
@@ -6506,22 +6524,30 @@ ORDER_CAPTURE_MUTATION = """
"""
[email protected]("saleor.plugins.manager.PluginsManager.notify")
+@patch("saleor.giftcard.utils.fulfill_non_shippable_gift_cards")
+@patch("saleor.plugins.manager.PluginsManager.notify")
def test_order_capture(
mocked_notify,
- channel_USD,
+ fulfill_non_shippable_gift_cards_mock,
staff_api_client,
permission_manage_orders,
payment_txn_preauth,
staff_user,
+ site_settings,
):
+ # given
order = payment_txn_preauth.order
+
order_id = graphene.Node.to_global_id("Order", order.id)
amount = float(payment_txn_preauth.total)
variables = {"id": order_id, "amount": amount}
+
+ # when
response = staff_api_client.post_graphql(
ORDER_CAPTURE_MUTATION, variables, permissions=[permission_manage_orders]
)
+
+ # then
content = get_graphql_content(response)
data = content["data"]["orderCapture"]["order"]
order.refresh_from_db()
@@ -6565,6 +6591,9 @@ def test_order_capture(
expected_payment_payload,
channel_slug=order.channel.slug,
)
+ fulfill_non_shippable_gift_cards_mock.assert_called_once_with(
+ order, list(order.lines.all()), site_settings, staff_api_client.user, None, ANY
+ )
@patch("saleor.plugins.manager.PluginsManager.is_event_active_for_any_plugin")
diff --git a/saleor/order/actions.py b/saleor/order/actions.py
index 6fc506e6a2..91303babb1 100644
--- a/saleor/order/actions.py
+++ b/saleor/order/actions.py
@@ -84,6 +84,7 @@ def order_created(
app: Optional["App"],
manager: "PluginsManager",
from_draft: bool = False,
+ site_settings: Optional["SiteSettings"] = None,
):
order = order_info.order
events.order_created_event(order=order, user=user, app=app, from_draft=from_draft)
@@ -98,6 +99,7 @@ def order_created(
amount=payment.total,
payment=payment,
manager=manager,
+ site_settings=site_settings,
)
elif order.is_pre_authorized():
order_authorized(
@@ -135,7 +137,10 @@ def handle_fully_paid_order(
order_info: "OrderInfo",
user: Optional["User"] = None,
app: Optional["App"] = None,
+ site_settings: Optional["SiteSettings"] = None,
):
+ from ..giftcard.utils import fulfill_non_shippable_gift_cards
+
order = order_info.order
events.order_fully_paid_event(order=order, user=user, app=app)
if order_info.customer_email:
@@ -147,6 +152,16 @@ def handle_fully_paid_order(
except Exception:
# Analytics failing should not abort the checkout flow
logger.exception("Recording order in analytics failed")
+
+ if site_settings is None:
+ site_settings = Site.objects.get_current().settings
+
+ if site_settings.automatically_fulfill_non_shippable_gift_card:
+ order_lines = [line.line for line in order_info.lines_data]
+ fulfill_non_shippable_gift_cards(
+ order, order_lines, site_settings, user, app, manager
+ )
+
manager.order_fully_paid(order)
manager.order_updated(order)
@@ -300,6 +315,7 @@ def order_captured(
amount: "Decimal",
payment: "Payment",
manager: "PluginsManager",
+ site_settings: Optional["SiteSettings"] = None,
):
order = order_info.order
events.payment_captured_event(
@@ -307,7 +323,7 @@ def order_captured(
)
manager.order_updated(order)
if order.is_fully_paid():
- handle_fully_paid_order(manager, order_info, user, app)
+ handle_fully_paid_order(manager, order_info, user, app, site_settings)
def fulfillment_tracking_updated(
diff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.py
index 713035c3c6..cf287f6b96 100644
--- a/saleor/order/tests/test_order_actions.py
+++ b/saleor/order/tests/test_order_actions.py
@@ -4,6 +4,8 @@ from unittest.mock import patch
import pytest
from prices import Money, TaxedMoney
+from ...giftcard import GiftCardEvents
+from ...giftcard.models import GiftCard, GiftCardEvent
from ...order.fetch import OrderLineInfo, fetch_order_info
from ...payment import ChargeStatus, PaymentError, TransactionKind
from ...payment.models import Payment
@@ -23,7 +25,7 @@ from ..actions import (
mark_order_as_paid,
order_refunded,
)
-from ..models import Fulfillment
+from ..models import Fulfillment, OrderLine
from ..notifications import (
send_fulfillment_confirmation_to_customer,
send_payment_confirmation,
@@ -135,6 +137,136 @@ def test_handle_fully_paid_order_no_email(mock_send_payment_confirmation, order)
assert not mock_send_payment_confirmation.called
+@patch("saleor.giftcard.utils.send_gift_card_notification")
+@patch("saleor.order.actions.send_payment_confirmation")
+def test_handle_fully_paid_order_gift_cards_created(
+ mock_send_payment_confirmation,
+ send_notification_mock,
+ site_settings,
+ order_with_lines,
+ non_shippable_gift_card_product,
+ shippable_gift_card_product,
+):
+ """Ensure the non shippable gift card are fulfilled when the flag for automatic
+ fulfillment non shippable gift card is set."""
+ # given
+ site_settings.automatically_fulfill_non_shippable_gift_card = True
+ site_settings.save(update_fields=["automatically_fulfill_non_shippable_gift_card"])
+
+ order = order_with_lines
+
+ non_shippable_gift_card_line = order_with_lines.lines.first()
+ non_shippable_variant = non_shippable_gift_card_product.variants.get()
+ non_shippable_gift_card_line.variant = non_shippable_variant
+ non_shippable_gift_card_line.is_gift_card = True
+ non_shippable_gift_card_line.is_shipping_required = False
+ non_shippable_gift_card_line.quantity = 1
+ allocation = non_shippable_gift_card_line.allocations.first()
+ allocation.quantity_allocated = 1
+ allocation.save(update_fields=["quantity_allocated"])
+
+ shippable_gift_card_line = order_with_lines.lines.last()
+ shippable_variant = shippable_gift_card_product.variants.get()
+ shippable_gift_card_line.variant = shippable_variant
+ shippable_gift_card_line.is_gift_card = True
+ shippable_gift_card_line.is_shipping_required = True
+ shippable_gift_card_line.quantity = 1
+
+ OrderLine.objects.bulk_update(
+ [non_shippable_gift_card_line, shippable_gift_card_line],
+ ["variant", "is_gift_card", "is_shipping_required", "quantity"],
+ )
+
+ manager = get_plugins_manager()
+
+ order.payments.add(Payment.objects.create())
+ order_info = fetch_order_info(order)
+
+ # when
+ handle_fully_paid_order(manager, order_info)
+
+ # then
+ flush_post_commit_hooks()
+ assert order.events.filter(type=OrderEvents.ORDER_FULLY_PAID)
+
+ mock_send_payment_confirmation.assert_called_once_with(order_info, manager)
+
+ flush_post_commit_hooks()
+ gift_card = GiftCard.objects.get()
+ assert gift_card.initial_balance == non_shippable_gift_card_line.unit_price_gross
+ assert GiftCardEvent.objects.filter(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
+
+ send_notification_mock.assert_called_once_with(
+ None,
+ None,
+ order.user,
+ order.user_email,
+ gift_card,
+ manager,
+ order.channel.slug,
+ resending=False,
+ )
+
+
+@patch("saleor.giftcard.utils.send_gift_card_notification")
+@patch("saleor.order.actions.send_payment_confirmation")
+def test_handle_fully_paid_order_gift_cards_not_created(
+ mock_send_payment_confirmation,
+ send_notification_mock,
+ site_settings,
+ order_with_lines,
+ non_shippable_gift_card_product,
+ shippable_gift_card_product,
+):
+ """Ensure the non shippable gift card are not fulfilled when the flag for
+ automatic fulfillment non shippable gift card is not set."""
+ # given
+ site_settings.automatically_fulfill_non_shippable_gift_card = False
+ site_settings.save(update_fields=["automatically_fulfill_non_shippable_gift_card"])
+
+ order = order_with_lines
+
+ non_shippable_gift_card_line = order_with_lines.lines.first()
+ non_shippable_variant = non_shippable_gift_card_product.variants.get()
+ non_shippable_gift_card_line.variant = non_shippable_variant
+ non_shippable_gift_card_line.is_gift_card = True
+ non_shippable_gift_card_line.is_shipping_required = False
+ non_shippable_gift_card_line.quantity = 1
+ allocation = non_shippable_gift_card_line.allocations.first()
+ allocation.quantity_allocated = 1
+ allocation.save(update_fields=["quantity_allocated"])
+
+ shippable_gift_card_line = order_with_lines.lines.last()
+ shippable_variant = shippable_gift_card_product.variants.get()
+ shippable_gift_card_line.variant = shippable_variant
+ shippable_gift_card_line.is_gift_card = True
+ shippable_gift_card_line.is_shipping_required = True
+ shippable_gift_card_line.quantity = 1
+
+ OrderLine.objects.bulk_update(
+ [non_shippable_gift_card_line, shippable_gift_card_line],
+ ["variant", "is_gift_card", "is_shipping_required", "quantity"],
+ )
+
+ manager = get_plugins_manager()
+
+ order.payments.add(Payment.objects.create())
+ order_info = fetch_order_info(order)
+
+ # when
+ handle_fully_paid_order(manager, order_info)
+
+ # then
+ flush_post_commit_hooks()
+ assert order.events.filter(type=OrderEvents.ORDER_FULLY_PAID)
+
+ mock_send_payment_confirmation.assert_called_once_with(order_info, manager)
+
+ flush_post_commit_hooks()
+ assert not GiftCard.objects.exists()
+ send_notification_mock.assert_not_called
+
+
def test_mark_as_paid(admin_user, draft_order):
manager = get_plugins_manager()
mark_order_as_paid(draft_order, admin_user, None, manager)