Skip to content

Commit

Permalink
Merge branch 'hotfix/2.8.5'
Browse files Browse the repository at this point in the history
  • Loading branch information
Alima Grine committed Mar 7, 2024
2 parents c628686 + 9bddcfe commit 0188146
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 14 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
61 changes: 53 additions & 8 deletions Controller/Adminhtml/System/Config/Support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
Expand All @@ -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());
Expand All @@ -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;
}
}
8 changes: 4 additions & 4 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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() : '';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"php" : "~7|~8"
},
"type" : "magento2-module",
"version" : "2.8.4",
"version" : "2.8.5",
"license" : "OSL-3.0",
"autoload" : {
"files" : [
Expand Down
2 changes: 1 addition & 1 deletion etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<payzen>
<general>
<support_email><![CDATA[[email protected]]]></support_email>
<plugin_version>2.8.4</plugin_version>
<plugin_version>2.8.5</plugin_version>
<gateway_version>V2</gateway_version>
<cms_identifier>Magento_2.x</cms_identifier>
<enable_logs>1</enable_logs>
Expand Down
15 changes: 15 additions & 0 deletions etc/email_templates.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
* Copyright © Lyra Network.
* This file is part of PayZen plugin for Magento 2. See COPYING.md for license details.
*
* @author Lyra Network (https://www.lyra.com/)
* @copyright Lyra Network
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Email:etc/email_templates.xsd">
<template id="payzen_support_email_template" label="Payzen Support Email" file="payzen_support_email.html" type="html" module="Lyranetwork_Payzen" area="adminhtml" />
</config>
13 changes: 13 additions & 0 deletions view/adminhtml/email/payzen_support_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!--
/**
* Copyright © Lyra Network.
* This file is part of PayZen plugin for Magento 2. See COPYING.md for license details.
*
* @author Lyra Network (https://www.lyra.com/)
* @copyright Lyra Network
* @license https://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
-->
<!--@subject {{var subject}} @-->

{{var content|raw}}

0 comments on commit 0188146

Please sign in to comment.