-
Notifications
You must be signed in to change notification settings - Fork 222
Expand file tree
/
Copy pathAdyenLogger.php
More file actions
executable file
·191 lines (174 loc) · 5.84 KB
/
AdyenLogger.php
File metadata and controls
executable file
·191 lines (174 loc) · 5.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
*
* Adyen Payment module (https://www.adyen.com/)
*
* Copyright (c) 2025 Adyen N.V. (https://www.adyen.com/)
* See LICENSE.txt for license details.
*
* Author: Adyen <magento@adyen.com>
*/
namespace Adyen\Payment\Logger;
use Adyen\Payment\Helper\Config;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\Phrase;
use Magento\Sales\Model\Order as MagentoOrder;
use Magento\Store\Model\StoreManagerInterface;
use Monolog\Level;
use Monolog\Logger;
use Monolog\LoggerFactory;
class AdyenLogger
{
const ADYEN_DEBUG = 101;
const ADYEN_NOTIFICATION = 201;
const ADYEN_RESULT = 202;
const ADYEN_INFO = 203;
const ADYEN_WARNING = 301;
const ADYEN_ERROR = 401;
/**
* @param Config $config
* @param StoreManagerInterface $storeManager
* @param LoggerFactory $loggerFactory
* @param array $handlers
* @param array $processors
*/
public function __construct(
private readonly Config $config,
private readonly StoreManagerInterface $storeManager,
private readonly LoggerFactory $loggerFactory,
private array $handlers = [],
private array $processors = []
) { }
/**
* Adds a webhook notification log record.
*
* This method allows for compatibility with common interfaces.
*
* @param string $message The log message
* @param array $context The log context
* @return Boolean Whether the record has been processed
*/
public function addAdyenNotification(string $message, array $context = []): bool
{
$logger = $this->generateLogger(self::ADYEN_NOTIFICATION);
return $logger->addRecord(Level::Info, $message, $context);
}
/**
* @param string $message
* @param array $context
* @return bool
* @throws NoSuchEntityException
*/
public function addAdyenDebug(string $message, array $context = []): bool
{
$storeId = $this->storeManager->getStore()->getId();
if ($this->config->debugLogsEnabled($storeId)) {
$logger = $this->generateLogger(self::ADYEN_DEBUG);
return $logger->addRecord(Level::Debug, $message, $context);
} else {
return false;
}
}
/**
* @param string $message
* @param array $context
* @return bool
*/
public function addAdyenWarning(string $message, array $context = []): bool
{
$logger = $this->generateLogger(self::ADYEN_WARNING);
return $logger->addRecord(Level::Warning, $message, $context);
}
/**
* @param string $message
* @param array $context
* @return bool
*/
public function addAdyenResult(string $message, array $context = []): bool
{
$logger = $this->generateLogger(self::ADYEN_RESULT);
return $logger->addRecord(Level::Info, $message, $context);
}
/**
* Adds a log record at the INFO level.
*
* This method allows for compatibility with common interfaces.
*
* @param string $message The log message
* @param array $context The log context
* @return Boolean Whether the record has been processed
*/
public function addAdyenInfoLog(string $message, array $context = []): bool
{
$logger = $this->generateLogger(self::ADYEN_INFO);
return $logger->addRecord(Level::Info, $message, $context);
}
/**
* @param string $message
* @param array $context
* @return bool
*/
public function error(string $message, array $context = []): bool
{
$logger = $this->generateLogger(self::ADYEN_ERROR);
return $logger->addRecord(Level::Error, $message, $context);
}
public function getOrderContext(MagentoOrder $order): array
{
return [
'orderId' => $order->getId(),
'orderIncrementId' => $order->getIncrementId(),
'orderState' => $order->getState(),
'orderStatus' => $order->getStatus()
];
}
public function getInvoiceContext(MagentoOrder\Invoice $invoice): array
{
$stateName = $invoice->getStateName();
return [
'invoiceId' => $invoice->getEntityId(),
'invoiceIncrementId' => $invoice->getIncrementId(),
'invoiceState' => $invoice->getState(),
'invoiceStateName' => $stateName instanceof Phrase ? $stateName->getText() : $stateName,
'invoiceWasPayCalled' => $invoice->wasPayCalled(),
'invoiceCanCapture' => $invoice->canCapture(),
'invoiceCanCancel' => $invoice->canCancel(),
'invoiceCanVoid' => $invoice->canVoid(),
'invoiceCanRefund' => $invoice->canRefund()
];
}
/**
* @param int $handler
* @return Logger
*/
private function generateLogger(int $handler): Logger
{
/** @var Logger $logger */
$logger = $this->loggerFactory->create(['name' => 'Adyen Logger']);
foreach ($this->processors as $processor) {
$logger->pushProcessor($processor);
}
switch ($handler) {
case self::ADYEN_NOTIFICATION:
$logger->pushHandler($this->handlers['adyenNotification']);
break;
case self::ADYEN_WARNING:
$logger->pushHandler($this->handlers['adyenWarning']);
break;
case self::ADYEN_RESULT:
$logger->pushHandler($this->handlers['adyenResult']);
break;
case self::ADYEN_INFO:
$logger->pushHandler($this->handlers['adyenInfo']);
break;
case self::ADYEN_ERROR:
$logger->pushHandler($this->handlers['adyenError']);
break;
case self::ADYEN_DEBUG:
default:
$logger->pushHandler($this->handlers['adyenDebug']);
break;
}
return $logger;
}
}