Passed
Pull Request — master (#23)
by Volodymyr
07:40
created

PaymentManager::prepareOrderShipment()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 27

Duplication

Lines 27
Ratio 100 %

Importance

Changes 0
Metric Value
dl 27
loc 27
rs 9.488
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
        $requestContainer = $this->prepareOrderShipment($orderTransfer, $requestContainer);
201
        $requestContainer = $this->prepareOrderDiscount($orderTransfer, $requestContainer);
202
        $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...
203
204
        $responseMapper = new AuthorizationResponseMapper();
205
        $responseTransfer = $responseMapper->getAuthorizationResponseTransfer($responseContainer);
206
207
        return $responseTransfer;
208
    }
209
210
    /**
211
     * @param int $idSalesOrder
212
     *
213
     * @return \Generated\Shared\Transfer\AuthorizationResponseTransfer
214
     */
215
    public function preAuthorizePayment($idSalesOrder)
216
    {
217
        $paymentEntity = $this->getPaymentEntity($idSalesOrder);
218
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
219
        $requestContainer = $paymentMethodMapper->mapPaymentToPreAuthorization($paymentEntity);
220
        $responseContainer = $this->performAuthorizationRequest($paymentEntity, $requestContainer);
221
222
        $responseMapper = new AuthorizationResponseMapper();
223
        $responseTransfer = $responseMapper->getAuthorizationResponseTransfer($responseContainer);
224
225
        return $responseTransfer;
226
    }
227
228
    /**
229
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
230
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AuthorizationContainerInterface $requestContainer
231
     *
232
     * @return \SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer
233
     */
234
    protected function performAuthorizationRequest(SpyPaymentPayone $paymentEntity, AuthorizationContainerInterface $requestContainer)
235
    {
236
        $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...
237
238
        $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...
239
        $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...
240
        $responseContainer = new AuthorizationResponseContainer($rawResponse);
241
        $this->updatePaymentAfterAuthorization($paymentEntity, $responseContainer);
242
        $this->updateApiLogAfterAuthorization($apiLogEntity, $responseContainer);
243
        $this->updatePaymentDetailAfterAuthorization($paymentEntity, $responseContainer);
244
245
        return $responseContainer;
246
    }
247
248
    /**
249
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
250
     *
251
     * @return \SprykerEco\Zed\Payone\Business\Payment\PaymentMethodMapperInterface
252
     */
253
    protected function getPaymentMethodMapper(SpyPaymentPayone $paymentEntity)
254
    {
255
        return $this->getRegisteredPaymentMethodMapper($paymentEntity->getPaymentMethod());
256
    }
257
258
    /**
259
     * @param int $orderId
260
     *
261
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayone
262
     */
263
    protected function getPaymentEntity($orderId)
264
    {
265
        return $this->queryContainer->createPaymentById($orderId)->findOne();
266
    }
267
268
    /**
269
     * @param \Generated\Shared\Transfer\PayoneCaptureTransfer $captureTransfer
270
     *
271
     * @return \Generated\Shared\Transfer\CaptureResponseTransfer
272
     */
273
    public function capturePayment(PayoneCaptureTransfer $captureTransfer): CaptureResponseTransfer
274
    {
275
        $paymentEntity = $this->getPaymentEntity($captureTransfer->getPayment()->getFkSalesOrder());
276
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
277
278
        $requestContainer = $paymentMethodMapper->mapPaymentToCapture($paymentEntity);
279
        $requestContainer = $this->prepareOrderItems($captureTransfer->getOrder(), $requestContainer);
280
        $requestContainer = $this->prepareOrderShipment($captureTransfer->getOrder(), $requestContainer);
281
        $requestContainer = $this->prepareOrderDiscount($captureTransfer->getOrder(), $requestContainer);
282
283
        if (!empty($captureTransfer->getSettleaccount())) {
284
            $businnessContainer = new BusinessContainer();
285
            $businnessContainer->setSettleAccount($captureTransfer->getSettleaccount());
286
            $requestContainer->setBusiness($businnessContainer);
287
        }
288
289
        $this->setStandardParameter($requestContainer);
290
291
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
292
293
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
294
        $responseContainer = new CaptureResponseContainer($rawResponse);
295
296
        $this->updateApiLogAfterCapture($apiLogEntity, $responseContainer);
297
298
        $responseMapper = new CaptureResponseMapper();
299
        $responseTransfer = $responseMapper->getCaptureResponseTransfer($responseContainer);
300
301
        return $responseTransfer;
302
    }
303
304
    /**
305
     * @param int $idPayment
306
     *
307
     * @return \Generated\Shared\Transfer\DebitResponseTransfer
308
     */
309
    public function debitPayment($idPayment)
