From 306a4c6fed4241961bf6b417108df47911969e2e Mon Sep 17 00:00:00 2001 From: Michiel Gerritsen Date: Mon, 4 Mar 2024 15:45:05 +0100 Subject: [PATCH] Feature: Option to set a custom template for payment link orders --- Block/Info/Base.php | 4 +- Config.php | 18 +++ .../Sender/SendPaymentLinkConfirmation.php | 47 ++++++++ .../Order/Email/PaymentLinkOrderIdentity.php | 42 +++++++ .../Order/PaymentLinkConfirmationEmail.php | 78 +++++++++++++ .../Service/Magento/PaymentLinkUrlFake.php | 44 ++++++++ Test/Integration/Block/Info/BaseTest.php | 50 +++++++++ etc/adminhtml/methods/paymentlink.xml | 45 +++++--- etc/config.xml | 2 + etc/di.xml | 4 + etc/email_templates.xml | 1 + view/adminhtml/web/css/source/_email.less | 9 ++ .../email/payment-link-confirmation.html | 106 ++++++++++++++++++ view/frontend/web/css/source/_email.less | 25 +++++ 14 files changed, 461 insertions(+), 14 deletions(-) create mode 100644 Plugin/Sales/Model/Order/Email/Sender/SendPaymentLinkConfirmation.php create mode 100644 Service/Order/Email/PaymentLinkOrderIdentity.php create mode 100644 Service/Order/PaymentLinkConfirmationEmail.php create mode 100644 Test/Fakes/Service/Magento/PaymentLinkUrlFake.php create mode 100644 view/adminhtml/web/css/source/_email.less create mode 100644 view/frontend/email/payment-link-confirmation.html create mode 100644 view/frontend/web/css/source/_email.less diff --git a/Block/Info/Base.php b/Block/Info/Base.php index 92b87db7646..146ca83a70e 100644 --- a/Block/Info/Base.php +++ b/Block/Info/Base.php @@ -94,7 +94,9 @@ public function getExpiresAt(): ?string public function getPaymentLink($storeId = null): ?string { - if (!$this->config->addPaymentLinkMessage($storeId)) { + if (!$this->config->addPaymentLinkMessage($storeId) || + $this->config->paymentLinkUseCustomEmailTemplate($storeId) + ) { return null; } diff --git a/Config.php b/Config.php index 6613d427cfe..8bc7ff9b65b 100644 --- a/Config.php +++ b/Config.php @@ -72,6 +72,8 @@ class Config const PAYMENT_PAYMENTLINK_NEW_STATUS = 'payment/mollie_methods_paymentlink/order_status_new'; const PAYMENT_PAYMENTLINK_ADD_MESSAGE = 'payment/mollie_methods_paymentlink/add_message'; const PAYMENT_PAYMENTLINK_MESSAGE = 'payment/mollie_methods_paymentlink/message'; + const PAYMENT_PAYMENTLINK_USE_CUSTOM_EMAIL_TEMPLATE = 'payment/mollie_methods_paymentlink/use_custom_email_template'; + const PAYMENT_PAYMENTLINK_EMAIL_TEMPLATE = 'payment/mollie_methods_paymentlink/email_template'; const PAYMENT_USE_CUSTOM_PAYMENTLINK_URL = 'payment/mollie_general/use_custom_paymentlink_url'; const PAYMENT_CUSTOM_PAYMENTLINK_URL = 'payment/mollie_general/custom_paymentlink_url'; const PAYMENT_POINTOFSALE_ALLOWED_CUSTOMER_GROUPS = 'payment/mollie_methods_pointofsale/allowed_customer_groups'; @@ -530,6 +532,22 @@ public function paymentLinkMessage($storeId = null): string ); } + public function paymentLinkUseCustomEmailTemplate(int $storeId = null): string + { + return $this->getPath( + static::PAYMENT_PAYMENTLINK_USE_CUSTOM_EMAIL_TEMPLATE, + $storeId + ); + } + + public function paymentLinkEmailTemplate(int $storeId = null): string + { + return $this->getPath( + static::PAYMENT_PAYMENTLINK_EMAIL_TEMPLATE, + $storeId + ); + } + public function useCustomPaymentLinkUrl($storeId = null): bool { return $this->isSetFlag(static::PAYMENT_USE_CUSTOM_PAYMENTLINK_URL, $storeId); diff --git a/Plugin/Sales/Model/Order/Email/Sender/SendPaymentLinkConfirmation.php b/Plugin/Sales/Model/Order/Email/Sender/SendPaymentLinkConfirmation.php new file mode 100644 index 00000000000..5df2013a06e --- /dev/null +++ b/Plugin/Sales/Model/Order/Email/Sender/SendPaymentLinkConfirmation.php @@ -0,0 +1,47 @@ +config = $config; + $this->paymentLinkConfirmationEmail = $paymentLinkConfirmationEmail; + } + + public function aroundSend(OrderSender $subject, callable $proceed, Order $order, $forceSyncMode = false) + { + // When the `send()` method of the OrderSender is called, we want to call our own class instead. + // But this class is also based on the OrderSender, so we need to check if we are not already in our own class. + if ($order->getPayment()->getMethod() == 'mollie_methods_paymentlink' && + $this->config->paymentLinkUseCustomEmailTemplate((int)$order->getStoreId()) && + !($subject instanceof $this->paymentLinkConfirmationEmail) + ) { + return $this->paymentLinkConfirmationEmail->send($order); + } + + return $proceed($order, $forceSyncMode); + } +} diff --git a/Service/Order/Email/PaymentLinkOrderIdentity.php b/Service/Order/Email/PaymentLinkOrderIdentity.php new file mode 100644 index 00000000000..f162f7049c8 --- /dev/null +++ b/Service/Order/Email/PaymentLinkOrderIdentity.php @@ -0,0 +1,42 @@ +config = $config; + } + + public function isEnabled() + { + return $this->config->paymentLinkUseCustomEmailTemplate(); + } + + public function getTemplateId() + { + return $this->config->paymentLinkEmailTemplate(); + } +} diff --git a/Service/Order/PaymentLinkConfirmationEmail.php b/Service/Order/PaymentLinkConfirmationEmail.php new file mode 100644 index 00000000000..e6e4c88a48f --- /dev/null +++ b/Service/Order/PaymentLinkConfirmationEmail.php @@ -0,0 +1,78 @@ +paymentLinkUrl = $paymentLinkUrl; + } + + protected function prepareTemplate(Order $order) + { + parent::prepareTemplate($order); + + $transportObject = new DataObject($this->templateContainer->getTemplateVars()); + $transportObject->setData('mollie_payment_link', $this->paymentLinkUrl->execute((int)$order->getEntityId())); + + $this->eventManager->dispatch( + 'mollie_email_paymenlink_order_set_template_vars_before', + ['sender' => $this, 'transportObject' => $transportObject] + ); + + $this->templateContainer->setTemplateVars($transportObject->getData()); + } +} diff --git a/Test/Fakes/Service/Magento/PaymentLinkUrlFake.php b/Test/Fakes/Service/Magento/PaymentLinkUrlFake.php new file mode 100644 index 00000000000..3d4b5139232 --- /dev/null +++ b/Test/Fakes/Service/Magento/PaymentLinkUrlFake.php @@ -0,0 +1,44 @@ +url = $url; + } + + public function shouldNotBeCalled() + { + $this->shouldNotBeCalled = true; + } + + public function execute(int $orderId): string + { + if ($this->shouldNotBeCalled === true) { + throw new \Exception('This method should not be called'); + } + + if ($this->url !== null) { + return $this->url; + } + + return parent::execute($orderId); + } +} diff --git a/Test/Integration/Block/Info/BaseTest.php b/Test/Integration/Block/Info/BaseTest.php index be349cfb4b5..4c43269cd79 100644 --- a/Test/Integration/Block/Info/BaseTest.php +++ b/Test/Integration/Block/Info/BaseTest.php @@ -8,6 +8,8 @@ use Magento\Sales\Model\Order\Payment\Info; use Mollie\Payment\Block\Info\Base; +use Mollie\Payment\Service\Magento\PaymentLinkUrl; +use Mollie\Payment\Test\Fakes\Service\Magento\PaymentLinkUrlFake; use Mollie\Payment\Test\Integration\IntegrationTestCase; class BaseTest extends IntegrationTestCase @@ -65,4 +67,52 @@ public function testReturnsTheRemainderAmount() $instance->setData('info', $info); $this->assertEquals('100', $instance->getRemainderAmount()); } + + /** + * @magentoConfigFixture current_store payment/mollie_methods_paymentlink/add_message 1 + * @magentoConfigFixture current_store payment/mollie_methods_paymentlink/use_custom_email_template 0 + * @magentoConfigFixture current_store payment/mollie_methods_paymentlink/message "Click here to complete your payment" + * @return void + */ + public function testReturnsThePaymentLinkMessageWithLink(): void + { + $paymentLinkFake = $this->objectManager->get(PaymentLinkUrlFake::class); + $paymentLinkFake->setUrl('http://www.example.com/mollie/checkout/paymentlink/order/-999/'); + $this->objectManager->addSharedInstance($paymentLinkFake, PaymentLinkUrl::class); + + /** @var Info $info */ + $info = $this->objectManager->create(Info::class); + + /** @var Base $instance */ + $instance = $this->objectManager->create(Base::class); + $instance->setData('info', $info); + + $result = $instance->getPaymentLink(); + + $this->assertStringNotContainsString('%link%', $result); + } + + /** + * @magentoConfigFixture current_store payment/mollie_methods_paymentlink/add_message 1 + * @magentoConfigFixture current_store payment/mollie_methods_paymentlink/use_custom_email_template 1 + * @magentoConfigFixture current_store payment/mollie_methods_paymentlink/message "Click here to complete your payment" + * @return void + */ + public function testReturnsNothingWhenPaymentLinkMailIsActive(): void + { + $paymentLinkFake = $this->objectManager->get(PaymentLinkUrlFake::class); + $paymentLinkFake->shouldNotBeCalled(); + $this->objectManager->addSharedInstance($paymentLinkFake, PaymentLinkUrl::class); + + /** @var Info $info */ + $info = $this->objectManager->create(Info::class); + + /** @var Base $instance */ + $instance = $this->objectManager->create(Base::class); + $instance->setData('info', $info); + + $result = $instance->getPaymentLink(); + + $this->assertNull($result); + } } diff --git a/etc/adminhtml/methods/paymentlink.xml b/etc/adminhtml/methods/paymentlink.xml index 224c1f61ac8..afb3c235c2d 100644 --- a/etc/adminhtml/methods/paymentlink.xml +++ b/etc/adminhtml/methods/paymentlink.xml @@ -36,7 +36,26 @@ 1 - + + Magento\Config\Model\Config\Source\Yesno + payment/mollie_methods_paymentlink/use_custom_email_template + + 1 + + + + + Magento\Config\Model\Config\Source\Email\Template + payment/mollie_methods_paymentlink/email_template + + 1 + 1 + + + Magento\Config\Model\Config\Source\Yesno @@ -47,7 +66,7 @@ 1 - + Mollie\Payment\Model\Adminhtml\Source\NewStatus payment/mollie_methods_paymentlink/order_status_new @@ -55,7 +74,7 @@ 1 - Magento\Payment\Model\Config\Source\Allspecificcountries @@ -64,7 +83,7 @@ 1 - Magento\Directory\Model\Config\Source\Country @@ -74,7 +93,7 @@ 1 - payment/mollie_methods_paymentlink/min_order_total @@ -82,7 +101,7 @@ 1 - payment/mollie_methods_paymentlink/max_order_total @@ -90,7 +109,7 @@ 1 - payment/mollie_methods_paymentlink/payment_surcharge_type @@ -99,7 +118,7 @@ 1 - payment/mollie_methods_paymentlink/payment_surcharge_fixed_amount @@ -110,7 +129,7 @@ fixed_fee,fixed_fee_and_percentage - payment/mollie_methods_paymentlink/payment_surcharge_percentage @@ -121,7 +140,7 @@ percentage,fixed_fee_and_percentage - payment/mollie_methods_paymentlink/payment_surcharge_limit @@ -133,7 +152,7 @@ percentage,fixed_fee_and_percentage - payment/mollie_methods_paymentlink/payment_surcharge_tax_class @@ -143,7 +162,7 @@ fixed_fee,percentage,fixed_fee_and_percentage - validate-number @@ -152,7 +171,7 @@ 1 - validate-digits-range digits-range-1-365 diff --git a/etc/config.xml b/etc/config.xml index 7fc0bffaf62..7fc25c91cef 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -411,6 +411,8 @@ 1 Mollie\Payment\Model\Methods\Paymentlink Mollie: Payment Link + 0 + mollie_payment_methods_mollie_methods_paymentlink_email_template {ordernumber} order order diff --git a/etc/di.xml b/etc/di.xml index 312a24c85ca..2ab14e61249 100644 --- a/etc/di.xml +++ b/etc/di.xml @@ -365,6 +365,10 @@ + + + + diff --git a/etc/email_templates.xml b/etc/email_templates.xml index d0e3d60ebf8..2a38e61dc83 100644 --- a/etc/email_templates.xml +++ b/etc/email_templates.xml @@ -1,4 +1,5 @@