Passed
Pull Request — master (#23)
by Volodymyr
08:09
created

PaymentManager::getDeliveryCosts()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 3
nc 3
nop 1
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\Zed\Payone\Business\Payment;
9
10
use ArrayObject;
11
use Generated\Shared\Transfer\CaptureResponseTransfer;
12
use Generated\Shared\Transfer\CheckoutErrorTransfer;
13
use Generated\Shared\Transfer\CheckoutResponseTransfer;
14
use Generated\Shared\Transfer\OrderTransfer;
15
use Generated\Shared\Transfer\PaymentDetailTransfer;
16
use Generated\Shared\Transfer\PayoneBankAccountCheckTransfer;
17
use Generated\Shared\Transfer\PayoneCaptureTransfer;
18
use Generated\Shared\Transfer\PayoneCreditCardCheckRequestDataTransfer;
19
use Generated\Shared\Transfer\PayoneCreditCardTransfer;
20
use Generated\Shared\Transfer\PayoneGetFileTransfer;
21
use Generated\Shared\Transfer\PayoneGetInvoiceTransfer;
22
use Generated\Shared\Transfer\PayoneGetSecurityInvoiceTransfer;
23
use Generated\Shared\Transfer\PayoneInitPaypalExpressCheckoutRequestTransfer;
24
use Generated\Shared\Transfer\PayoneManageMandateTransfer;
25
use Generated\Shared\Transfer\PayonePaymentLogCollectionTransfer;
26
use Generated\Shared\Transfer\PayonePaymentLogTransfer;
27
use Generated\Shared\Transfer\PayonePaymentTransfer;
28
use Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer;
29
use Generated\Shared\Transfer\PayoneRefundTransfer;
30
use Generated\Shared\Transfer\PayoneStandardParameterTransfer;
31
use Generated\Shared\Transfer\QuoteTransfer;
32
use Orm\Zed\Payone\Persistence\SpyPaymentPayone;
33
use Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog;
34
use Spryker\Shared\Shipment\ShipmentConstants;
35
use SprykerEco\Shared\Payone\Dependency\ModeDetectorInterface;
36
use SprykerEco\Shared\Payone\PayoneApiConstants;
37
use SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface;
38
use SprykerEco\Zed\Payone\Business\Api\Call\CreditCardCheck;
39
use SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer;
40
use SprykerEco\Zed\Payone\Business\Api\Request\Container\AuthorizationContainerInterface;
41
use SprykerEco\Zed\Payone\Business\Api\Request\Container\Capture\BusinessContainer;
42
use SprykerEco\Zed\Payone\Business\Api\Request\Container\CaptureContainer;
43
use SprykerEco\Zed\Payone\Business\Api\Request\Container\DebitContainer;
44
use SprykerEco\Zed\Payone\Business\Api\Request\Container\GenericPaymentContainer;
45
use SprykerEco\Zed\Payone\Business\Api\Request\Container\RefundContainer;
46
use SprykerEco\Zed\Payone\Business\Api\Response\Container\AbstractResponseContainer;
47
use SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer;
48
use SprykerEco\Zed\Payone\Business\Api\Response\Container\BankAccountCheckResponseContainer;
49
use SprykerEco\Zed\Payone\Business\Api\Response\Container\CaptureResponseContainer;
50
use SprykerEco\Zed\Payone\Business\Api\Response\Container\CreditCardCheckResponseContainer;
51
use SprykerEco\Zed\Payone\Business\Api\Response\Container\DebitResponseContainer;
52
use SprykerEco\Zed\Payone\Business\Api\Response\Container\GenericPaymentResponseContainer;
53
use SprykerEco\Zed\Payone\Business\Api\Response\Container\GetFileResponseContainer;
54
use SprykerEco\Zed\Payone\Business\Api\Response\Container\GetInvoiceResponseContainer;
55
use SprykerEco\Zed\Payone\Business\Api\Response\Container\GetSecurityInvoiceResponseContainer;
56
use SprykerEco\Zed\Payone\Business\Api\Response\Container\ManageMandateResponseContainer;
57
use SprykerEco\Zed\Payone\Business\Api\Response\Container\RefundResponseContainer;
58
use SprykerEco\Zed\Payone\Business\Api\Response\Mapper\AuthorizationResponseMapper;
59
use SprykerEco\Zed\Payone\Business\Api\Response\Mapper\CaptureResponseMapper;
60
use SprykerEco\Zed\Payone\Business\Api\Response\Mapper\CreditCardCheckResponseMapper;
61
use SprykerEco\Zed\Payone\Business\Api\Response\Mapper\DebitResponseMapper;
62
use SprykerEco\Zed\Payone\Business\Api\Response\Mapper\RefundResponseMapper;
63
use SprykerEco\Zed\Payone\Business\Exception\InvalidPaymentMethodException;
64
use SprykerEco\Zed\Payone\Business\Key\HashGenerator;
65
use SprykerEco\Zed\Payone\Business\Key\HmacGeneratorInterface;
66
use SprykerEco\Zed\Payone\Business\SequenceNumber\SequenceNumberProviderInterface;
67
use SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface;
68
69
class PaymentManager implements PaymentManagerInterface
70
{
71
    const LOG_TYPE_API_LOG = 'SpyPaymentPayoneApiLog';
72
    const LOG_TYPE_TRANSACTION_STATUS_LOG = 'SpyPaymentPayoneTransactionStatusLog';
73
    const ERROR_ACCESS_DENIED_MESSAGE = 'Access denied';
74
75
    /**
76
     * @var \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface
77
     */
78
    protected $executionAdapter;
79
80
    /**
81
     * @var \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface
82
     */
83
    protected $queryContainer;
84
85
    /**
86
     * @var \Generated\Shared\Transfer\PayoneStandardParameterTransfer
87
     */
88
    protected $standardParameter;
89
90
    /**
91
     * @var \SprykerEco\Zed\Payone\Business\SequenceNumber\SequenceNumberProviderInterface
92
     */
93
    protected $sequenceNumberProvider;
94
95
    /**
96
     * @var \SprykerEco\Shared\Payone\Dependency\ModeDetectorInterface
97
     */
98
    protected $modeDetector;
99
100
    /**
101
     * @var \SprykerEco\Zed\Payone\Business\Key\HmacGeneratorInterface
102
     */
103
    protected $hashGenerator;
104
105
    /**
106
     * @var \SprykerEco\Zed\Payone\Business\Key\UrlHmacGenerator
107
     */
108
    protected $urlHmacGenerator;
109
110
    /**
111
     * @var \SprykerEco\Zed\Payone\Business\Payment\PaymentMethodMapperInterface[]
112
     */
113
    protected $registeredMethodMappers;
114
115
    /**
116
     * @param \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface $executionAdapter
117
     * @param \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface $queryContainer
118
     * @param \Generated\Shared\Transfer\PayoneStandardParameterTransfer $standardParameter
119
     * @param \SprykerEco\Zed\Payone\Business\Key\HashGenerator $hashGenerator
120
     * @param \SprykerEco\Zed\Payone\Business\SequenceNumber\SequenceNumberProviderInterface $sequenceNumberProvider
121
     * @param \SprykerEco\Shared\Payone\Dependency\ModeDetectorInterface $modeDetector
122
     * @param \SprykerEco\Zed\Payone\Business\Key\HmacGeneratorInterface $urlHmacGenerator
123
     */
124
    public function __construct(
125
        AdapterInterface $executionAdapter,
126
        PayoneQueryContainerInterface $queryContainer,
127
        PayoneStandardParameterTransfer $standardParameter,
128
        HashGenerator $hashGenerator,
129
        SequenceNumberProviderInterface $sequenceNumberProvider,
130
        ModeDetectorInterface $modeDetector,
131
        HmacGeneratorInterface $urlHmacGenerator
132
    ) {
133
134
        $this->executionAdapter = $executionAdapter;
135
        $this->queryContainer = $queryContainer;
136
        $this->standardParameter = $standardParameter;
137
        $this->hashGenerator = $hashGenerator;
0 ignored issues
show
Documentation Bug introduced by
It seems like $hashGenerator of type object<SprykerEco\Zed\Pa...ness\Key\HashGenerator> is incompatible with the declared type object<SprykerEco\Zed\Pa...HmacGeneratorInterface> of property $hashGenerator.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
138
        $this->sequenceNumberProvider = $sequenceNumberProvider;
139
        $this->modeDetector = $modeDetector;
140
        $this->urlHmacGenerator = $urlHmacGenerator;
0 ignored issues
show
Documentation Bug introduced by
$urlHmacGenerator is of type object<SprykerEco\Zed\Pa...HmacGeneratorInterface>, but the property $urlHmacGenerator was declared to be of type object<SprykerEco\Zed\Pa...s\Key\UrlHmacGenerator>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
141
    }
142
143
    /**
144
     * @param \SprykerEco\Zed\Payone\Business\Payment\PaymentMethodMapperInterface $paymentMethodMapper
145
     *
146
     * @return void
147
     */
148
    public function registerPaymentMethodMapper(PaymentMethodMapperInterface $paymentMethodMapper)
149
    {
150
        $paymentMethodMapper->setStandardParameter($this->standardParameter);
151
        $paymentMethodMapper->setSequenceNumberProvider($this->sequenceNumberProvider);
152
        $paymentMethodMapper->setUrlHmacGenerator($this->urlHmacGenerator);
153
        $this->registeredMethodMappers[$paymentMethodMapper->getName()] = $paymentMethodMapper;
154
    }
155
156
    /**
157
     * @param string $name
158
     *
159
     * @return \SprykerEco\Zed\Payone\Business\Payment\PaymentMethodMapperInterface|null
160
     */
161
    protected function findPaymentMethodMapperByName($name)
162
    {
163
        if (array_key_exists($name, $this->registeredMethodMappers)) {
164
            return $this->registeredMethodMappers[$name];
165
        }
166
167
        return null;
168
    }
169
170
    /**
171
     * @param string $paymentMethodName
172
     *
173
     * @throws \SprykerEco\Zed\Payone\Business\Exception\InvalidPaymentMethodException
174
     *
175
     * @return \SprykerEco\Zed\Payone\Business\Payment\PaymentMethodMapperInterface
176
     */
177
    protected function getRegisteredPaymentMethodMapper($paymentMethodName)
178
    {
179
        $paymentMethodMapper = $this->findPaymentMethodMapperByName($paymentMethodName);
180
        if ($paymentMethodMapper === null) {
181
            throw new InvalidPaymentMethodException(
182
                sprintf('No registered payment method mapper found for given method name %s', $paymentMethodName)
183
            );
184
        }
185
186
        return $paymentMethodMapper;
187
    }
188
189
    /**
190
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
191
     *
192
     * @return \Generated\Shared\Transfer\AuthorizationResponseTransfer
193
     */
194
    public function authorizePayment(OrderTransfer $orderTransfer)
195
    {
196
        $paymentEntity = $this->getPaymentEntity($orderTransfer->getIdSalesOrder());
197
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
198
        $requestContainer = $paymentMethodMapper->mapPaymentToAuthorization($paymentEntity, $orderTransfer);
199
        $requestContainer = $this->prepareOrderItems($orderTransfer, $requestContainer);
200
        $responseContainer = $this->performAuthorizationRequest($paymentEntity, $requestContainer);
0 ignored issues
show
Documentation introduced by
$requestContainer is of type object<SprykerEco\Zed\Pa...stractRequestContainer>, but the function expects a object<SprykerEco\Zed\Pa...tionContainerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
201
202
        $responseMapper = new AuthorizationResponseMapper();
203
        $responseTransfer = $responseMapper->getAuthorizationResponseTransfer($responseContainer);
204
205
        return $responseTransfer;
206
    }
207
208
    /**
209
     * @param int $idSalesOrder
210
     *
211
     * @return \Generated\Shared\Transfer\AuthorizationResponseTransfer
212
     */
213
    public function preAuthorizePayment($idSalesOrder)
214
    {
215
        $paymentEntity = $this->getPaymentEntity($idSalesOrder);
216
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
217
        $requestContainer = $paymentMethodMapper->mapPaymentToPreAuthorization($paymentEntity);
218
        $responseContainer = $this->performAuthorizationRequest($paymentEntity, $requestContainer);
219
220
        $responseMapper = new AuthorizationResponseMapper();
221
        $responseTransfer = $responseMapper->getAuthorizationResponseTransfer($responseContainer);
222
223
        return $responseTransfer;
224
    }
225
226
    /**
227
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
228
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AuthorizationContainerInterface $requestContainer
229
     *
230
     * @return \SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer
231
     */
232
    protected function performAuthorizationRequest(SpyPaymentPayone $paymentEntity, AuthorizationContainerInterface $requestContainer)
233
    {
234
        $this->setStandardParameter($requestContainer);
0 ignored issues
show
Documentation introduced by
$requestContainer is of type object<SprykerEco\Zed\Pa...tionContainerInterface>, but the function expects a object<SprykerEco\Zed\Pa...stractRequestContainer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
235
236
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
0 ignored issues
show
Documentation introduced by
$requestContainer is of type object<SprykerEco\Zed\Pa...tionContainerInterface>, but the function expects a object<SprykerEco\Zed\Pa...stractRequestContainer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
237
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
0 ignored issues
show
Documentation introduced by
$requestContainer is of type object<SprykerEco\Zed\Pa...tionContainerInterface>, but the function expects a object<SprykerEco\Zed\Pa...stractRequestContainer>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
238
        $responseContainer = new AuthorizationResponseContainer($rawResponse);
239
        $this->updatePaymentAfterAuthorization($paymentEntity, $responseContainer);
240
        $this->updateApiLogAfterAuthorization($apiLogEntity, $responseContainer);
241
        $this->updatePaymentDetailAfterAuthorization($paymentEntity, $responseContainer);
242
243
        return $responseContainer;
244
    }
245
246
    /**
247
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
248
     *
249
     * @return \SprykerEco\Zed\Payone\Business\Payment\PaymentMethodMapperInterface
250
     */
251
    protected function getPaymentMethodMapper(SpyPaymentPayone $paymentEntity)
252
    {
253
        return $this->getRegisteredPaymentMethodMapper($paymentEntity->getPaymentMethod());
254
    }
255
256
    /**
257
     * @param int $orderId
258
     *
259
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayone
260
     */
261
    protected function getPaymentEntity($orderId)
262
    {
263
        return $this->queryContainer->createPaymentById($orderId)->findOne();
264
    }
265
266
    /**
267
     * @param \Generated\Shared\Transfer\PayoneCaptureTransfer $captureTransfer
268
     *
269
     * @return \Generated\Shared\Transfer\CaptureResponseTransfer
270
     */
271
    public function capturePayment(PayoneCaptureTransfer $captureTransfer): CaptureResponseTransfer
272
    {
273
        $paymentEntity = $this->getPaymentEntity($captureTransfer->getPayment()->getFkSalesOrder());
274
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
275
276
        $requestContainer = $paymentMethodMapper->mapPaymentToCapture($paymentEntity);
277
        $requestContainer = $this->prepareOrderItems($captureTransfer->getOrder(), $requestContainer);
278
279
        if (!empty($captureTransfer->getSettleaccount())) {
280
            $businnessContainer = new BusinessContainer();
281
            $businnessContainer->setSettleAccount($captureTransfer->getSettleaccount());
282
            $requestContainer->setBusiness($businnessContainer);
283
        }
284
285
        $this->setStandardParameter($requestContainer);
286
287
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
288
289
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
290
        $responseContainer = new CaptureResponseContainer($rawResponse);
291
292
        $this->updateApiLogAfterCapture($apiLogEntity, $responseContainer);
293
294
        $responseMapper = new CaptureResponseMapper();
295
        $responseTransfer = $responseMapper->getCaptureResponseTransfer($responseContainer);
296
297
        return $responseTransfer;
298
    }
299
300
    /**
301
     * @param int $idPayment
302
     *
303
     * @return \Generated\Shared\Transfer\DebitResponseTransfer
304
     */
305
    public function debitPayment($idPayment)
306
    {
307
        $paymentEntity = $this->getPaymentEntity($idPayment);
308
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
309
310
        $requestContainer = $paymentMethodMapper->mapPaymentToDebit($paymentEntity);
311
        $this->setStandardParameter($requestContainer);
312
313
        $paymentEntity = $this->findPaymentByTransactionId($paymentEntity->getTransactionId());
314
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
315
316
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
317
        $responseContainer = new DebitResponseContainer($rawResponse);
318
319
        $this->updateApiLogAfterDebit($apiLogEntity, $responseContainer);
320
321
        $responseMapper = new DebitResponseMapper();
322
        $responseTransfer = $responseMapper->getDebitResponseTransfer($responseContainer);
323
324
        return $responseTransfer;
325
    }
326
327
    /**
328
     * @param \Generated\Shared\Transfer\PayoneCreditCardTransfer $creditCardData
329
     *
330
     * @return \Generated\Shared\Transfer\CreditCardCheckResponseTransfer
331
     */
332
    public function creditCardCheck(PayoneCreditCardTransfer $creditCardData)
333
    {
334
        /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\CreditCardPseudo $paymentMethodMapper */
335
        $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper($creditCardData->getPayment()->getPaymentMethod());
336
        $requestContainer = $paymentMethodMapper->mapCreditCardCheck($creditCardData);
337
338
        $this->setStandardParameter($requestContainer);
339
340
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
341
        $responseContainer = new CreditCardCheckResponseContainer($rawResponse);
342
343
        $responseMapper = new CreditCardCheckResponseMapper();
344
        $responseTransfer = $responseMapper->getCreditCardCheckResponseTransfer($responseContainer);
345
346
        return $responseTransfer;
347
    }
348
349
    /**
350
     * @param \Generated\Shared\Transfer\PayoneBankAccountCheckTransfer $bankAccountCheckTransfer
351
     *
352
     * @return \Generated\Shared\Transfer\PayoneBankAccountCheckTransfer
353
     */
354
    public function bankAccountCheck(PayoneBankAccountCheckTransfer $bankAccountCheckTransfer)
355
    {
356
        /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\DirectDebit $paymentMethodMapper */
357
        $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_ONLINE_BANK_TRANSFER);
358
        $requestContainer = $paymentMethodMapper->mapBankAccountCheck($bankAccountCheckTransfer);
359
        $this->setStandardParameter($requestContainer);
360
361
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
362
        $responseContainer = new BankAccountCheckResponseContainer($rawResponse);
363
364
        $bankAccountCheckTransfer->setErrorCode($responseContainer->getErrorcode());
365
        $bankAccountCheckTransfer->setCustomerErrorMessage($responseContainer->getCustomermessage());
366
        $bankAccountCheckTransfer->setStatus($responseContainer->getStatus());
367
        $bankAccountCheckTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
368
369
        return $bankAccountCheckTransfer;
370
    }
371
372
    /**
373
     * @param \Generated\Shared\Transfer\PayoneManageMandateTransfer $manageMandateTransfer
374
     *
375
     * @return \Generated\Shared\Transfer\PayoneManageMandateTransfer
376
     */
377
    public function manageMandate(PayoneManageMandateTransfer $manageMandateTransfer)
378
    {
379
        /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\DirectDebit $paymentMethodMapper */
380
        $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_DIRECT_DEBIT);
381
        $requestContainer = $paymentMethodMapper->mapManageMandate($manageMandateTransfer);
382
        $this->setStandardParameter($requestContainer);
383
384
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
385
        $responseContainer = new ManageMandateResponseContainer($rawResponse);
386
387
        $manageMandateTransfer->setErrorCode($responseContainer->getErrorcode());
388
        $manageMandateTransfer->setCustomerErrorMessage($responseContainer->getCustomermessage());
389
        $manageMandateTransfer->setStatus($responseContainer->getStatus());
390
        $manageMandateTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
391
        $manageMandateTransfer->setMandateIdentification($responseContainer->getMandateIdentification());
392
        $manageMandateTransfer->setMandateText($responseContainer->getMandateText());
393
        $manageMandateTransfer->setIban($responseContainer->getIban());
394
        $manageMandateTransfer->setBic($responseContainer->getBic());
395
396
        return $manageMandateTransfer;
397
    }
