1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Mobilpay; |
4
|
|
|
|
5
|
|
|
use ByTIC\Payments\Gateways\Providers\AbstractGateway\Form as AbstractForm; |
6
|
|
|
use ByTIC\Payments\Models\Methods\Types\CreditCards; |
7
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Form |
11
|
|
|
* @package ByTIC\Payments\Mobilpay |
12
|
|
|
*/ |
13
|
|
|
class Form extends AbstractForm |
14
|
|
|
{ |
15
|
|
|
public function initElements() |
16
|
|
|
{ |
17
|
|
|
parent::initElements(); |
18
|
|
|
$this->initElementSandbox(); |
19
|
|
|
$this->addInput('signature', 'Signature', false); |
20
|
|
|
$this->addInput('sellerId', 'Seller ID', false); |
21
|
|
|
$this->addInput('username', 'Username', false); |
22
|
|
|
|
23
|
|
|
$this->addFile('file', 'Certificate', false); |
24
|
|
|
$this->addTextarea('certificate', 'Certificate', false); |
25
|
|
|
$element = $this->getForm()->getElement('mobilpay[certificate]'); |
26
|
|
|
$element->setAttrib('readonly', 'readonly'); |
27
|
|
|
|
28
|
|
|
$this->addFile('private-key', 'Private key', false); |
29
|
|
|
$this->addTextarea('privateKey', 'Private key', false); |
30
|
|
|
$element = $this->getForm()->getElement('mobilpay[privateKey]'); |
31
|
|
|
$element->setAttrib('readonly', 'readonly'); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function getDataFromModel() |
35
|
|
|
{ |
36
|
|
|
$type = $this->getForm()->getModel()->getType(); |
37
|
|
|
if ($type instanceof CreditCards) { |
|
|
|
|
38
|
|
|
$type->getGateway(); |
39
|
|
|
} |
40
|
|
|
parent::getDataFromModel(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return bool |
45
|
|
|
*/ |
46
|
|
|
public function processValidation() |
47
|
|
|
{ |
48
|
|
|
parent::processValidation(); |
49
|
|
|
|
50
|
|
|
if (isset($_FILES['mobilpay'])) { |
51
|
|
|
$files = ['file', 'private-key']; |
52
|
|
|
foreach ($files as $fileType) { |
53
|
|
|
$element = $this->getForm()->getElement('mobilpay['.$fileType.']'); |
54
|
|
|
|
55
|
|
|
/** @var UploadedFile $uploadedFile */ |
56
|
|
|
$uploadedFile = $element->getValue(); |
|
|
|
|
57
|
|
|
if ($uploadedFile instanceof UploadedFile) { |
58
|
|
|
if ($uploadedFile->isValid()) { |
59
|
|
|
$extension = $fileType == 'file' ? 'cer' : 'key'; |
60
|
|
|
if ($uploadedFile->getClientOriginalExtension() != $extension) { |
61
|
|
|
$element->addError( |
62
|
|
|
'Invalid extension ['.$extension.']['.$uploadedFile->getClientOriginalName().']' |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
|
|
$element->addError($uploadedFile->getErrorMessage()); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @inheritdoc |
77
|
|
|
*/ |
78
|
|
|
protected function generateModelOptions() |
79
|
|
|
{ |
80
|
|
|
$options = parent::generateModelOptions(); |
|
|
|
|
81
|
|
|
|
82
|
|
|
$files = ['certificate', 'privateKey']; |
|
|
|
|
83
|
|
|
|
84
|
|
|
return $this->files[$type]; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function getOptionsForSaveToModel() |
91
|
|
|
{ |
92
|
|
|
$options = parent::getOptionsForSaveToModel(); |
93
|
|
|
unset($options['file'], $options['private-key']); |
94
|
|
|
return $options; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
public function process() |
101
|
|
|
{ |
102
|
|
|
parent::process(); |
103
|
|
|
|
104
|
|
|
$model = $this->getForm()->getModel(); |
105
|
|
|
$options = $model->getPaymentGatewayOptions(); |
106
|
|
|
|
107
|
|
|
$files = ['file' => 'certificate', 'private-key' => 'privateKey']; |
108
|
|
|
foreach ($files as $file => $variable) { |
109
|
|
|
$fileData = $this->getForm()->getElement('mobilpay[' . $file . ']')->getValue(); |
|
|
|
|
110
|
|
|
if (is_array($fileData) && $fileData['tmp_name']) { |
111
|
|
|
$content = file_get_contents($fileData["tmp_name"]); |
112
|
|
|
$options[$variable] = $content; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$model->setPaymentGatewayOptions($options); |
117
|
|
|
$model->saveRecord(); |
118
|
|
|
|
119
|
|
|
return $options; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|