Completed
Push — master ( 962ec1...932646 )
by
unknown
19s queued 15s
created

addDataToCaptureTransferFromDistributedOrder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

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

filter:
    dependency_paths: ["lib/*"]

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

Loading history...
12
use Generated\Shared\Transfer\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...
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\Request\Container\Capture\BusinessContainer;
16
use SprykerEco\Zed\Payone\Business\Api\Response\Container\CaptureResponseContainer;
17
use SprykerEco\Zed\Payone\Business\Api\Response\Mapper\CaptureResponseMapperInterface;
18
use SprykerEco\Zed\Payone\Business\Distributor\OrderPriceDistributorInterface;
19
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\ExpenseMapperInterface;
20
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\PayoneRequestProductDataMapperInterface;
21
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface;
22
use SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface;
23
use SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface;
24
25
class PayoneCaptureRequestSender extends AbstractPayoneRequestSender implements PayoneCaptureRequestSenderInterface
26
{
27
    /**
28
     * @deprecated Necessary in order to save compatibility with spryker/shipping version less than "^8.0.0".
29
     * use \Spryker\Shared\Shipment\ShipmentConfig::SHIPMENT_EXPENSE_TYPE instead if shipping version is higher
30
     *
31
     * @see \Spryker\Shared\Shipment\ShipmentConstants::SHIPMENT_EXPENSE_TYPE
32
     * @see \Spryker\Shared\Shipment\ShipmentConfig::SHIPMENT_EXPENSE_TYPE
33
     */
34
    protected const SHIPMENT_EXPENSE_TYPE = 'SHIPMENT_EXPENSE_TYPE';
35
36
    /**
37
     * @var \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface
38
     */
39
    protected $executionAdapter;
40
41
    /**
42
     * @var \Generated\Shared\Transfer\PayoneStandardParameterTransfer
43
     */
44
    protected $standardParameter;
45
46
    /**
47
     * @var \SprykerEco\Zed\Payone\Business\Distributor\OrderPriceDistributorInterface
48
     */
49
    protected $orderPriceDistributor;
50
51
    /**
52
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface
53
     */
54
    protected $standartParameterMapper;
55
56
    /**
57
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\PayoneRequestProductDataMapperInterface
58
     */
59
    protected $payoneRequestProductDataMapper;
60
61
    /**
62
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\ExpenseMapperInterface
63
     */
64
    protected $expenseMapper;
65
66
    /**
67
     * @var \SprykerEco\Zed\Payone\Business\Api\Response\Mapper\CaptureResponseMapperInterface
68
     */
69
    protected $captureResponseMapper;
70
71
    /**
72
     * @param \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface $executionAdapter
73
     * @param \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface $queryContainer
74
     * @param \SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface $paymentMapperReader
75
     * @param \Generated\Shared\Transfer\PayoneStandardParameterTransfer $standardParameter
76
     * @param \SprykerEco\Zed\Payone\Business\Distributor\OrderPriceDistributorInterface $orderPriceDistributor
77
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface $standartParameterMapper
78
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\PayoneRequestProductDataMapperInterface $payoneRequestProductDataMapper
79
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\ExpenseMapperInterface $expenseMapper
80
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Mapper\CaptureResponseMapperInterface $captureResponseMapper
81
     */
82
    public function __construct(
83
        AdapterInterface $executionAdapter,
84
        PayoneQueryContainerInterface $queryContainer,
85
        PaymentMapperReaderInterface $paymentMapperReader,
86
        PayoneStandardParameterTransfer $standardParameter,
87
        OrderPriceDistributorInterface $orderPriceDistributor,
88
        StandartParameterMapperInterface $standartParameterMapper,
89
        PayoneRequestProductDataMapperInterface $payoneRequestProductDataMapper,
90
        ExpenseMapperInterface $expenseMapper,
91
        CaptureResponseMapperInterface $captureResponseMapper
92
    ) {
93
        parent::__construct($queryContainer, $paymentMapperReader);
94
        $this->executionAdapter = $executionAdapter;
95
        $this->standardParameter = $standardParameter;
96
        $this->orderPriceDistributor = $orderPriceDistributor;
97
        $this->standartParameterMapper = $standartParameterMapper;
98
        $this->payoneRequestProductDataMapper = $payoneRequestProductDataMapper;
99
        $this->expenseMapper = $expenseMapper;
100
        $this->captureResponseMapper = $captureResponseMapper;
101
    }
102
103
    /**
104
     * @param \Generated\Shared\Transfer\PayoneCaptureTransfer $captureTransfer
105
     *
106
     * @return \Generated\Shared\Transfer\CaptureResponseTransfer
107
     */
108
    public function capturePayment(PayoneCaptureTransfer $captureTransfer): CaptureResponseTransfer
109
    {
110
        $captureTransfer = $this->addDataToCaptureTransferFromDistributedOrder($captureTransfer);
111
112
        $paymentEntity = $this->getPaymentEntity($captureTransfer->getPayment()->getFkSalesOrder());
113
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
114
115
        $requestContainer = $paymentMethodMapper->mapPaymentToCapture($paymentEntity);
116
117
        if ($captureTransfer->getAmount()) {
118
            $requestContainer = $this->payoneRequestProductDataMapper->mapProductData($captureTransfer->getOrder(), $requestContainer);
119
            $requestContainer = $this->expenseMapper->mapExpenses($captureTransfer->getOrder(), $requestContainer);
120
        }
121
122
        /** @var \SprykerEco\Zed\Payone\Business\Api\Request\Container\CaptureContainer $requestContainer */
123
        if (!empty($captureTransfer->getSettleaccount())) {
124
            $businnessContainer = new BusinessContainer();
125
            $businnessContainer->setSettleAccount($captureTransfer->getSettleaccount());
126
            $requestContainer->setBusiness($businnessContainer);
127
        }
128
129
        if ($captureTransfer->getAmount() !== null) {
130
            $requestContainer->setAmount($captureTransfer->getAmount());
131
        }
132
133
        $this->standartParameterMapper->setStandardParameter($requestContainer, $this->standardParameter);
134
135
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
136
137
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
138
        $responseContainer = new CaptureResponseContainer($rawResponse);
139
        $this->updateApiLogAfterCapture($apiLogEntity, $responseContainer);
140
141
        return $this->captureResponseMapper->getCaptureResponseTransfer($responseContainer);
142
    }
143
144
    /**
145
     * @param \Generated\Shared\Transfer\PayoneCaptureTransfer $captureTransfer
146
     *
147
     * @return \Generated\Shared\Transfer\CaptureResponseTransfer
148
     */
149
    protected function addDataToCaptureTransferFromDistributedOrder(PayoneCaptureTransfer $captureTransfer): CaptureResponseTransfer
150
    {
151
        $distributedPriceOrderTransfer = $this->orderPriceDistributor->distributeOrderPrice(
152
            $captureTransfer->getOrder()
153
        );
154
155
        $captureTransfer->setOrder($distributedPriceOrderTransfer);
156
157
        if ($captureTransfer->getAmount() === null) {
158
            $captureTransfer->setAmount($distributedPriceOrderTransfer->getTotals()->getGrandTotal());
159
        }
160
161
        return $captureTransfer;
162
    }
163
164
    /**
165
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
166
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\CaptureResponseContainer $responseContainer
167
     *
168
     * @return void
169
     */
170
    protected function updateApiLogAfterCapture(SpyPaymentPayoneApiLog $apiLogEntity, CaptureResponseContainer $responseContainer): void
171
    {
172
        $apiLogEntity->setStatus($responseContainer->getStatus());
173
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
174
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
175
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
176
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
177
178
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
179
        $apiLogEntity->save();
180
    }
181
}
182