1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Gateways\Providers\AbstractGateway; |
4
|
|
|
|
5
|
|
|
use ByTIC\Payments\Forms\Traits\PaymentMethodFormTrait; |
6
|
|
|
use ByTIC\Payments\Gateways\Providers\AbstractGateway\Traits\GatewayTrait; |
7
|
|
|
use Nip\Form\AbstractForm; |
8
|
|
|
use Nip\Form\Traits\MagicMethodElementsFormTrait; |
9
|
|
|
use Nip_Form as NipForm; |
10
|
|
|
use Nip_Form_Element_Select as SelectElement; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class Form |
14
|
|
|
* @package ByTIC\Payments\Gateways\Providers\AbstractGateway |
15
|
|
|
*/ |
16
|
|
|
abstract class Form |
17
|
|
|
{ |
18
|
|
|
use MagicMethodElementsFormTrait; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var GatewayTrait |
22
|
|
|
*/ |
23
|
|
|
protected $gateway; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var NipForm |
27
|
|
|
*/ |
28
|
|
|
protected $form; |
29
|
|
|
|
30
|
|
|
protected $elements; |
31
|
|
|
|
32
|
|
|
public function init() |
33
|
|
|
{ |
34
|
|
|
$this->initElements(); |
35
|
|
|
if (is_array($this->elements) && count($this->elements) > 0) { |
36
|
|
|
$this->getForm()->addDisplayGroup($this->elements, $this->getGateway()->getLabel()); |
37
|
|
|
$this->getForm()->getDisplayGroup($this->getGateway()->getLabel()) |
38
|
|
|
->setAttrib('class', 'payment_gateway_fieldset') |
39
|
|
|
->setAttrib('id', 'payment_gateway_' . $this->getGateway()->getName()); |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function initElements() |
47
|
|
|
{ |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return NipForm|PaymentMethodFormTrait |
52
|
|
|
*/ |
53
|
|
|
public function getForm() |
54
|
|
|
{ |
55
|
|
|
return $this->form; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param $form |
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function setForm($form) |
63
|
|
|
{ |
64
|
|
|
$this->form = $form; |
65
|
|
|
|
66
|
|
|
return $this; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return GatewayTrait |
71
|
|
|
*/ |
72
|
|
|
public function getGateway() |
73
|
|
|
{ |
74
|
|
|
return $this->gateway; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param GatewayTrait $gateway |
79
|
|
|
* @return $this |
80
|
|
|
*/ |
81
|
|
|
public function setGateway($gateway) |
82
|
|
|
{ |
83
|
|
|
$this->gateway = $gateway; |
84
|
|
|
|
85
|
|
|
return $this; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* |
90
|
|
|
*/ |
91
|
|
|
public function getDataFromModel() |
92
|
|
|
{ |
93
|
|
|
if (is_array($this->elements) && count($this->elements) > 0) { |
94
|
|
|
$gName = $this->getGateway()->getName(); |
95
|
|
|
$options = $this->getForm()->getModel()->getOption($gName); |
96
|
|
|
foreach ($this->elements as $name => $inputName) { |
97
|
|
|
$element = $this->getForm()->{$inputName}; |
98
|
|
|
if (isset($options[$name])) { |
99
|
|
|
$element->setValue($options[$name]); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param $request |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
public function getDataFromRequest($request) |
|
|
|
|
110
|
|
|
{ |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
public function processValidation() |
117
|
|
|
{ |
118
|
|
|
return true; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
public function saveToModel() |
122
|
|
|
{ |
123
|
|
|
if (is_array($this->elements) && count($this->elements) > 0) { |
124
|
|
|
$gName = $this->getGateway()->getName(); |
125
|
|
|
if ($this->getForm()->getModel()->getOption('payment_gateway') == $gName) { |
126
|
|
|
$options = $this->getOptionsForSaveToModel(); |
127
|
|
|
$this->getForm()->getModel()->setOption($gName, $options); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
|
|
protected function getOptionsForSaveToModel() |
136
|
|
|
{ |
137
|
|
|
$options = []; |
138
|
|
|
foreach ($this->elements as $name => $inputName) { |
139
|
|
|
$element = $this->getForm()->{$inputName}; |
140
|
|
|
$options[$name] = $element->getValue(); |
141
|
|
|
} |
142
|
|
|
return $options; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return bool |
147
|
|
|
*/ |
148
|
|
|
public function process() |
149
|
|
|
{ |
150
|
|
|
return true; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected function initElementSandbox() |
154
|
|
|
{ |
155
|
|
|
$this->addRadioGroup('sandbox', 'sandbox', true); |
156
|
|
|
/** @var SelectElement $element */ |
157
|
|
|
$element = $this->getForm()->getElement($this->getGateway()->getName() . '[sandbox]'); |
158
|
|
|
$element->getRenderer()->setSeparator(''); |
|
|
|
|
159
|
|
|
$element->addOption('yes', 'Yes'); |
160
|
|
|
$element->addOption('no', 'No'); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* @param $name |
165
|
|
|
* @param $label |
166
|
|
|
* @param $type |
167
|
|
|
* @param $isRequired |
168
|
|
|
* @return AbstractForm |
169
|
|
|
*/ |
170
|
|
|
protected function add($name, $label, $type, $isRequired) |
171
|
|
|
{ |
172
|
|
|
return $this->getForm()->add($name, $label, $type, $isRequired); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param $arguments |
177
|
|
|
* @return mixed |
178
|
|
|
*/ |
179
|
|
|
protected function getElementNameFromMagicMethodArguments($arguments) |
180
|
|
|
{ |
181
|
|
|
$name = '' . $this->getGateway()->getName() . '[' . $arguments[0] . ']'; |
182
|
|
|
$this->elements[$arguments[0]] = $name; |
183
|
|
|
|
184
|
|
|
return $name; |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.