310
    {
311
        $paymentEntity = $this->getPaymentEntity($idPayment);
312
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
313
314
        $requestContainer = $paymentMethodMapper->mapPaymentToDebit($paymentEntity);
315
        $this->setStandardParameter($requestContainer);
316
317
        $paymentEntity = $this->findPaymentByTransactionId($paymentEntity->getTransactionId());
318
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
319
320
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
321
        $responseContainer = new DebitResponseContainer($rawResponse);
322
323
        $this->updateApiLogAfterDebit($apiLogEntity, $responseContainer);
324
325
        $responseMapper = new DebitResponseMapper();
326
        $responseTransfer = $responseMapper->getDebitResponseTransfer($responseContainer);
327
328
        return $responseTransfer;
329
    }
330
331
    /**
332
     * @param \Generated\Shared\Transfer\PayoneCreditCardTransfer $creditCardData
333
     *
334
     * @return \Generated\Shared\Transfer\CreditCardCheckResponseTransfer
335
     */
336
    public function creditCardCheck(PayoneCreditCardTransfer $creditCardData)
337
    {
338
        /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\CreditCardPseudo $paymentMethodMapper */
339
        $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper($creditCardData->getPayment()->getPaymentMethod());
340
        $requestContainer = $paymentMethodMapper->mapCreditCardCheck($creditCardData);
341
342
        $this->setStandardParameter($requestContainer);
343
344
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
345
        $responseContainer = new CreditCardCheckResponseContainer($rawResponse);
346
347
        $responseMapper = new CreditCardCheckResponseMapper();
348
        $responseTransfer = $responseMapper->getCreditCardCheckResponseTransfer($responseContainer);
349
350
        return $responseTransfer;
351
    }
352
353
    /**
354
     * @param \Generated\Shared\Transfer\PayoneBankAccountCheckTransfer $bankAccountCheckTransfer
355
     *
356
     * @return \Generated\Shared\Transfer\PayoneBankAccountCheckTransfer
357
     */
358
    public function bankAccountCheck(PayoneBankAccountCheckTransfer $bankAccountCheckTransfer)
359
    {
360
        /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\DirectDebit $paymentMethodMapper */
361
        $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_ONLINE_BANK_TRANSFER);
362
        $requestContainer = $paymentMethodMapper->mapBankAccountCheck($bankAccountCheckTransfer);
363
        $this->setStandardParameter($requestContainer);
364
365
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
366
        $responseContainer = new BankAccountCheckResponseContainer($rawResponse);
367
368
        $bankAccountCheckTransfer->setErrorCode($responseContainer->getErrorcode());
369
        $bankAccountCheckTransfer->setCustomerErrorMessage($responseContainer->getCustomermessage());
370
        $bankAccountCheckTransfer->setStatus($responseContainer->getStatus());
371
        $bankAccountCheckTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
372
373
        return $bankAccountCheckTransfer;
374
    }
375
376
    /**
377
     * @param \Generated\Shared\Transfer\PayoneManageMandateTransfer $manageMandateTransfer
378
     *
379
     * @return \Generated\Shared\Transfer\PayoneManageMandateTransfer
380
     */
381
    public function manageMandate(PayoneManageMandateTransfer $manageMandateTransfer)
382
    {
383
        /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\DirectDebit $paymentMethodMapper */
384
        $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_DIRECT_DEBIT);
385
        $requestContainer = $paymentMethodMapper->mapManageMandate($manageMandateTransfer);
386
        $this->setStandardParameter($requestContainer);
387
388
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
389
        $responseContainer = new ManageMandateResponseContainer($rawResponse);
390
391
        $manageMandateTransfer->setErrorCode($responseContainer->getErrorcode());
392
        $manageMandateTransfer->setCustomerErrorMessage($responseContainer->getCustomermessage());
393
        $manageMandateTransfer->setStatus($responseContainer->getStatus());
394
        $manageMandateTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
395
        $manageMandateTransfer->setMandateIdentification($responseContainer->getMandateIdentification());
396
        $manageMandateTransfer->setMandateText($responseContainer->getMandateText());
397
        $manageMandateTransfer->setIban($responseContainer->getIban());
398
        $manageMandateTransfer->setBic($responseContainer->getBic());
399
400
        return $manageMandateTransfer;
401
    }
402
403
    /**
404
     * @param \Generated\Shared\Transfer\PayoneGetFileTransfer $getFileTransfer
405
     *
406
     * @return \Generated\Shared\Transfer\PayoneGetFileTransfer
407
     */
408
    public function getFile(PayoneGetFileTransfer $getFileTransfer)
409
    {
410
        $responseContainer = new GetFileResponseContainer();
411
        $paymentEntity = $this->findPaymentByFileReferenceAndCustomerId(
412
            $getFileTransfer->getReference(),
413
            $getFileTransfer->getCustomerId()
414
        );
415
416
        if (!$paymentEntity) {
417
            $this->setAccessDeniedError($responseContainer);
418
419
            return $getFileTransfer;
420
        }
421
422
        /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\DirectDebit $paymentMethodMapper */
423
        $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_DIRECT_DEBIT);
424
        $requestContainer = $paymentMethodMapper->mapGetFile($getFileTransfer);
