Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Send email when order is canceled and refunded
IKarbowiak04f852ab9452
diff --git a/saleor/checkout/tests/test_checkout.py b/saleor/checkout/tests/test_checkout.pyindex b7fd562647..7da6c1bd8f 100644--- a/saleor/checkout/tests/test_checkout.py+++ b/saleor/checkout/tests/test_checkout.py@@ -197,7 +197,7 @@ def test_create_order_captured_payment_creates_expected_events(# ensure the correct parameters were setassert order_placed_email_sent_event.parameters == {"email": order.get_customer_email(),- "email_type": OrderEventsEmails.ORDER,+ "email_type": OrderEventsEmails.ORDER_CONFIRMATION,}# Ensure the correct customer event was created if the user was not anonymous@@ -320,7 +320,7 @@ def test_create_order_captured_payment_creates_expected_events_anonymous_user(# ensure the correct parameters were setassert order_placed_email_sent_event.parameters == {"email": order.get_customer_email(),- "email_type": OrderEventsEmails.ORDER,+ "email_type": OrderEventsEmails.ORDER_CONFIRMATION,}# Check no event was created if the user was anonymous@@ -406,7 +406,7 @@ def test_create_order_preauth_payment_creates_expected_events(# ensure the correct parameters were setassert order_placed_email_sent_event.parameters == {"email": order.get_customer_email(),- "email_type": OrderEventsEmails.ORDER,+ "email_type": OrderEventsEmails.ORDER_CONFIRMATION,}# Ensure the correct customer event was created if the user was not anonymous@@ -500,7 +500,7 @@ def test_create_order_preauth_payment_creates_expected_events_anonymous_user(# ensure the correct parameters were setassert order_placed_email_sent_event.parameters == {"email": order.get_customer_email(),- "email_type": OrderEventsEmails.ORDER,+ "email_type": OrderEventsEmails.ORDER_CONFIRMATION,}# Check no event was created if the user was anonymousdiff --git a/saleor/graphql/order/tests/test_order.py b/saleor/graphql/order/tests/test_order.pyindex b879b778e0..9dbd5186d0 100644--- a/saleor/graphql/order/tests/test_order.py+++ b/saleor/graphql/order/tests/test_order.py@@ -1781,9 +1781,15 @@ def test_order_refund(staff_api_client, permission_manage_orders, payment_txn_caassert data["paymentStatusDisplay"] == payment_status_displayassert data["isPaid"] is False- order_event = order.events.last()- assert order_event.parameters["amount"] == str(amount)- assert order_event.type == order_events.OrderEvents.PAYMENT_REFUNDED+ refund_order_event = order.events.filter(+ type=order_events.OrderEvents.PAYMENT_REFUNDED+ ).first()+ assert refund_order_event.parameters["amount"] == str(amount)++ email_send_event = order.events.filter(+ type=order_events.OrderEvents.EMAIL_SENT+ ).first()+ assert email_send_event.parameters["email_type"]@pytest.mark.parametrize(diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphqlindex 1dbe3ace45..d5f75c8911 100644--- a/saleor/graphql/schema.graphql+++ b/saleor/graphql/schema.graphql@@ -3045,6 +3045,8 @@ enum OrderEventsEmailsEnum {SHIPPING_CONFIRMATIONTRACKING_UPDATEDORDER_CONFIRMATION+ ORDER_CANCEL+ ORDER_REFUNDFULFILLMENT_CONFIRMATIONDIGITAL_LINKS}diff --git a/saleor/order/__init__.py b/saleor/order/__init__.pyindex 89ba7e21c3..47786b7ee7 100644--- a/saleor/order/__init__.py+++ b/saleor/order/__init__.py@@ -104,7 +104,9 @@ class OrderEventsEmails:PAYMENT = "payment_confirmation"SHIPPING = "shipping_confirmation"TRACKING_UPDATED = "tracking_updated"- ORDER = "order_confirmation"+ ORDER_CONFIRMATION = "order_confirmation"+ ORDER_CANCEL = "order_cancel"+ ORDER_REFUND = "order_refund"FULFILLMENT = "fulfillment_confirmation"DIGITAL_LINKS = "digital_links"@@ -112,7 +114,9 @@ class OrderEventsEmails:(PAYMENT, "The payment confirmation email was sent"),(SHIPPING, "The shipping confirmation email was sent"),(TRACKING_UPDATED, "The fulfillment tracking code email was sent"),- (ORDER, "The order placement confirmation email was sent"),+ (ORDER_CONFIRMATION, "The order placement confirmation email was sent"),+ (ORDER_CANCEL, "The order cancel confirmation email was sent"),+ (ORDER_REFUND, "The order refund confirmation email was sent"),(FULFILLMENT, "The fulfillment confirmation email was sent"),(DIGITAL_LINKS, "The email containing the digital links was sent"),]diff --git a/saleor/order/actions.py b/saleor/order/actions.pyindex 7aa83f0e8c..9992d78485 100644--- a/saleor/order/actions.py+++ b/saleor/order/actions.py@@ -10,7 +10,12 @@ from ..payment import ChargeStatus, CustomPaymentChoices, PaymentErrorfrom ..plugins.manager import get_plugins_managerfrom ..warehouse.management import deallocate_stock_for_order, decrease_stockfrom . import FulfillmentStatus, OrderStatus, emails, events, utils-from .emails import send_fulfillment_confirmation_to_customer, send_payment_confirmation+from .emails import (+ send_fulfillment_confirmation_to_customer,+ send_order_canceled_confirmation,+ send_order_refunded_confirmation,+ send_payment_confirmation,+)from .models import Fulfillment, FulfillmentLinefrom .utils import (order_line_needs_automatic_fulfillment,@@ -83,6 +88,8 @@ def cancel_order(order: "Order", user: Optional["User"]):manager.order_cancelled(order)manager.order_updated(order)+ send_order_canceled_confirmation(order, user)+def order_refunded(order: "Order", user: Optional["User"], amount: "Decimal", payment: "Payment"@@ -92,6 +99,8 @@ def order_refunded()get_plugins_manager().order_updated(order)+ send_order_refunded_confirmation(order, user)+def order_voided(order: "Order", user: "User", payment: "Payment"):events.payment_voided_event(order=order, user=user, payment=payment)diff --git a/saleor/order/emails.py b/saleor/order/emails.pyindex 78fc582477..6487b8f492 100644--- a/saleor/order/emails.py+++ b/saleor/order/emails.py@@ -1,3 +1,4 @@+from typing import TYPE_CHECKING, Optionalfrom urllib.parse import urlencodefrom templated_email import send_templated_mail@@ -10,6 +11,9 @@ from ..seo.schema.email import get_order_confirmation_markupfrom . import eventsfrom .models import Fulfillment, Order+if TYPE_CHECKING:+ from ..account.models import User+CONFIRM_ORDER_TEMPLATE = "order/confirm_order"STAFF_CONFIRM_ORDER_TEMPLATE = "order/staff_confirm_order"CONFIRM_FULFILLMENT_TEMPLATE = "order/confirm_fulfillment"@@ -90,7 +94,7 @@ def send_order_confirmation(order_pk, redirect_url, user_pk=None):order=email_data["context"]["order"],user=None,user_pk=user_pk,- email_type=events.OrderEventsEmails.ORDER,+ email_type=events.OrderEventsEmails.ORDER_CONFIRMATION,)@@ -142,15 +146,29 @@ def send_payment_confirmation(order_pk):send_templated_mail(**email_data)+def send_order_canceled_confirmation(order: "Order", user: Optional["User"]):+ send_order_canceled.delay(order.pk)+ events.email_sent_event(+ order=order, user=user, email_type=events.OrderEventsEmails.ORDER_CANCEL+ )++@app.task-def send_order_cancel(order_pk):+def send_order_canceled(order_pk):"""Send order cancel email."""email_data = collect_data_for_email(order_pk, ORDER_CANCEl_TEMPLATE)send_templated_mail(**email_data)+def send_order_refunded_confirmation(order: "Order", user: Optional["User"]):+ send_order_refunded.delay(order.pk)+ events.email_sent_event(+ order=order, user=user, email_type=events.OrderEventsEmails.ORDER_REFUND+ )++@app.task-def send_order_refund(order_pk):+def send_order_refunded(order_pk):"""Send order refund email."""email_data = collect_data_for_email(order_pk, ORDER_REFUND_TEMPLATE)send_templated_mail(**email_data)diff --git a/saleor/order/tests/test_emails.py b/saleor/order/tests/test_emails.pyindex 2cf4cc35cd..ff8f52ad1d 100644--- a/saleor/order/tests/test_emails.py+++ b/saleor/order/tests/test_emails.py@@ -282,12 +282,12 @@ def test_send_fulfillment_emails_with_tracking_number_as_url(@mock.patch("saleor.order.emails.send_templated_mail")-def test_send_email_order_cancel(mocked_templated_email, order, site_settings):+def test_send_email_order_canceled(mocked_templated_email, order, site_settings):# giventemplate = emails.ORDER_CANCEl_TEMPLATE# when- emails.send_order_cancel(order.pk)+ emails.send_order_canceled(order.pk)# thenemail_data = emails.collect_data_for_email(order.pk, template)@@ -308,12 +308,12 @@ def test_send_email_order_cancel(mocked_templated_email, order, site_settings):@mock.patch("saleor.order.emails.send_templated_mail")-def test_send_email_order_refund(mocked_templated_email, order, site_settings):+def test_send_email_order_refunded(mocked_templated_email, order, site_settings):# giventemplate = emails.ORDER_REFUND_TEMPLATE# when- emails.send_order_refund(order.pk)+ emails.send_order_refunded(order.pk)# thenemail_data = emails.collect_data_for_email(order.pk, template)diff --git a/saleor/order/tests/test_order_actions.py b/saleor/order/tests/test_order_actions.pyindex 152f200f11..4d0cee27b5 100644--- a/saleor/order/tests/test_order_actions.py+++ b/saleor/order/tests/test_order_actions.py@@ -5,6 +5,7 @@ import pytestfrom prices import Money, TaxedMoneyfrom ...payment import ChargeStatus, PaymentError+from ...payment.models import Paymentfrom ...product.models import DigitalContentfrom ...product.tests.utils import create_imagefrom ...warehouse.models import Allocation, Stock@@ -17,6 +18,7 @@ from ..actions import (fulfill_order_line,handle_fully_paid_order,mark_order_as_paid,+ order_refunded,)from ..models import Fulfillment@@ -174,15 +176,22 @@ def test_cancel_fulfillment_variant_witout_inventory_tracking(assert stock_quantity_before == line.order_line.variant.stocks.get().quantity-def test_cancel_order(fulfilled_order_with_all_cancelled_fulfillments):+@patch("saleor.order.actions.send_order_canceled_confirmation")+def test_cancel_order(+ send_order_canceled_confirmation_mock,+ fulfilled_order_with_all_cancelled_fulfillments,+):+ # givenorder = fulfilled_order_with_all_cancelled_fulfillmentsassert Allocation.objects.filter(order_line__order=order, quantity_allocated__gt=0).exists()+ # whencancel_order(order, None)+ # thenorder_event = order.events.last()assert order_event.type == OrderEvents.CANCELED@@ -191,6 +200,27 @@ def test_cancel_order(fulfilled_order_with_all_cancelled_fulfillments):order_line__order=order, quantity_allocated__gt=0).exists()+ send_order_canceled_confirmation_mock.assert_called_once_with(order, None)+++@patch("saleor.order.actions.send_order_refunded_confirmation")+def test_order_refunded(+ send_order_refunded_confirmation_mock, order, checkout_with_item,+):+ # given+ payment = Payment.objects.create(+ gateway="mirumee.payments.dummy", is_active=True, checkout=checkout_with_item+ )++ # when+ order_refunded(order, order.user, order.total.gross.amount, payment)++ # then+ order_event = order.events.last()+ assert order_event.type == OrderEvents.PAYMENT_REFUNDED++ send_order_refunded_confirmation_mock.assert_called_once_with(order, order.user)+def test_fulfill_order_line(order_with_lines):order = order_with_lines