398
399
    /**
400
     * @param \Generated\Shared\Transfer\PayoneGetFileTransfer $getFileTransfer
401
     *
402
     * @return \Generated\Shared\Transfer\PayoneGetFileTransfer
403
     */
404 View Code Duplication
    public function getFile(PayoneGetFileTransfer $getFileTransfer)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
405
    {
406
        $responseContainer = new GetFileResponseContainer();
407
        $paymentEntity = $this->findPaymentByFileReferenceAndCustomerId(
408
            $getFileTransfer->getReference(),
409
            $getFileTransfer->getCustomerId()
410
        );
411
412
        if ($paymentEntity) {
413
            /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\DirectDebit $paymentMethodMapper */
414
            $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_DIRECT_DEBIT);
415
            $requestContainer = $paymentMethodMapper->mapGetFile($getFileTransfer);
416
            $this->setStandardParameter($requestContainer);
417
            $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
418
            $responseContainer->init($rawResponse);
419
        } else {
420
            $this->setAccessDeniedError($responseContainer);
421
        }
422
423
        $getFileTransfer->setRawResponse($responseContainer->getRawResponse());
424
        $getFileTransfer->setStatus($responseContainer->getStatus());
425
        $getFileTransfer->setErrorCode($responseContainer->getErrorcode());