425
        $this->setStandardParameter($requestContainer);
426
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
427
        $responseContainer->init($rawResponse);
428
429
        $getFileTransfer->setRawResponse($responseContainer->getRawResponse());
430
        $getFileTransfer->setStatus($responseContainer->getStatus());
431
        $getFileTransfer->setErrorCode($responseContainer->getErrorcode());
432
        $getFileTransfer->setCustomerErrorMessage($responseContainer->getCustomermessage());
433
        $getFileTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
434
435
        return $getFileTransfer;
436
    }
437
438
    /**
439
     * @param \Generated\Shared\Transfer\PayoneGetInvoiceTransfer $getInvoiceTransfer
440
     *
441
     * @return \Generated\Shared\Transfer\PayoneGetInvoiceTransfer
442
     */
443 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...
444
    {
445
        $responseContainer = new GetInvoiceResponseContainer();
446
        $paymentEntity = $this->findPaymentByInvoiceTitleAndCustomerId(
447
            $getInvoiceTransfer->getReference(),
448
            $getInvoiceTransfer->getCustomerId()
449
        );
450
451
        if ($paymentEntity) {
452
            /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\Invoice $paymentMethodMapper */
453
            $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_INVOICE);
454
            $requestContainer = $paymentMethodMapper->mapGetInvoice($getInvoiceTransfer);
455
            $this->setStandardParameter($requestContainer);
456
            $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
457
            $responseContainer->init($rawResponse);
458
        } else {
459
            $this->setAccessDeniedError($responseContainer);
460
        }
461
462
        $getInvoiceTransfer->setRawResponse($responseContainer->getRawResponse());
463
        $getInvoiceTransfer->setStatus($responseContainer->getStatus());
464
        $getInvoiceTransfer->setErrorCode($responseContainer->getErrorcode());
465
        $getInvoiceTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
466
467
        return $getInvoiceTransfer;
468
    }
469
470
    /**
471
     * @param \Generated\Shared\Transfer\PayoneGetSecurityInvoiceTransfer $getSecurityInvoiceTransfer
472
     *
473
     * @return \Generated\Shared\Transfer\PayoneGetSecurityInvoiceTransfer
474
     */
475 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...
476
    {
477
        $responseContainer = new GetSecurityInvoiceResponseContainer();
478
        $paymentEntity = $this->findPaymentByInvoiceTitleAndCustomerId(
479
            $getSecurityInvoiceTransfer->getReference(),
480
            $getSecurityInvoiceTransfer->getCustomerId()
481
        );
482
483
        if ($paymentEntity) {
484
            /** @var \SprykerEco\Zed\Payone\Business\Payment\MethodMapper\SecurityInvoice $paymentMethodMapper */
485
            $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(PayoneApiConstants::PAYMENT_METHOD_SECURITY_INVOICE);
486
            $requestContainer = $paymentMethodMapper->mapGetSecurityInvoice($getSecurityInvoiceTransfer);
487
            $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...
488
            $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...
489
            $responseContainer->init($rawResponse);
490
        } else {
491
            $this->setAccessDeniedError($responseContainer);
492
        }
493
494
        $getSecurityInvoiceTransfer->setRawResponse($responseContainer->getRawResponse());
495
        $getSecurityInvoiceTransfer->setStatus($responseContainer->getStatus());
496
        $getSecurityInvoiceTransfer->setErrorCode($responseContainer->getErrorcode());
497
        $getSecurityInvoiceTransfer->setInternalErrorMessage($responseContainer->getErrormessage());
498
499
        return $getSecurityInvoiceTransfer;
500
    }
501
502
    /**
503
     * @param int $transactionId
504
     *
505
     * @return \SprykerEco\Zed\Payone\Business\Api\Response\Container\GetInvoiceResponseContainer
506
     */
507
    public function getInvoiceTitle($transactionId)
508
    {
509
        return implode('-', [
510
            PayoneApiConstants::INVOICE_TITLE_PREFIX_INVOICE,
511
            $transactionId,
512
            0,
513
        ]);
514
    }
515
516
    /**
517
     * @param \Generated\Shared\Transfer\PayoneRefundTransfer $refundTransfer
518
     *
519
     * @return \Generated\Shared\Transfer\RefundResponseTransfer
520
     */
521
    public function refundPayment(PayoneRefundTransfer $refundTransfer)
