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

PayoneCaptureRequestSender::capturePayment()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 22
c 0
b 0
f 0
dl 0
loc 38
rs 9.568
cc 4
nc 8
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
        $distributedPriceOrderTransfer = $this->orderPriceDistributor->distributeOrderPrice(
111
            $captureTransfer->getOrder()
112
        );
113
        $captureTransfer->setOrder($distributedPriceOrderTransfer);
114
        $captureTransfer->setAmount($distributedPriceOrderTransfer->getTotals()->getGrandTotal());
115
116
        $paymentEntity = $this->getPaymentEntity($captureTransfer->getPayment()->getFkSalesOrder());
117
        $paymentMethodMapper = $this->getPaymentMethodMapper($paymentEntity);
118
119
        $requestContainer = $paymentMethodMapper->mapPaymentToCapture($paymentEntity);
120
121
        if ($captureTransfer->getAmount()) {
122
            $requestContainer = $this->payoneRequestProductDataMapper->mapProductData($captureTransfer->getOrder(), $requestContainer);
123
            $requestContainer = $this->expenseMapper->mapExpenses($captureTransfer->getOrder(), $requestContainer);
124
        }
125
126
        /** @var \SprykerEco\Zed\Payone\Business\Api\Request\Container\CaptureContainer $requestContainer */
127
        if (!empty($captureTransfer->getSettleaccount())) {
128
            $businnessContainer = new BusinessContainer();
129
            $businnessContainer->setSettleAccount($captureTransfer->getSettleaccount());
130
            $requestContainer->setBusiness($businnessContainer);
131
        }
132
133
        if ($captureTransfer->getAmount() !== null) {
134
            $requestContainer->setAmount($captureTransfer->getAmount());
135
        }
136
137
        $this->standartParameterMapper->setStandardParameter($requestContainer, $this->standardParameter);
138
139
        $rawResponse = $this->executionAdapter->sendRequest($requestContainer);
140
141
        $apiLogEntity = $this->initializeApiLog($paymentEntity, $requestContainer);
142
        $responseContainer = new CaptureResponseContainer($rawResponse);
143
        $this->updateApiLogAfterCapture($apiLogEntity, $responseContainer);
144
145
        return $this->captureResponseMapper->getCaptureResponseTransfer($responseContainer);
146
    }
147
148
    /**
149
     * @param \Orm\Zed\Payone\Persistence\SpyPaymentPayoneApiLog $apiLogEntity
150
     * @param \SprykerEco\Zed\Payone\Business\Api\Response\Container\CaptureResponseContainer $responseContainer
151
     *
152
     * @return void
153
     */
154
    protected function updateApiLogAfterCapture(SpyPaymentPayoneApiLog $apiLogEntity, CaptureResponseContainer $responseContainer): void
155
    {
156
        $apiLogEntity->setStatus($responseContainer->getStatus());
157
        $apiLogEntity->setTransactionId($responseContainer->getTxid());
158
        $apiLogEntity->setErrorMessageInternal($responseContainer->getErrormessage());
159
        $apiLogEntity->setErrorMessageUser($responseContainer->getCustomermessage());
160
        $apiLogEntity->setErrorCode($responseContainer->getErrorcode());
161
162
        $apiLogEntity->setRawResponse(json_encode($responseContainer->toArray()));
163
        $apiLogEntity->save();
164
    }
165
}
166