From 1a2ee6b3123ddc883e6152ba3bdef87a7aa8da3f Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 17:16:52 -0400 Subject: [PATCH 01/22] feat: add method getLogFileName --- plugin/shared/Helpers/PluginLogger.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index 08f05066..31ede5c9 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -122,4 +122,9 @@ private function formatBytes($path) } return $bytes; } + + private function getLogFileName(): string { + $uniqueId = uniqid('', true); + return 'log_transbank_' . $uniqueId . 'log'; + } } From d1de9d9620853ce647bbae00e9bed6cbc1fd9ca7 Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 17:29:42 -0400 Subject: [PATCH 02/22] feat: add constant CACHE_LOG_NAME --- plugin/shared/Helpers/PluginLogger.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index 31ede5c9..485d210d 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -8,6 +8,9 @@ use Transbank\Plugin\Model\LogConfig; final class PluginLogger implements ILogger { + + const CACHE_LOG_NAME = 'transbank_log_name'; + private $logger; private $config; @@ -22,7 +25,7 @@ public function __construct(LogConfig $config) { $this->config = $config; $logDir = $this->config->getLogDir(); $cacheLogName = 'transbank_log_name'; - $logFile = get_transient($cacheLogName); + $logFile = get_transient(self::CACHE_LOG_NAME); if (!$logFile) { $uniqueId = uniqid('', true); $logFile = "{$logDir}/log_transbank_{$uniqueId}.log"; From e0e3f52d79cc72a736f6844d9dd51b60c430e95d Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 17:31:17 -0400 Subject: [PATCH 03/22] feat: add method saveLogFileNameInCache --- plugin/shared/Helpers/PluginLogger.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index 485d210d..ef04551d 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -130,4 +130,9 @@ private function getLogFileName(): string { $uniqueId = uniqid('', true); return 'log_transbank_' . $uniqueId . 'log'; } + + private function saveLogFileNameInCache(string $logFileName, int $expireTime) + { + set_transient(self::CACHE_LOG_NAME, $logFileName, $expireTime); + } } From 31530775ba73288ea503b674ad352419e378f831 Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 17:40:41 -0400 Subject: [PATCH 04/22] feat: add method getLogDir --- plugin/shared/Helpers/PluginLogger.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index ef04551d..0b396010 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -126,7 +126,14 @@ private function formatBytes($path) return $bytes; } - private function getLogFileName(): string { + private function getLogDir(): string + { + $logDir = $this->config->getLogDir(); + return trailingslashit($logDir); + } + + private function getLogFileName(): string + { $uniqueId = uniqid('', true); return 'log_transbank_' . $uniqueId . 'log'; } From ba7d7a0cd833e6a85fa2f3804a6612d3959c89b3 Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 17:41:34 -0400 Subject: [PATCH 05/22] feat: add method getLogFileNameFromCache --- plugin/shared/Helpers/PluginLogger.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index 0b396010..3c1c5ced 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -138,6 +138,11 @@ private function getLogFileName(): string return 'log_transbank_' . $uniqueId . 'log'; } + private function getLogFileNameFromCache(): string | bool + { + return get_transient(self::CACHE_LOG_NAME); + } + private function saveLogFileNameInCache(string $logFileName, int $expireTime) { set_transient(self::CACHE_LOG_NAME, $logFileName, $expireTime); From 3450a2e7e14699d41ecb8b74702acf5d604f4774 Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 17:42:52 -0400 Subject: [PATCH 06/22] fix: save in cache only the log file name --- plugin/shared/Helpers/PluginLogger.php | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index 3c1c5ced..81afba2c 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -8,9 +8,9 @@ use Transbank\Plugin\Model\LogConfig; final class PluginLogger implements ILogger { - + const CACHE_LOG_NAME = 'transbank_log_name'; - + private $logger; private $config; @@ -23,19 +23,21 @@ final class PluginLogger implements ILogger { */ public function __construct(LogConfig $config) { $this->config = $config; - $logDir = $this->config->getLogDir(); - $cacheLogName = 'transbank_log_name'; - $logFile = get_transient(self::CACHE_LOG_NAME); - if (!$logFile) { - $uniqueId = uniqid('', true); - $logFile = "{$logDir}/log_transbank_{$uniqueId}.log"; + $logFileName = $this->getLogFileNameFromCache(); + + if (!$logFileName) { + $logFileName = $this->getLogFileName(); $expireTime = strtotime('tomorrow') - time(); - set_transient($cacheLogName, $logFile, $expireTime); + $this->saveLogFileNameInCache($logFileName, $expireTime); } + + $logDir = $this->getLogDir(); + $logFilePath = $logDir . $logFileName; + $dateFormat = "Y-m-d H:i:s"; $output = "%datetime% > %level_name% > %message% %context% %extra%\n"; $formatter = new LineFormatter($output, $dateFormat); - $stream = new RotatingFileHandler($logFile, + $stream = new RotatingFileHandler($logFilePath, 100, Logger::DEBUG); $stream->setFormatter($formatter); $this->logger = new Logger('transbank'); From 0a6ea6ebdbb01f94dba4adb58828199ba972686b Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 17:45:37 -0400 Subject: [PATCH 07/22] fix: remove incompatible return type --- plugin/shared/Helpers/PluginLogger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index 81afba2c..f9b1929b 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -140,7 +140,7 @@ private function getLogFileName(): string return 'log_transbank_' . $uniqueId . 'log'; } - private function getLogFileNameFromCache(): string | bool + private function getLogFileNameFromCache() { return get_transient(self::CACHE_LOG_NAME); } From 3778e0fc77359969b290356bed21bc0ef1034419 Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 17:52:56 -0400 Subject: [PATCH 08/22] fix: use new cache name --- plugin/shared/Helpers/PluginLogger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index f9b1929b..f7a9294d 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -9,7 +9,7 @@ final class PluginLogger implements ILogger { - const CACHE_LOG_NAME = 'transbank_log_name'; + const CACHE_LOG_NAME = 'transbank_log_file_name'; private $logger; private $config; From 834b2ae7b85e0a138b624e786a4f77e2a1207ebe Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 18:11:22 -0400 Subject: [PATCH 09/22] fix: add dot in log file extension --- plugin/shared/Helpers/PluginLogger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index f7a9294d..b3bccb58 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -137,7 +137,7 @@ private function getLogDir(): string private function getLogFileName(): string { $uniqueId = uniqid('', true); - return 'log_transbank_' . $uniqueId . 'log'; + return 'log_transbank_' . $uniqueId . '.log'; } private function getLogFileNameFromCache() From c2f01e787ff90299b3da979e117200b6d5da7358 Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 18:14:47 -0400 Subject: [PATCH 10/22] fix: duplicated record in log list --- plugin/templates/admin/log.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/plugin/templates/admin/log.php b/plugin/templates/admin/log.php index 03d38e09..d2fbfb90 100644 --- a/plugin/templates/admin/log.php +++ b/plugin/templates/admin/log.php @@ -42,12 +42,14 @@ if (!$folderHasLogs) { $options = ""; } + foreach ($resume['logs'] as $index) { - $options .= ""; - if($index['filename'] == basename($lastLog['filename'])) { $options .= ""; + continue; } + + $options .= ""; } echo $options; From a7a1b4c31319c4f61b576bf1002f680cbf46afba Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 18:24:22 -0400 Subject: [PATCH 11/22] feat: add method getLogFilePath --- plugin/shared/Helpers/PluginLogger.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index b3bccb58..5b9fbf34 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -43,6 +43,19 @@ public function __construct(LogConfig $config) { $this->logger = new Logger('transbank'); $this->logger->pushHandler($stream); } + private function getLogFilePath(): string + { + $logFileName = $this->getLogFileNameFromCache(); + + if (!$logFileName) { + $logFileName = $this->getLogFileName(); + $expireTime = strtotime('tomorrow') - time(); + $this->saveLogFileNameInCache($logFileName, $expireTime); + } + + $logDir = $this->getLogDir(); + return $logDir . $logFileName; + } public function getLogger(){ return $this->logger; From f7943f23ba5f902df52586ffb094ebef2a6511a7 Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 18:25:43 -0400 Subject: [PATCH 12/22] refactor: implement method getLogFilePath --- plugin/shared/Helpers/PluginLogger.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index 5b9fbf34..f0f38158 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -23,16 +23,8 @@ final class PluginLogger implements ILogger { */ public function __construct(LogConfig $config) { $this->config = $config; - $logFileName = $this->getLogFileNameFromCache(); - - if (!$logFileName) { - $logFileName = $this->getLogFileName(); - $expireTime = strtotime('tomorrow') - time(); - $this->saveLogFileNameInCache($logFileName, $expireTime); - } - $logDir = $this->getLogDir(); - $logFilePath = $logDir . $logFileName; + $logFilePath = $this->getLogFilePath(); $dateFormat = "Y-m-d H:i:s"; $output = "%datetime% > %level_name% > %message% %context% %extra%\n"; From 459cda48ee0b1aab47d993ee2d19d66199bbf1b9 Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 18:25:57 -0400 Subject: [PATCH 13/22] feat: add method initializeLogger --- plugin/shared/Helpers/PluginLogger.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index f0f38158..a90108f2 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -35,6 +35,21 @@ public function __construct(LogConfig $config) { $this->logger = new Logger('transbank'); $this->logger->pushHandler($stream); } + + private function initializeLogger(string $logFilePath) + { + $dateFormat = "Y-m-d H:i:s"; + $output = "%datetime% > %level_name% > %message% %context% %extra%\n"; + $formatter = new LineFormatter($output, $dateFormat); + + $stream = new RotatingFileHandler($logFilePath, + 100, Logger::DEBUG); + $stream->setFormatter($formatter); + + $this->logger = new Logger('transbank'); + $this->logger->pushHandler($stream); + } + private function getLogFilePath(): string { $logFileName = $this->getLogFileNameFromCache(); From 94bc7b3ccec987c5ce1191cf51caf0232143ab7c Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 18:26:57 -0400 Subject: [PATCH 14/22] refactor: implement method initializeLogger --- plugin/shared/Helpers/PluginLogger.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index a90108f2..e7d2b54d 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -25,15 +25,7 @@ public function __construct(LogConfig $config) { $this->config = $config; $logFilePath = $this->getLogFilePath(); - - $dateFormat = "Y-m-d H:i:s"; - $output = "%datetime% > %level_name% > %message% %context% %extra%\n"; - $formatter = new LineFormatter($output, $dateFormat); - $stream = new RotatingFileHandler($logFilePath, - 100, Logger::DEBUG); - $stream->setFormatter($formatter); - $this->logger = new Logger('transbank'); - $this->logger->pushHandler($stream); + $this->initializeLogger($logFilePath); } private function initializeLogger(string $logFilePath) From e5734a89246aab05ec8c9b4475a1731a2fe094ee Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 18:27:39 -0400 Subject: [PATCH 15/22] style: format file --- plugin/shared/Helpers/PluginLogger.php | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index e7d2b54d..ea368d9a 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -7,7 +7,8 @@ use Monolog\Formatter\LineFormatter; use Transbank\Plugin\Model\LogConfig; -final class PluginLogger implements ILogger { +final class PluginLogger implements ILogger +{ const CACHE_LOG_NAME = 'transbank_log_file_name'; @@ -21,7 +22,8 @@ final class PluginLogger implements ILogger { * output format : "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n" * @param Throwable $e */ - public function __construct(LogConfig $config) { + public function __construct(LogConfig $config) + { $this->config = $config; $logFilePath = $this->getLogFilePath(); @@ -34,8 +36,11 @@ private function initializeLogger(string $logFilePath) $output = "%datetime% > %level_name% > %message% %context% %extra%\n"; $formatter = new LineFormatter($output, $dateFormat); - $stream = new RotatingFileHandler($logFilePath, - 100, Logger::DEBUG); + $stream = new RotatingFileHandler( + $logFilePath, + 100, + Logger::DEBUG + ); $stream->setFormatter($formatter); $this->logger = new Logger('transbank'); @@ -56,11 +61,13 @@ private function getLogFilePath(): string return $logDir . $logFileName; } - public function getLogger(){ + public function getLogger() + { return $this->logger; } - public function getConfig(){ + public function getConfig() + { return $this->config; } @@ -81,7 +88,7 @@ public function logError($msg) public function getInfo() { - $files = glob($this->config->getLogDir().'/*.log'); + $files = glob($this->config->getLogDir() . '/*.log'); if (!$files) { return [ 'dir' => $this->config->getLogDir(), @@ -94,7 +101,7 @@ public function getInfo() arsort($files); $logs = []; - foreach($files as $key=>$value) { + foreach ($files as $key => $value) { $logs[] = [ "filename" => basename($key), "modified" => $value @@ -114,7 +121,7 @@ public function getLogDetail($filename, $replaceNewline = false) if ($filename == '') { return []; } - $fle = $this->config->getLogDir().'/'.$filename; + $fle = $this->config->getLogDir() . '/' . $filename; $content = file_get_contents($fle); if ($replaceNewline && $content !== false) { $content = str_replace("\n", '#@#', $content); From b1e750cefb145384f2ea028186012fb414e80a40 Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 22:14:49 -0400 Subject: [PATCH 16/22] fix: warning when input is null --- plugin/src/Helpers/MaskData.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/plugin/src/Helpers/MaskData.php b/plugin/src/Helpers/MaskData.php index 2b71451b..85e2609c 100644 --- a/plugin/src/Helpers/MaskData.php +++ b/plugin/src/Helpers/MaskData.php @@ -57,10 +57,11 @@ private function maskWithFormat($input){ * @return string a string masked. */ private function mask($input, $pattern = null, $charsToKeep = 4 ){ - $len = strlen($input); + $cleanInput = $input ?? ''; + $len = strlen($cleanInput); if ( $pattern != null ) { - $patternPos = strpos($input, $pattern); + $patternPos = strpos($cleanInput, $pattern); if ( $patternPos === 0 ) { $startString = $pattern; } @@ -68,8 +69,8 @@ private function mask($input, $pattern = null, $charsToKeep = 4 ){ $endString = $pattern; } } - $startString = $startString ?? substr($input, 0, $charsToKeep); - $endString = $endString ?? substr($input, -$charsToKeep, $charsToKeep); + $startString = $startString ?? substr($cleanInput, 0, $charsToKeep); + $endString = $endString ?? substr($cleanInput, -$charsToKeep, $charsToKeep); $charsToReplace = $len - (strlen($startString) + strlen($endString)); $replaceString = str_repeat("x", $charsToReplace); return $startString . $replaceString . $endString; From bfb36e82ae0056a2a734c76d76a98d0f4a01c4ce Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Sun, 2 Jun 2024 22:18:25 -0400 Subject: [PATCH 17/22] style: format file --- plugin/src/Helpers/MaskData.php | 124 ++++++++++++++++++-------------- 1 file changed, 72 insertions(+), 52 deletions(-) diff --git a/plugin/src/Helpers/MaskData.php b/plugin/src/Helpers/MaskData.php index 85e2609c..5dfda054 100644 --- a/plugin/src/Helpers/MaskData.php +++ b/plugin/src/Helpers/MaskData.php @@ -1,7 +1,9 @@ isIntegration = $environment == Options::ENVIRONMENT_INTEGRATION; $this->log = TbkFactory::createLogger(); } @@ -42,7 +45,8 @@ public function __construct($environment){ * @param string $input a string to be masked. * @return string a string with substrings masked. */ - private function maskWithFormat($input){ + private function maskWithFormat($input) + { return preg_replace_callback('/(?<=-).+?(?=-)/', function ($matches) { return str_repeat('x', strlen($matches[0])); }, $input); @@ -56,19 +60,20 @@ private function maskWithFormat($input){ * @param int $charsToKeep number of original chars to keep at start and end. * @return string a string masked. */ - private function mask($input, $pattern = null, $charsToKeep = 4 ){ + private function mask($input, $pattern = null, $charsToKeep = 4) + { $cleanInput = $input ?? ''; $len = strlen($cleanInput); - if ( $pattern != null ) { - $patternPos = strpos($cleanInput, $pattern); - if ( $patternPos === 0 ) { + if ($pattern != null) { + $patternPos = strpos($cleanInput, $pattern); + if ($patternPos === 0) { $startString = $pattern; - } - else { + } else { $endString = $pattern; } } + $startString = $startString ?? substr($cleanInput, 0, $charsToKeep); $endString = $endString ?? substr($cleanInput, -$charsToKeep, $charsToKeep); $charsToReplace = $len - (strlen($startString) + strlen($endString)); @@ -82,7 +87,8 @@ private function mask($input, $pattern = null, $charsToKeep = 4 ){ * @param string $email An email to be masked. * @return string email masked. */ - private function maskEmail($email){ + private function maskEmail($email) + { return preg_replace_callback('/^(.{1,4})[^@]*(@.*)$/', function ($match) { return $match[1] . str_repeat('x', strlen($match[0]) - strlen($match[1]) - strlen($match[2])) . $match[2]; }, $email); @@ -95,14 +101,16 @@ private function maskEmail($email){ * @param string $pattern A pattern to maintain, like `child` or `sessionId`. * @return string input masked. */ - private function maskWithPattern($input, $pattern){ + private function maskWithPattern($input, $pattern) + { $regexPattern = "/(wc:($pattern:)?\w{2})\w+:(\w{2})/"; - return preg_replace_callback($regexPattern, function($matches) use ($input){ - $prefix = $matches[1]; - $suffix = $matches[3]; - $maskLength = strlen($input) - strlen($prefix) - strlen($suffix) - 1; - return $prefix . str_repeat('x', $maskLength) . $suffix; - }, $input); + + return preg_replace_callback($regexPattern, function ($matches) use ($input) { + $prefix = $matches[1]; + $suffix = $matches[3]; + $maskLength = strlen($input) - strlen($prefix) - strlen($suffix) - 1; + return $prefix . str_repeat('x', $maskLength) . $suffix; + }, $input); } /** @@ -112,10 +120,12 @@ private function maskWithPattern($input, $pattern){ * @param string $buyOrder An string with buy order to mask. * @return string buy order masked. */ - public function maskBuyOrder($buyOrder){ + public function maskBuyOrder($buyOrder) + { if ($this->isIntegration) { return $buyOrder; } + $pattern = 'child'; return $this->maskWithPattern($buyOrder, $pattern); } @@ -127,10 +137,12 @@ public function maskBuyOrder($buyOrder){ * @param string $sessionId An string with session id to mask. * @return string session id masked. */ - public function maskSessionId($sessionId){ - if($this->isIntegration){ + public function maskSessionId($sessionId) + { + if ($this->isIntegration) { return $sessionId; } + $sessionIdPattern = 'sessionId'; return $this->maskWithPattern($sessionId, $sessionIdPattern); } @@ -141,7 +153,8 @@ public function maskSessionId($sessionId){ * @param array $array an array to evaluate * @return boolean `true` if is associative, `false` otherwise */ - private function isAssociative($array) { + private function isAssociative($array) + { return (bool)count(array_filter(array_keys($array), 'is_string')); } @@ -152,14 +165,15 @@ private function isAssociative($array) { * @param array $data An array containing data to mask. * @return array copy of input, with fields masked. */ - public function maskData($data){ - try{ - if ($this->isIntegration){ + public function maskData($data) + { + try { + if ($this->isIntegration) { return $data; } $newData = $this->copyWithSubArray($data); foreach ($newData as $key => $value) { - switch($this->getValueType($value)){ + switch ($this->getValueType($value)) { case $this::OBJECT: $newData[$key] = $this->maskObject($value); break; @@ -176,8 +190,7 @@ public function maskData($data){ } } return $newData; - } - catch (\Exception $e){ + } catch (\Exception $e) { $this->log->logError('Error on Mask Data: ' . $e->getMessage()); return $data; } @@ -190,17 +203,19 @@ public function maskData($data){ * @param array $array An array to be copied. * @return array copy of input array. */ - private function copyWithSubArray($array){ + private function copyWithSubArray($array) + { $newArray = null; foreach ($array as $key => $value) { if (is_array($value)) { - $clonedValue = array_map(function ($item) { - return is_object($item) ? clone $item : $item; - }, - $value); + $clonedValue = array_map( + function ($item) { + return is_object($item) ? clone $item : $item; + }, + $value + ); $newArray[$key] = $clonedValue; - } - else { + } else { $newArray[$key] = is_object($value) ? clone $value : $value; } } @@ -214,11 +229,14 @@ private function copyWithSubArray($array){ * @param mixed $value the value to be masked. * @return mixed masked value if key exists, `false` otherwise. */ - private function getMaskedValue($key, $value){ + private function getMaskedValue($key, $value) + { $keyExists = array_key_exists($key, $this->keysToMask); - if($keyExists){ + + if ($keyExists) { return call_user_func([$this, $this->keysToMask[$key]], $value); } + return $value; } @@ -228,16 +246,18 @@ private function getMaskedValue($key, $value){ * @param string $value to evaluate. * @return int a constant representing the type of element */ - private function getValueType($value){ + private function getValueType($value) + { $valueType = $this::NO_ITERABLE; - if(is_object($value)){ + + if (is_object($value)) { $valueType = $this::OBJECT; } - if (is_array($value)){ - if ($this->isAssociative($value)){ + + if (is_array($value)) { + if ($this->isAssociative($value)) { $valueType = $this::ASSOCIATIVE_ARRAY; - } - else { + } else { $valueType = $this::INDEXED_ARRAY; } } @@ -251,8 +271,9 @@ private function getValueType($value){ * @param object $object a reference to an object to mask * @return object the object with masked attributes */ - private function maskObject($object){ - foreach($object as $detailKey => $detailValue) { + private function maskObject($object) + { + foreach ($object as $detailKey => $detailValue) { $maskedValue = $this->getMaskedValue($detailKey, $detailValue); $object->$detailKey = $maskedValue; } @@ -265,8 +286,9 @@ private function maskObject($object){ * @param array $array the array to mask. * @return array the array with masked values. */ - private function maskAssociativeArray($array){ - foreach($array as $detailKey => $detailValue) { + private function maskAssociativeArray($array) + { + foreach ($array as $detailKey => $detailValue) { $maskedValue = $this->getMaskedValue($detailKey, $detailValue); $array[$detailKey] = $maskedValue; } @@ -279,17 +301,15 @@ private function maskAssociativeArray($array){ * @param array $array the array to mask. * @return array the array with masked values. */ - private function maskIndexedArray($array){ - foreach($array as $detail) { - if(is_object($detail)) { + private function maskIndexedArray($array) + { + foreach ($array as $detail) { + if (is_object($detail)) { $detail = $this->maskObject($detail); - } - else { + } else { $detail = $this->maskAssociativeArray($array); } - } return $array; } - } From 8b10ce54cf7f18882387ac74c4e257d60d79bf97 Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Mon, 3 Jun 2024 10:29:28 -0400 Subject: [PATCH 18/22] refactor: return empty string when input is null --- plugin/src/Helpers/MaskData.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/plugin/src/Helpers/MaskData.php b/plugin/src/Helpers/MaskData.php index 5dfda054..b4022eef 100644 --- a/plugin/src/Helpers/MaskData.php +++ b/plugin/src/Helpers/MaskData.php @@ -62,11 +62,14 @@ private function maskWithFormat($input) */ private function mask($input, $pattern = null, $charsToKeep = 4) { - $cleanInput = $input ?? ''; - $len = strlen($cleanInput); + if(is_null($input)) { + return ''; + } + + $len = strlen($input); if ($pattern != null) { - $patternPos = strpos($cleanInput, $pattern); + $patternPos = strpos($input, $pattern); if ($patternPos === 0) { $startString = $pattern; } else { @@ -74,8 +77,8 @@ private function mask($input, $pattern = null, $charsToKeep = 4) } } - $startString = $startString ?? substr($cleanInput, 0, $charsToKeep); - $endString = $endString ?? substr($cleanInput, -$charsToKeep, $charsToKeep); + $startString = $startString ?? substr($input, 0, $charsToKeep); + $endString = $endString ?? substr($input, -$charsToKeep, $charsToKeep); $charsToReplace = $len - (strlen($startString) + strlen($endString)); $replaceString = str_repeat("x", $charsToReplace); return $startString . $replaceString . $endString; From cbf16aaf46fc8bf06e14715d791e007aebb051b6 Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Mon, 3 Jun 2024 12:52:38 -0400 Subject: [PATCH 19/22] fix: timezone for logs --- plugin/shared/Helpers/PluginLogger.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index ea368d9a..0db91f05 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -2,6 +2,7 @@ namespace Transbank\Plugin\Helpers; +use DateTimeZone; use Monolog\Logger; use Monolog\Handler\RotatingFileHandler; use Monolog\Formatter\LineFormatter; @@ -32,6 +33,7 @@ public function __construct(LogConfig $config) private function initializeLogger(string $logFilePath) { + $ecommerceTz = new DateTimeZone(wc_timezone_string()); $dateFormat = "Y-m-d H:i:s"; $output = "%datetime% > %level_name% > %message% %context% %extra%\n"; $formatter = new LineFormatter($output, $dateFormat); @@ -44,6 +46,7 @@ private function initializeLogger(string $logFilePath) $stream->setFormatter($formatter); $this->logger = new Logger('transbank'); + $this->logger->setTimezone($ecommerceTz); $this->logger->pushHandler($stream); } From 1b7359273003e4dc2bc984fabd63234c36e0ea9c Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Mon, 3 Jun 2024 12:53:38 -0400 Subject: [PATCH 20/22] feat: add timezone in format for logs --- plugin/shared/Helpers/PluginLogger.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/shared/Helpers/PluginLogger.php b/plugin/shared/Helpers/PluginLogger.php index 0db91f05..1796f66a 100644 --- a/plugin/shared/Helpers/PluginLogger.php +++ b/plugin/shared/Helpers/PluginLogger.php @@ -34,7 +34,7 @@ public function __construct(LogConfig $config) private function initializeLogger(string $logFilePath) { $ecommerceTz = new DateTimeZone(wc_timezone_string()); - $dateFormat = "Y-m-d H:i:s"; + $dateFormat = "Y-m-d H:i:s P"; $output = "%datetime% > %level_name% > %message% %context% %extra%\n"; $formatter = new LineFormatter($output, $dateFormat); From ba8109b93880bc995358875830b6f3d5e533c30d Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Mon, 3 Jun 2024 14:59:01 -0400 Subject: [PATCH 21/22] chore: separate asset uploads --- .github/workflows/release.yml | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index f36d7788..91197f4f 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -29,16 +29,21 @@ jobs: env: WP_ORG_PASSWORD: ${{ secrets.WP_ORG_PASSWORD }} TAG: ${{ github.event.release.tag_name }} - - name: Upload assets + - name: Upload transbank-webpay-plus-rest.zip uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ github.event.release.upload_url }} - asset_path: | - transbank-webpay-plus-rest.zip - transbank-webpay-plus-rest-guzzle7.zip - asset_name: | - transbank-webpay-plus-rest.zip - transbank-webpay-plus-rest-guzzle7.zip + asset_path: transbank-webpay-plus-rest.zip + asset_name: transbank-webpay-plus-rest.zip + asset_content_type: application/zip + - name: Upload transbank-webpay-plus-rest-guzzle7.zip + uses: actions/upload-release-asset@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + upload_url: ${{ github.event.release.upload_url }} + asset_path: transbank-webpay-plus-rest-guzzle7.zip + asset_name: transbank-webpay-plus-rest-guzzle7.zip asset_content_type: application/zip From 819ee116899d4b9e9b68de7297ff37cb78b94e80 Mon Sep 17 00:00:00 2001 From: Mauricio Astudillo Toledo Date: Mon, 3 Jun 2024 15:36:09 -0400 Subject: [PATCH 22/22] docs: update readme --- plugin/readme.txt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/plugin/readme.txt b/plugin/readme.txt index 3d799df4..d697e461 100644 --- a/plugin/readme.txt +++ b/plugin/readme.txt @@ -14,6 +14,12 @@ Recibe pagos en línea con tarjetas de crédito, débito y prepago en tu WooComm Recibe pagos en línea con tarjetas de crédito, débito y prepago en tu WooCommerce a través de Webpay Plus y Webpay Oneclick == Changelog == += 1.9.1 = +* Se arregla un mensaje de warning provocado por la función maskData en PHP mayor o igual a 8.x. +* Se arregla un problema que impedía encontrar el archivo de log al migrar el sitio de un servidor a otro. +* Se arregla la zona horaria de los logs. Se usa la que este configurada en el ecommerce del comercio. +* Se arregla un problema que provocaba registros duplicados en el selector de archivos logs de la vista registros. + = 1.9.0 = * Se agrega la opción de poder seleccionar el archivo log en la sección de registros del menú de configuración. * Se agrega la funcionalidad para que se muestren las tarjetas registradas de Oneclick dependiendo del entorno. @@ -163,6 +169,12 @@ Arreglado: * Initial release. == Upgrade Notice == += 1.9.1 = +* Se arregla un mensaje de warning provocado por la función maskData en PHP mayor o igual a 8.x. +* Se arregla un problema que impedía encontrar el archivo de log al migrar el sitio de un servidor a otro. +* Se arregla la zona horaria de los logs. Se usa la que este configurada en el ecommerce del comercio. +* Se arregla un problema que provocaba registros duplicados en el selector de archivos logs de la vista registros. + = 1.9.0 = * Se agrega la opción de poder seleccionar el archivo log en la sección de registros del menú de configuración. * Se agrega la funcionalidad para que se muestren las tarjetas registradas de Oneclick dependiendo del entorno.