522
    {
523
        $payonePaymentTransfer = $refundTransfer->getPayment();
524
525
        $paymentEntity = $this->getPaymentEntity($payonePaymentTransfer->getFkSalesOrder());
526
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
527
        $requestContainer = $paymentMethodMapper->mapPaymentToRefund($paymentEntity);
528
        $requestContainer->setAmount(0 - $paymentEntity->getSpyPaymentPayoneDetail()->getAmount());
529
        $requestContainer = $this->prepareOrderItems($refundTransfer->getOrder(), $requestContainer);
530
        $requestContainer = $this->prepareOrderShipment($refundTransfer->getOrder(), $requestContainer);
531
        $requestContainer = $this->prepareOrderDiscount($refundTransfer->getOrder(), $requestContainer);
532
533
        $this->setStandardParameter($requestContainer);
534
535
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
536
537
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
538
        $responseContainer = new RefundResponseContainer($rawResponse);
539
540
        $this->updateApiLogAfterRefund($apiLogEntity, $responseContainer);
541
542
        $responseMapper = new RefundResponseMapper();
543
        $responseTransfer = $responseMapper->getRefundResponseTransfer($responseContainer);
544
545
        return $responseTransfer;
546
    }
547
548
    /**
549
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
550
     *
551
     * @return \Generated\Shared\Transfer\PayonePaymentTransfer
552
     */
553
    protected function getPayment(OrderTransfer $orderTransfer)
554
    {
555
        $payment = $this->queryContainer->createPaymentByOrderId($orderTransfer->getIdSalesOrder())->findOne();
556
        $paymentDetail = $payment->getSpyPaymentPayoneDetail();
557
558
        $paymentDetailTransfer = new PaymentDetailTransfer();
559
        $paymentDetailTransfer->fromArray($paymentDetail->toArray(), true);
560
561
        $paymentTransfer = new PayonePaymentTransfer();
562
        $paymentTransfer->fromArray($payment->toArray(), true);
563
        $paymentTransfer->setPaymentDetail($paymentDetailTransfer);
564
565
        return $paymentTransfer;
566
    }
567
568
    /**
569
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
570
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer $responseContainer
571
     *
572
     * @return void
573
     */
574
    protected function updatePaymentAfterAuthorization(SpyPaymentPayone $paymentEntity, AuthorizationResponseContainer $responseContainer)
575
    {
576
        $paymentEntity->setTransactionId($responseContainer->getTxid());
577
        $paymentEntity->save();
578
    }
579
580
    /**
581
     * @param string $transactionId
582
     *
583
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayone
584
     */
585
    protected function findPaymentByTransactionId($transactionId)
586
    {
587
        return $this->queryContainer->createPaymentByTransactionIdQuery($transactionId)->findOne();
588
    }
589
590
    /**
591
     * @param string $invoiceTitle
592
     * @param int $customerId
593
     *
594
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneQuery
595
     */
596
    protected function findPaymentByInvoiceTitleAndCustomerId($invoiceTitle, $customerId)
597
    {
598
        return $this->queryContainer->createPaymentByInvoiceTitleAndCustomerIdQuery($invoiceTitle, $customerId)->findOne();
599
    }
600
601
    /**
602
     * @param string $fileReference
603
     * @param int $customerId
604
     *
605
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneQuery
606
     */
607
    protected function findPaymentByFileReferenceAndCustomerId($fileReference, $customerId)
608
    {
609
        return $this->queryContainer->createPaymentByFileReferenceAndCustomerIdQuery($fileReference, $customerId)->findOne();
610
    }
611
612
    /**
613
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
614
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer $container
615
     *
616
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog
617
     */
618
    protected function initializeApiLog(SpyPaymentPayone $paymentEntity, AbstractRequestContainer $container)
619
    {
620
        $entity = new SpyPaymentPayoneApiLog();
621
        $entity->setSpyPaymentPayone($paymentEntity);
622
        $entity->setRequest($container->getRequest());
623
        $entity->setMode($container->getMode());
624
        $entity->setMerchantId($container->getMid());
625
        $entity->setPortalId($container->getPortalid());
626
        if ($container instanceof CaptureContainer || $container instanceof RefundContainer || $container instanceof DebitContainer) {
627
            $entity->setSequenceNumber($container->getSequenceNumber());
628
        }
629
        // Logging request data for debug
630
        $entity->setRawRequest(json_encode($container->toArray()));
631
        $entity->save();
632
633
        return $entity;
634
    }
635
636
    /**
637
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
638
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer $responseContainer
639
     *
640
     * @return void
641
     */
642
    protected function updateApiLogAfterAuthorization(SpyPaymentPayoneApiLog $apiLogEntity, AuthorizationResponseContainer $responseContainer)
643
    {
644
        $apiLogEntity->setStatus($responseContainer->getStatus());
645
        $apiLogEntity->setUserId($responseContainer->getUserid());
646
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
647
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
648
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
649
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
650
        $apiLogEntity->setRedirectUrl($responseContainer->getRedirecturl());
651
        $apiLogEntity->setSequenceNumber(0);
652
653
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
654
        $apiLogEntity->save();
655
    }
656
657
    /**
658
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
659
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\CaptureResponseContainer $responseContainer
660
     *
661
     * @return void
662
     */
663 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...
664
    {
665
        $apiLogEntity->setStatus($responseContainer->getStatus());
666
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
667
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
668
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
669
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
670
671
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
672
        $apiLogEntity->save();
673
    }