426
        $getFileTransfer->setCustomerErrorMessage($responseContainer->getCustomermessage());
427
        $getFileTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
428
429
        return $getFileTransfer;
430
    }
431
432
    /**
433
     * @param \Generated\Shared\Transfer\PayoneGetInvoiceTransfer $getInvoiceTransfer
434
     *
435
     * @return \Generated\Shared\Transfer\PayoneGetInvoiceTransfer
436
     */
437 View Code Duplication
    public function getInvoice(PayoneGetInvoiceTransfer $getInvoiceTransfer)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
438
    {
439
        $responseContainer = new GetInvoiceResponseContainer();
440
        $paymentEntity = $this->findPaymentByInvoiceTitleAndCustomerId(
441
            $getInvoiceTransfer->getReference(),
442
            $getInvoiceTransfer->getCustomerId()
443
        );
444
445
        if ($paymentEntity) {
446
            /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\Invoice $paymentMethodMapper */
447
            $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_INVOICE);
448
            $requestContainer = $paymentMethodMapper->mapGetInvoice($getInvoiceTransfer);
449
            $this->setStandardParameter($requestContainer);
450
            $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
451
            $responseContainer->init($rawResponse);
452
        } else {
453
            $this->setAccessDeniedError($responseContainer);
454
        }
455
456
        $getInvoiceTransfer->setRawResponse($responseContainer->getRawResponse());
