Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add checkout_created and checkout_updated hooks to manager and to webhook plugin
Maciej Korycinski4b782352d552
diff --git a/saleor/core/permissions.py b/saleor/core/permissions.pyindex 9cb29055f7..e33099a847 100644--- a/saleor/core/permissions.py+++ b/saleor/core/permissions.py@@ -35,7 +35,6 @@ class MenuPermissions(BasePermissionEnum):MANAGE_MENUS = "menu.manage_menus"-# Depreacted. It will be removed in Saleor 3.0class CheckoutPermissions(BasePermissionEnum):MANAGE_CHECKOUTS = "checkout.manage_checkouts"diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.pyindex 18a804554c..1d0b98ab5c 100644--- a/saleor/graphql/checkout/mutations.py+++ b/saleor/graphql/checkout/mutations.py@@ -320,6 +320,7 @@ class CheckoutCreate(ModelMutation, I18nMixin):cls.clean_instance(info, checkout)cls.save(info, checkout, cleaned_input)cls._save_m2m(info, checkout, cleaned_input)+ info.context.plugins.checkout_created(checkout)return CheckoutCreate(checkout=checkout, created=True)@@ -375,7 +376,7 @@ class CheckoutLinesAdd(BaseMutation):checkout, lines, info.context.discounts)recalculate_checkout_discount(checkout, lines, info.context.discounts)-+ info.context.plugins.checkout_updated(checkout)return CheckoutLinesAdd(checkout=checkout)@@ -423,6 +424,7 @@ class CheckoutLineDelete(BaseMutation):)recalculate_checkout_discount(checkout, lines, info.context.discounts)+ info.context.plugins.checkout_updated(checkout)return CheckoutLineDelete(checkout=checkout)@@ -465,6 +467,8 @@ class CheckoutCustomerAttach(BaseMutation):checkout.user = info.context.usercheckout.save(update_fields=["user", "last_change"])++ info.context.plugins.checkout_updated(checkout)return CheckoutCustomerAttach(checkout=checkout)@@ -495,6 +499,8 @@ class CheckoutCustomerDetach(BaseMutation):checkout.user = Nonecheckout.save(update_fields=["user", "last_change"])++ info.context.plugins.checkout_updated(checkout)return CheckoutCustomerDetach(checkout=checkout)@@ -579,6 +585,7 @@ class CheckoutShippingAddressUpdate(BaseMutation, I18nMixin):change_shipping_address_in_checkout(checkout, shipping_address)recalculate_checkout_discount(checkout, lines, info.context.discounts)+ info.context.plugins.checkout_updated(checkout)return CheckoutShippingAddressUpdate(checkout=checkout)@@ -608,6 +615,7 @@ class CheckoutBillingAddressUpdate(CheckoutShippingAddressUpdate):with transaction.atomic():billing_address.save()change_billing_address_in_checkout(checkout, billing_address)+ info.context.plugins.checkout_updated(checkout)return CheckoutBillingAddressUpdate(checkout=checkout)@@ -632,6 +640,7 @@ class CheckoutEmailUpdate(BaseMutation):checkout.email = emailcls.clean_instance(info, checkout)checkout.save(update_fields=["email", "last_change"])+ info.context.plugins.checkout_updated(checkout)return CheckoutEmailUpdate(checkout=checkout)@@ -706,7 +715,7 @@ class CheckoutShippingMethodUpdate(BaseMutation):checkout.shipping_method = shipping_methodcheckout.save(update_fields=["shipping_method", "last_change"])recalculate_checkout_discount(checkout, lines, info.context.discounts)-+ info.context.plugins.checkout_updated(checkout)return CheckoutShippingMethodUpdate(checkout=checkout)@@ -878,6 +887,7 @@ class CheckoutAddPromoCode(BaseMutation):)lines = list(checkout)add_promo_code_to_checkout(checkout, lines, promo_code, info.context.discounts)+ info.context.plugins.checkout_updated(checkout)return CheckoutAddPromoCode(checkout=checkout)@@ -903,6 +913,7 @@ class CheckoutRemovePromoCode(BaseMutation):info, checkout_id, only_type=Checkout, field="checkout_id")remove_promo_code_from_checkout(checkout, promo_code)+ info.context.plugins.checkout_updated(checkout)return CheckoutRemovePromoCode(checkout=checkout)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 4925d28f57..a53c8348c3 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -5254,6 +5254,8 @@ enum WebhookEventTypeEnum {CUSTOMER_CREATEDPRODUCT_CREATEDCHECKOUT_QUANTITY_CHANGED+ CHECKOUT_CREATED+ CHECKOUT_UPDATEDFULFILLMENT_CREATED}@@ -5271,6 +5273,8 @@ enum WebhookSampleEventTypeEnum {CUSTOMER_CREATEDPRODUCT_CREATEDCHECKOUT_QUANTITY_CHANGED+ CHECKOUT_CREATED+ CHECKOUT_UPDATEDFULFILLMENT_CREATED}diff --git a/saleor/graphql/webhook/tests/test_webhook.py b/saleor/graphql/webhook/tests/test_webhook.pyindex 2552b05323..4efa0f46ce 100644--- a/saleor/graphql/webhook/tests/test_webhook.py+++ b/saleor/graphql/webhook/tests/test_webhook.py@@ -571,6 +571,8 @@ SAMPLE_PAYLOAD_QUERY = """(WebhookSampleEventTypeEnum.CUSTOMER_CREATED, False),(WebhookSampleEventTypeEnum.PRODUCT_CREATED, False),(WebhookSampleEventTypeEnum.CHECKOUT_QUANTITY_CHANGED, False),+ (WebhookSampleEventTypeEnum.CHECKOUT_CREATED, False),+ (WebhookSampleEventTypeEnum.CHECKOUT_UPDATED, False),(WebhookSampleEventTypeEnum.FULFILLMENT_CREATED, True),],)@@ -609,6 +611,8 @@ def test_sample_payload_query_by_app((WebhookSampleEventTypeEnum.CUSTOMER_CREATED, True),(WebhookSampleEventTypeEnum.PRODUCT_CREATED, True),(WebhookSampleEventTypeEnum.CHECKOUT_QUANTITY_CHANGED, True),+ (WebhookSampleEventTypeEnum.CHECKOUT_CREATED, True),+ (WebhookSampleEventTypeEnum.CHECKOUT_UPDATED, True),(WebhookSampleEventTypeEnum.FULFILLMENT_CREATED, False),],)diff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.pyindex 02f698aaf9..a736dea406 100644--- a/saleor/plugins/base_plugin.py+++ b/saleor/plugins/base_plugin.py@@ -291,6 +291,28 @@ class BasePlugin:"""return NotImplemented+ # Deprecated. This method will be removed in Saleor 3.0+ def checkout_quantity_changed(+ self, checkout: "Checkout", previous_value: Any+ ) -> Any:+ return NotImplemented++ def checkout_created(self, checkout: "Checkout", previous_value: Any) -> Any:+ """Trigger when checkout is created.++ Overwrite this method if you need to trigger specific logic when a checkout is+ created.+ """+ return NotImplemented++ def checkout_updated(self, checkout: "Checkout", previous_value: Any) -> Any:+ """Trigger when checkout is updated.++ Overwrite this method if you need to trigger specific logic when a checkout is+ updated.+ """+ return NotImplemented+def fetch_taxes_data(self, previous_value: Any) -> Any:"""Triggered when ShopFetchTaxRates mutation is called."""return NotImplementeddiff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.pyindex b970ea2548..035e6c6f31 100644--- a/saleor/plugins/manager.py+++ b/saleor/plugins/manager.py@@ -93,9 +93,6 @@ class PluginsManager(PaymentInterface):"change_user_address", default_value, address, address_type, user)- def checkout_quantity_changed(self, checkout: "Checkout") -> None:- self.__run_method_on_plugins("checkout_quantity_changed", None, checkout)-def calculate_checkout_total(self,checkout: "Checkout",@@ -249,6 +246,21 @@ class PluginsManager(PaymentInterface):"fulfillment_created", default_value, fulfillment)+ # Deprecated. This method will be removed in Saleor 3.0+ def checkout_quantity_changed(self, checkout: "Checkout"):+ default_value = None+ return self.__run_method_on_plugins(+ "checkout_quantity_changed", default_value, checkout+ )++ def checkout_created(self, checkout: "Checkout"):+ default_value = None+ return self.__run_method_on_plugins("checkout_created", default_value, checkout)++ def checkout_updated(self, checkout: "Checkout"):+ default_value = None+ return self.__run_method_on_plugins("checkout_updated", default_value, checkout)+def authorize_payment(self, gateway: str, payment_information: "PaymentData") -> "GatewayResponse":diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.pyindex 29aac5256c..66d2bd53e8 100644--- a/saleor/plugins/webhook/plugin.py+++ b/saleor/plugins/webhook/plugin.py@@ -81,6 +81,7 @@ class WebhookPlugin(BasePlugin):product_data = generate_product_payload(product)trigger_webhooks_for_event.delay(WebhookEventType.PRODUCT_CREATED, product_data)+ # Deprecated. This method will be removed in Saleor 3.0def checkout_quantity_changed(self, checkout: "Checkout", previous_value: Any) -> Any:@@ -90,3 +91,19 @@ class WebhookPlugin(BasePlugin):trigger_webhooks_for_event.delay(WebhookEventType.CHECKOUT_QUANTITY_CHANGED, checkout_data)++ def checkout_created(self, checkout: "Checkout", previous_value: Any) -> Any:+ if not self.active:+ return previous_value+ checkout_data = generate_checkout_payload(checkout)+ trigger_webhooks_for_event.delay(+ WebhookEventType.CHECKOUT_CREATED, checkout_data+ )++ def checkout_updated(self, checkout: "Checkout", previous_value: Any) -> Any:+ if not self.active:+ return previous_value+ checkout_data = generate_checkout_payload(checkout)+ trigger_webhooks_for_event.delay(+ WebhookEventType.CHECKOUT_UPADTED, checkout_data+ )diff --git a/saleor/plugins/webhook/tests/test_webhook.py b/saleor/plugins/webhook/tests/test_webhook.pyindex b3f02b4fdf..d1e2a1264e 100644--- a/saleor/plugins/webhook/tests/test_webhook.py+++ b/saleor/plugins/webhook/tests/test_webhook.py@@ -216,3 +216,27 @@ def test_checkout_quantity_changed(mocked_webhook_trigger.assert_called_once_with(WebhookEventType.CHECKOUT_QUANTITY_CHANGED, expected_data)++[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")+def test_checkout_created(mocked_webhook_trigger, settings, checkout_with_items):+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ manager = get_plugins_manager()+ manager.checkout_created(checkout_with_items)++ expected_data = generate_checkout_payload(checkout_with_items)+ mocked_webhook_trigger.assert_called_once_with(+ WebhookEventType.CHECKOUT_CREATED, expected_data+ )++[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")+def test_checkout_updated(mocked_webhook_trigger, settings, checkout_with_items):+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ manager = get_plugins_manager()+ manager.checkout_updated(checkout_with_items)++ expected_data = generate_checkout_payload(checkout_with_items)+ mocked_webhook_trigger.assert_called_once_with(+ WebhookEventType.CHECKOUT_UPADTED, expected_data+ )diff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.pyindex 6fb271d4e8..1caca8c954 100644--- a/saleor/webhook/event_types.py+++ b/saleor/webhook/event_types.py@@ -23,6 +23,9 @@ class WebhookEventType:# This event is deprecated. It will be removed in Saleor 3.0CHECKOUT_QUANTITY_CHANGED = "checkout_quantity_changed"+ CHECKOUT_CREATED = "checkout_created"+ CHECKOUT_UPADTED = "checkout_updated"+DISPLAY_LABELS = {ANY: "Any events",ORDER_CREATED: "Order created",@@ -34,6 +37,8 @@ class WebhookEventType:PRODUCT_CREATED: "Product created",# CHECKOUT_QUANTITY_CHANGED is deprecated. It will be removed in Saleor 3.0CHECKOUT_QUANTITY_CHANGED: "Checkout quantity changed",+ CHECKOUT_CREATED: "Checkout created",+ CHECKOUT_UPADTED: "Checkout updated",FULFILLMENT_CREATED: "Fulfillment_created",}@@ -48,6 +53,8 @@ class WebhookEventType:(PRODUCT_CREATED, DISPLAY_LABELS[PRODUCT_CREATED]),# CHECKOUT_QUANTITY_CHANGED is deprecated. It will be removed in Saleor 3.0(CHECKOUT_QUANTITY_CHANGED, DISPLAY_LABELS[CHECKOUT_QUANTITY_CHANGED]),+ (CHECKOUT_CREATED, DISPLAY_LABELS[CHECKOUT_CREATED]),+ (CHECKOUT_UPADTED, DISPLAY_LABELS[CHECKOUT_UPADTED]),(FULFILLMENT_CREATED, DISPLAY_LABELS[FULFILLMENT_CREATED]),]@@ -60,5 +67,7 @@ class WebhookEventType:CUSTOMER_CREATED: AccountPermissions.MANAGE_USERS,PRODUCT_CREATED: ProductPermissions.MANAGE_PRODUCTS,CHECKOUT_QUANTITY_CHANGED: CheckoutPermissions.MANAGE_CHECKOUTS,+ CHECKOUT_CREATED: CheckoutPermissions.MANAGE_CHECKOUTS,+ CHECKOUT_UPADTED: CheckoutPermissions.MANAGE_CHECKOUTS,FULFILLMENT_CREATED: OrderPermissions.MANAGE_ORDERS,}diff --git a/saleor/webhook/payloads.py b/saleor/webhook/payloads.pyindex 17ab7b3b2f..988e737788 100644--- a/saleor/webhook/payloads.py+++ b/saleor/webhook/payloads.py@@ -292,6 +292,11 @@ def _generate_sample_order_payload(event_name):def generate_sample_payload(event_name: str) -> Optional[dict]:+ checkout_events = [+ WebhookEventType.CHECKOUT_QUANTITY_CHANGED,+ WebhookEventType.CHECKOUT_UPADTED,+ WebhookEventType.CHECKOUT_CREATED,+ ]if event_name == WebhookEventType.CUSTOMER_CREATED:user = generate_fake_user()payload = generate_customer_payload(user)@@ -300,7 +305,7 @@ def generate_sample_payload(event_name: str) -> Optional[dict]:Product.objects.prefetch_related("category", "collections", "variants"))payload = generate_product_payload(product) if product else None- elif event_name == WebhookEventType.CHECKOUT_QUANTITY_CHANGED:+ elif event_name in checkout_events:checkout = _get_sample_object(Checkout.objects.prefetch_related("lines__variant__product"))