updateApiLogAfterAuthorization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 13
rs 9.9332
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\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...
11
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...
12
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...
13
use SprykerEco\Shared\Payone\PayoneApiConstants;
14
use SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface;
15
use SprykerEco\Zed\Payone\Business\Api\Request\Container\AuthorizationContainerInterface;
16
use SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer;
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 PayoneBaseAuthorizeSender extends AbstractPayoneRequestSender implements PayoneBaseAuthorizeSenderInterface
22
{
23
    /**
24
     * @var \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface
25
     */
26
    protected $executionAdapter;
27
28
    /**
29
     * @var \Generated\Shared\Transfer\PayoneStandardParameterTransfer
30
     */
31
    protected $standardParameter;
32
33
    /**
34
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface
35
     */
36
    protected $standartParameterMapper;
37
38
    /**
39
     * @param \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface $executionAdapter
40
     * @param \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface $queryContainer
41
     * @param \SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface $paymentMapperReader
42
     * @param \Generated\Shared\Transfer\PayoneStandardParameterTransfer $standardParameter
43
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface $standartParameterMapper
44
     */
45
    public function __construct(
46
        AdapterInterface $executionAdapter,
47
        PayoneQueryContainerInterface $queryContainer,
48
        PaymentMapperReaderInterface $paymentMapperReader,
49
        PayoneStandardParameterTransfer $standardParameter,
50
        StandartParameterMapperInterface $standartParameterMapper
51
    ) {
52
        parent::__construct($queryContainer, $paymentMapperReader);
53
        $this->executionAdapter = $executionAdapter;
54
        $this->standardParameter = $standardParameter;
55
        $this->standartParameterMapper = $standartParameterMapper;
56
    }
57
58
    /**
59
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
60
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AuthorizationContainerInterface $requestContainer
61
     *
62
     * @return \SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer
63
     */
64
    public function performAuthorizationRequest(
65
        SpyPaymentPayone $paymentEntity,
66
        AuthorizationContainerInterface $requestContainer
67
    ): AuthorizationResponseContainer {
68
        $this->standartParameterMapper->setStandardParameter($requestContainer, $this->standardParameter);
69
70
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
71
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
72
        $responseContainer = new AuthorizationResponseContainer($rawResponse);
73
        $this->updatePaymentAfterAuthorization($paymentEntity, $responseContainer);
74
        $this->updateApiLogAfterAuthorization($apiLogEntity, $responseContainer);
75
        $this->updatePaymentDetailAfterAuthorization($paymentEntity, $responseContainer);
76
77
        return $responseContainer;
78
    }
79
80
    /**
81
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
82
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer $responseContainer
83
     *
84
     * @return void
85
     */
86
    protected function updatePaymentAfterAuthorization(SpyPaymentPayone $paymentEntity, AuthorizationResponseContainer $responseContainer): void
87
    {
88
        $paymentEntity->setTransactionId($responseContainer->getTxid());
89
        $paymentEntity->save();
90
    }
91
92
    /**
93
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
94
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer $responseContainer
95
     *
96
     * @return void
97
     */
98
    protected function updateApiLogAfterAuthorization(SpyPaymentPayoneApiLog $apiLogEntity, AuthorizationResponseContainer $responseContainer): void
99
    {
100
        $apiLogEntity->setStatus($responseContainer->getStatus());
101
        $apiLogEntity->setUserId($responseContainer->getUserid());
102
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
103
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
104
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
105
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
106
        $apiLogEntity->setRedirectUrl($responseContainer->getRedirecturl());
107
        $apiLogEntity->setSequenceNumber(0);
108
109
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
110
        $apiLogEntity->save();
111
    }
112
113
    /**
114
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayone $paymentEntity
115
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\AuthorizationResponseContainer $responseContainer
116
     *
117
     * @return void
118
     */
119
    protected function updatePaymentDetailAfterAuthorization(SpyPaymentPayone $paymentEntity, AuthorizationResponseContainer $responseContainer): void
120
    {
121
        $paymentDetailEntity = $paymentEntity->getSpyPaymentPayoneDetail();
122
123
        $paymentDetailEntity->setClearingBankAccountHolder($responseContainer->getClearingBankaccountholder());
124
        $paymentDetailEntity->setClearingBankCountry($responseContainer->getClearingBankcountry());
125
        $paymentDetailEntity->setClearingBankAccount($responseContainer->getClearingBankaccount());
126
        $paymentDetailEntity->setClearingBankCode($responseContainer->getClearingBankcode());
127
        $paymentDetailEntity->setClearingBankIban($responseContainer->getClearingBankiban());
128
        $paymentDetailEntity->setClearingBankBic($responseContainer->getClearingBankbic());
129
        $paymentDetailEntity->setClearingBankCity($responseContainer->getClearingBankcity());
130
        $paymentDetailEntity->setClearingBankName($responseContainer->getClearingBankname());
131
132
        if ($responseContainer->getMandateIdentification()) {
133
            $paymentDetailEntity->setMandateIdentification($responseContainer->getMandateIdentification());
134
        }
135
136
        if ($paymentEntity->getPaymentMethod() == PayoneApiConstants::PAYMENT_METHOD_INVOICE) {
137
            $invoiceTitle = $this->getInvoiceTitle($paymentEntity->getTransactionId());
138
            $paymentDetailEntity->setInvoiceTitle($invoiceTitle);
139
        }
140
141
        $paymentDetailEntity->save();
142
    }
143
144
    /**
145
     * @param int|null $transactionId
146
     *
147
     * @return string
148
     */
149
    public function getInvoiceTitle(?int $transactionId): string
150
    {
151
        return implode('-', [
152
            PayoneApiConstants::INVOICE_TITLE_PREFIX_INVOICE,
153
            $transactionId,
154
            0,
155
        ]);
156
    }
157
}
158