457
        $getInvoiceTransfer->setStatus($responseContainer->getStatus());
458
        $getInvoiceTransfer->setErrorCode($responseContainer->getErrorcode());
459
        $getInvoiceTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
460
461
        return $getInvoiceTransfer;
462
    }
463
464
    /**
465
     * @param \Generated\Shared\Transfer\PayoneGetSecurityInvoiceTransfer $getSecurityInvoiceTransfer
466
     *
467
     * @return \Generated\Shared\Transfer\PayoneGetSecurityInvoiceTransfer
468
     */
469 View Code Duplication
    public function getSecurityInvoice(PayoneGetSecurityInvoiceTransfer $getSecurityInvoiceTransfer): PayoneGetSecurityInvoiceTransfer
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
470
    {
471
        $responseContainer = new GetSecurityInvoiceResponseContainer();
472
        $paymentEntity = $this->findPaymentByInvoiceTitleAndCustomerId(
473
            $getSecurityInvoiceTransfer->getReference(),
474
            $getSecurityInvoiceTransfer->getCustomerId()
475
        );
476
477
        if ($paymentEntity) {
478
            /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\SecurityInvoice $paymentMethodMapper */
479
            $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_SECURITY_INVOICE);
480
            $requestContainer = $paymentMethodMapper->mapGetSecurityInvoice($getSecurityInvoiceTransfer);
481
            $this->setStandardParameter($requestContainer);
0 ignored issues
show
Compatibility introduced by
$requestContainer of type object<SprykerEco\Zed\Pa...ner\ContainerInterface> is not a sub-type of object<SprykerEco\Zed\Pa...stractRequestContainer>. It seems like you assume a concrete implementation of the interface SprykerEco\Zed\Payone\Bu...iner\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
482
            $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
0 ignored issues
show
Compatibility introduced by
$requestContainer of type object<SprykerEco\Zed\Pa...ner\ContainerInterface> is not a sub-type of object<SprykerEco\Zed\Pa...stractRequestContainer>. It seems like you assume a concrete implementation of the interface SprykerEco\Zed\Payone\Bu...iner\ContainerInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
483
            $responseContainer->init($rawResponse);
484
        } else {
485
            $this->setAccessDeniedError($responseContainer);
486
        }
487
488
        $getSecurityInvoiceTransfer->setRawResponse($responseContainer->getRawResponse());
