1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Gateways\Providers\Mobilpay\FileLoader; |
4
|
|
|
|
5
|
|
|
use ByTIC\Payments\Models\Methods\Traits\RecordTrait as PaymentMethodRecord; |
6
|
|
|
use Nip\Filesystem\Filesystem; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Trait HasFileLoader |
10
|
|
|
* @package ByTIC\Payments\Gateways\Providers\Mobilpay\FileLoader |
11
|
|
|
* |
12
|
|
|
* @method PaymentMethodRecord getPaymentMethod |
13
|
|
|
*/ |
14
|
|
|
trait HasFileLoader |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @inheritDoc |
18
|
|
|
*/ |
19
|
9 |
|
public function initialize(array $parameters = []) |
20
|
|
|
{ |
21
|
9 |
|
$this->initParamsForKeys($parameters); |
22
|
|
|
|
23
|
9 |
|
return parent::initialize($parameters); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param $parameters |
28
|
|
|
*/ |
29
|
9 |
|
protected function initParamsForKeys(&$parameters) |
30
|
|
|
{ |
31
|
9 |
|
if (isset($parameters['private-key'])) { |
32
|
8 |
|
$parameters['privateKey'] = $parameters['private-key']; |
33
|
8 |
|
unset($parameters['private-key']); |
34
|
|
|
} |
35
|
|
|
|
36
|
9 |
|
if (isset($parameters['file']) && is_array($parameters['file'])) { |
37
|
|
|
$parameters['file'] = 'public.cer'; |
38
|
|
|
} |
39
|
|
|
|
40
|
9 |
|
if (isset($parameters['privateKey']) && is_array($parameters['privateKey'])) { |
41
|
|
|
$parameters['privateKey'] = 'private.key'; |
42
|
|
|
} |
43
|
9 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param $certificate |
47
|
|
|
* @throws \Exception |
48
|
|
|
*/ |
49
|
8 |
|
public function setFile($certificate) |
50
|
|
|
{ |
51
|
8 |
|
if ($certificate == 'public.cer') { |
52
|
|
|
$certificate = $this->loadFileIntoModel('certificate'); |
53
|
|
|
} |
54
|
|
|
|
55
|
8 |
|
return parent::setCertificate($certificate); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $key |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
8 |
|
public function setPrivateKey(string $key) |
63
|
|
|
{ |
64
|
8 |
|
if ($key == 'private.key') { |
65
|
|
|
$key = $this->loadFileIntoModel('privateKey'); |
66
|
|
|
} |
67
|
|
|
|
68
|
8 |
|
return parent::setPrivateKey($key); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $type |
73
|
|
|
* @return false|string |
74
|
|
|
*/ |
75
|
|
|
protected function loadFileIntoModel($type) |
76
|
|
|
{ |
77
|
|
|
$filename = $this->getFilePath($type); |
78
|
|
|
$filesystem = new Filesystem(); |
79
|
|
|
if (!file_exists($filename)) { |
80
|
|
|
return $filename; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$options = $this->getPaymentMethod()->getPaymentGatewayOptions(); |
84
|
|
|
$content = file_get_contents($filename); |
85
|
|
|
$options[$type] = $content; |
86
|
|
|
if ($type == 'certificate') { |
87
|
|
|
unset($options['file']); |
88
|
|
|
} elseif ($type == 'privateKey') { |
89
|
|
|
unset($options['private-key']); |
90
|
|
|
} |
91
|
|
|
$this->getPaymentMethod()->setPaymentGatewayOptions($options); |
92
|
|
|
$this->getPaymentMethod()->save(); |
93
|
|
|
|
94
|
|
|
$filesystem->remove($filename); |
95
|
|
|
@rmdir($this->getFileDirectoryPath()); |
|
|
|
|
96
|
|
|
return $content; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param $type |
101
|
|
|
* @return bool|string |
102
|
|
|
* @throws \Exception |
103
|
|
|
*/ |
104
|
|
|
protected function validateFilePath($type) |
105
|
|
|
{ |
106
|
|
|
$path = $this->getParameter($type); |
|
|
|
|
107
|
|
|
if (strpos($path, DIRECTORY_SEPARATOR) === false) { |
108
|
|
|
$path = $this->getFilePath($type); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $this->setParameter($type, $path); |
|
|
|
|
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param $type |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
protected function getFilePath($type) |
119
|
|
|
{ |
120
|
|
|
$fileName = [ |
121
|
|
|
'certificate' => 'public.cer', |
122
|
|
|
'privateKey' => 'private.key', |
123
|
|
|
]; |
124
|
|
|
|
125
|
|
|
return $this->getFileDirectoryPath() . $fileName[$type]; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return string |
130
|
|
|
*/ |
131
|
|
|
protected function getFileDirectoryPath() |
132
|
|
|
{ |
133
|
|
|
return $this->getPaymentMethod() ? $this->getPaymentMethod()->getFilesDirectory() : null; |
|
|
|
|
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: