Passed
Push — feature/eco-3623-payone-pay-u-... ( f81b95...9736bb )
by Roman
04:54
created

saveComputopPayuCeeSingleInitResponse()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
c 0
b 0
f 0
dl 0
loc 29
rs 9.584
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\Computop\Persistence;
9
10
use Generated\Shared\Transfer\ComputopNotificationTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...topNotificationTransfer 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\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...
12
use Orm\Zed\Computop\Persistence\SpyPaymentComputopDetail;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Computop\Persist...pyPaymentComputopDetail 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\Computop\Persistence\SpyPaymentComputopOrderItem;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Computop\Persist...aymentComputopOrderItem 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 Propel\Runtime\Collection\ObjectCollection;
15
use Spryker\Shared\Kernel\Transfer\TransferInterface;
16
use Spryker\Zed\Kernel\Persistence\AbstractEntityManager;
17
use SprykerEco\Shared\Computop\ComputopConfig as SharedComputopConfig;
18
use SprykerEco\Zed\Computop\ComputopConfig;
19
20
/**
21
 * @method \SprykerEco\Zed\Computop\Persistence\ComputopPersistenceFactory getFactory()
22
 */
23
class ComputopEntityManager extends AbstractEntityManager implements ComputopEntityManagerInterface
24
{
25
    /**
26
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
27
     *
28
     * @return void
29
     */
30
    public function savePaymentComputopNotification(ComputopNotificationTransfer $computopNotificationTransfer): void
31
    {
32
        $paymentComputopNotificationEntity = $this->getFactory()
33
            ->createPaymentComputopNotificationQuery()
34
            ->filterByPayId($computopNotificationTransfer->getPayId())
35
            ->filterByTransId($computopNotificationTransfer->getTransId())
36
            ->filterByXId($computopNotificationTransfer->getXId())
37
            ->findOneOrCreate();
38
39
        $paymentComputopNotificationEntity->fromArray(
40
            $computopNotificationTransfer->modifiedToArray()
41
        );
42
        $paymentComputopNotificationEntity->save();
43
    }
44
45
    /**
46
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
47
     *
48
     * @return bool
49
     */
50
    public function updatePaymentComputopOrderItemPaymentConfirmation(
51
        ComputopNotificationTransfer $computopNotificationTransfer
52
    ): bool {
53
        /** @var \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem[]|\Propel\Runtime\Collection\ObjectCollection $paymentComputopOrderItemEntities */
54
        $paymentComputopOrderItemEntities = $this->getFactory()
55
            ->createPaymentComputopOrderItemQuery()
56
            ->useSpyPaymentComputopQuery()
57
                ->filterByTransId($computopNotificationTransfer->getTransId())
58
                ->filterByPayId($computopNotificationTransfer->getPayId())
59
            ->endUse()
60
            ->find();
61
62
        if (!$paymentComputopOrderItemEntities->count()) {
63
            return false;
64
        }
65
66
        foreach ($paymentComputopOrderItemEntities as $paymentComputopOrderItemEntity) {
67
            $orderItemEntityStatus = $this->getCurrentOrderItemEntityStatus($computopNotificationTransfer, $paymentComputopOrderItemEntity);
68
            $paymentComputopOrderItemEntity
69
                ->setIsPaymentConfirmed((bool)$computopNotificationTransfer->getIsSuccess())
70
                ->setStatus($orderItemEntityStatus)
71
                ->save();
72
        }
73
74
        return true;
75
    }
76
77
    /**
78
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
79
     *
80
     * @return void
81
     */
82
    public function saveComputopPayuCeeSingleInitResponse(ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer): void {
83
        $computopApiResponseHeaderTransfer = $computopPayuCeeSingleInitResponseTransfer->requireHeader()
84
            ->getHeader()
85
                ->requireTransId()
86
                ->requirePayId()
87
                ->requireXid();
88
89
        $paymentComputopEntity = $this->getFactory()
90
            ->createPaymentComputopQuery()
91
            ->filterByTransId($computopApiResponseHeaderTransfer->getTransId())
92
            ->findOne();
93
94
        if (!$paymentComputopEntity) {
95
            return;
96
        }
97
98
        $paymentComputopEntity
99
            ->setPayId($computopApiResponseHeaderTransfer->getPayId())
100
            ->setXId($computopApiResponseHeaderTransfer->getXId())
101
            ->save();
102
103
        $this->savePaymentComputopDetailEntity(
104
            $paymentComputopEntity->getSpyPaymentComputopDetail(),
105
            $computopPayuCeeSingleInitResponseTransfer
106
        );
107
108
        $this->savePaymentComputopOrderItems(
109
            $paymentComputopEntity->getSpyPaymentComputopOrderItems(),
110
            $this->getPaymentStatus($computopPayuCeeSingleInitResponseTransfer)
111
        );
112
    }
113
114
    /**
115
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopDetail $paymentComputopDetailEntity
116
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
117
     *
118
     * @return void
119
     */
120
    protected function savePaymentComputopDetailEntity(
121
        SpyPaymentComputopDetail $paymentComputopDetailEntity,
122
        ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
123
    ): void {
124
        $paymentComputopDetailEntity->fromArray($computopPayuCeeSingleInitResponseTransfer->toArray());
125
        $customerTransactionId = $computopPayuCeeSingleInitResponseTransfer->getCustomerTransactionId();
126
        if ($customerTransactionId) {
127
            $paymentComputopDetailEntity->setCustomerTransactionId((int)$customerTransactionId);
128
        }
129
130
        $paymentComputopDetailEntity->save();
131
    }
132
133
    /**
134
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem[]|\Propel\Runtime\Collection\ObjectCollection $paymentComputopOrderItemEntities
135
     * @param string|null $paymentStatus
136
     *
137
     * @return void
138
     */
139
    protected function savePaymentComputopOrderItems(ObjectCollection $paymentComputopOrderItemEntities, string $paymentStatus): void
140
    {
141
        foreach ($paymentComputopOrderItemEntities as $paymentComputopOrderItem) {
142
            $paymentComputopOrderItem->setStatus($paymentStatus);
143
            $paymentComputopOrderItem->save();
144
        }
145
    }
146
147
    /**
148
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
149
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem $paymentComputopOrderItemEntity
150
     *
151
     * @return string
152
     */
153
    protected function getCurrentOrderItemEntityStatus(
154
        ComputopNotificationTransfer $computopNotificationTransfer,
155
        SpyPaymentComputopOrderItem $paymentComputopOrderItemEntity
156
    ): string {
157
        if (
158
            $paymentComputopOrderItemEntity->getStatus() === ComputopConfig::OMS_STATUS_NEW &&
159
            (int)$computopNotificationTransfer->getAmountauth() > 0
160
        ) {
161
            return $this->getFactory()->getConfig()->getOmsStatusAuthorized();
162
        }
163
164
        if (
165
            $paymentComputopOrderItemEntity->getStatus() === ComputopConfig::OMS_STATUS_AUTHORIZED &&
166
            (int)$computopNotificationTransfer->getAmountcap() === (int)$computopNotificationTransfer->getAmountauth()
167
        ) {
168
            return $this->getFactory()->getConfig()->getOmsStatusCaptured();
169
        }
170
171
        return $paymentComputopOrderItemEntity->getStatus();
172
    }
173
174
    /**
175
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer
176
     *
177
     * @return string
178
     */
179
    protected function getPaymentStatus(ComputopPayuCeeSingleInitResponseTransfer $computopPayuCeeSingleInitResponseTransfer): string
180
    {
181
        $computopConfig = $this->getFactory()->getConfig();
182
183
        $computopApiResponseHeaderTransfer = $computopPayuCeeSingleInitResponseTransfer->getHeader();
184
        if ($computopApiResponseHeaderTransfer === null) {
185
            return $computopConfig->getOmsStatusNew();
186
        }
187
188
        $responseStatus = $computopApiResponseHeaderTransfer->getStatus();
189
        if ($responseStatus === null) {
190
            return $computopConfig->getOmsStatusNew();
191
        }
192
193
        if ($responseStatus === SharedComputopConfig::AUTHORIZE_REQUEST_STATUS) {
194
            return $computopConfig->getAuthorizeRequestOmsStatus();
195
        }
196
197
        if (
198
            $responseStatus === SharedComputopConfig::SUCCESS_OK &&
199
            $computopApiResponseHeaderTransfer->getDescription() === SharedComputopConfig::SUCCESS_STATUS
200
        ) {
201
            return $computopConfig->getOmsStatusAuthorized();
202
        }
203
204
        return $computopConfig->getOmsStatusNew();
205
    }
206
}
207