489
        $getSecurityInvoiceTransfer->setStatus($responseContainer->getStatus());
490
        $getSecurityInvoiceTransfer->setErrorCode($responseContainer->getErrorcode());
491
        $getSecurityInvoiceTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
492
493
        return $getSecurityInvoiceTransfer;
494
    }
495
496
    /**
497
     * @param int $transactionId
498
     *
499
     * @return \SprykerEco\Zed\Payone\Business\Api\Response\Container\GetInvoiceResponseContainer
500
     */
501
    public function getInvoiceTitle($transactionId)
502
    {
503
        return implode('-', [
504
            PayoneApiConstants::INVOICE_TITLE_PREFIX_INVOICE,
505
            $transactionId,
506
            0,
507
        ]);
508
    }
509
510
    /**
511
     * @param \Generated\Shared\Transfer\PayoneRefundTransfer $refundTransfer
512
     *
513
     * @return \Generated\Shared\Transfer\RefundResponseTransfer
514
     */
515
    public function refundPayment(PayoneRefundTransfer $refundTransfer)
516
    {
517
        $payonePaymentTransfer = $refundTransfer->getPayment();
518
519
        $paymentEntity = $this->getPaymentEntity($payonePaymentTransfer->getFkSalesOrder());
520
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
521
        $requestContainer = $paymentMethodMapper->mapPaymentToRefund($paymentEntity);
522
        $requestContainer = $this->prepareOrderItems($refundTransfer->getOrder(), $requestContainer);
523
524
        $this->setStandardParameter($requestContainer);
525
526
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
527
528
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
529
        $responseContainer = new RefundResponseContainer($rawResponse);
530
531
        $this->updateApiLogAfterRefund($apiLogEntity, $responseContainer);
532
533
        $responseMapper = new RefundResponseMapper();
534
        $responseTransfer = $responseMapper->getRefundResponseTransfer($responseContainer);
535
536
        return $responseTransfer;
537
    }
538
539
    /**
540
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
541
     *
542
     * @return \Generated\Shared\Transfer\PayonePaymentTransfer
543
     */
544
    protected function getPayment(OrderTransfer $orderTransfer)
545
    {
546
        $payment = $this->queryContainer->createPaymentByOrderId($orderTransfer->getIdSalesOrder())->findOne();
547
        $paymentDetail = $payment->getSpyPaymentPayoneDetail();
548
549
        $paymentDetailTransfer = new PaymentDetailTransfer();
550
        $paymentDetailTransfer->fromArray($paymentDetail->toArray(), true);
551
552
        $paymentTransfer = new PayonePaymentTransfer();
553
        $paymentTransfer->fromArray($payment->toArray(), true);
554
        $paymentTransfer->setPaymentDetail($paymentDetailTransfer);
555
556
        return $paymentTransfer;
557
    }
558
559
    /**
560
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
561
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer $responseContainer
562
     *
563
     * @return void
564
     */
565
    protected function updatePaymentAfterAuthorization(SpyPaymentPayone $paymentEntity, AuthorizationResponseContainer $responseContainer)
566
    {
567
        $paymentEntity->setTransactionId($responseContainer->getTxid());
568
        $paymentEntity->save();
569
    }
570
571
    /**
572
     * @param string $transactionId
573
     *
574
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayone
575
     */
576
    protected function findPaymentByTransactionId($transactionId)
577
    {
578
        return $this->queryContainer->createPaymentByTransactionIdQuery($transactionId)->findOne();
579
    }
580
581
    /**
582
     * @param string $invoiceTitle
583
     * @param int $customerId
584
     *
585
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneQuery
586
     */
587
    protected function findPaymentByInvoiceTitleAndCustomerId($invoiceTitle, $customerId)
588
    {
589
        return $this->queryContainer->createPaymentByInvoiceTitleAndCustomerIdQuery($invoiceTitle, $customerId)->findOne();
590
    }
591
592
    /**
593
     * @param string $fileReference
594
     * @param int $customerId
595
     *
596
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneQuery
597
     */
598
    protected function findPaymentByFileReferenceAndCustomerId($fileReference, $customerId)
599
    {
600
        return $this->queryContainer->createPaymentByFileReferenceAndCustomerIdQuery($fileReference, $customerId)->findOne();
601
    }
602
603
    /**
604
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
605
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer $container
606
     *
607
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog
608
     */
609
    protected function initializeApiLog(SpyPaymentPayone $paymentEntity, AbstractRequestContainer $container)
610
    {
611
        $entity = new SpyPaymentPayoneApiLog();
612
        $entity->setSpyPaymentPayone($paymentEntity);
613
        $entity->setRequest($container->getRequest());
614
        $entity->setMode($container->getMode());
615
        $entity->setMerchantId($container->getMid());
616
        $entity->setPortalId($container->getPortalid());
617
        if ($container instanceof CaptureContainer || $container instanceof RefundContainer || $container instanceof DebitContainer) {
618
            $entity->setSequenceNumber($container->getSequenceNumber());
619
        }
620
        // Logging request data for debug
621
        $entity->setRawRequest(json_encode($container->toArray()));
622
        $entity->save();
623
624
        return $entity;
625
    }
626
627
    /**
628
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
629
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer $responseContainer
630
     *
631
     * @return void
632
     */
633
    protected function updateApiLogAfterAuthorization(SpyPaymentPayoneApiLog $apiLogEntity, AuthorizationResponseContainer $responseContainer)
634
    {
635
        $apiLogEntity->setStatus($responseContainer->getStatus());
636
        $apiLogEntity->setUserId($responseContainer->getUserid());
637
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
638
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
639
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
640
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
641
        $apiLogEntity->setRedirectUrl($responseContainer->getRedirecturl());
642
        $apiLogEntity->setSequenceNumber(0);
643
644
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
645
        $apiLogEntity->save();
646
    }
647
648
    /**
649
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
650
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\CaptureResponseContainer $responseContainer
651
     *
652
     * @return void
653
     */
654 View Code Duplication
    protected function updateApiLogAfterCapture(SpyPaymentPayoneApiLog $apiLogEntity, CaptureResponseContainer $responseContainer)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
655
    {
656
        $apiLogEntity->setStatus($responseContainer->getStatus());
657
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
658
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
659
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
660
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
661
662
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
663
        $apiLogEntity->save();
664
    }
665
666
    /**
667
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
668
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\DebitResponseContainer $responseContainer
669
     *
670
     * @return void
671
     */
