Skip to content

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

Merge branch 'master' into feature/gift-cards-mvp

IKarbowiakde976c6c24f8
diff --cc saleor/giftcard/notifications.py
index 6c39fcfbfe,0000000000..e7a6aad8de
mode 100644,000000..100644
--- a/saleor/giftcard/notifications.py
+++ b/saleor/giftcard/notifications.py
@@@ -1,43 -1,0 +1,43 @@@
+from typing import TYPE_CHECKING
+
+from ..account.notifications import get_default_user_payload
- from ..core.notifications import get_site_context
++from ..core.notification.utils import get_site_context
+from ..core.notify_events import NotifyEventType
+
+if TYPE_CHECKING:
+ from .models import GiftCard
+
+
+def send_gift_card_notification(
+ requester_user,
+ app,
+ customer_user,
+ email,
+ gift_card,
+ manager,
+ channel_slug,
+ *,
+ resending
+):
+ """Trigger sending a gift card notification for the given recipient."""
+ payload = {
+ "gift_card": get_default_gift_card_payload(gift_card),
+ "user": get_default_user_payload(customer_user) if customer_user else None,
+ "requester_user_id": requester_user.id if requester_user else None,
+ "requester_app_id": app.id if app else None,
+ "recipient_email": email,
+ "resending": resending,
+ **get_site_context(),
+ }
+ manager.notify(
+ NotifyEventType.SEND_GIFT_CARD, payload=payload, channel_slug=channel_slug
+ )
+
+
+def get_default_gift_card_payload(gift_card: "GiftCard"):
+ return {
+ "id": gift_card.id,
+ "code": gift_card.code,
+ "balance": gift_card.current_balance_amount,
+ "currency": gift_card.currency,
+ }
diff --cc saleor/graphql/checkout/tests/test_checkout_complete.py
index 1c60ab4697,e83b51b81a..89c48c4750
--- a/saleor/graphql/checkout/tests/test_checkout_complete.py
+++ b/saleor/graphql/checkout/tests/test_checkout_complete.py
@@@ -278,76 -274,134 +279,204 @@@ def test_checkout_complete
order_confirmed_mock.assert_called_once_with(order)
+ @pytest.mark.integration
+ @patch("saleor.graphql.checkout.mutations.complete_checkout")
+ def test_checkout_complete_by_app(
+ mocked_complete_checkout,
+ app_api_client,
+ checkout_with_item,
+ customer_user,
+ permission_impersonate_user,
+ payment_dummy,
+ address,
+ shipping_method,
+ ):
+ mocked_complete_checkout.return_value = (None, True, {})
+ checkout = checkout_with_item
+ checkout.user = customer_user
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.save()
+
+ manager = get_plugins_manager()
+ lines = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ total = calculations.calculate_checkout_total_with_gift_cards(
+ manager, checkout_info, lines, 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()
+
+ redirect_url = "https://www.example.com"
+ variables = {"token": checkout.token, "redirectUrl": redirect_url}
+
+ response = app_api_client.post_graphql(
+ MUTATION_CHECKOUT_COMPLETE,
+ variables,
+ permissions=[permission_impersonate_user],
+ check_no_permissions=False,
+ )
+
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+
+ assert not data["errors"]
+
+ mocked_complete_checkout.assert_called_once_with(
+ manager=ANY,
+ checkout_info=ANY,
+ lines=ANY,
+ payment_data=ANY,
+ store_source=ANY,
+ discounts=ANY,
+ user=checkout.user,
+ app=ANY,
+ site_settings=ANY,
+ tracking_code=ANY,
+ redirect_url=ANY,
+ )
+
+
+ @pytest.mark.integration
+ @patch("saleor.graphql.checkout.mutations.complete_checkout")
+ def test_checkout_complete_by_app_with_missing_permission(
+ mocked_complete_checkout,
+ app_api_client,
+ checkout_with_item,
+ customer_user,
+ permission_manage_users,
+ payment_dummy,
+ address,
+ shipping_method,
+ ):
+ mocked_complete_checkout.return_value = (None, True, {})
+ checkout = checkout_with_item
+ checkout.user = customer_user
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.save()
+
+ manager = get_plugins_manager()
+ lines = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ total = calculations.calculate_checkout_total_with_gift_cards(
+ manager, checkout_info, lines, 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()
+
+ redirect_url = "https://www.example.com"
+ variables = {"token": checkout.token, "redirectUrl": redirect_url}
+
+ response = app_api_client.post_graphql(
+ MUTATION_CHECKOUT_COMPLETE,
+ variables,
+ permissions=[permission_manage_users],
+ check_no_permissions=False,
+ )
+
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+
+ assert not data["errors"]
+
+ mocked_complete_checkout.assert_called_once_with(
+ manager=ANY,
+ checkout_info=ANY,
+ lines=ANY,
+ payment_data=ANY,
+ store_source=ANY,
+ discounts=ANY,
+ user=AnonymousUser(),
+ app=ANY,
+ site_settings=ANY,
+ tracking_code=ANY,
+ redirect_url=ANY,
+ )
+
+
+@patch("saleor.giftcard.utils.send_gift_card_notification")
+@patch("saleor.plugins.manager.PluginsManager.order_confirmed")
+def test_checkout_complete_gift_card_bought(
+ order_confirmed_mock,
+ send_notification_mock,
+ site_settings,
+ customer_user,
+ user_api_client,
+ checkout_with_gift_card_items,
+ payment_dummy,
+ address,
+ shipping_method,
+):
+ checkout = checkout_with_gift_card_items
+ checkout.shipping_address = address
+ checkout.shipping_method = shipping_method
+ checkout.billing_address = address
+ checkout.store_value_in_metadata(items={"accepted": "true"})
+ checkout.store_value_in_private_metadata(items={"accepted": "false"})
+ checkout.user = customer_user
+ checkout.save()
+
+ manager = get_plugins_manager()
+ lines = fetch_checkout_lines(checkout)
+ checkout_info = fetch_checkout_info(checkout, lines, [], manager)
+ total = calculations.calculate_checkout_total_with_gift_cards(
+ manager, checkout_info, lines, address
+ )
+ site_settings.automatically_confirm_all_new_orders = True
+ site_settings.automatically_fulfill_non_shippable_gift_card = True
+ site_settings.save()
+
+ 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()
+
+ orders_count = Order.objects.count()
+ redirect_url = "https://www.example.com"
+ variables = {"token": checkout.token, "redirectUrl": redirect_url}
+ response = user_api_client.post_graphql(MUTATION_CHECKOUT_COMPLETE, variables)
+
+ content = get_graphql_content(response)
+ data = content["data"]["checkoutComplete"]
+ assert not data["errors"]
+
+ assert Order.objects.count() == orders_count + 1
+ order = Order.objects.first()
+ assert order.status == OrderStatus.PARTIALLY_FULFILLED
+
+ gift_card = GiftCard.objects.get()
+ assert GiftCardEvent.objects.filter(gift_card=gift_card, type=GiftCardEvents.BOUGHT)
+ send_notification_mock.assert_called_once_with(
+ customer_user,
+ None,
+ customer_user,
+ customer_user.email,
+ gift_card,
+ ANY,
+ checkout.channel.slug,
+ resending=False,
+ )
+ order_confirmed_mock.assert_called_once_with(order)
+ assert Fulfillment.objects.count() == 1
+
+
def test_checkout_complete_with_variant_without_price(
site_settings,
user_api_client,
diff --cc saleor/graphql/core/enums.py
index 122192ec9f,f8b8a04a07..5555893f82
--- a/saleor/graphql/core/enums.py
+++ b/saleor/graphql/core/enums.py
@@@ -31,6 -30,8 +31,7 @@@ from ...site import error_codes as site
from ...warehouse import error_codes as warehouse_error_codes
from ...webhook import error_codes as webhook_error_codes
from ...wishlist import error_codes as wishlist_error_codes
+ from ..notifications import error_codes as external_notifications_error_codes
-from ..shop import error_codes as shop_error_codes
from .utils import str_to_enum
diff --cc saleor/graphql/core/types/common.py
index 22743e1cc6,6357b8ec42..2e06f7b528
--- a/saleor/graphql/core/types/common.py
+++ b/saleor/graphql/core/types/common.py
@@@ -15,8 -15,8 +15,9 @@@ from ..enums import
CollectionErrorCode,
DiscountErrorCode,
ExportErrorCode,
+ ExternalNotificationTriggerErrorCode,
GiftCardErrorCode,
+ GiftCardSettingsErrorCode,
InvoiceErrorCode,
JobStatusEnum,
LanguageCodeEnum,
diff --cc saleor/graphql/core/utils/error_codes.py
index a09b23f861,c80b466067..0fe50a2876
--- a/saleor/graphql/core/utils/error_codes.py
+++ b/saleor/graphql/core/utils/error_codes.py
@@@ -22,7 -22,8 +22,8 @@@ from ....payment.error_codes import Pay
from ....plugins.error_codes import PluginErrorCode
from ....product.error_codes import ProductErrorCode
from ....shipping.error_codes import ShippingErrorCode
+from ....site.error_codes import GiftCardSettingsErrorCode, OrderSettingsErrorCode
+ from ...notifications.error_codes import ExternalNotificationErrorCodes
-from ...shop.error_codes import OrderSettingsErrorCode
DJANGO_VALIDATORS_ERROR_CODES = [
"invalid",
@@@ -60,7 -61,7 +61,8 @@@ SALEOR_ERROR_CODE_ENUMS =
CheckoutErrorCode,
ExportErrorCode,
DiscountErrorCode,
+ GiftCardSettingsErrorCode,
+ ExternalNotificationErrorCodes,
PluginErrorCode,
GiftCardErrorCode,
InvoiceErrorCode,
diff --cc saleor/graphql/schema.graphql
index fcd67413fd,89db584b44..9c5b568dde
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@@ -3833,12 -3783,8 +3856,13 @@@ type Mutation
giftCardDelete(id: ID!): GiftCardDelete
giftCardDeactivate(id: ID!): GiftCardDeactivate
giftCardUpdate(id: ID!, input: GiftCardUpdateInput!): GiftCardUpdate
+ giftCardResend(input: GiftCardResendInput!): GiftCardResend
+ giftCardAddNote(id: ID!, input: GiftCardAddNoteInput!): GiftCardAddNote
+ giftCardBulkDelete(ids: [ID]!): GiftCardBulkDelete
+ giftCardBulkActivate(ids: [ID]!): GiftCardBulkActivate
+ giftCardBulkDeactivate(ids: [ID]!): GiftCardBulkDeactivate
pluginUpdate(channelId: ID, id: ID!, input: PluginUpdateInput!): PluginUpdate
+ externalNotificationTrigger(channel: String!, input: ExternalNotificationTriggerInput!, pluginId: String): ExternalNotificationTrigger
saleCreate(input: SaleInput!): SaleCreate
saleDelete(id: ID!): SaleDelete
saleBulkDelete(ids: [ID]!): SaleBulkDelete
diff --cc saleor/plugins/user_email/plugin.py
index 963bac8299,c6a6c7376b..5691f33f5a
--- a/saleor/plugins/user_email/plugin.py
+++ b/saleor/plugins/user_email/plugin.py
@@@ -430,10 -408,9 +430,10 @@@ class UserEmailPlugin(BasePlugin)
order_refund_confirmation=configuration[
constants.ORDER_REFUND_CONFIRMATION_TEMPLATE_FIELD
],
+ send_gift_card=configuration[constants.SEND_GIFT_CARD_TEMPLATE_FIELD],
)
- def notify(self, event: NotifyEventType, payload: dict, previous_value):
+ def notify(self, event: Union[NotifyEventType, str], payload: dict, previous_value):
if not self.active:
return previous_value
event_map = get_user_event_map()