1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* MIT License |
5
|
|
|
* Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace SprykerEco\Yves\Payone\Form; |
9
|
|
|
|
10
|
|
|
use Generated\Shared\Transfer\PaymentTransfer; |
11
|
|
|
use Generated\Shared\Transfer\PayonePaymentDirectDebitTransfer; |
12
|
|
|
use Spryker\Shared\Kernel\Store; |
13
|
|
|
use Spryker\Yves\StepEngine\Dependency\Form\SubFormInterface; |
14
|
|
|
use SprykerEco\Shared\Payone\PayoneConstants; |
15
|
|
|
use SprykerEco\Yves\Payone\Form\Constraint\ManageMandate; |
16
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
17
|
|
|
use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
18
|
|
|
use Symfony\Component\Form\Extension\Core\Type\TextType; |
19
|
|
|
use Symfony\Component\Form\FormBuilderInterface; |
20
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
21
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
22
|
|
|
|
23
|
|
|
class DirectDebitSubForm extends AbstractPayoneSubForm |
24
|
|
|
{ |
25
|
|
|
const PAYMENT_METHOD = 'direct_debit'; |
26
|
|
|
const FIELD_IBAN = 'iban'; |
27
|
|
|
const FIELD_BIC = 'bic'; |
28
|
|
|
const FIELD_BANK_COUNTRY = 'bankcountry'; |
29
|
|
|
const FIELD_BANK_ACCOUNT = 'bankaccount'; |
30
|
|
|
const FIELD_BANK_ACCOUNT_MODE = 'bankaccountmode'; |
31
|
|
|
const FIELD_BANK_CODE = 'bankcode'; |
32
|
|
|
const OPTION_BANK_COUNTRIES = 'direct debit bank countries'; |
33
|
|
|
const OPTION_BANK_ACCOUNT_MODE = 'direct debit bank account mode'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function getName() |
39
|
|
|
{ |
40
|
|
|
return PaymentTransfer::PAYONE_DIRECT_DEBIT; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getPropertyPath() |
47
|
|
|
{ |
48
|
|
|
return PaymentTransfer::PAYONE_DIRECT_DEBIT; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getTemplatePath() |
55
|
|
|
{ |
56
|
|
|
return PayoneConstants::PROVIDER_NAME . '/' . self::PAYMENT_METHOD; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver |
61
|
|
|
* |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function configureOptions(OptionsResolver $resolver) |
65
|
|
|
{ |
66
|
|
|
$resolver->setDefaults([ |
67
|
|
|
'data_class' => PayonePaymentDirectDebitTransfer::class, |
68
|
|
|
])->setRequired(SubFormInterface::OPTIONS_FIELD_NAME); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver |
73
|
|
|
* |
74
|
|
|
* @return void |
75
|
|
|
*/ |
76
|
|
|
public function setDefaultOptions(OptionsResolver $resolver) |
77
|
|
|
{ |
78
|
|
|
$this->configureOptions($resolver); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @param \Symfony\Component\Form\FormBuilderInterface $builder |
83
|
|
|
* @param array $options |
84
|
|
|
* |
85
|
|
|
* @return void |
86
|
|
|
*/ |
87
|
|
|
public function buildForm(FormBuilderInterface $builder, array $options) |
88
|
|
|
{ |
89
|
|
|
$this->addIBAN($builder) |
90
|
|
|
->addBIC($builder); |
91
|
|
|
|
92
|
|
|
if (Store::getInstance()->getCurrentCountry() === 'DE') { |
93
|
|
|
$this->addBankAccount($builder) |
94
|
|
|
->addBankCode($builder) |
95
|
|
|
->addBankCountry($builder, $options) |
96
|
|
|
->addModeSwitch($builder, $options); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param \Symfony\Component\Form\FormBuilderInterface $builder |
102
|
|
|
* @param array $options |
103
|
|
|
* |
104
|
|
|
* @return \SprykerEco\Yves\Payone\Form\DirectDebitSubForm |
105
|
|
|
*/ |
106
|
|
|
protected function addModeSwitch(FormBuilderInterface $builder, array $options) |
107
|
|
|
{ |
108
|
|
|
$builder->add( |
109
|
|
|
static::FIELD_BANK_ACCOUNT_MODE, |
110
|
|
|
ChoiceType::class, |
111
|
|
|
[ |
112
|
|
|
'label' => false, |
113
|
|
|
'required' => true, |
114
|
|
|
'expanded' => true, |
115
|
|
|
'multiple' => false, |
116
|
|
|
'placeholder' => false, |
117
|
|
|
'choices' => $options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_ACCOUNT_MODE], |
118
|
|
|
'constraints' => [ |
119
|
|
|
], |
120
|
|
|
] |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param \Symfony\Component\Form\FormBuilderInterface $builder |
128
|
|
|
* |
129
|
|
|
* @return \SprykerEco\Yves\Payone\Form\DirectDebitSubForm |
130
|
|
|
*/ |
131
|
|
|
protected function addBankAccount(FormBuilderInterface $builder) |
132
|
|
|
{ |
133
|
|
|
$builder->add( |
134
|
|
|
static::FIELD_BANK_ACCOUNT, |
135
|
|
|
TextType::class, |
136
|
|
|
[ |
137
|
|
|
'label' => false, |
138
|
|
|
'required' => true, |
139
|
|
|
'constraints' => [ |
140
|
|
|
], |
141
|
|
|
] |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param \Symfony\Component\Form\FormBuilderInterface $builder |
149
|
|
|
* |
150
|
|
|
* @return \SprykerEco\Yves\Payone\Form\DirectDebitSubForm |
151
|
|
|
*/ |
152
|
|
|
protected function addBankCode(FormBuilderInterface $builder) |
153
|
|
|
{ |
154
|
|
|
$builder->add( |
155
|
|
|
static::FIELD_BANK_CODE, |
156
|
|
|
TextType::class, |
157
|
|
|
[ |
158
|
|
|
'label' => false, |
159
|
|
|
'required' => true, |
160
|
|
|
'constraints' => [ |
161
|
|
|
], |
162
|
|
|
] |
163
|
|
|
); |
164
|
|
|
|
165
|
|
|
return $this; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @param \Symfony\Component\Form\FormBuilderInterface $builder |
170
|
|
|
* @param array $options |
171
|
|
|
* |
172
|
|
|
* @return \SprykerEco\Yves\Payone\Form\DirectDebitSubForm |
173
|
|
|
*/ |
174
|
|
|
protected function addBankCountry(FormBuilderInterface $builder, array $options) |
175
|
|
|
{ |
176
|
|
|
if (count($options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_COUNTRIES]) == 1) { |
177
|
|
|
$builder->add( |
178
|
|
|
static::FIELD_BANK_COUNTRY, |
179
|
|
|
HiddenType::class, |
180
|
|
|
[ |
181
|
|
|
'label' => false, |
182
|
|
|
'data' => array_keys($options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_COUNTRIES])[0], |
183
|
|
|
] |
184
|
|
|
); |
185
|
|
|
} else { |
186
|
|
|
$builder->add( |
187
|
|
|
static::FIELD_BANK_COUNTRY, |
188
|
|
|
ChoiceType::class, |
189
|
|
|
[ |
190
|
|
|
'label' => false, |
191
|
|
|
'required' => true, |
192
|
|
|
'expanded' => false, |
193
|
|
|
'multiple' => false, |
194
|
|
|
'placeholder' => false, |
195
|
|
|
'choices' => $options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_COUNTRIES], |
196
|
|
|
'constraints' => [ |
197
|
|
|
], |
198
|
|
|
] |
199
|
|
|
); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $this; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param \Symfony\Component\Form\FormBuilderInterface $builder |
207
|
|
|
* |
208
|
|
|
* @return \SprykerEco\Yves\Payone\Form\DirectDebitSubForm |
209
|
|
|
*/ |
210
|
|
View Code Duplication |
protected function addIBAN(FormBuilderInterface $builder) |
|
|
|
|
211
|
|
|
{ |
212
|
|
|
$builder->add( |
213
|
|
|
self::FIELD_IBAN, |
214
|
|
|
TextType::class, |
215
|
|
|
[ |
216
|
|
|
'label' => false, |
217
|
|
|
'required' => true, |
218
|
|
|
'constraints' => [ |
219
|
|
|
], |
220
|
|
|
] |
221
|
|
|
); |
222
|
|
|
|
223
|
|
|
return $this; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @param \Symfony\Component\Form\FormBuilderInterface $builder |
228
|
|
|
* |
229
|
|
|
* @return \SprykerEco\Yves\Payone\Form\DirectDebitSubForm |
230
|
|
|
*/ |
231
|
|
|
protected function addBIC(FormBuilderInterface $builder) |
232
|
|
|
{ |
233
|
|
|
$builder->add( |
234
|
|
|
self::FIELD_BIC, |
235
|
|
|
TextType::class, |
236
|
|
|
[ |
237
|
|
|
'label' => false, |
238
|
|
|
'required' => true, |
239
|
|
|
'constraints' => [ |
240
|
|
|
$this->createManageMandateConstraint(), |
241
|
|
|
], |
242
|
|
|
] |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
return $this; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @return \Symfony\Component\Validator\Constraint |
250
|
|
|
*/ |
251
|
|
|
protected function createNotBlankConstraint() |
252
|
|
|
{ |
253
|
|
|
return new NotBlank(['groups' => $this->getPropertyPath()]); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* @return \Symfony\Component\Validator\Constraint |
258
|
|
|
*/ |
259
|
|
|
protected function createManageMandateConstraint() |
260
|
|
|
{ |
261
|
|
|
return new ManageMandate(['payoneClient' => $this->getClient(), 'groups' => $this->getPropertyPath()]); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.