672 View Code Duplication
    protected function updateApiLogAfterDebit(SpyPaymentPayoneApiLog $apiLogEntity, DebitResponseContainer $responseContainer)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
673
    {
674
        $apiLogEntity->setStatus($responseContainer->getStatus());
675
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
676
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
677
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
678
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
679
680
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
681
        $apiLogEntity->save();
682
    }
683
684
    /**
685
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
686
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\RefundResponseContainer $responseContainer
687
     *
688
     * @return void
689
     */
690 View Code Duplication
    protected function updateApiLogAfterRefund(SpyPaymentPayoneApiLog $apiLogEntity, RefundResponseContainer $responseContainer)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
691
    {
692
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
693
        $apiLogEntity->setStatus($responseContainer->getStatus());
694
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
695
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
696
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
697
698
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
699
        $apiLogEntity->save();
700
    }
701
702
    /**
703
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer $container
704
     *
705
     * @return void
706
     */
707
    protected function setStandardParameter(AbstractRequestContainer $container)
708
    {
709
        $container->setApiVersion(PayoneApiConstants::API_VERSION_3_9);
710
        $container->setEncoding($this->standardParameter->getEncoding());
711
        $container->setKey($this->hashGenerator->hash($this->standardParameter->getKey()));
0 ignored issues
show
Bug introduced by
The call to hash() misses a required argument $key.

This check looks for function calls that miss required arguments.

Loading history...
712
        $container->setMid($this->standardParameter->getMid());
713
        $container->setPortalid($this->standardParameter->getPortalId());
714
        $container->setMode($this->modeDetector->getMode());
715
        $container->setIntegratorName(PayoneApiConstants::INTEGRATOR_NAME_SPRYKER);
716
        $container->setIntegratorVersion(PayoneApiConstants::INTEGRATOR_VERSION_3_0_0);
717
        $container->setSolutionName(PayoneApiConstants::SOLUTION_NAME_SPRYKER);
718
        $container->setSolutionVersion(PayoneApiConstants::SOLUTION_VERSION_3_0_0);
719
    }
720
721
    /**
722
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\AbstractResponseContainer $container
723
     *
724
     * @return void
725
     */
726
    protected function setAccessDeniedError(AbstractResponseContainer $container)
727
    {
728
        $container->setStatus(PayoneApiConstants::RESPONSE_TYPE_ERROR);
729
        $container->setErrormessage(static::ERROR_ACCESS_DENIED_MESSAGE);
730
        $container->setCustomermessage(static::ERROR_ACCESS_DENIED_MESSAGE);
731
    }
732
733
    /**
734
     * @param int $idOrder
735
     *
736
     * @return \Generated\Shared\Transfer\PaymentDetailTransfer
737
     */
738 View Code Duplication
    public function getPaymentDetail($idOrder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
739
    {
740
        $paymentEntity = $this->queryContainer->createPaymentByOrderId($idOrder)->findOne();
741
        $paymentDetailEntity = $paymentEntity->getSpyPaymentPayoneDetail();
742
        $paymentDetailTransfer = new PaymentDetailTransfer();
743
        $paymentDetailTransfer->fromArray($paymentDetailEntity->toArray(), true);
744
745
        return $paymentDetailTransfer;
746
    }
747
748
    /**
749
     * Gets payment logs (both api and transaction status) for specific orders in chronological order.
750
     *
751
     * @param \Propel\Runtime\Collection\ObjectCollection|\ArrayObject $orders
752
     *
753
     * @return \Generated\Shared\Transfer\PayonePaymentLogCollectionTransfer
754
     */
755
    public function getPaymentLogs($orders)
756
    {
757
        $apiLogs = $this->queryContainer->createApiLogsByOrderIds($orders)->find()->getData();
0 ignored issues
show
Bug introduced by
It seems like $orders defined by parameter $orders on line 755 can also be of type object<ArrayObject>; however, SprykerEco\Zed\Payone\Pe...eateApiLogsByOrderIds() does only seem to accept object<Propel\Runtime\Co...ction\ObjectCollection>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
758
759
        $transactionStatusLogs = $this->queryContainer->createTransactionStatusLogsByOrderIds($orders)->find()->getData();
0 ignored issues
show
Bug introduced by
It seems like $orders defined by parameter $orders on line 755 can also be of type object<ArrayObject>; however, SprykerEco\Zed\Payone\Pe...nStatusLogsByOrderIds() does only seem to accept object<Propel\Runtime\Co...ction\ObjectCollection>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
760
761
        $logs = [];
762
        /** @var \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLog */
763 View Code Duplication
        foreach ($apiLogs as $apiLog) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
764
            $key = $apiLog->getCreatedAt()->format('Y-m-d\TH:i:s\Z') . 'a' . $apiLog->getIdPaymentPayoneApiLog();
765
            $payonePaymentLogTransfer = new PayonePaymentLogTransfer();
766
            $payonePaymentLogTransfer->fromArray($apiLog->toArray(), true);
767
            $payonePaymentLogTransfer->setLogType(self::LOG_TYPE_API_LOG);
768
            $logs[$key] = $payonePaymentLogTransfer;
769
        }
770
        /** @var \Orm\Zed\Payone\Persistence\SpyPaymentPayoneTransactionStatusLog $transactionStatusLog */
771 View Code Duplication
        foreach ($transactionStatusLogs as $transactionStatusLog) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
772
            $key = $transactionStatusLog->getCreatedAt()->format('Y-m-d\TH:i:s\Z') . 't' . $transactionStatusLog->getIdPaymentPayoneTransactionStatusLog();
773
            $payonePaymentLogTransfer = new PayonePaymentLogTransfer();
774
            $payonePaymentLogTransfer->fromArray($transactionStatusLog->toArray(), true);
775
            $payonePaymentLogTransfer->setLogType(self::LOG_TYPE_TRANSACTION_STATUS_LOG);
776
            $logs[$key] = $payonePaymentLogTransfer;
777
        }
778
779
        ksort($logs);
780
781
        $payonePaymentLogCollectionTransfer = new PayonePaymentLogCollectionTransfer();
782
783
        foreach ($logs as $log) {
784
            $payonePaymentLogCollectionTransfer->addPaymentLogs($log);
785
        }
786
787
        return $payonePaymentLogCollectionTransfer;
788
    }
