Passed
Pull Request — master (#74)
by oleksandr
04:34
created

AbstractOnlineTransferSubForm::addBankCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 14
rs 10
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Generated\Shared\Transfer\PayoneBankAccountCheckTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ankAccountCheckTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Generated\Shared\Transfer\PayonePaymentOnlinetransferTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...tOnlinetransferTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Spryker\Yves\StepEngine\Dependency\Form\SubFormInterface;
14
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
15
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
16
use Symfony\Component\Form\Extension\Core\Type\TextType;
17
use Symfony\Component\Form\FormBuilderInterface;
18
use Symfony\Component\OptionsResolver\OptionsResolver;
19
use Symfony\Component\Validator\Context\ExecutionContextInterface;
20
21
abstract class AbstractOnlineTransferSubForm extends AbstractPayoneSubForm
22
{
23
    /**
24
     * @var string
25
     */
26
    public const PAYMENT_METHOD = 'online_transfer';
27
28
    /**
29
     * @var string
30
     */
31
    public const FIELD_IBAN = 'iban';
32
33
    /**
34
     * @var string
35
     */
36
    public const FIELD_BIC = 'bic';
37
38
    /**
39
     * @var string
40
     */
41
    public const FIELD_BANK_COUNTRY = 'bankcountry';
42
43
    /**
44
     * @var string
45
     */
46
    public const FIELD_BANK_ACCOUNT = 'bankaccount';
47
48
    /**
49
     * @var string
50
     */
51
    public const FIELD_BANK_CODE = 'bankcode';
52
53
    /**
54
     * @var string
55
     */
56
    public const FIELD_BANK_BRANCH_CODE = 'bankbranchcode';
57
58
    /**
59
     * @var string
60
     */
61
    public const FIELD_BANK_CHECK_DIGIT = 'bankcheckdigit';
62
63
    /**
64
     * @var string
65
     */
66
    public const FIELD_ONLINE_BANK_TRANSFER_TYPE = 'onlinebanktransfertype';
67
68
    /**
69
     * @var string
70
     */
71
    public const FIELD_BANK_GROUP_TYPE = 'bankgrouptype';
72
73
    /**
74
     * @var string
75
     */
76
    public const OPTION_ONLINE_BANK_TRANSFER_TYPES = 'online bank transfer types';
77
78
    /**
79
     * @var string
80
     */
81
    public const OPTION_BANK_COUNTRIES = '';
82
83
    /**
84
     * @var string
85
     */
86
    protected const BANK_ACCOUNT_UNKNOWN_ERROR_MESSAGE = 'payone.bank_account.unknown_error';
87
88
    /**
89
     * @return string
90
     */
91
    public function getName(): string
92
    {
93
        return PaymentTransfer::PAYONE_ONLINE_TRANSFER;
94
    }
95
96
    /**
97
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
98
     *
99
     * @return void
100
     */
101
    public function configureOptions(OptionsResolver $resolver): void
102
    {
103
        $resolver->setDefaults([
104
            'data_class' => PayonePaymentOnlinetransferTransfer::class,
105
        ])->setRequired(SubFormInterface::OPTIONS_FIELD_NAME);
106
    }
107
108
    /**
109
     * @param \Symfony\Component\OptionsResolver\OptionsResolver $resolver
110
     *
111
     * @return void
112
     */
113
    public function setDefaultOptions(OptionsResolver $resolver): void
114
    {
115
        $this->configureOptions($resolver);
116
    }
117
118
    /**
119
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
120
     * @param array $options
121
     *
122
     * @return void
123
     */
124
    public function buildForm(FormBuilderInterface $builder, array $options): void
125
    {
126
        $this->addOnlineBankTransferType($builder, $options)
127
            ->addBankCountry($builder, $options);
128
    }
129
130
    /**
131
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
132
     *
133
     * @return $this
134
     */
135
    protected function addBankAccount(FormBuilderInterface $builder)
136
    {
137
        $builder->add(
138
            static::FIELD_BANK_ACCOUNT,
139
            TextType::class,
140
            [
141
                'label' => false,
142
                'required' => true,
143
                'constraints' => [
144
                ],
145
            ],
146
        );
147
148
        return $this;
149
    }
150
151
    /**
152
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
153
     *
154
     * @return $this
155
     */
156
    protected function addBankCode(FormBuilderInterface $builder)
157
    {
158
        $builder->add(
159
            static::FIELD_BANK_CODE,
160
            TextType::class,
161
            [
162
                'label' => false,
163
                'required' => true,
164
                'constraints' => [
165
                ],
166
            ],
167
        );
168
169
        return $this;
170
    }
171
172
    /**
173
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
174
     *
175
     * @return $this
176
     */
177
    protected function addBankBranchCode(FormBuilderInterface $builder)
178
    {
179
        $builder->add(
180
            static::FIELD_BANK_BRANCH_CODE,
181
            TextType::class,
182
            [
183
                'label' => false,
184
                'required' => true,
185
                'constraints' => [
186
                ],
187
            ],
188
        );
189
190
        return $this;
191
    }
192
193
    /**
194
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
195
     *
196
     * @return $this
197
     */
198
    protected function addBankCheckDigit(FormBuilderInterface $builder)
199
    {
200
        $builder->add(
201
            static::FIELD_BANK_CHECK_DIGIT,
202
            TextType::class,
203
            [
204
                'label' => false,
205
                'required' => true,
206
                'constraints' => [
207
                ],
208
            ],
209
        );
210
211
        return $this;
212
    }
213
214
    /**
215
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
216
     *
217
     * @return $this
218
     */
219
    protected function addIBAN(FormBuilderInterface $builder)
220
    {
221
        $builder->add(
222
            static::FIELD_IBAN,
223
            TextType::class,
224
            [
225
                'label' => false,
226
                'required' => true,
227
                'constraints' => [
228
                ],
229
            ],
230
        );
231
232
        return $this;
233
    }
234
235
    /**
236
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
237
     * @param array $options
238
     *
239
     * @return $this
240
     */
241
    protected function addBankCountry(FormBuilderInterface $builder, array $options)
242
    {
243
        if (count($options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_COUNTRIES]) == 1) {
244
            $builder->add(
245
                static::FIELD_BANK_COUNTRY,
246
                HiddenType::class,
247
                [
248
                    'label' => false,
249
                    'data' => array_keys($options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_COUNTRIES])[0],
250
                ],
251
            );
252
        } else {
253
            $builder->add(
254
                static::FIELD_BANK_COUNTRY,
255
                ChoiceType::class,
256
                [
257
                    'label' => false,
258
                    'required' => true,
259
                    'expanded' => false,
260
                    'multiple' => false,
261
                    'placeholder' => false,
262
                    'choices' => $options[static::OPTIONS_FIELD_NAME][static::OPTION_BANK_COUNTRIES],
263
                    'constraints' => [
264
                    ],
265
                ],
266
            );
267
        }
268
269
        return $this;
270
    }