674
675
    /**
676
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
677
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\DebitResponseContainer $responseContainer
678
     *
679
     * @return void
680
     */
681 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...
682
    {
683
        $apiLogEntity->setStatus($responseContainer->getStatus());
684
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
685
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
686
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
687
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
688
689
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
690
        $apiLogEntity->save();
691
    }
692
693
    /**
694
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
695
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\RefundResponseContainer $responseContainer
696
     *
697
     * @return void
698
     */
699 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...
700
    {
701
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
702
        $apiLogEntity->setStatus($responseContainer->getStatus());
703
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
704
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
705
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
706
707
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
708
        $apiLogEntity->save();
709
    }
710
711
    /**
712
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer $container
713
     *
714
     * @return void
715
     */
716
    protected function setStandardParameter(AbstractRequestContainer $container)
717
    {
718
        $container->setApiVersion(PayoneApiConstants::API_VERSION_3_9);
719
        $container->setEncoding($this->standardParameter->getEncoding());
720
        $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...
721
        $container->setMid($this->standardParameter->getMid());
722
        $container->setPortalid($this->standardParameter->getPortalId());
723
        $container->setMode($this->modeDetector->getMode());
724
        $container->setIntegratorName(PayoneApiConstants::INTEGRATOR_NAME_SPRYKER);
725
        $container->setIntegratorVersion(PayoneApiConstants::INTEGRATOR_VERSION_3_0_0);
726
        $container->setSolutionName(PayoneApiConstants::SOLUTION_NAME_SPRYKER);
727
        $container->setSolutionVersion(PayoneApiConstants::SOLUTION_VERSION_3_0_0);
728
    }
729
730
    /**
731
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\AbstractResponseContainer $container
732
     *
733
     * @return void
734
     */
735
    protected function setAccessDeniedError(AbstractResponseContainer $container)
736
    {
737
        $container->setStatus(PayoneApiConstants::RESPONSE_TYPE_ERROR);
738
        $container->setErrormessage(static::ERROR_ACCESS_DENIED_MESSAGE);
739
        $container->setCustomermessage(static::ERROR_ACCESS_DENIED_MESSAGE);
740
    }
741
742
    /**
743
     * @param int $idOrder
744
     *
745
     * @return \Generated\Shared\Transfer\PaymentDetailTransfer
746
     */
747 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...
748
    {
749
        $paymentEntity = $this->queryContainer->createPaymentByOrderId($idOrder)->findOne();
750
        $paymentDetailEntity = $paymentEntity->getSpyPaymentPayoneDetail();
751
        $paymentDetailTransfer = new PaymentDetailTransfer();
752
        $paymentDetailTransfer->fromArray($paymentDetailEntity->toArray(), true);
753
754
        return $paymentDetailTransfer;
755
    }
756
757
    /**
758
     * Gets payment logs (both api and transaction status) for specific orders in chronological order.
759
     *
760
     * @param \Propel\Runtime\Collection\ObjectCollection|\ArrayObject $orders
761
     *
762
     * @return \Generated\Shared\Transfer\PayonePaymentLogCollectionTransfer
763
     */
764
    public function getPaymentLogs($orders)
765
    {
766
        $apiLogs = $this->queryContainer->createApiLogsByOrderIds($orders)->find()->getData();
0 ignored issues
show
Bug introduced by
It seems like $orders defined by parameter $orders on line 764 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...
767
768
        $transactionStatusLogs = $this->queryContainer->createTransactionStatusLogsByOrderIds($orders)->find()->getData();
0 ignored issues
show
Bug introduced by
It seems like $orders defined by parameter $orders on line 764 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...
769
770
        $logs = [];
771
        /** @var \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLog */
772 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...
773
            $key = $apiLog->getCreatedAt()->format('Y-m-d\TH:i:s\Z') . 'a' . $apiLog->getIdPaymentPayoneApiLog();
774
            $payonePaymentLogTransfer = new PayonePaymentLogTransfer();
775
            $payonePaymentLogTransfer->fromArray($apiLog->toArray(), true);
776
            $payonePaymentLogTransfer->setLogType(self::LOG_TYPE_API_LOG);
777
            $logs[$key] = $payonePaymentLogTransfer;
778
        }
779
        /** @var \Orm\Zed\Payone\Persistence\SpyPaymentPayoneTransactionStatusLog $transactionStatusLog */
780 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...
781
            $key = $transactionStatusLog->getCreatedAt()->format('Y-m-d\TH:i:s\Z') . 't' . $transactionStatusLog->getIdPaymentPayoneTransactionStatusLog();
782
            $payonePaymentLogTransfer = new PayonePaymentLogTransfer();
783
            $payonePaymentLogTransfer->fromArray($transactionStatusLog->toArray(), true);
784
            $payonePaymentLogTransfer->setLogType(self::LOG_TYPE_TRANSACTION_STATUS_LOG);
785
            $logs[$key] = $payonePaymentLogTransfer;
786
        }
787
788
        ksort($logs);