789
790
    /**
791
     * @param \Generated\Shared\Transfer\PayoneCreditCardCheckRequestDataTransfer $creditCardCheckRequestDataTransfer
792
     *
793
     * @return array
794
     */
795
    public function getCreditCardCheckRequestData(PayoneCreditCardCheckRequestDataTransfer $creditCardCheckRequestDataTransfer)
796
    {
797
        $this->standardParameter->fromArray($creditCardCheckRequestDataTransfer->toArray(), true);
798
799
        $creditCardCheck = new CreditCardCheck($this->standardParameter, $this->hashGenerator, $this->modeDetector);
0 ignored issues
show
Documentation introduced by
$this->hashGenerator is of type object<SprykerEco\Zed\Pa...HmacGeneratorInterface>, but the function expects a object<SprykerEco\Zed\Pa...HashGeneratorInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
800
801
        $data = $creditCardCheck->mapCreditCardCheckData();
802
803
        return $data->toArray();
804
    }
805
806
    /**
807
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
808
     *
809
     * @return bool
810
     */
811
    public function isRefundPossible(OrderTransfer $orderTransfer)
812
    {
813
        $paymentTransfer = $this->getPayment($orderTransfer);
814
815
        if (!$this->isPaymentDataRequired($orderTransfer)) {
816
            return true;
817
        }
818
819
        $paymentDetailTransfer = $paymentTransfer->getPaymentDetail();
820
821
        return $paymentDetailTransfer->getBic() && $paymentDetailTransfer->getIban();
822
    }
823
824
    /**
825
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
826
     *
827
     * @return bool
828
     */
829
    public function isPaymentDataRequired(OrderTransfer $orderTransfer)
830
    {
831
        $paymentTransfer = $this->getPayment($orderTransfer);
832
833
        // Return early if we don't need the iban or bic data
834
        $paymentMethod = $paymentTransfer->getPaymentMethod();
835
        $whiteList = [
836
            PayoneApiConstants::PAYMENT_METHOD_E_WALLET,
837
            PayoneApiConstants::PAYMENT_METHOD_CREDITCARD_PSEUDO,
838
            PayoneApiConstants::PAYMENT_METHOD_ONLINE_BANK_TRANSFER,
839
        ];
840
841
        if (in_array($paymentMethod, $whiteList)) {
842
            return false;
843
        }
844
845
        return true;
846
    }
847
848
    /**
849
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
850
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponse
851
     *
852
     * @return \Generated\Shared\Transfer\CheckoutResponseTransfer
853
     */
854
    public function postSaveHook(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponse)
855
    {
856
        $apiLogsQuery = $this->queryContainer->createLastApiLogsByOrderId($quoteTransfer->getPayment()->getPayone()->getFkSalesOrder());
857
        $apiLog = $apiLogsQuery->findOne();
858
859
        if ($apiLog) {
860
            $redirectUrl = $apiLog->getRedirectUrl();
861
862
            if ($redirectUrl !== null) {
863
                $checkoutResponse->setIsExternalRedirect(true);
864
                $checkoutResponse->setRedirectUrl($redirectUrl);
865
            }
866
867
            $errorCode = $apiLog->getErrorCode();
868
869
            if ($errorCode) {
870
                $error = new CheckoutErrorTransfer();
871
                $error->setMessage($apiLog->getErrorMessageUser());
872
                $error->setErrorCode($errorCode);
873
                $checkoutResponse->addError($error);
874
                $checkoutResponse->setIsSuccess(false);
875
            }
876
        }
877
878
        return $checkoutResponse;
879
    }
880
881
    /**
882
     * @param \Generated\Shared\Transfer\PaymentDetailTransfer $paymentDataTransfer
883
     * @param int $idOrder
884
     *
885
     * @return void
886
     */
887 View Code Duplication
    public function updatePaymentDetail(PaymentDetailTransfer $paymentDataTransfer, $idOrder)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
888
    {
889
        $paymentEntity = $this->queryContainer->createPaymentByOrderId($idOrder)->findOne();
890
        $paymentDetailEntity = $paymentEntity->getSpyPaymentPayoneDetail();
891
892
        $paymentDetailEntity->fromArray($paymentDataTransfer->toArray());
893
894
        $paymentDetailEntity->save();
895
    }
896
897
    /**
898
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
899
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer $responseContainer
900
     *
901
     * @return void
902
     */
903
    protected function updatePaymentDetailAfterAuthorization(SpyPaymentPayone $paymentEntity, AuthorizationResponseContainer $responseContainer)
904
    {
905
        $paymentDetailEntity = $paymentEntity->getSpyPaymentPayoneDetail();
906
907
        $paymentDetailEntity->setClearingBankAccountHolder($responseContainer->getClearingBankaccountholder());
908
        $paymentDetailEntity->setClearingBankCountry($responseContainer->getClearingBankcountry());
909
        $paymentDetailEntity->setClearingBankAccount($responseContainer->getClearingBankaccount());
910
        $paymentDetailEntity->setClearingBankCode($responseContainer->getClearingBankcode());
911
        $paymentDetailEntity->setClearingBankIban($responseContainer->getClearingBankiban());
912
        $paymentDetailEntity->setClearingBankBic($responseContainer->getClearingBankbic());
913
        $paymentDetailEntity->setClearingBankCity($responseContainer->getClearingBankcity());
914
        $paymentDetailEntity->setClearingBankName($responseContainer->getClearingBankname());
915
916
        if ($responseContainer->getMandateIdentification()) {
917
            $paymentDetailEntity->setMandateIdentification($responseContainer->getMandateIdentification());
918
        }
919
920
        if ($paymentEntity->getPaymentMethod() == PayoneApiConstants::PAYMENT_METHOD_INVOICE) {
921
            $paymentDetailEntity->setInvoiceTitle($this->getInvoiceTitle($paymentEntity->getTransactionId()));
922
        }
923
924
        $paymentDetailEntity->save();
925
    }
926
927
    /**
928
     * @param \Generated\Shared\Transfer\PayoneInitPaypalExpressCheckoutRequestTransfer $requestTransfer
929
     *
930
     * @return \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer
931
     */
932 View Code Duplication
    public function initPaypalExpressCheckout(PayoneInitPaypalExpressCheckoutRequestTransfer $requestTransfer)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
