Skip to content

Commit

Permalink
Merge branch 'feature/custom-template-option-for-payment-link' into r…
Browse files Browse the repository at this point in the history
…elease-week-11
  • Loading branch information
michielgerritsen committed Mar 19, 2024
2 parents 7921c60 + 306a4c6 commit 052e3ff
Show file tree
Hide file tree
Showing 14 changed files with 461 additions and 14 deletions.
4 changes: 3 additions & 1 deletion Block/Info/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
18 changes: 18 additions & 0 deletions Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Plugin\Sales\Model\Order\Email\Sender;

use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
use Mollie\Payment\Service\Order\PaymentLinkConfirmationEmail;

class SendPaymentLinkConfirmation
{
/**
* @var \Mollie\Payment\Config
*/
private $config;
/**
* @var PaymentLinkConfirmationEmail
*/
private $paymentLinkConfirmationEmail;

public function __construct(
\Mollie\Payment\Config $config,
PaymentLinkConfirmationEmail $paymentLinkConfirmationEmail
) {
$this->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);
}
}
42 changes: 42 additions & 0 deletions Service/Order/Email/PaymentLinkOrderIdentity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Service\Order\Email;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Sales\Model\Order\Email\Container\OrderIdentity;
use Magento\Store\Model\StoreManagerInterface;
use Mollie\Payment\Config;

class PaymentLinkOrderIdentity extends OrderIdentity
{
/**
* @var Config
*/
private $config;

public function __construct(
ScopeConfigInterface $scopeConfig,
StoreManagerInterface $storeManager,
Config $config
) {
parent::__construct($scopeConfig, $storeManager);

$this->config = $config;
}

public function isEnabled()
{
return $this->config->paymentLinkUseCustomEmailTemplate();
}

public function getTemplateId()
{
return $this->config->paymentLinkEmailTemplate();
}
}
78 changes: 78 additions & 0 deletions Service/Order/PaymentLinkConfirmationEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Service\Order;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Mail\Template\SenderResolverInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Payment\Helper\Data as PaymentHelper;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\Order\Address\Renderer;
use Magento\Sales\Model\Order\Email\Container\OrderIdentity;
use Magento\Sales\Model\Order\Email\Container\Template;
use Magento\Sales\Model\Order\Email\Sender\OrderSender;
use Magento\Sales\Model\Order\Email\SenderBuilderFactory;
use Magento\Sales\Model\ResourceModel\Order as OrderResource;
use Mollie\Payment\Config;
use Mollie\Payment\Service\Magento\PaymentLinkUrl;
use Mollie\Payment\Service\Order\Email\PaymentLinkOrderIdentity;
use Psr\Log\LoggerInterface;

class PaymentLinkConfirmationEmail extends OrderSender
{
/**
* @var PaymentLinkUrl
*/
private $paymentLinkUrl;

public function __construct(
Template $templateContainer,
SenderBuilderFactory $senderBuilderFactory,
LoggerInterface $logger,
Renderer $addressRenderer,
PaymentHelper $paymentHelper,
OrderResource $orderResource,
ScopeConfigInterface $globalConfig,
ManagerInterface $eventManager,
PaymentLinkOrderIdentity $identityContainer,
PaymentLinkUrl $paymentLinkUrl
) {
parent::__construct(
$templateContainer,
$identityContainer,
$senderBuilderFactory,
$logger,
$addressRenderer,
$paymentHelper,
$orderResource,
$globalConfig,
$eventManager
);

$this->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());
}
}
44 changes: 44 additions & 0 deletions Test/Fakes/Service/Magento/PaymentLinkUrlFake.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/*
* Copyright Magmodules.eu. All rights reserved.
* See COPYING.txt for license details.
*/

declare(strict_types=1);

namespace Mollie\Payment\Test\Fakes\Service\Magento;

use Mollie\Payment\Service\Magento\PaymentLinkUrl;

class PaymentLinkUrlFake extends PaymentLinkUrl
{
/**
* @var string|null
*/
private $url = null;

private $shouldNotBeCalled = false;

public function setUrl(string $url)
{
$this->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);
}
}
50 changes: 50 additions & 0 deletions Test/Integration/Block/Info/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 <a href="%link%">here</a> 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 <a href="%link%">here</a> 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);
}
}
Loading

0 comments on commit 052e3ff

Please sign in to comment.