Passed
Pull Request — master (#67)
by
unknown
09:59
created

PayoneDebitRequestSender::updateApiLogAfterDebit()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

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

filter:
    dependency_paths: ["lib/*"]

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

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

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

filter:
    dependency_paths: ["lib/*"]

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

Loading history...
12
use Orm\Zed\Payone\Persistence\SpyPaymentPayone;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persistence\SpyPaymentPayone was not found. Maybe you did not declare it correctly or list all dependencies?

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

filter:
    dependency_paths: ["lib/*"]

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

Loading history...
13
use Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog was not found. Maybe you did not declare it correctly or list all dependencies?

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

filter:
    dependency_paths: ["lib/*"]

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

Loading history...
14
use SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface;
15
use SprykerEco\Zed\Payone\Business\Api\Response\Container\DebitResponseContainer;
16
use SprykerEco\Zed\Payone\Business\Api\Response\Mapper\DebitResponseMapperInterface;
17
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface;
18
use SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface;
19
use SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface;
20
21
class PayoneDebitRequestSender extends AbstractPayoneRequestSender implements PayoneDebitRequestSenderInterface
22
{
23
    /**
24
     * @var \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface
25
     */
26
    protected $executionAdapter;
27
28
    /**
29
     * @var \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface
30
     */
31
    protected $queryContainer;
32
33
    /**
34
     * @var \Generated\Shared\Transfer\PayoneStandardParameterTransfer
35
     */
36
    protected $standardParameter;
37
38
    /**
39
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface
40
     */
41
    protected $standartParameterMapper;
42
43
    /**
44
     * @var \SprykerEco\Zed\Payone\Business\Api\Response\Mapper\DebitResponseMapperInterface
45
     */
46
    protected $debitResponseMapper;
47
48
    /**
49
     * @param \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface $executionAdapter
50
     * @param \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface $queryContainer
51
     * @param \Generated\Shared\Transfer\PayoneStandardParameterTransfer $standardParameter
52
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface $standartParameterMapper
53
     * @param \SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface $paymentMapperReader
54
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Mapper\DebitResponseMapperInterface $debitResponseMapper
55
     */
56
    public function __construct(
57
        AdapterInterface $executionAdapter,
58
        PayoneQueryContainerInterface $queryContainer,
59
        PayoneStandardParameterTransfer $standardParameter,
60
        StandartParameterMapperInterface $standartParameterMapper,
61
        PaymentMapperReaderInterface $paymentMapperReader,
62
        DebitResponseMapperInterface $debitResponseMapper
63
    ) {
64
        parent::__construct($queryContainer, $paymentMapperReader);
65
        $this->executionAdapter = $executionAdapter;
66
        $this->queryContainer = $queryContainer;
67
        $this->standardParameter = $standardParameter;
68
        $this->standartParameterMapper = $standartParameterMapper;
69
        $this->debitResponseMapper = $debitResponseMapper;
70
    }
71
72
    /**
73
     * @param int $idPayment
74
     *
75
     * @return \Generated\Shared\Transfer\DebitResponseTransfer
76
     */
77
    public function debitPayment(int $idPayment): DebitResponseTransfer
78
    {
79
        $paymentEntity = $this->getPaymentEntity($idPayment);
80
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
81
82
        $requestContainer = $paymentMethodMapper->mapPaymentToDebit($paymentEntity);
83
        $this->standartParameterMapper->setStandardParameter($requestContainer, $this->standardParameter);
84
85
        $paymentEntity = $this->findPaymentByTransactionId($paymentEntity->getTransactionId());
86
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
87
88
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
89
        $responseContainer = new DebitResponseContainer($rawResponse);
90
91
        $this->updateApiLogAfterDebit($apiLogEntity, $responseContainer);
92
93
        return $this->debitResponseMapper->getDebitResponseTransfer($responseContainer);
94
    }
95
96
    /**
97
     * @param int $transactionId
98
     *
99
     * @return \Orm\Zed\Payone\Persistence\SpyPaymentPayone
100
     */
101
    protected function findPaymentByTransactionId(int $transactionId): SpyPaymentPayone
102
    {
103
        return $this->queryContainer->createPaymentByTransactionIdQuery($transactionId)->findOne();
104
    }
105
106
    /**
107
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
108
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\DebitResponseContainer $responseContainer
109
     *
110
     * @return void
111
     */
112
    protected function updateApiLogAfterDebit(SpyPaymentPayoneApiLog $apiLogEntity, DebitResponseContainer $responseContainer): void
113
    {
114
        $apiLogEntity->setStatus($responseContainer->getStatus());
115
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
116
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
117
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
118
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
119
120
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
121
        $apiLogEntity->save();
122
    }
123
}
124