789
790
        $payonePaymentLogCollectionTransfer = new PayonePaymentLogCollectionTransfer();
791
792
        foreach ($logs as $log) {
793
            $payonePaymentLogCollectionTransfer->addPaymentLogs($log);
794
        }
795
796
        return $payonePaymentLogCollectionTransfer;
797
    }
798
799
    /**
800
     * @param \Generated\Shared\Transfer\PayoneCreditCardCheckRequestDataTransfer $creditCardCheckRequestDataTransfer
801
     *
802
     * @return array
803
     */
804
    public function getCreditCardCheckRequestData(PayoneCreditCardCheckRequestDataTransfer $creditCardCheckRequestDataTransfer)
805
    {
806
        $this->standardParameter->fromArray($creditCardCheckRequestDataTransfer->toArray(), true);
807
808
        $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...
809
810
        $data = $creditCardCheck->mapCreditCardCheckData();
811
812
        return $data->toArray();
813
    }
814
815
    /**
816
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
817
     *
818
     * @return bool
819
     */
820
    public function isRefundPossible(OrderTransfer $orderTransfer)
821
    {
822
        $paymentTransfer = $this->getPayment($orderTransfer);
823
824
        if (!$this->isPaymentDataRequired($orderTransfer)) {
825
            return true;
826
        }
827
828
        $paymentDetailTransfer = $paymentTransfer->getPaymentDetail();
829
830
        return $paymentDetailTransfer->getBic() && $paymentDetailTransfer->getIban();
831
    }
832
833
    /**
834
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
835
     *
836
     * @return bool
837
     */
838
    public function isPaymentDataRequired(OrderTransfer $orderTransfer)
839
    {
840
        $paymentTransfer = $this->getPayment($orderTransfer);
841
842
        // Return early if we don't need the iban or bic data
843
        $paymentMethod = $paymentTransfer->getPaymentMethod();
844
        $whiteList = [
845
            PayoneApiConstants::PAYMENT_METHOD_E_WALLET,
846
            PayoneApiConstants::PAYMENT_METHOD_CREDITCARD_PSEUDO,
847
            PayoneApiConstants::PAYMENT_METHOD_ONLINE_BANK_TRANSFER,
848
        ];
849
850
        if (in_array($paymentMethod, $whiteList)) {
851
            return false;
852
        }
853
854
        return true;
855
    }
856
857
    /**
858
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
859
     * @param \Generated\Shared\Transfer\CheckoutResponseTransfer $checkoutResponse
860
     *
861
     * @return \Generated\Shared\Transfer\CheckoutResponseTransfer
862
     */
863
    public function postSaveHook(QuoteTransfer $quoteTransfer, CheckoutResponseTransfer $checkoutResponse)
864
    {
865
        $apiLogsQuery = $this->queryContainer->createLastApiLogsByOrderId($quoteTransfer->getPayment()->getPayone()->getFkSalesOrder());
866
        $apiLog = $apiLogsQuery->findOne();
867
868
        if ($apiLog) {
869
            $redirectUrl = $apiLog->getRedirectUrl();
870
871
            if ($redirectUrl !== null) {
872
                $checkoutResponse->setIsExternalRedirect(true);
873
                $checkoutResponse->setRedirectUrl($redirectUrl);
874
            }
875
876
            $errorCode = $apiLog->getErrorCode();
877
878
            if ($errorCode) {
879
                $error = new CheckoutErrorTransfer();
880
                $error->setMessage($apiLog->getErrorMessageUser());
881
                $error->setErrorCode($errorCode);
882
                $checkoutResponse->addError($error);
883
                $checkoutResponse->setIsSuccess(false);
884
            }
885
        }
886
887
        return $checkoutResponse;
888
    }
889
890
    /**
891
     * @param \Generated\Shared\Transfer\PaymentDetailTransfer $paymentDataTransfer
892
     * @param int $idOrder
893
     *
894
     * @return void
895
     */
896 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...
897
    {
898
        $paymentEntity = $this->queryContainer->createPaymentByOrderId($idOrder)->findOne();
899
        $paymentDetailEntity = $paymentEntity->getSpyPaymentPayoneDetail();
900
901
        $paymentDetailEntity->fromArray($paymentDataTransfer->toArray());
902
903
        $paymentDetailEntity->save();
904
    }
905
906
    /**
907
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
908
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer $responseContainer
909
     *
910
     * @return void
911
     */
912
    protected function updatePaymentDetailAfterAuthorization(SpyPaymentPayone $paymentEntity, AuthorizationResponseContainer $responseContainer)