933
    {
934
        $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(
935
            PayoneApiConstants::PAYMENT_METHOD_PAYPAL_EXPRESS_CHECKOUT
936
        );
937
        $baseGenericPaymentContainer = $paymentMethodMapper->createBaseGenericPaymentContainer();
938
        $baseGenericPaymentContainer->getPaydata()->setAction(PayoneApiConstants::PAYONE_EXPRESS_CHECKOUT_SET_ACTION);
939
        $requestContainer = $paymentMethodMapper->mapRequestTransferToGenericPayment(
940
            $baseGenericPaymentContainer,
941
            $requestTransfer
942
        );
943
        $responseTransfer = $this->performGenericRequest($requestContainer);
944
945
        return $responseTransfer;
946
    }
947
948
    /**
949
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
950
     *
951
     * @return \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer
952
     */
953 View Code Duplication
    public function getPaypalExpressCheckoutDetails(QuoteTransfer $quoteTransfer)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
954
    {
955
        $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(
956
            PayoneApiConstants::PAYMENT_METHOD_PAYPAL_EXPRESS_CHECKOUT
957
        );
958
959
        $baseGenericPaymentContainer = $paymentMethodMapper->createBaseGenericPaymentContainer();
960
        $baseGenericPaymentContainer->getPaydata()->setAction(
961
            PayoneApiConstants::PAYONE_EXPRESS_CHECKOUT_GET_DETAILS_ACTION
962
        );
963
        $requestContainer = $paymentMethodMapper->mapQuoteTransferToGenericPayment(
964
            $baseGenericPaymentContainer,
965
            $quoteTransfer
966
        );
967
        $responseTransfer = $this->performGenericRequest($requestContainer);
968
969
        return $responseTransfer;
970
    }
971
972
    /**
973
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\GenericPaymentContainer $requestContainer
974
     *
975
     * @return \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer
976
     */
977
    protected function performGenericRequest(GenericPaymentContainer $requestContainer)
978
    {
979
        $this->setStandardParameter($requestContainer);
980
981
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
982
        $responseContainer = new GenericPaymentResponseContainer($rawResponse);
983
        $responseTransfer = new PayonePaypalExpressCheckoutGenericPaymentResponseTransfer();
984
        $responseTransfer->setRedirectUrl($responseContainer->getRedirectUrl());
985
        $responseTransfer->setWorkOrderId($responseContainer->getWorkOrderId());
986
        $responseTransfer->setRawResponse(json_encode($rawResponse));
987
        $responseTransfer->setStatus($responseContainer->getStatus());
988
        $responseTransfer->setCustomerMessage($responseContainer->getCustomermessage());
989
        $responseTransfer->setErrorMessage($responseContainer->getErrormessage());
990
        $responseTransfer->setErrorCode($responseContainer->getErrorcode());
991
        $responseTransfer->setEmail($responseContainer->getEmail());
992
        $responseTransfer->setShippingFirstName($responseContainer->getShippingFirstname());
993
        $responseTransfer->setShippingLastName($responseContainer->getShippingLastname());
994
        $responseTransfer->setShippingCompany($responseContainer->getShippingCompany());
995
        $responseTransfer->setShippingCountry($responseContainer->getShippingCountry());
996
        $responseTransfer->setShippingState($responseContainer->getShippingState());
997
        $responseTransfer->setShippingStreet($responseContainer->getShippingStreet());
998
        $responseTransfer->setShippingAddressAdition($responseContainer->getShippingAddressaddition());
999
        $responseTransfer->setShippingCity($responseContainer->getShippingCity());
1000
        $responseTransfer->setShippingZip($responseContainer->getShippingZip());
1001
1002
        return $responseTransfer;
1003
    }
1004
1005
    /**
1006
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
1007
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer $container
1008
     *
1009
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer
1010
     */
1011
    public function prepareOrderItems(OrderTransfer $orderTransfer, AbstractRequestContainer $container): AbstractRequestContainer
1012
    {
1013
        $arrayIt = [];
1014
        $arrayId = [];
1015
        $arrayPr = [];
1016
        $arrayNo = [];
1017
        $arrayDe = [];
1018
        $arrayVa = [];
1019
1020
        $key = 1;
1021
        $expenses = $orderTransfer->getExpenses();
1022
1023
        foreach ($orderTransfer->getItems() as $itemTransfer) {
1024
            $arrayIt[$key] = PayoneApiConstants::INVOICING_ITEM_TYPE_GOODS;
1025
            $arrayId[$key] = $itemTransfer->getSku();
1026
            $arrayPr[$key] = $itemTransfer->getSumPrice();
1027
            $arrayNo[$key] = $itemTransfer->getQuantity();
1028
            $arrayDe[$key] = $itemTransfer->getName();
1029
            $arrayVa[$key] = (int)$itemTransfer->getTaxRate();
1030
            $key++;
1031
        }
1032
1033
        //Set shipment values
1034
        $arrayIt[$key] = PayoneApiConstants::INVOICING_ITEM_TYPE_SHIPMENT;
1035
        $arrayId[$key] = PayoneApiConstants::INVOICING_ITEM_TYPE_SHIPMENT;
1036
        $arrayPr[$key] = $this->getDeliveryCosts($expenses);
1037
        $arrayNo[$key] = 1;
1038
        $arrayDe[$key] = 'Shipment';
1039
        $key++;
1040
1041
        //set discount values
1042
        $arrayIt[$key] = PayoneApiConstants::INVOICING_ITEM_TYPE_VOUCHER;
1043
        $arrayId[$key] = PayoneApiConstants::INVOICING_ITEM_TYPE_VOUCHER;
1044
        $arrayPr[$key] = - $orderTransfer->getTotals()->getDiscountTotal();
1045
        $arrayNo[$key] = 1;
1046
        $arrayDe[$key] = 'Discount';
1047
1048
        $container->setIt($arrayIt);
1049
        $container->setId($arrayId);
1050
        $container->setPr($arrayPr);
1051
        $container->setNo($arrayNo);
1052
        $container->setDe($arrayDe);
1053
        $container->setVa($arrayVa);
1054
1055
        return $container;
1056
    }
1057
1058
    /**
1059
     * @param \ArrayObject $expenses
1060
     *
1061
     * @return string
1062
     */
1063
    protected function getDeliveryCosts(ArrayObject $expenses): string
1064
    {
1065
        foreach ($expenses as $expense) {
1066
            if ($expense->getType() === ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
1067
                return $expense->getSumGrossPrice();
1068
            }
1069
        }
1070
        return 0;
1071
    }
1072
}
1073