PayonePartialCaptureRequestSender::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 23
rs 9.9332
cc 1
nc 1
nop 11

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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\OrderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\OrderTransfer 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\PayoneOrderItemFilterTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...OrderItemFilterTransfer 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 Generated\Shared\Transfer\PayonePartialOperationRequestTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...perationRequestTransfer 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 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...
15
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...
16
use Spryker\Shared\Shipment\ShipmentConfig;
17
use SprykerEco\Shared\Payone\PayoneApiConstants;
18
use SprykerEco\Shared\Payone\PayoneTransactionStatusConstants;
19
use SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface;
20
use SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer;
21
use SprykerEco\Zed\Payone\Business\Api\Request\Container\Capture\BusinessContainer;
22
use SprykerEco\Zed\Payone\Business\Api\Response\Container\CaptureResponseContainer;
23
use SprykerEco\Zed\Payone\Business\Api\Response\Mapper\CaptureResponseMapperInterface;
24
use SprykerEco\Zed\Payone\Business\Distributor\OrderPriceDistributorInterface;
25
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\ExpenseMapperInterface;
26
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\ShipmentMapperInterface;
27
use SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface;
28
use SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface;
29
use SprykerEco\Zed\Payone\Persistence\PayoneEntityManagerInterface;
30
use SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface;
31
use SprykerEco\Zed\Payone\Persistence\PayoneRepositoryInterface;
32
33
class PayonePartialCaptureRequestSender extends AbstractPayoneRequestSender implements PayonePartialCaptureRequestSenderInterface
34
{
35
    /**
36
     * @var \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface
37
     */
38
    protected $executionAdapter;
39
40
    /**
41
     * @var \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface
42
     */
43
    protected $queryContainer;
44
45
    /**
46
     * @var \Generated\Shared\Transfer\PayoneStandardParameterTransfer
47
     */
48
    protected $standardParameter;
49
50
    /**
51
     * @var \SprykerEco\Zed\Payone\Persistence\PayoneRepositoryInterface
52
     */
53
    protected $payoneRepository;
54
55
    /**
56
     * @var \SprykerEco\Zed\Payone\Persistence\PayoneEntityManagerInterface
57
     */
58
    protected $payoneEntityManager;
59
60
    /**
61
     * @var \SprykerEco\Zed\Payone\Business\Distributor\OrderPriceDistributorInterface
62
     */
63
    protected $orderPriceDistributor;
64
65
    /**
66
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface
67
     */
68
    protected $standartParameterMapper;
69
70
    /**
71
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\ShipmentMapperInterface
72
     */
73
    protected $shipmentMapper;
74
75
    /**
76
     * @var \SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface
77
     */
78
    protected $paymentMapperReader;
79
80
    /**
81
     * @var \SprykerEco\Zed\Payone\Business\Payment\DataMapper\ExpenseMapperInterface
82
     */
83
    protected $expenseMapper;
84
85
    /**
86
     * @var \SprykerEco\Zed\Payone\Business\Api\Response\Mapper\CaptureResponseMapperInterface
87
     */
88
    protected $captureResponseMapper;
89
90
    /**
91
     * @param \SprykerEco\Zed\Payone\Business\Api\Adapter\AdapterInterface $executionAdapter
92
     * @param \SprykerEco\Zed\Payone\Persistence\PayoneQueryContainerInterface $queryContainer
93
     * @param \SprykerEco\Zed\Payone\Business\Payment\PaymentMapperReaderInterface $paymentMapperReader
94
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\ExpenseMapperInterface $expenseMapper
95
     * @param \Generated\Shared\Transfer\PayoneStandardParameterTransfer $standardParameter
96
     * @param \SprykerEco\Zed\Payone\Persistence\PayoneRepositoryInterface $payoneRepository
97
     * @param \SprykerEco\Zed\Payone\Persistence\PayoneEntityManagerInterface $payoneEntityManager
98
     * @param \SprykerEco\Zed\Payone\Business\Distributor\OrderPriceDistributorInterface $orderPriceDistributor
99
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\StandartParameterMapperInterface $standartParameterMapper
100
     * @param \SprykerEco\Zed\Payone\Business\Payment\DataMapper\ShipmentMapperInterface $shipmentMapper
101
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Mapper\CaptureResponseMapperInterface $captureResponseMapper
102
     */
103
    public function __construct(
104
        AdapterInterface $executionAdapter,
105
        PayoneQueryContainerInterface $queryContainer,
106
        PaymentMapperReaderInterface $paymentMapperReader,
107
        ExpenseMapperInterface $expenseMapper,
108
        PayoneStandardParameterTransfer $standardParameter,
109
        PayoneRepositoryInterface $payoneRepository,
110
        PayoneEntityManagerInterface $payoneEntityManager,
111
        OrderPriceDistributorInterface $orderPriceDistributor,
112
        StandartParameterMapperInterface $standartParameterMapper,
113
        ShipmentMapperInterface $shipmentMapper,
114
        CaptureResponseMapperInterface $captureResponseMapper
115
    ) {
116
        parent::__construct($queryContainer, $paymentMapperReader);
117
        $this->executionAdapter = $executionAdapter;
118
        $this->standardParameter = $standardParameter;
119
        $this->payoneRepository = $payoneRepository;
120
        $this->payoneEntityManager = $payoneEntityManager;
121
        $this->orderPriceDistributor = $orderPriceDistributor;
122
        $this->standartParameterMapper = $standartParameterMapper;
123
        $this->shipmentMapper = $shipmentMapper;
124
        $this->expenseMapper = $expenseMapper;
125
        $this->captureResponseMapper = $captureResponseMapper;
126
    }
127
128
    /**
129
     * @param \Generated\Shared\Transfer\PayonePartialOperationRequestTransfer $payonePartialOperationRequestTransfer
130
     *
131
     * @return \Generated\Shared\Transfer\CaptureResponseTransfer
132
     */
133
    public function executePartialCapture(
134
        PayonePartialOperationRequestTransfer $payonePartialOperationRequestTransfer
135
    ): CaptureResponseTransfer {
136
        $distributedPriceOrderTransfer = $this->orderPriceDistributor->distributeOrderPrice(
137
            $payonePartialOperationRequestTransfer->getOrder()
138
        );
139
        $payonePartialOperationRequestTransfer->setOrder($distributedPriceOrderTransfer);
140
141
        $paymentEntity = $this->getPaymentEntity($payonePartialOperationRequestTransfer->getOrder()->getIdSalesOrder());
142
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
143
144
        $requestContainer = $paymentMethodMapper->mapPaymentToCapture($paymentEntity);
145
        $requestContainer = $this->preparePartialCaptureOrderItems($payonePartialOperationRequestTransfer, $requestContainer);
146
        $captureAmount = $this->calculatePartialCaptureItemsAmount($payonePartialOperationRequestTransfer);
147
148
        $captureAmount += $this->getDeliveryCosts($payonePartialOperationRequestTransfer->getOrder());
149
        $requestContainer = $this->shipmentMapper->mapShipment($payonePartialOperationRequestTransfer->getOrder(), $requestContainer);
150
151
        $captureAmount += $this->calculateExpensesCost($payonePartialOperationRequestTransfer->getOrder());
152
        /** @var \SprykerEco\Zed\Payone\Business\Api\Request\Container\CaptureContainer $requestContainer */
153
        $requestContainer = $this->expenseMapper->mapExpenses($payonePartialOperationRequestTransfer->getOrder(), $requestContainer);
154
155
        $businessContainer = new BusinessContainer();
156
        $businessContainer->setSettleAccount(PayoneApiConstants::SETTLE_ACCOUNT_YES);
157
        $requestContainer->setBusiness($businessContainer);
158
159
        $requestContainer->setAmount($captureAmount);
160
        $this->standartParameterMapper->setStandardParameter($requestContainer, $this->standardParameter);
161
162
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
163
164
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
165
        $responseContainer = new CaptureResponseContainer($rawResponse);
166
167
        $this->updateApiLogAfterCapture($apiLogEntity, $responseContainer);
168
        $this->updatePaymentPayoneOrderItemsWithStatus(
169
            $payonePartialOperationRequestTransfer,
170
            $this->getPartialCaptureStatus($responseContainer)
171
        );
172
173
        return $this->captureResponseMapper->getCaptureResponseTransfer($responseContainer);
174
    }
175
176
    /**
177
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
178
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\CaptureResponseContainer $responseContainer
179
     *
180
     * @return void
181
     */
182
    protected function updateApiLogAfterCapture(SpyPaymentPayoneApiLog $apiLogEntity, CaptureResponseContainer $responseContainer): void
183
    {
184
        $apiLogEntity->setStatus($responseContainer->getStatus());
185
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
186
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
187
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
188
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
189
190
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
191
        $apiLogEntity->save();
192
    }
193
194
    /**
195
     * @param \Generated\Shared\Transfer\PayonePartialOperationRequestTransfer $payonePartialOperationRequestTransfer
196
     * @param \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer $container
197
     *
198
     * @return \SprykerEco\Zed\Payone\Business\Api\Request\Container\AbstractRequestContainer
199
     */
200
    protected function preparePartialCaptureOrderItems(
201
        PayonePartialOperationRequestTransfer $payonePartialOperationRequestTransfer,
202
        AbstractRequestContainer $container
203
    ): AbstractRequestContainer {
204
        $arrayIt = [];
205
        $arrayId = [];
206
        $arrayPr = [];
207
        $arrayNo = [];
208
        $arrayDe = [];
209
        $arrayVa = [];
210
211
        $key = 1;
212
213
        foreach ($payonePartialOperationRequestTransfer->getOrder()->getItems() as $itemTransfer) {
214
            if (!in_array($itemTransfer->getIdSalesOrderItem(), $payonePartialOperationRequestTransfer->getSalesOrderItemIds())) {
215
                continue;
216
            }
217
218
            $arrayIt[$key] = PayoneApiConstants::INVOICING_ITEM_TYPE_GOODS;
219
            $arrayId[$key] = substr($itemTransfer->getSku(), 0, 32);
220
            $arrayPr[$key] = $itemTransfer->getUnitPriceToPayAggregation();
221
            $arrayNo[$key] = $itemTransfer->getQuantity();
222
            $arrayDe[$key] = $itemTransfer->getName();
223
            $arrayVa[$key] = (int)$itemTransfer->getTaxRate();
224
            $key++;
225
        }
226
227
        $container->setIt($arrayIt);
228
        $container->setId($arrayId);
229
        $container->setPr($arrayPr);
230
        $container->setNo($arrayNo);
231
        $container->setDe($arrayDe);
232
        $container->setVa($arrayVa);
233
234
        return $container;
235
    }
236
237
    /**
238
     * @param \Generated\Shared\Transfer\PayonePartialOperationRequestTransfer $payonePartialOperationRequestTransfer
239
     *
240
     * @return int
241
     */
242
    protected function calculatePartialCaptureItemsAmount(PayonePartialOperationRequestTransfer $payonePartialOperationRequestTransfer): int
243
    {
244
        $itemsAmount = 0;
245
        foreach ($payonePartialOperationRequestTransfer->getOrder()->getItems() as $itemTransfer) {
246
            if (in_array($itemTransfer->getIdSalesOrderItem(), $payonePartialOperationRequestTransfer->getSalesOrderItemIds())) {
247
                $itemsAmount += $itemTransfer->getSumPriceToPayAggregation();
248
            }
249
        }
250
251
        return $itemsAmount;
252
    }
253
254
    /**
255
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
256
     *
257
     * @return int
258
     */
259
    protected function getDeliveryCosts(OrderTransfer $orderTransfer): int
260
    {
261
        foreach ($orderTransfer->getExpenses() as $expense) {
262
            if ($expense->getType() === ShipmentConfig::SHIPMENT_EXPENSE_TYPE) {
263
                return $expense->getSumGrossPrice();
264
            }
265
        }
266
267
        return 0;
268
    }
269
270
    /**
271
     * @param \Generated\Shared\Transfer\OrderTransfer $orderTransfer
272
     *
273
     * @return int
274
     */
275
    protected function calculateExpensesCost(OrderTransfer $orderTransfer): int
276
    {
277
        $expensesCostAmount = 0;
278
279
        foreach ($orderTransfer->getExpenses() as $expense) {
280
            if ($expense->getType() !== ShipmentConfig::SHIPMENT_EXPENSE_TYPE) {
281
                $expensesCostAmount += $expense->getSumGrossPrice();
282
            }
283
        }
284
285
        return $expensesCostAmount;
286
    }
287
288
    /**
289
     * @param \Generated\Shared\Transfer\PayonePartialOperationRequestTransfer $payonePartialOperationRequestTransfer
290
     * @param string $refundStatus
291
     *
292
     * @return void
293
     */
294
    protected function updatePaymentPayoneOrderItemsWithStatus(
295
        PayonePartialOperationRequestTransfer $payonePartialOperationRequestTransfer,
296
        string $refundStatus
297
    ): void {
298
        $payoneOrderItemFilterTransfer = (new PayoneOrderItemFilterTransfer())
299
            ->setIdSalesOrder($payonePartialOperationRequestTransfer->getOrder()->getIdSalesOrder())
300
            ->setSalesOrderItemIds($payonePartialOperationRequestTransfer->getSalesOrderItemIds());
301
302
        $payoneOrderItemTransfers = $this->payoneRepository->findPaymentPayoneOrderItemByFilter($payoneOrderItemFilterTransfer);
303
304
        foreach ($payoneOrderItemTransfers as $payoneOrderItemTransfer) {
305
            $payoneOrderItemTransfer->setStatus($refundStatus);
306
            $this->payoneEntityManager->updatePaymentPayoneOrderItem($payoneOrderItemTransfer);
307
        }
308
    }
309
310
    /**
311
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\CaptureResponseContainer $responseContainer
312
     *
313
     * @return string
314
     */
315
    protected function getPartialCaptureStatus(CaptureResponseContainer $responseContainer): string
316
    {
317
        if ($responseContainer->getStatus() === PayoneApiConstants::RESPONSE_TYPE_APPROVED) {
318
            return PayoneTransactionStatusConstants::STATUS_CAPTURE_APPROVED;
319
        }
320
321
        return PayoneTransactionStatusConstants::STATUS_CAPTURE_FAILED;
322
    }
323
}
324