Skip to content

Commit

Permalink
Merge pull request #107 from paynl/feature/PLUG-1800
Browse files Browse the repository at this point in the history
PLUG-1800 - Code update
  • Loading branch information
woutse authored May 2, 2023
2 parents e903ff7 + bba4b58 commit 0d2136e
Show file tree
Hide file tree
Showing 6 changed files with 750 additions and 461 deletions.
150 changes: 128 additions & 22 deletions Helper/PayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,35 @@

use Psr\Log\LoggerInterface;
use Magento\Framework\App\ResourceConnection;
use \Paynl\Payment\Model\Config\Source\LogOptions;
use Paynl\Payment\Model\Config\Source\LogOptions;
use Magento\Framework\HTTP\PhpEnvironment\RemoteAddress;
use Magento\Framework\HTTP\Header;


class PayHelper extends \Magento\Framework\App\Helper\AbstractHelper
{
const PAY_LOG_PREFIX = 'PAY.: ';
public const PAY_LOG_PREFIX = 'PAY.: ';

private static $objectManager;
private static $store;
private $resource;
private $remoteAddress;
private $httpHeader;

public function __construct(
ResourceConnection $resource,
RemoteAddress $remoteAddress,
Header $httpHeader
) {
/**
* @param ResourceConnection $resource
* @param RemoteAddress $remoteAddress
* @param Header $httpHeader
*/
public function __construct(ResourceConnection $resource, RemoteAddress $remoteAddress, Header $httpHeader)
{
$this->remoteAddress = $remoteAddress;
$this->httpHeader = $httpHeader;
$this->resource = $resource;
}

/**
* @return \Magento\Framework\App\ObjectManager
*/
public static function getObjectManager()
{
if (empty(self::$objectManager)) {
Expand All @@ -38,7 +42,11 @@ public static function getObjectManager()
return self::$objectManager;
}

public static function getStore($store)
/**
* @param \Magento\Store\Model\Store|null $store
* @return \Magento\Store\Model\Store|mixed
*/
public static function getStore(\Magento\Store\Model\Store $store = null)
{
if (empty($store)) {
if (empty(self::$store)) {
Expand All @@ -51,6 +59,11 @@ public static function getStore($store)
return $store;
}

/**
* @param integer $level
* @param string $type
* @return boolean
*/
public static function hasCorrectLevel($level, $type)
{
if ($level == LogOptions::LOG_ONLY_CRITICAL && $type == 'critical') {
Expand All @@ -66,50 +79,82 @@ public static function hasCorrectLevel($level, $type)
return false;
}

public static function logCritical($text, $params = array(), $store = null)
/**
* @param string $text
* @param array $params
* @param \Magento\Store\Model\Store $store
* @return void
*/
public static function logCritical($text, array $params = array(), \Magento\Store\Model\Store $store = null)
{
self::writeLog($text, 'critical', $params, $store);
}

public static function logNotice($text, $params = array(), $store = null)
/**
* @param string $text
* @param array $params
* @param \Magento\Store\Model\Store $store
* @return void
*/
public static function logNotice($text, array $params = array(), \Magento\Store\Model\Store $store = null)
{
self::writeLog($text, 'notice', $params, $store);
}

public static function logInfo($text, $params = array(), $store = null)
/**
* @param string $text
* @param array $params
* @param \Magento\Store\Model\Store $store
* @return void
*/
public static function logInfo($text, array $params = array(), \Magento\Store\Model\Store $store = null)
{
self::writeLog($text, 'info', $params, $store);
}

public static function logDebug($text, $params = array(), $store = null)
/**
* @param string $text
* @param array $params
* @param \Magento\Store\Model\Store|null $store
* @return void
*/
public static function logDebug($text, array $params = array(), \Magento\Store\Model\Store $store = null)
{
self::writeLog($text, 'debug', $params, $store);
}

/**
* Logs while bypassing the loglevel setting.
*
* @param $text
* @param string $text
* @param array $params
* @param null $store
* @param \Magento\Store\Model\Store |null $store
* @return void
*/
public static function log($text, $params = array(), $store = null)
public static function log($text, array $params = array(), \Magento\Store\Model\Store $store = null)
{
$objectManager = self::getObjectManager();
$logger = $objectManager->get(\Paynl\Payment\Logging\Logger::class);
$logger->notice($text, $params);
}

public static function writeLog($text, $type, $params, $store)
/**
* @param string $text
* @param string $type
* @param array $params
* @param \Magento\Store\Model\Store|null $store
* @return void
*/
public static function writeLog($text, $type, array $params, \Magento\Store\Model\Store $store = null)
{
$objectManager = self::getObjectManager();
$store = self::getStore($store);
$level = $store->getConfig('payment/paynl/logging_level');

if (self::hasCorrectLevel($level, $type)) {
if (!is_array($params)) {
$params = array();
}
$objectManager = self::getObjectManager();
$logger = $objectManager->get(\Paynl\Payment\Logging\Logger::class);
switch ($type) {
case 'critical':
Expand All @@ -128,6 +173,11 @@ public static function writeLog($text, $type, $params, $store)
}
}

/**
* @param string $cookieName
* @param string $value
* @return void
*/
public static function setCookie($cookieName, $value)
{
$objectManager = self::getObjectManager();
Expand All @@ -148,13 +198,23 @@ public static function setCookie($cookieName, $value)
);
}

/**
* @param string $cookieName
* @return mixed
*/
public static function getCookie($cookieName)
{
$objectManager = self::getObjectManager();
$cookieManager = $objectManager->get(\Magento\Framework\Stdlib\CookieManagerInterface::class);
return $cookieManager->getCookie($cookieName);
}

/**
* @param string $cookieName
* @phpcs:disable PSR12.Functions.ReturnTypeDeclaration
* @phpcs:disable PEAR.Commenting.FunctionComment.MissingReturn
* @return void|mixed
*/
public static function deleteCookie($cookieName)
{
$objectManager = self::getObjectManager();
Expand All @@ -163,15 +223,15 @@ public static function deleteCookie($cookieName)
if ($cookieManager->getCookie($cookieName)) {
$metadata = $cookieMetadataFactory->createPublicCookieMetadata();
$metadata->setPath('/');
return $cookieManager->deleteCookie($cookieName, $metadata);
return $cookieManager->deleteCookie($cookieName, $metadata); // phpcs:ignore
}
}

/**
* Checks if new-ppt is already processing, mark as processing if not marked already
*
* @param $payOrderId
* @return bool
* @param string $payOrderId
* @return boolean
*/
public function checkProcessing($payOrderId)
{
Expand Down Expand Up @@ -199,7 +259,8 @@ public function checkProcessing($payOrderId)
/**
* Removes processing mark after new-ppt is finished
*
* @param $payOrderId
* @param string $payOrderId
* @return void
*/
public function removeProcessing($payOrderId)
{
Expand All @@ -211,13 +272,58 @@ public function removeProcessing($payOrderId)
);
}

/**
* @return false|string
*/
public function getClientIp()
{
return $this->remoteAddress->getRemoteAddress();
}

/**
* @return string
*/
public function getHttpUserAgent()
{
return $this->httpHeader->getHttpUserAgent();
}

/**
* @param string $exceptionMessage
* @return \Magento\Framework\Phrase
*/
public static function getFriendlyMessage($exceptionMessage)
{
$exceptionMessage = strtolower(trim($exceptionMessage));

if (stripos($exceptionMessage, 'minimum amount') !== false) {
$strMessage = __('Unfortunately the order amount does not fit the requirements for this payment method.');
} elseif (stripos($exceptionMessage, 'not enabled for this service') !== false) {
$strMessage = __('The selected payment method is not enabled. Please select another payment method.');
} else {
$strMessage = __('Unfortunately something went wrong.');
}

return $strMessage;
}

/**
* @param string $gender
* @return string|null
*/
public static function genderConversion($gender)
{
switch ($gender) {
case '1':
$gender = 'M';
break;
case '2':
$gender = 'F';
break;
default:
$gender = null;
break;
}
return $gender;
}
}
Loading

0 comments on commit 0d2136e

Please sign in to comment.