913
    {
914
        $paymentDetailEntity = $paymentEntity->getSpyPaymentPayoneDetail();
915
916
        $paymentDetailEntity->setClearingBankAccountHolder($responseContainer->getClearingBankaccountholder());
917
        $paymentDetailEntity->setClearingBankCountry($responseContainer->getClearingBankcountry());
918
        $paymentDetailEntity->setClearingBankAccount($responseContainer->getClearingBankaccount());
919
        $paymentDetailEntity->setClearingBankCode($responseContainer->getClearingBankcode());
920
        $paymentDetailEntity->setClearingBankIban($responseContainer->getClearingBankiban());
921
        $paymentDetailEntity->setClearingBankBic($responseContainer->getClearingBankbic());
922
        $paymentDetailEntity->setClearingBankCity($responseContainer->getClearingBankcity());
923
        $paymentDetailEntity->setClearingBankName($responseContainer->getClearingBankname());
924
925
        if ($responseContainer->getMandateIdentification()) {
926
            $paymentDetailEntity->setMandateIdentification($responseContainer->getMandateIdentification());
927
        }
928
929
        if ($paymentEntity->getPaymentMethod() == PayoneApiConstants::PAYMENT_METHOD_INVOICE) {
930
            $paymentDetailEntity->setInvoiceTitle($this->getInvoiceTitle($paymentEntity->getTransactionId()));
931
        }
932
933
        $paymentDetailEntity->save();
934
    }
935
936
    /**
937
     * @param \Generated\Shared\Transfer\PayoneInitPaypalExpressCheckoutRequestTransfer $requestTransfer
938
     *
939
     * @return \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer
940
     */
941 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...
942
    {
943
        $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(
944
            PayoneApiConstants::PAYMENT_METHOD_PAYPAL_EXPRESS_CHECKOUT
945
        );
946
        $baseGenericPaymentContainer = $paymentMethodMapper->createBaseGenericPaymentContainer();
947
        $baseGenericPaymentContainer->getPaydata()->setAction(PayoneApiConstants::PAYONE_EXPRESS_CHECKOUT_SET_ACTION);
948
        $requestContainer = $paymentMethodMapper->mapRequestTransferToGenericPayment(
949
            $baseGenericPaymentContainer,
950
            $requestTransfer
951
        );
952
        $responseTransfer = $this->performGenericRequest($requestContainer);
953
954
        return $responseTransfer;
955
    }
956
957
    /**
958
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
959
     *
960
     * @return \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer
961
     */
962 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...
963
    {
964
        $paymentMethodMapper = $this->getRegisteredPaymentMethodMapper(
965
            PayoneApiConstants::PAYMENT_METHOD_PAYPAL_EXPRESS_CHECKOUT
966
        );
967
968
        $baseGenericPaymentContainer = $paymentMethodMapper->createBaseGenericPaymentContainer();
969
        $baseGenericPaymentContainer->getPaydata()->setAction(
970
            PayoneApiConstants::PAYONE_EXPRESS_CHECKOUT_GET_DETAILS_ACTION
971
        );
972
        $requestContainer = $paymentMethodMapper->mapQuoteTransferToGenericPayment(
973
            $baseGenericPaymentContainer,
974
            $quoteTransfer
975
        );
976
        $responseTransfer = $this->performGenericRequest($requestContainer);
977
978
        return $responseTransfer;
979
    }
980
981
    /**
982
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\GenericPaymentContainer $requestContainer
983
     *
984
     * @return \Generated\Shared\Transfer\PayonePaypalExpressCheckoutGenericPaymentResponseTransfer
985
     */
986
    protected function performGenericRequest(GenericPaymentContainer $requestContainer)
987
    {
988
        $this->setStandardParameter($requestContainer);
989
990
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
991
        $responseContainer = new GenericPaymentResponseContainer($rawResponse);
992
        $responseTransfer = new PayonePaypalExpressCheckoutGenericPaymentResponseTransfer();
993
        $responseTransfer->setRedirectUrl($responseContainer->getRedirectUrl());
994
        $responseTransfer->setWorkOrderId($responseContainer->getWorkOrderId());
995
        $responseTransfer->setRawResponse(json_encode($rawResponse));
996
        $responseTransfer->setStatus($responseContainer->getStatus());
997
        $responseTransfer->setCustomerMessage($responseContainer->getCustomermessage());
998
        $responseTransfer->setErrorMessage($responseContainer->getErrormessage());
999
        $responseTransfer->setErrorCode($responseContainer->getErrorcode());
1000
        $responseTransfer->setEmail($responseContainer->getEmail());
1001
        $responseTransfer->setShippingFirstName($responseContainer->getShippingFirstname());
1002
        $responseTransfer->setShippingLastName($responseContainer->getShippingLastname());
1003
        $responseTransfer->setShippingCompany($responseContainer->getShippingCompany());
1004
        $responseTransfer->setShippingCountry($responseContainer->getShippingCountry());
1005
        $responseTransfer->setShippingState($responseContainer->getShippingState());
1006
        $responseTransfer->setShippingStreet($responseContainer->getShippingStreet());
1007
        $responseTransfer->setShippingAddressAdition($responseContainer->getShippingAddressaddition());
1008
        $responseTransfer->setShippingCity($responseContainer->getShippingCity());
1009
        $responseTransfer->setShippingZip($responseContainer->getShippingZip());
1010
1011
        return $responseTransfer;
1012
    }