271
272
    /**
273
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
274
     *
275
     * @return $this
276
     */
277
    protected function addBIC(FormBuilderInterface $builder)
278
    {
279
        $builder->add(
280
            static::FIELD_BIC,
281
            TextType::class,
282
            [
283
                'label' => false,
284
                'required' => true,
285
                'constraints' => [
286
                ],
287
            ],
288
        );
289
290
        return $this;
291
    }
292
293
    /**
294
     * @param \Generated\Shared\Transfer\PayonePaymentOnlinetransferTransfer $data
295
     * @param \Symfony\Component\Validator\Context\ExecutionContextInterface $context
296
     *
297
     * @return void
298
     */
299
    public function checkBankAccount(PayonePaymentOnlinetransferTransfer $data, ExecutionContextInterface $context): void
300
    {
301
        $quoteTransfer = $context->getRoot()->getData();
302
        if ($quoteTransfer->getPayment()->getPaymentSelection() != $this->getPropertyPath()) {
303
            return;
304
        }
305
306
        $bankAccountCheckTransfer = new PayoneBankAccountCheckTransfer();
307
        $bankAccountCheckTransfer->setBankCountry($data->getBankcountry());
308
        $bankAccountCheckTransfer->setBankAccount($data->getBankaccount());
309
        $bankAccountCheckTransfer->setBankCode($data->getBankcode());
310
        $bankAccountCheckTransfer->setBankBranchCode($data->getBankbranchcode());
311
        $bankAccountCheckTransfer->setBankCheckDigit($data->getBankcheckdigit());
312
        $bankAccountCheckTransfer->setIban($data->getIban());
313
        $bankAccountCheckTransfer->setBic($data->getBic());
314
315
        $response = $this->getClient()->bankAccountCheck($bankAccountCheckTransfer);
316
        if ($response->getStatus() == 'ERROR' || $response->getStatus() == 'INVALID') {
317
            $context->addViolation($response->getCustomerErrorMessage() ?? static::BANK_ACCOUNT_UNKNOWN_ERROR_MESSAGE);
318
        }
319
    }
320
321
    /**
322
     * @param \Symfony\Component\Form\FormBuilderInterface $builder
323
     * @param array $options
324
     *
325
     * @return $this
326
     */
327
    abstract public function addOnlineBankTransferType(FormBuilderInterface $builder, array $options);
328
}
329