1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright © Getnet. All rights reserved. |
4
|
|
|
* |
5
|
|
|
* @author Bruno Elisei <[email protected]> |
6
|
|
|
* See LICENSE for license details. |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Getnet\PaymentMagento\Gateway\Config; |
11
|
|
|
|
12
|
|
|
use Endroid\QrCode\QrCode; |
13
|
|
|
use Endroid\QrCode\Writer\PngWriter; |
14
|
|
|
use Magento\Framework\App\Config\ScopeConfigInterface; |
15
|
|
|
use Magento\Framework\App\Filesystem\DirectoryList; |
16
|
|
|
use Magento\Framework\Filesystem; |
17
|
|
|
use Magento\Framework\Filesystem\Directory\WriteInterface; |
18
|
|
|
use Magento\Framework\Filesystem\Io\File; |
19
|
|
|
use Magento\Payment\Gateway\Config\Config as PaymentConfig; |
20
|
|
|
use Magento\Store\Model\ScopeInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class ConfigWallet - Returns form of payment configuration properties. |
24
|
|
|
* |
25
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
26
|
|
|
*/ |
27
|
|
|
class ConfigWallet extends PaymentConfig |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @const string |
31
|
|
|
*/ |
32
|
|
|
public const METHOD = 'getnet_paymentmagento_wallet'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @const string |
36
|
|
|
*/ |
37
|
|
|
public const ACTIVE = 'active'; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @const string |
41
|
|
|
*/ |
42
|
|
|
public const TITLE = 'title'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @const string |
46
|
|
|
*/ |
47
|
|
|
public const INSTRUCTION_CHECKOUT = 'instruction_checkout'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var ScopeConfigInterface |
51
|
|
|
*/ |
52
|
|
|
protected $scopeConfig; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var File |
56
|
|
|
*/ |
57
|
|
|
protected $fileIo; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var Filesystem |
61
|
|
|
*/ |
62
|
|
|
protected $filesystem; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var Config |
66
|
|
|
*/ |
67
|
|
|
protected $config; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param ScopeConfigInterface $scopeConfig |
71
|
|
|
* @param File $fileIo |
72
|
|
|
* @param Filesystem $filesystem |
73
|
|
|
* @param DirectoryList $directoryList |
74
|
|
|
* @param Config $config |
75
|
|
|
* @param string|null $methodCode |
76
|
|
|
*/ |
77
|
|
|
public function __construct( |
78
|
|
|
ScopeConfigInterface $scopeConfig, |
79
|
|
|
File $fileIo, |
80
|
|
|
Filesystem $filesystem, |
81
|
|
|
DirectoryList $directoryList, |
82
|
|
|
Config $config, |
83
|
|
|
$methodCode = null |
84
|
|
|
) { |
85
|
|
|
parent::__construct($scopeConfig, $methodCode); |
86
|
|
|
$this->scopeConfig = $scopeConfig; |
87
|
|
|
$this->fileIo = $fileIo; |
88
|
|
|
$this->filesystem = $filesystem; |
89
|
|
|
$this->directoryList = $directoryList; |
|
|
|
|
90
|
|
|
$this->config = $config; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Get Payment configuration status. |
95
|
|
|
* |
96
|
|
|
* @param int|null $storeId |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function isActive($storeId = null): bool |
101
|
|
|
{ |
102
|
|
|
$pathPattern = 'payment/%s/%s'; |
103
|
|
|
|
104
|
|
|
return (bool) $this->scopeConfig->getValue( |
105
|
|
|
sprintf($pathPattern, self::METHOD, self::ACTIVE), |
106
|
|
|
ScopeInterface::SCOPE_STORE, |
107
|
|
|
$storeId |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Get title of payment. |
113
|
|
|
* |
114
|
|
|
* @param int|null $storeId |
115
|
|
|
* |
116
|
|
|
* @return string|null |
117
|
|
|
*/ |
118
|
|
|
public function getTitle($storeId = null): ?string |
119
|
|
|
{ |
120
|
|
|
$pathPattern = 'payment/%s/%s'; |
121
|
|
|
|
122
|
|
|
return $this->scopeConfig->getValue( |
123
|
|
|
sprintf($pathPattern, self::METHOD, self::TITLE), |
124
|
|
|
ScopeInterface::SCOPE_STORE, |
125
|
|
|
$storeId |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Get Instruction - Checkoout. |
131
|
|
|
* |
132
|
|
|
* @param int|null $storeId |
133
|
|
|
* |
134
|
|
|
* @return string|null |
135
|
|
|
*/ |
136
|
|
|
public function getInstructionCheckout($storeId = null): ?string |
137
|
|
|
{ |
138
|
|
|
$pathPattern = 'payment/%s/%s'; |
139
|
|
|
|
140
|
|
|
return $this->scopeConfig->getValue( |
141
|
|
|
sprintf($pathPattern, self::METHOD, self::INSTRUCTION_CHECKOUT), |
142
|
|
|
ScopeInterface::SCOPE_STORE, |
143
|
|
|
$storeId |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Generate Image Qr Code. |
149
|
|
|
* |
150
|
|
|
* @param string $qrCode |
151
|
|
|
* @param string $transactionId |
152
|
|
|
* |
153
|
|
|
* @return string |
154
|
|
|
*/ |
155
|
|
|
public function generateImageQrCode($qrCode, $transactionId) |
156
|
|
|
{ |
157
|
|
|
$fileName = null; |
158
|
|
|
if ($this->hasPathDir()) { |
159
|
|
|
$fileName = 'getnet/pix/'.$transactionId.'.png'; |
160
|
|
|
$mediaDirectory = $this->filesystem->getDirectoryWrite(DirectoryList::MEDIA); |
161
|
|
|
$filePath = $this->getPathFile($mediaDirectory, $transactionId); |
|
|
|
|
162
|
|
|
$generate = $this->createImageQrCode($mediaDirectory, $filePath, $qrCode); |
163
|
|
|
if (!$generate) { |
164
|
|
|
return false; |
|
|
|
|
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $fileName; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Get Path File. |
173
|
|
|
* |
174
|
|
|
* @param string $mediaDirectory |
175
|
|
|
* @param string $transactionId |
176
|
|
|
* |
177
|
|
|
* @return string |
178
|
|
|
*/ |
179
|
|
|
public function getPathFile($mediaDirectory, $transactionId): string |
180
|
|
|
{ |
181
|
|
|
$filePath = $mediaDirectory->getAbsolutePath('getnet/pix/'.$transactionId.'.png'); |
182
|
|
|
|
183
|
|
|
return $filePath; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Create Image Qr Code. |
188
|
|
|
* |
189
|
|
|
* @param WriteInterface $writeDirectory |
190
|
|
|
* @param string $filePath |
191
|
|
|
* @param string $qrCode |
192
|
|
|
* |
193
|
|
|
* @throws FileSystemException |
194
|
|
|
* |
195
|
|
|
* @return bool |
196
|
|
|
*/ |
197
|
|
|
public function createImageQrCode(WriteInterface $writeDirectory, $filePath, $qrCode): bool |
198
|
|
|
{ |
199
|
|
|
$qrCode = new QrCode($qrCode); |
200
|
|
|
$qrCode->setSize(150); |
201
|
|
|
$qrCode->setErrorCorrectionLevel('high'); |
|
|
|
|
202
|
|
|
$qrCode->setForegroundColor(['r' => 0, 'g' => 0, 'b' => 0, 'a' => 0]); |
203
|
|
|
$qrCode->setBackgroundColor(['r' => 255, 'g' => 255, 'b' => 255, 'a' => 0]); |
204
|
|
|
$qrCode->setLabelFontSize(16); |
205
|
|
|
$qrCode->setEncoding('UTF-8'); |
206
|
|
|
$writer = new PngWriter(); |
207
|
|
|
$pngData = $writer->writeString($qrCode); |
208
|
|
|
|
209
|
|
|
try { |
210
|
|
|
$stream = $writeDirectory->openFile($filePath, 'w+'); |
211
|
|
|
$stream->lock(); |
212
|
|
|
$stream->write($pngData); |
213
|
|
|
$stream->unlock(); |
214
|
|
|
$stream->close(); |
215
|
|
|
} catch (FileSystemException $ex) { |
|
|
|
|
216
|
|
|
new Phrase($ex->getMessage()); |
|
|
|
|
217
|
|
|
|
218
|
|
|
return false; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
return true; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Check or Create Dir. |
226
|
|
|
* |
227
|
|
|
* @return bool |
228
|
|
|
*/ |
229
|
|
|
public function hasPathDir(): bool |
230
|
|
|
{ |
231
|
|
|
$pixPath = $this->filesystem->getDirectoryRead(DirectoryList::MEDIA)->getAbsolutePath('/getnet/pix'); |
232
|
|
|
|
233
|
|
|
return $this->fileIo->checkAndCreateFolder($pixPath); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|