1013
1014
    /**
1015
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
1016
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer $container
1017
     *
1018
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer
1019
     */
1020
    protected function prepareOrderItems(OrderTransfer $orderTransfer, AbstractRequestContainer $container): AbstractRequestContainer
1021
    {
1022
        $arrayIt = $container->getIt();
1023
        $arrayId = $container->getId();
1024
        $arrayPr = $container->getPr();
1025
        $arrayNo = $container->getNo();
1026
        $arrayDe = $container->getDe();
1027
        $arrayVa = $container->getVa();
1028
1029
        $key = count($arrayId);
1030
1031
        foreach ($orderTransfer->getItems() as $itemTransfer) {
1032
            $arrayIt[$key] = PayoneApiConstants::INVOICING_ITEM_TYPE_GOODS;
1033
            $arrayId[$key] = $itemTransfer->getSku();
1034
            $arrayPr[$key] = $itemTransfer->getSumPrice();
1035
            $arrayNo[$key] = $itemTransfer->getQuantity();
1036
            $arrayDe[$key] = $itemTransfer->getName();
1037
            $arrayVa[$key] = (int)$itemTransfer->getTaxRate();
1038
            $key++;
1039
        }
1040
1041
        $container->setIt($arrayIt);
1042
        $container->setId($arrayId);
1043
        $container->setPr($arrayPr);
1044
        $container->setNo($arrayNo);
1045
        $container->setDe($arrayDe);
1046
        $container->setVa($arrayVa);
1047
1048
        return $container;
1049
    }
1050
1051
    /**
1052
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
1053
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer $container
1054
     *
1055
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer
1056
     */
1057 View Code Duplication
    protected function prepareOrderShipment(OrderTransfer $orderTransfer, AbstractRequestContainer $container): AbstractRequestContainer
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...
1058
    {
1059
        $arrayIt = $container->getIt();
1060
        $arrayId = $container->getId();
1061
        $arrayPr = $container->getPr();
1062
        $arrayNo = $container->getNo();
1063
        $arrayDe = $container->getDe();
1064
        $arrayVa = $container->getVa();
1065
1066
        $key = count($arrayId);
1067
        $expenses = $orderTransfer->getExpenses();
1068
1069
        $arrayIt[$key] = PayoneApiConstants::INVOICING_ITEM_TYPE_SHIPMENT;
1070
        $arrayId[$key] = PayoneApiConstants::INVOICING_ITEM_TYPE_SHIPMENT;
1071
        $arrayPr[$key] = $this->getDeliveryCosts($expenses);
1072
        $arrayNo[$key] = 1;
1073
        $arrayDe[$key] = 'Shipment';
1074
1075
        $container->setIt($arrayIt);
1076
        $container->setId($arrayId);
1077
        $container->setPr($arrayPr);
1078
        $container->setNo($arrayNo);
1079
        $container->setDe($arrayDe);
1080
        $container->setVa($arrayVa);
1081
1082
        return $container;
1083
    }
1084
1085
    /**
1086
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
1087
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer $container
1088
     *
1089
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer
1090
     */
1091 View Code Duplication
    protected function prepareOrderDiscount(OrderTransfer $orderTransfer, AbstractRequestContainer $container): AbstractRequestContainer
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...
1092
    {
1093
        $arrayIt = $container->getIt();
1094
        $arrayId = $container->getId();
1095
        $arrayPr = $container->getPr();
1096
        $arrayNo = $container->getNo();
1097
        $arrayDe = $container->getDe();
1098
        $arrayVa = $container->getVa();
1099
1100
        $key = count($arrayId);
1101
1102
        $arrayIt[$key] = PayoneApiConstants::INVOICING_ITEM_TYPE_VOUCHER;
1103
        $arrayId[$key] = PayoneApiConstants::INVOICING_ITEM_TYPE_VOUCHER;
1104
        $arrayPr[$key] = - $orderTransfer->getTotals()->getDiscountTotal();
1105
        $arrayNo[$key] = 1;
1106
        $arrayDe[$key] = 'Discount';
1107
1108
        $container->setIt($arrayIt);
1109
        $container->setId($arrayId);
1110
        $container->setPr($arrayPr);
1111
        $container->setNo($arrayNo);
1112
        $container->setDe($arrayDe);
1113
        $container->setVa($arrayVa);
1114
1115
        return $container;
1116
    }
1117
1118
    /**
1119
     * @param \ArrayObject $expenses
1120
     *
1121
     * @return string
1122
     */
1123
    protected function getDeliveryCosts(ArrayObject $expenses): string
1124
    {
1125
        foreach ($expenses as $expense) {
1126
            if ($expense->getType() === ShipmentConstants::SHIPMENT_EXPENSE_TYPE) {
1127
                return $expense->getSumGrossPrice();
1128
            }
1129
        }
1130
        return 0;
1131
    }
1132
}
1133