Passed
Push — feature/eco-3623-payone-pay-u-... ( ca7903...0352c0 )
by Roman
03:37
created

getPaymentTransferFromQuote()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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\Computop\Business\Payment\Handler\Saver\Init;
9
10
use Generated\Shared\Transfer\ComputopApiResponseHeaderTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...iResponseHeaderTransfer 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\ComputopPaymentComputopOrderItemCollectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...rItemCollectionTransfer 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\ComputopPaymentComputopTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...PaymentComputopTransfer 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\ComputopPayuCeeSingleInitResponseTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...gleInitResponseTransfer 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\ComputopSalesOrderItemTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...pSalesOrderItemTransfer 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 Generated\Shared\Transfer\PaymentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\PaymentTransfer 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 Generated\Shared\Transfer\QuoteTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfer\QuoteTransfer 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...
17
use Spryker\Zed\Kernel\Persistence\EntityManager\TransactionTrait;
18
use SprykerEco\Shared\Computop\ComputopConfig as SharedComputopConfig;
19
use SprykerEco\Zed\Computop\ComputopConfig;
20
use SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface;
21
use SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface;
22
23
class PayuCeeSingleResponseSaver implements InitResponseSaverInterface
24
{
25
    use TransactionTrait;
26
27
    /**
28
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface
29
     */
30
    protected $computopEntityManager;
31
32
    /**
33
     * @var \SprykerEco\Zed\Computop\ComputopConfig
34
     */
35
    protected $computopConfig;
36
37
    /**
38
     * @var \SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface
39
     */
40
    protected $computopRepository;
41
42
    /**
43
     * @param \SprykerEco\Zed\Computop\ComputopConfig $computopConfig
44
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopRepositoryInterface $computopRepository
45
     * @param \SprykerEco\Zed\Computop\Persistence\ComputopEntityManagerInterface $computopEntityManager
46
     */
47
    public function __construct(
48
        ComputopConfig $computopConfig,
49
        ComputopRepositoryInterface $computopRepository,
50
        ComputopEntityManagerInterface $computopEntityManager
51
    ) {
52
        $this->computopConfig = $computopConfig;
53
        $this->computopRepository = $computopRepository;
54
        $this->computopEntityManager = $computopEntityManager;
55
    }
56
57
    /**
58
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
59
     *
60
     * @return \Generated\Shared\Transfer\QuoteTransfer
61
     */
62
    public function save(QuoteTransfer $quoteTransfer): QuoteTransfer
63
    {
64
        $computopPayuCeeSingleInitResponseTransfer = $this->getPaymentTransferFromQuote($quoteTransfer)
65
            ->getComputopPayuCeeSingleOrFail()
66
            ->getPayuCeeSingleInitResponseOrFail();
67
68
        $computopApiResponseHeaderTransfer = $computopPayuCeeSingleInitResponseTransfer->getHeaderOrFail()
69
            ->requireTransId()
70
            ->requirePayId()
71
            ->requireXid();
72
73
        if (!$computopApiResponseHeaderTransfer->getIsSuccess()) {
74
            return $quoteTransfer;
75
        }
76
77
        $computopPaymentComputopTransfer = $this->computopRepository->findComputopPaymentByComputopTransId(
78
            $computopApiResponseHeaderTransfer->getTransId()
79
        );
80
        if ($computopPaymentComputopTransfer === null) {
81
            return $quoteTransfer;
82
        }
83
84
        $this->getTransactionHandler()->handleTransaction(function () use ($computopPaymentComputopTransfer, $computopPayuCeeSingleInitResponseTransfer) {
85
            $this->executeSavePaymentResponseTransaction($computopPaymentComputopTransfer, $computopPayuCeeSingleInitResponseTransfer);
86
        });
87
88
        return $quoteTransfer;
89
    }
90
91
    /**
92
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
93
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
94
     *
95
     * @return void
96
     */
97
    protected function executeSavePaymentResponseTransaction(
98
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
99
        ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
100
    ): void {
101
        $this->savePaymentComputopEntity($computopPaymentComputopTransfer, $computopPayuCeeSingleInitResponseTransfer->getHeader());
102
        $this->savePaymentComputopDetailEntity($computopPaymentComputopTransfer, $computopPayuCeeSingleInitResponseTransfer);
103
        $paymentStatus = $this->getOrderItemPaymentStatusFromResponseHeader($computopPayuCeeSingleInitResponseTransfer->getHeader());
104
        if ($paymentStatus) {
105
            $this->savePaymentComputopOrderItemsEntities($computopPaymentComputopTransfer, $paymentStatus);
106
        }
107
    }
108
109
    /**
110
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
111
     *
112
     * @return \Generated\Shared\Transfer\PaymentTransfer
113
     */
114
    protected function getPaymentTransferFromQuote(QuoteTransfer $quoteTransfer): PaymentTransfer
115
    {
116
        foreach ($quoteTransfer->getPayments() as $paymentTransfer) {
117
            if ($paymentTransfer !== null) {
118
                return $paymentTransfer;
119
            }
120
        }
121
122
        return $quoteTransfer->getPaymentOrFail();
123
    }
124
125
    /**
126
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
127
     * @param \Generated\Shared\Transfer\ComputopApiResponseHeaderTransfer $computopApiResponseHeaderTransfer
128
     *
129
     * @return void
130
     */
131
    protected function savePaymentComputopEntity(
132
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
133
        ComputopApiResponseHeaderTransfer $computopApiResponseHeaderTransfer
134
    ): void {
135
        $computopPaymentComputopTransfer
136
            ->setPayId($computopApiResponseHeaderTransfer->getPayId())
137
            ->setXId($computopApiResponseHeaderTransfer->getXId());
138
139
        $this->computopEntityManager->updateComputopPayment($computopPaymentComputopTransfer);
140
    }
141
142
    /**
143
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
144
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
145
     *
146
     * @return void
147
     */
148
    protected function savePaymentComputopDetailEntity(
149
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
150
        ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
151
    ): void {
152
        $computopPaymentComputopDetailTransfer = $this->computopRepository->findComputopPaymentDetail($computopPaymentComputopTransfer);
153
        if (!$computopPaymentComputopDetailTransfer) {
154
            return;
155
        }
156
157
        $computopPaymentComputopDetailTransfer->fromArray($computopPayuCeeSingleInitResponseTransfer->toArray(), true);
158
        $computopPaymentComputopDetailTransfer->setCustomerTransactionId(
159
            (int)$computopPaymentComputopDetailTransfer->getCustomerTransactionId()
160
        );
161
162
        $this->computopEntityManager->updateComputopPaymentDetail($computopPaymentComputopDetailTransfer);
163
    }
164
165
    /**
166
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopTransfer $computopPaymentComputopTransfer
167
     * @param string $paymentStatus
168
     *
169
     * @return void
170
     */
171
    protected function savePaymentComputopOrderItemsEntities(
172
        ComputopPaymentComputopTransfer $computopPaymentComputopTransfer,
173
        string $paymentStatus
174
    ): void {
175
        $salesOrderItemsCollectionTransfer = $this->computopRepository
176
            ->getComputopSalesOrderItemCollection($computopPaymentComputopTransfer);
177
178
        $computopPaymentComputopOrderItemsCollection = $this->computopRepository
179
            ->getComputopPaymentComputopOrderItemCollection($computopPaymentComputopTransfer);
180
181
        foreach ($salesOrderItemsCollectionTransfer->getComputopSalesOrderItems() as $computopSalesOrderItem) {
182
            $this->updateComputopSalesOrderItem(
183
                $computopPaymentComputopOrderItemsCollection,
184
                $computopSalesOrderItem,
185
                $paymentStatus
186
            );
187
        }
188
    }
189
190
    /**
191
     * @param \Generated\Shared\Transfer\ComputopPaymentComputopOrderItemCollectionTransfer $computopPaymentComputopOrderItemsCollection
192
     * @param \Generated\Shared\Transfer\ComputopSalesOrderItemTransfer $computopSalesOrderItem
193
     * @param string $paymentStatus
194
     *
195
     * @return void
196
     */
197
    protected function updateComputopSalesOrderItem(
198
        ComputopPaymentComputopOrderItemCollectionTransfer $computopPaymentComputopOrderItemsCollection,
199
        ComputopSalesOrderItemTransfer $computopSalesOrderItem,
200
        string $paymentStatus
201
    ): void {
202
        foreach ($computopPaymentComputopOrderItemsCollection->getComputopPaymentComputopOrderItems() as $computopPaymentComputopOrderItem) {
203
            if ($computopSalesOrderItem->getIdSalesOrderItem() !== $computopPaymentComputopOrderItem->getFkSalesOrderItem()) {
204
                continue;
205
            }
206
207
            $computopPaymentComputopOrderItem->setStatus($paymentStatus);
208
            $this->computopEntityManager->updateComputopPaymentComputopOrderItem($computopPaymentComputopOrderItem);
209
210
            return;
211
        }
212
    }
213
214
    /**
215
     * @param \Generated\Shared\Transfer\ComputopApiResponseHeaderTransfer $computopApiResponseHeaderTransfer
216
     *
217
     * @return string
218
     */
219
    protected function getOrderItemPaymentStatusFromResponseHeader(
220
        ComputopApiResponseHeaderTransfer $computopApiResponseHeaderTransfer
221
    ): ?string {
222
        if ($computopApiResponseHeaderTransfer->getStatus() === SharedComputopConfig::AUTHORIZE_REQUEST_STATUS) {
223
            return $this->computopConfig->getAuthorizeRequestOmsStatus();
224
        }
225
226
        if ($computopApiResponseHeaderTransfer->getStatus() === SharedComputopConfig::SUCCESS_OK) {
227
            return $this->computopConfig->getOmsStatusAuthorized();
228
        }
229
230
        return null;
231
    }
232
}
233