diff --git a/CHANGELOG.md b/CHANGELOG.md index 16597f2..432326b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +2.8.5, 2024-03-07: +- Bug fix: Fix errors related to the deprecation of the Framework Zend. + 2.8.4, 2024-02-13: - Improve IPN errors management. diff --git a/Controller/Adminhtml/System/Config/Support.php b/Controller/Adminhtml/System/Config/Support.php index 99bbbad..9f46cd7 100644 --- a/Controller/Adminhtml/System/Config/Support.php +++ b/Controller/Adminhtml/System/Config/Support.php @@ -10,6 +10,7 @@ namespace Lyranetwork\Payzen\Controller\Adminhtml\System\Config; use Magento\Framework\DataObject; +use Magento\Framework\Mail\Template\TransportBuilder; class Support extends \Magento\Backend\App\AbstractAction { @@ -23,20 +24,36 @@ class Support extends \Magento\Backend\App\AbstractAction */ protected $resultJsonFactory; + /** + * @var TransportBuilder + */ + protected $transportBuilder; + + /** + * @var \Magento\User\Model\UserFactory + */ + protected $userFactory; + /** * @param \Magento\Backend\App\Action\Context $context * @param \Lyranetwork\Payzen\Helper\Data $dataHelper * @param \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory + * @param TransportBuilder $transportBuilder + * @param \Magento\User\Model\UserFactory $userFactory */ public function __construct( \Magento\Backend\App\Action\Context $context, \Lyranetwork\Payzen\Helper\Data $dataHelper, - \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory + \Magento\Framework\Controller\Result\JsonFactory $resultJsonFactory, + TransportBuilder $transportBuilder, + \Magento\User\Model\UserFactory $userFactory ) { parent::__construct($context); $this->dataHelper = $dataHelper; $this->resultJsonFactory = $resultJsonFactory; + $this->transportBuilder = $transportBuilder; + $this->userFactory = $userFactory; } public function execute() @@ -48,18 +65,14 @@ public function execute() if (isset($params['submitter']) && $params['submitter'] === 'payzen_send_support') { if (isset($params['sender']) && isset($params['subject']) && isset($params['message'])) { + $sender = $params['sender']; $recipient = $this->dataHelper->getCommonConfigData('support_email'); $subject = $params['subject']; $content = $params['message']; + $senderName = $this->getUserName($sender); try { - $email = new \Zend_Mail('UTF-8'); - $email->setSubject($subject); - $email->setBodyHtml($content); - $email->setFrom($params['sender']); - $email->addTo($recipient); - - $email->send(); + $this->sendSupportEmail($subject, $content, $sender, $senderName, $recipient); $this->messageManager->addSuccessMessage(__('Thank you for contacting us. Your email has been successfully sent.')); } catch (\Exception $e) { $this->dataHelper->log('An error occurred when trying to send email to Support: ' . $e->getMessage()); @@ -77,4 +90,36 @@ public function execute() return $result->setData($data->getData()); } + + public function sendSupportEmail(string $subject, string $content, string $fromEmail, string $fromName, string $toEmail) + { + $transport = $this->transportBuilder + ->setTemplateIdentifier('payzen_support_email_template') + ->setTemplateOptions([ + 'area' => \Magento\Backend\App\Area\FrontNameResolver::AREA_CODE, + 'store' => \Magento\Store\Model\Store::DEFAULT_STORE_ID, + ]) + ->setTemplateVars(['subject' => $subject, 'content' => $content]) + ->setFrom(['email' => $fromEmail, 'name' => $fromName]) + ->addTo($toEmail) + ->getTransport(); + + $transport->sendMessage(); + } + + public function getUserName(string $email) + { + try { + $user = $this->userFactory->create()->load($email, 'email'); + if ($user->getId()) { + return $user->getFirstName() . ' ' . $user->getLastName(); + } else { + $this->dataHelper->log('An error occurred when trying to retrieve sender: User does not exist.'); + } + } catch (\Exception $e) { + $this->dataHelper->log('An error occurred when trying to retrieve sender. ' . $e->getMessage()); + } + + return $email; + } } diff --git a/Helper/Data.php b/Helper/Data.php index c04b3da..a056f3d 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -91,7 +91,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper protected $appState; /** - * @var \Zend\Http\PhpEnvironment\RemoteAddress + * @var \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress */ protected $remoteAddress; @@ -138,7 +138,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper * @param \Magento\Framework\Filesystem $filesystem * @param \Lyranetwork\Payzen\Model\Logger\Payzen * @param \Magento\Framework\App\State $appState - * @param \Zend\Http\PhpEnvironment\RemoteAddress $remoteAddress + * @param \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress * @param \Magento\Framework\Filesystem\Io\File $file * @param \Magento\Framework\View\Asset\Repository $assetRepo * @param \Magento\Payment\Helper\Data $paymentHelper @@ -155,7 +155,7 @@ public function __construct( \Magento\Framework\Filesystem $filesystem, \Lyranetwork\Payzen\Model\Logger\Payzen $logger, \Magento\Framework\App\State $appState, - \Zend\Http\PhpEnvironment\RemoteAddress $remoteAddress, + \Magento\Framework\HTTP\PhpEnvironment\RemoteAddress $remoteAddress, \Magento\Framework\Filesystem\Io\File $file, \Magento\Framework\View\Asset\Repository $assetRepo, \Magento\Payment\Helper\Data $paymentHelper, @@ -224,7 +224,7 @@ public function getCommonConfigData($field, $storeId = null) */ public function getIpAddress() { - return $this->remoteAddress->getIpAddress(); + return (! empty($this->remoteAddress->getRemoteAddress())) ? $this->remoteAddress->getRemoteAddress() : ''; } /** diff --git a/composer.json b/composer.json index bc4c2cd..8770ad2 100644 --- a/composer.json +++ b/composer.json @@ -5,7 +5,7 @@ "php" : "~7|~8" }, "type" : "magento2-module", - "version" : "2.8.4", + "version" : "2.8.5", "license" : "OSL-3.0", "autoload" : { "files" : [ diff --git a/etc/config.xml b/etc/config.xml index 39b66bc..ae6123e 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -25,7 +25,7 @@ - 2.8.4 + 2.8.5 V2 Magento_2.x 1 diff --git a/etc/email_templates.xml b/etc/email_templates.xml new file mode 100644 index 0000000..fd32e75 --- /dev/null +++ b/etc/email_templates.xml @@ -0,0 +1,15 @@ + + + + +