diff --git a/Api/Config/ConfigInterface.php b/Api/Config/ConfigInterface.php
index 199697b..45930ac 100644
--- a/Api/Config/ConfigInterface.php
+++ b/Api/Config/ConfigInterface.php
@@ -26,6 +26,7 @@ interface ConfigInterface
public const XML_PATH_GENERAL_MODE = 'cm_payments/general/mode';
public const XML_PATH_GENERAL_UPDATE_ON_RESULT_PAGE = 'cm_payments/general/update_on_result_page';
public const XML_PATH_GENERAL_CHECK_AVAILABLE_PAYMENT_METHODS = 'cm_payments/general/check_available_methods';
+ public const XML_PATH_GENERAL_SEND_ORDER_EMAIL_FOR_PAID = 'cm_payments/general/send_order_email_for_paid';
public const XML_PATH_GENERAL_CUSTOM_SUCCESS_URL = 'cm_payments/general/custom_success_url';
public const XML_PATH_GENERAL_CUSTOM_ERROR_URL = 'cm_payments/general/custom_error_url';
public const XML_PATH_GENERAL_SHIPPING_FEE_NAME = 'cm_payments/general/shipping_fee_name';
@@ -250,6 +251,11 @@ public function isUpdateOnResultPageEnabled(): ?bool;
*/
public function isAvailablePaymentMethodsCheckEnabled(): ?bool;
+ /**
+ * @return bool
+ */
+ public function isSendOrderEmailForPaid(): bool;
+
/**
* @return string
*/
diff --git a/Config/Config.php b/Config/Config.php
index ba5f307..f16e992 100644
--- a/Config/Config.php
+++ b/Config/Config.php
@@ -81,6 +81,19 @@ public function isAvailablePaymentMethodsCheckEnabled(): ?bool
);
}
+ /**
+ * @inheritDoc
+ */
+ public function isSendOrderEmailForPaid(): bool
+ {
+ return $this->getConfig(
+ self::XML_PATH_GENERAL_SEND_ORDER_EMAIL_FOR_PAID,
+ ScopeInterface::SCOPE_STORES,
+ (string)$this->storeManager->getStore()->getId(),
+ true
+ );
+ }
+
/**
* @inheritDoc
*/
diff --git a/Observer/SendOrderEmailAfterInvoicePay.php b/Observer/SendOrderEmailAfterInvoicePay.php
new file mode 100644
index 0000000..7788771
--- /dev/null
+++ b/Observer/SendOrderEmailAfterInvoicePay.php
@@ -0,0 +1,60 @@
+config = $config;
+ $this->orderSender = $orderSender;
+ }
+
+ /**
+ * Observer for sales_order_invoice_pay
+ *
+ * @param Observer $observer
+ * @return void
+ * @throws \Exception
+ */
+ public function execute(Observer $observer)
+ {
+ if (!$this->config->isSendOrderEmailForPaid()) {
+ return;
+ }
+
+ $event = $observer->getEvent();
+ /** @var Invoice $invoice */
+ $invoice = $event->getInvoice();
+ $order = $invoice->getOrder();
+
+ if (\strpos($order->getPayment()->getMethod(), ConfigProvider::CODE) !== false) {
+ $this->orderSender->send($order);
+ }
+ }
+}
diff --git a/Plugin/AdjustOrderEmailSendFlag.php b/Plugin/AdjustOrderEmailSendFlag.php
new file mode 100644
index 0000000..c296a45
--- /dev/null
+++ b/Plugin/AdjustOrderEmailSendFlag.php
@@ -0,0 +1,56 @@
+config = $config;
+ }
+
+ /**
+ * Adjusts order flag to not send email for CM Payments orders which are not yet paid
+ *
+ * @param SubmitObserver $subject
+ * @param Observer $observer
+ * @return Observer[]
+ * @SuppressWarnings(PHPMD.UnusedFormalParameter)
+ */
+ public function beforeExecute(SubmitObserver $subject, Observer $observer): array
+ {
+ if (!$this->config->isSendOrderEmailForPaid()) {
+ return [$observer];
+ }
+
+ /** @var Order $order */
+ $order = $observer->getEvent()->getOrder();
+ /** @var Order\Payment $payment */
+ $payment = $order->getPayment();
+
+ if (\strpos($payment->getMethod(), ConfigProvider::CODE) !== false) {
+ $order->setCanSendNewEmailFlag(false);
+ }
+
+ return [$observer];
+ }
+}
diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml
index ac3cc07..46b78cf 100644
--- a/etc/adminhtml/system.xml
+++ b/etc/adminhtml/system.xml
@@ -44,6 +44,14 @@
payment methods you want to show to the customer.]]>
payment/cm_payments_methods/profile
+
+
+
+ Magento\Config\Model\Config\Source\Yesno
+ cm_payments/general/send_order_email_for_paid
+
diff --git a/etc/config.xml b/etc/config.xml
index 7d179fa..1f1f44d 100644
--- a/etc/config.xml
+++ b/etc/config.xml
@@ -16,6 +16,7 @@
1
+ 0
1
CM Shipping Fee
CM Adjustment Fee
diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml
index 2ea4dbe..658314e 100644
--- a/etc/frontend/di.xml
+++ b/etc/frontend/di.xml
@@ -36,4 +36,8 @@
Magento\Checkout\Model\Session\Proxy
+
+
+
+
diff --git a/etc/frontend/events.xml b/etc/frontend/events.xml
new file mode 100644
index 0000000..bc006bf
--- /dev/null
+++ b/etc/frontend/events.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+
diff --git a/etc/webapi_rest/di.xml b/etc/webapi_rest/di.xml
new file mode 100644
index 0000000..ec3a7bc
--- /dev/null
+++ b/etc/webapi_rest/di.xml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
diff --git a/etc/webapi_rest/events.xml b/etc/webapi_rest/events.xml
new file mode 100644
index 0000000..d584487
--- /dev/null
+++ b/etc/webapi_rest/events.xml
@@ -0,0 +1,7 @@
+
+
+
+
+
+