Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge CMP-185 to main branch #36

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Api/Config/ConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -250,6 +251,11 @@ public function isUpdateOnResultPageEnabled(): ?bool;
*/
public function isAvailablePaymentMethodsCheckEnabled(): ?bool;

/**
* @return bool
*/
public function isSendOrderEmailForPaid(): bool;

/**
* @return string
*/
Expand Down
13 changes: 13 additions & 0 deletions Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
60 changes: 60 additions & 0 deletions Observer/SendOrderEmailAfterInvoicePay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace CM\Payments\Observer;

use CM\Payments\Config\Config;
use CM\Payments\Model\ConfigProvider;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Event\Observer;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
use Magento\Sales\Model\Order\Invoice;

class SendOrderEmailAfterInvoicePay implements ObserverInterface
{
/**
* @var Config
*/
private $config;

/**
* @var OrderSender
*/
private $orderSender;

/**
* @param Config $config
* @param OrderSender $orderSender
*/
public function __construct(
Config $config,
OrderSender $orderSender
) {
$this->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);
}
}
}
56 changes: 56 additions & 0 deletions Plugin/AdjustOrderEmailSendFlag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace CM\Payments\Plugin;

use CM\Payments\Config\Config;
use CM\Payments\Model\ConfigProvider;
use Magento\Framework\Event\Observer;
use Magento\Quote\Observer\SubmitObserver;
use Magento\Sales\Model\Order;

/**
* Send admin order confirmation
*/
class AdjustOrderEmailSendFlag
{
/**
* @var Config
*/
private $config;

/**
* @param Config $config
*/
public function __construct(Config $config)
{
$this->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];
}
}
8 changes: 8 additions & 0 deletions etc/adminhtml/system.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@
payment methods you want to show to the customer.]]></comment>
<config_path>payment/cm_payments_methods/profile</config_path>
</field>
<field id="send_order_email_for_paid" translate="label comment" type="select" sortOrder="10"
showInDefault="1" showInWebsite="1" showInStore="1">
<label>Send new order email for only paid orders</label>
<comment><![CDATA[This will disable default Magento order sending right after order is placed
(for only CM Payments) and instead will send email when invoice is paid]]></comment>
<source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
<config_path>cm_payments/general/send_order_email_for_paid</config_path>
</field>
<field id="update_on_result_page" translate="label" type="select" sortOrder="2" showInDefault="1"
showInWebsite="1" showInStore="1" canRestore="1">
<label>Update order status on result page</label>
Expand Down
1 change: 1 addition & 0 deletions etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<live_merchant_key backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
<live_merchant_password backend_model="Magento\Config\Model\Config\Backend\Encrypted" />
<update_on_result_page>1</update_on_result_page>
<send_order_email_for_paid>0</send_order_email_for_paid>
<check_available_methods>1</check_available_methods>
<shipping_fee_name>CM Shipping Fee</shipping_fee_name>
<adjustment_fee_name>CM Adjustment Fee</adjustment_fee_name>
Expand Down
4 changes: 4 additions & 0 deletions etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,8 @@
<argument name="checkoutSession" xsi:type="object">Magento\Checkout\Model\Session\Proxy</argument>
</arguments>
</type>

<type name="Magento\Quote\Observer\SubmitObserver">
<plugin name="adjust_order_email_send_flag" type="CM\Payments\Plugin\AdjustOrderEmailSendFlag" sortOrder="1" disabled="false" />
</type>
</config>
7 changes: 7 additions & 0 deletions etc/frontend/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_invoice_pay">
<observer name="send_order_email_after_invoice_pay" instance="CM\Payments\Observer\SendOrderEmailAfterInvoicePay"/>
</event>
</config>
11 changes: 11 additions & 0 deletions etc/webapi_rest/di.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0"?>
<!--
~ Copyright © CM.com. All rights reserved.
~ See LICENSE.txt for license details.
-->

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Quote\Observer\SubmitObserver">
<plugin name="adjust_order_email_send_flag_rest" type="CM\Payments\Plugin\AdjustOrderEmailSendFlag" sortOrder="1" disabled="false" />
</type>
</config>
7 changes: 7 additions & 0 deletions etc/webapi_rest/events.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_invoice_pay">
<observer name="send_order_email_after_invoice_pay_rest" instance="CM\Payments\Observer\SendOrderEmailAfterInvoicePay"/>
</event>
</config>
Loading