|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\Payments\Forms\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait; |
|
6
|
|
|
use ByTIC\MediaLibrary\HasMedia\Traits\AddMediaTrait; |
|
7
|
|
|
use ByTIC\Payments\Gateways\Manager as GatewaysManager; |
|
8
|
|
|
use ByTIC\Payments\Gateways\Providers\AbstractGateway\Traits\GatewayTrait; |
|
9
|
|
|
use ByTIC\Payments\Models\Methods\Traits\RecordTrait as PaymentMethod; |
|
10
|
|
|
use ByTIC\Payments\Models\Methods\Types\CreditCards; |
|
11
|
|
|
use Nip\Form\Elements\AbstractElement as FormElementAbstract; |
|
12
|
|
|
use Nip_Form_Element_Select as FormSelect; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class PaymentMethodFormTrait |
|
16
|
|
|
* @package ByTIC\Payments\Forms\Traits |
|
17
|
|
|
* |
|
18
|
|
|
* @method addInput($name, $label = false, $type = 'input', $isRequired = false) |
|
19
|
|
|
* @method addHidden($name, $label = false, $type = 'input', $isRequired = false) |
|
20
|
|
|
* @method addSelect($name, $label = false, $type = 'input', $isRequired = false) |
|
21
|
|
|
* @method addDisplayGroup(array $elements, $name) |
|
22
|
|
|
*/ |
|
23
|
|
|
trait PaymentMethodFormTrait |
|
24
|
|
|
{ |
|
25
|
|
|
// use AbstractFormTrait; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var null|GatewaysManager |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $paymentGatewaysManager = null; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var null|GatewayTrait[] |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $paymentGatewaysItems = null; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var null|array |
|
39
|
|
|
*/ |
|
40
|
|
|
protected $paymentGatewaysNames = null; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param $request |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getDataFromRequest($request) |
|
46
|
|
|
{ |
|
47
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
48
|
|
|
parent::getDataFromRequest($request); |
|
49
|
|
|
|
|
50
|
|
|
$this->getDataFromRequestPaymentGateways($request); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param $request |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function getDataFromRequestPaymentGateways($request) |
|
57
|
|
|
{ |
|
58
|
|
|
$gateways = $this->getPaymentGatewaysItems(); |
|
59
|
|
|
foreach ($gateways as $gateway) { |
|
60
|
|
|
$gateway->getOptionsForm()->getDataFromRequest($request); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return GatewayTrait[] |
|
66
|
|
|
*/ |
|
67
|
|
|
public function getPaymentGatewaysItems() |
|
68
|
|
|
{ |
|
69
|
|
|
$this->checkPaymentGatewaysValues(); |
|
70
|
|
|
|
|
71
|
|
|
return $this->paymentGatewaysItems; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param null $paymentGatewaysItems |
|
|
|
|
|
|
76
|
|
|
*/ |
|
77
|
|
|
public function setPaymentGatewaysItems($paymentGatewaysItems) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->paymentGatewaysItems = $paymentGatewaysItems; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
protected function checkPaymentGatewaysValues() |
|
83
|
|
|
{ |
|
84
|
|
|
if ($this->paymentGatewaysItems == null) { |
|
85
|
|
|
$this->paymentGatewaysItems = $this->getPaymentGatewaysManager()::getAll(); |
|
|
|
|
|
|
86
|
|
|
$this->paymentGatewaysNames = $this->paymentGatewaysItems->keys(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return GatewaysManager |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getPaymentGatewaysManager() |
|
94
|
|
|
{ |
|
95
|
|
|
if ($this->paymentGatewaysManager == null) { |
|
96
|
|
|
$this->initPaymentGatewaysManager(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return $this->paymentGatewaysManager; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @param ?GatewaysManager $paymentGatewaysManager |
|
104
|
|
|
*/ |
|
105
|
|
|
public function setPaymentGatewaysManager($paymentGatewaysManager) |
|
106
|
|
|
{ |
|
107
|
|
|
$this->paymentGatewaysManager = $paymentGatewaysManager; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
protected function initPaymentGatewaysManager() |
|
111
|
|
|
{ |
|
112
|
|
|
$this->setPaymentGatewaysManager($this->newPaymentGatewaysManager()); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return GatewaysManager |
|
117
|
|
|
*/ |
|
118
|
|
|
protected function newPaymentGatewaysManager() |
|
119
|
|
|
{ |
|
120
|
|
|
return new GatewaysManager(); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function processValidation() |
|
124
|
|
|
{ |
|
125
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
126
|
|
|
parent::processValidation(); |
|
127
|
|
|
|
|
128
|
|
|
$this->processValidationPaymentGateways(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
protected function processValidationPaymentGateways() |
|
132
|
|
|
{ |
|
133
|
|
|
$gateways = $this->getPaymentGatewaysItems(); |
|
134
|
|
|
foreach ($gateways as $gateway) { |
|
135
|
|
|
$gateway->getOptionsForm()->processValidation(); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function process() |
|
140
|
|
|
{ |
|
141
|
|
|
$this->saveToModel(); |
|
142
|
|
|
$this->getModel()->save(); |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
$this->processFormPaymentGateways(); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
public function saveToModel() |
|
148
|
|
|
{ |
|
149
|
|
|
/** @noinspection PhpUndefinedClassInspection */ |
|
150
|
|
|
parent::saveToModel(); |
|
151
|
|
|
|
|
152
|
|
|
$this->saveToModelTypeMethod(); |
|
153
|
|
|
$this->saveToModelPaymentGateways(); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
protected function saveToModelTypeMethod() |
|
157
|
|
|
{ |
|
158
|
|
|
/** @var FormSelect $typeInput */ |
|
159
|
|
|
$typeInput = $this->getElement('type'); |
|
160
|
|
|
$type = $typeInput->getValue(); |
|
161
|
|
|
|
|
162
|
|
|
if (in_array($type, $this->getPaymentGatewaysNames())) { |
|
|
|
|
|
|
163
|
|
|
$this->getModel()->type = 'credit-cards'; |
|
164
|
|
|
$this->getModel()->setOption('payment_gateway', $type); |
|
|
|
|
|
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* @param $name |
|
170
|
|
|
* @return FormElementAbstract |
|
171
|
|
|
*/ |
|
172
|
|
|
abstract public function getElement($name); |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return array|null |
|
176
|
|
|
*/ |
|
177
|
|
|
public function getPaymentGatewaysNames() |
|
178
|
|
|
{ |
|
179
|
|
|
$this->checkPaymentGatewaysValues(); |
|
180
|
|
|
|
|
181
|
|
|
return $this->paymentGatewaysNames; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return PaymentMethod|HasMediaTrait|AddMediaTrait |
|
186
|
|
|
*/ |
|
187
|
|
|
abstract public function getModel(); |
|
188
|
|
|
|
|
189
|
|
|
protected function saveToModelPaymentGateways() |
|
190
|
|
|
{ |
|
191
|
|
|
$gateways = $this->getPaymentGatewaysItems(); |
|
192
|
|
|
foreach ($gateways as $gateway) { |
|
193
|
|
|
$gateway->getOptionsForm()->saveToModel(); |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
protected function processFormPaymentGateways() |
|
198
|
|
|
{ |
|
199
|
|
|
$gateways = $this->getPaymentGatewaysItems(); |
|
200
|
|
|
foreach ($gateways as $gateway) { |
|
201
|
|
|
$gateway->getOptionsForm()->process(); |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
protected function initNameElements() |
|
206
|
|
|
{ |
|
207
|
|
|
$this->addInput('internal_name', translator()->trans('internal_name'), true); |
|
208
|
|
|
$this->addInput('name', translator()->trans('name'), true); |
|
209
|
|
|
|
|
210
|
|
|
$this->addDisplayGroup(['internal_name', 'name'], 'Details'); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
protected function initTypeElement() |
|
214
|
|
|
{ |
|
215
|
|
|
if ($this->getModel()->getPurchasesCount() > 0) { |
|
|
|
|
|
|
216
|
|
|
$this->addHidden('type', translator()->trans('type'), true); |
|
217
|
|
|
$this->addInput('typeText', translator()->trans('type'), false); |
|
218
|
|
|
$this->getElement('typeText')->setAttrib('readonly', 'readonly'); |
|
219
|
|
|
} else { |
|
220
|
|
|
$this->initTypeSelect(); |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
$this->getElement('type')->setId('payment_type'); |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
protected function initTypeSelect() |
|
227
|
|
|
{ |
|
228
|
|
|
$this->addSelect('type', translator()->trans('type'), true); |
|
229
|
|
|
$types = $this->getModel()->getManager()->getTypes(); |
|
|
|
|
|
|
230
|
|
|
foreach ($types as $type) { |
|
231
|
|
|
$this->getElement('type') |
|
232
|
|
|
->addOption($type->getName(), $type->getLabel()); |
|
|
|
|
|
|
233
|
|
|
} |
|
234
|
|
|
$this->appendPaymentGatewaysOptgroupOption(); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
protected function appendPaymentGatewaysOptgroupOption() |
|
238
|
|
|
{ |
|
239
|
|
|
$gateways = $this->getPaymentGatewaysItems(); |
|
240
|
|
|
/** @var FormSelect $typeInput */ |
|
241
|
|
|
$typeInput = $this->getElement('type'); |
|
242
|
|
|
foreach ($gateways as $name => $gateway) { |
|
243
|
|
|
$typeInput->appendOptgroupOption( |
|
244
|
|
|
$this->getPaymentGatewaysManager()->getLabel('title'), |
|
245
|
|
|
$gateway->getName(), |
|
246
|
|
|
$gateway->getLabel() |
|
247
|
|
|
); |
|
248
|
|
|
} |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
protected function parseTypeForPaymentGateway() |
|
252
|
|
|
{ |
|
253
|
|
|
$model = $this->getModel(); |
|
254
|
|
|
if ($model->getType() instanceof CreditCards && $model->getOption('payment_gateway')) { |
|
|
|
|
|
|
255
|
|
|
$this->getElement('type')->setValue($this->getModel()->getOption('payment_gateway')); |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
protected function initPaymentGatewaysOptionsForm() |
|
260
|
|
|
{ |
|
261
|
|
|
$gateways = $this->getPaymentGatewaysItems(); |
|
262
|
|
|
|
|
263
|
|
|
foreach ($gateways as $name => $gateway) { |
|
264
|
|
|
$gateway->getOptionsForm()->setForm($this)->init(); |
|
265
|
|
|
} |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
protected function getDataFromModelPaymentGateways() |
|
269
|
|
|
{ |
|
270
|
|
|
$gateways = $this->getPaymentGatewaysItems(); |
|
271
|
|
|
foreach ($gateways as $gateway) { |
|
272
|
|
|
$gateway->getOptionsForm()->getDataFromModel(); |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
|