Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add webhook events for invoices
Tomasz Szymanskic992066bcc78
diff --git a/saleor/graphql/invoice/mutations.py b/saleor/graphql/invoice/mutations.pyindex b86ddcdbfb..227abe690d 100644--- a/saleor/graphql/invoice/mutations.py+++ b/saleor/graphql/invoice/mutations.py@@ -274,4 +274,5 @@ class SendInvoiceEmail(ModelMutation):cls.clean_instance(info, instance)send_invoice.delay(instance.pk)events.invoice_sent_event(user=info.context.user, invoice=instance)+ info.context.plugins.invoice_sent(instance, instance.order.user.email)return SendInvoiceEmail(invoice=instance)diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 6d0a7ecf9c..8b1f70c93a 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -5314,6 +5314,9 @@ enum WebhookEventTypeEnum {ORDER_UPDATEDORDER_CANCELLEDORDER_FULFILLED+ INVOICE_REQUESTED+ INVOICE_DELETED+ INVOICE_SENTCUSTOMER_CREATEDPRODUCT_CREATEDCHECKOUT_QUANTITY_CHANGED@@ -5331,6 +5334,9 @@ enum WebhookSampleEventTypeEnum {ORDER_UPDATEDORDER_CANCELLEDORDER_FULFILLED+ INVOICE_REQUESTED+ INVOICE_DELETED+ INVOICE_SENTCUSTOMER_CREATEDPRODUCT_CREATEDCHECKOUT_QUANTITY_CHANGEDdiff --git a/saleor/plugins/base_plugin.py b/saleor/plugins/base_plugin.pyindex 57843d54d9..ebe0fb30d9 100644--- a/saleor/plugins/base_plugin.py+++ b/saleor/plugins/base_plugin.py@@ -234,6 +234,10 @@ class BasePlugin:"""return NotImplemented+ def invoice_sent(self, invoice: "Invoice", email: str, previous_value: Any):+ """Trigger after invoice is sent."""+ return NotImplemented+def assign_tax_code_to_object_meta(self, obj: Union["Product", "ProductType"], tax_code: str, previous_value: Any):diff --git a/saleor/plugins/manager.py b/saleor/plugins/manager.pyindex ef1b40894f..ab778f8e85 100644--- a/saleor/plugins/manager.py+++ b/saleor/plugins/manager.py@@ -240,6 +240,12 @@ class PluginsManager(PaymentInterface):default_value = Nonereturn self.__run_method_on_plugins("invoice_delete", default_value, invoice)+ def invoice_sent(self, invoice: "Invoice", email: str):+ default_value = None+ return self.__run_method_on_plugins(+ "invoice_sent", default_value, invoice, email+ )+def order_fully_paid(self, order: "Order"):default_value = Nonereturn self.__run_method_on_plugins("order_fully_paid", default_value, order)diff --git a/saleor/plugins/webhook/plugin.py b/saleor/plugins/webhook/plugin.pyindex 29aac5256c..3fd7325683 100644--- a/saleor/plugins/webhook/plugin.py+++ b/saleor/plugins/webhook/plugin.py@@ -1,10 +1,11 @@-from typing import TYPE_CHECKING, Any+from typing import TYPE_CHECKING, Any, Optionalfrom ...webhook.event_types import WebhookEventTypefrom ...webhook.payloads import (generate_checkout_payload,generate_customer_payload,generate_fulfillment_payload,+ generate_invoice_payload,generate_order_payload,generate_product_payload,)@@ -16,6 +17,7 @@ if TYPE_CHECKING:from ...account.models import Userfrom ...product.models import Productfrom ...checkout.models import Checkout+ from ...invoice.models import Invoiceclass WebhookPlugin(BasePlugin):@@ -45,6 +47,32 @@ class WebhookPlugin(BasePlugin):order_data = generate_order_payload(order)trigger_webhooks_for_event.delay(WebhookEventType.ORDER_UPDATED, order_data)+ def invoice_request(+ self,+ order: "Order",+ invoice: "Invoice",+ number: Optional[str],+ previous_value: Any,+ ) -> Any:+ if not self.active:+ return previous_value+ invoice_data = generate_invoice_payload(invoice)+ trigger_webhooks_for_event.delay(+ WebhookEventType.INVOICE_REQUESTED, invoice_data+ )++ def invoice_delete(self, invoice: "Invoice", previous_value: Any):+ if not self.active:+ return previous_value+ invoice_data = generate_invoice_payload(invoice)+ trigger_webhooks_for_event.delay(WebhookEventType.INVOICE_DELETED, invoice_data)++ def invoice_sent(self, invoice: "Invoice", email: str, previous_value: Any) -> Any:+ if not self.active:+ return previous_value+ invoice_data = generate_invoice_payload(invoice)+ trigger_webhooks_for_event.delay(WebhookEventType.INVOICE_SENT, invoice_data)+def order_cancelled(self, order: "Order", previous_value: Any) -> Any:if not self.active:return previous_valuediff --git a/saleor/webhook/event_types.py b/saleor/webhook/event_types.pyindex 33ada9faf7..0b8601e4d2 100644--- a/saleor/webhook/event_types.py+++ b/saleor/webhook/event_types.py@@ -14,6 +14,10 @@ class WebhookEventType:ORDER_CANCELLED = "order_cancelled"ORDER_FULFILLED = "order_fulfilled"+ INVOICE_REQUESTED = "invoice_requested"+ INVOICE_DELETED = "invoice_deleted"+ INVOICE_SENT = "invoice_sent"+FULFILLMENT_CREATED = "fulfillment_created"CUSTOMER_CREATED = "customer_created"@@ -29,6 +33,9 @@ class WebhookEventType:ORDER_UPDATED: "Order updated",ORDER_CANCELLED: "Order cancelled",ORDER_FULFILLED: "Order fulfilled",+ INVOICE_REQUESTED: "Invoice requested",+ INVOICE_DELETED: "Invoice deleted",+ INVOICE_SENT: "Invoice sent",CUSTOMER_CREATED: "Customer created",PRODUCT_CREATED: "Product created",CHECKOUT_QUANTITY_CHANGED: "Checkout quantity changed",@@ -42,6 +49,9 @@ class WebhookEventType:(ORDER_UPDATED, DISPLAY_LABELS[ORDER_UPDATED]),(ORDER_CANCELLED, DISPLAY_LABELS[ORDER_CANCELLED]),(ORDER_FULFILLED, DISPLAY_LABELS[ORDER_FULFILLED]),+ (INVOICE_REQUESTED, DISPLAY_LABELS[INVOICE_REQUESTED]),+ (INVOICE_DELETED, DISPLAY_LABELS[INVOICE_DELETED]),+ (INVOICE_SENT, DISPLAY_LABELS[INVOICE_SENT]),(CUSTOMER_CREATED, DISPLAY_LABELS[CUSTOMER_CREATED]),(PRODUCT_CREATED, DISPLAY_LABELS[PRODUCT_CREATED]),(CHECKOUT_QUANTITY_CHANGED, DISPLAY_LABELS[CHECKOUT_QUANTITY_CHANGED]),@@ -54,6 +64,9 @@ class WebhookEventType:ORDER_UPDATED: OrderPermissions.MANAGE_ORDERS,ORDER_CANCELLED: OrderPermissions.MANAGE_ORDERS,ORDER_FULFILLED: OrderPermissions.MANAGE_ORDERS,+ INVOICE_REQUESTED: OrderPermissions.MANAGE_ORDERS,+ INVOICE_DELETED: OrderPermissions.MANAGE_ORDERS,+ INVOICE_SENT: OrderPermissions.MANAGE_ORDERS,CUSTOMER_CREATED: AccountPermissions.MANAGE_USERS,PRODUCT_CREATED: ProductPermissions.MANAGE_PRODUCTS,CHECKOUT_QUANTITY_CHANGED: CheckoutPermissions.MANAGE_CHECKOUTS,diff --git a/saleor/webhook/payloads.py b/saleor/webhook/payloads.pyindex 75bac83214..6fca7bd014 100644--- a/saleor/webhook/payloads.py+++ b/saleor/webhook/payloads.py@@ -10,6 +10,7 @@ from ..core.utils.anonymization import (anonymize_order,generate_fake_user,)+from ..invoice.models import Invoicefrom ..order import FulfillmentStatus, OrderStatusfrom ..order.models import Fulfillment, FulfillmentLine, Orderfrom ..order.utils import get_order_country@@ -105,6 +106,12 @@ def generate_order_payload(order: "Order"):return order_data+def generate_invoice_payload(invoice: "Invoice"):+ serializer = PayloadSerializer()+ invoice_fields = ("id", "number", "external_url", "created")+ return serializer.serialize([invoice], fields=invoice_fields,)++def generate_checkout_payload(checkout: "Checkout"):serializer = PayloadSerializer()checkout_fields = (diff --git a/tests/plugins/test_webhook.py b/tests/plugins/test_webhook.pyindex 4989ba6f66..95cfdaee85 100644--- a/tests/plugins/test_webhook.py+++ b/tests/plugins/test_webhook.py@@ -12,6 +12,7 @@ from saleor.webhook.event_types import WebhookEventTypefrom saleor.webhook.payloads import (generate_checkout_payload,generate_customer_payload,+ generate_invoice_payload,generate_order_payload,generate_product_payload,)@@ -216,3 +217,39 @@ 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_invoice_requeste(mocked_webhook_trigger, settings, fulfilled_order):+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ manager = get_plugins_manager()+ invoice = fulfilled_order.invoices.first()+ manager.invoice_request(fulfilled_order, invoice, invoice.number)+ expected_data = generate_invoice_payload(invoice)+ mocked_webhook_trigger.assert_called_once_with(+ WebhookEventType.INVOICE_REQUESTED, expected_data+ )++[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")+def test_invoice_delete(mocked_webhook_trigger, settings, fulfilled_order):+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ manager = get_plugins_manager()+ invoice = fulfilled_order.invoices.first()+ manager.invoice_delete(invoice)+ expected_data = generate_invoice_payload(invoice)+ mocked_webhook_trigger.assert_called_once_with(+ WebhookEventType.INVOICE_DELETED, expected_data+ )++[email protected]("saleor.plugins.webhook.plugin.trigger_webhooks_for_event.delay")+def test_invoice_sent(mocked_webhook_trigger, settings, fulfilled_order):+ settings.PLUGINS = ["saleor.plugins.webhook.plugin.WebhookPlugin"]+ manager = get_plugins_manager()+ invoice = fulfilled_order.invoices.first()+ manager.invoice_sent(invoice, fulfilled_order.user.email)+ expected_data = generate_invoice_payload(invoice)+ mocked_webhook_trigger.assert_called_once_with(+ WebhookEventType.INVOICE_SENT, expected_data+ )