Passed
Push — feature/eco-3623-payone-pay-u-... ( b91903...15185e )
by Roman
05:04
created

savePaymentComputopNotification()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 13
rs 9.9666
cc 1
nc 1
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 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...
12
use Propel\Runtime\Collection\ObjectCollection;
13
use Spryker\Shared\Kernel\Transfer\TransferInterface;
14
use Spryker\Zed\Kernel\Persistence\AbstractEntityManager;
15
use SprykerEco\Zed\Computop\ComputopConfig;
16
17
/**
18
 * @method \SprykerEco\Zed\Computop\Persistence\ComputopPersistenceFactory getFactory()
19
 */
20
class ComputopEntityManager extends AbstractEntityManager implements ComputopEntityManagerInterface
21
{
22
    /**
23
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
24
     *
25
     * @return void
26
     */
27
    public function savePaymentComputopNotification(ComputopNotificationTransfer $computopNotificationTransfer): void
28
    {
29
        $paymentComputopNotificationEntity = $this->getFactory()
30
            ->createPaymentComputopNotificationQuery()
31
            ->filterByPayId($computopNotificationTransfer->getPayId())
32
            ->filterByTransId($computopNotificationTransfer->getTransId())
33
            ->filterByXId($computopNotificationTransfer->getXId())
34
            ->findOneOrCreate();
35
36
        $paymentComputopNotificationEntity->fromArray(
37
            $computopNotificationTransfer->modifiedToArray()
38
        );
39
        $paymentComputopNotificationEntity->save();
40
    }
41
42
    /**
43
     * @param \Generated\Shared\Transfer\ComputopNotificationTransfer $computopNotificationTransfer
44
     *
45
     * @return bool
46
     */
47
    public function updatePaymentComputopOrderItemPaymentConfirmation(
48
        ComputopNotificationTransfer $computopNotificationTransfer
49
    ): bool {
50
        /** @var \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem[]|\Propel\Runtime\Collection\ObjectCollection $paymentComputopOrderItemEntities */
51
        $paymentComputopOrderItemEntities = $this->getFactory()
52
            ->createPaymentComputopOrderItemQuery()
53
            ->useSpyPaymentComputopQuery()
54
                ->filterByTransId($computopNotificationTransfer->getTransId())
55
                ->filterByPayId($computopNotificationTransfer->getPayId())
56
            ->endUse()
57
            ->find();
58
59
        if (!$paymentComputopOrderItemEntities->count()) {
60
            return false;
61
        }
62
63
        foreach ($paymentComputopOrderItemEntities as $paymentComputopOrderItemEntity) {
64
            $paymentComputopOrderItemEntity->setIsPaymentConfirmed((bool)$computopNotificationTransfer->getIsSuccess());
65
66
            if (
67
                $paymentComputopOrderItemEntity->getStatus() === ComputopConfig::OMS_STATUS_NEW &&
68
                (int)$computopNotificationTransfer->getAmountauth() > 0
69
            ) {
70
                $paymentComputopOrderItemEntity->setStatus(
71
                    $this->getFactory()->getConfig()->getOmsStatusAuthorized()
72
                );
73
            }
74
75
            if (
76
                $paymentComputopOrderItemEntity->getStatus() === ComputopConfig::OMS_STATUS_AUTHORIZED &&
77
                (int)$computopNotificationTransfer->getAmountcap() === (int)$computopNotificationTransfer->getAmountauth()
78
            ) {
79
                $paymentComputopOrderItemEntity->setStatus(
80
                    $this->getFactory()->getConfig()->getOmsStatusCaptured()
81
                );
82
            }
83
84
            $paymentComputopOrderItemEntity->save();
85
        }
86
87
        return true;
88
    }
89
90
    /**
91
     * @param \Spryker\Shared\Kernel\Transfer\TransferInterface $responseTransfer
92
     *
93
     * @return void
94
     */
95
    public function savePaymentResponse(TransferInterface $responseTransfer): void
96
    {
97
        $header = $responseTransfer->requireHeader()->getHeader();
0 ignored issues
show
Bug introduced by
The method requireHeader() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

97
        $header = $responseTransfer->/** @scrutinizer ignore-call */ requireHeader()->getHeader();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
98
        $header
99
            ->requireTransId()
100
            ->requirePayId()
101
            ->requireXid();
102
103
        $paymentEntity = $this->getFactory()
104
            ->createPaymentComputopQuery()
105
            ->filterByTransId($header->getTransId())
106
            ->findOne();
107
108
        $paymentEntity
109
            ->setPayId($header->getPayId())
110
            ->setXId($header->getXId())
111
            ->save();
112
113
        $this->savePaymentDetailEntity($paymentEntity->getSpyPaymentComputopDetail(), $responseTransfer);
114
115
        $this->saveAuthorizedPaymentOrderItems($paymentEntity->getSpyPaymentComputopOrderItems());
116
    }
117
118
    /**
119
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopDetail $paymentEntityDetails
120
     * @param \Generated\Shared\Transfer\ComputopPayuCeeSingleInitResponseTransfer $responseTransfer
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...
121
     *
122
     * @return void
123
     */
124
    public function savePaymentDetailEntity(
125
        SpyPaymentComputopDetail $paymentEntityDetails,
126
        TransferInterface $responseTransfer
127
    ): void {
128
        $paymentEntityDetails->fromArray($responseTransfer->toArray());
129
        $customerTransactionId = $responseTransfer->getCustomerTransactionId();
0 ignored issues
show
Bug introduced by
The method getCustomerTransactionId() does not exist on Spryker\Shared\Kernel\Transfer\TransferInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

129
        /** @scrutinizer ignore-call */ 
130
        $customerTransactionId = $responseTransfer->getCustomerTransactionId();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
130
        if ($customerTransactionId) {
131
            $paymentEntityDetails->setCustomerTransactionId((int)$customerTransactionId);
132
        }
133
134
        $paymentEntityDetails->save();
135
    }
136
137
    /**
138
     * @param \Orm\Zed\Computop\Persistence\SpyPaymentComputopOrderItem[]|\Propel\Runtime\Collection\ObjectCollection $paymentComputopOrderItems
139
     *
140
     * @return void
141
     */
142
    public function saveAuthorizedPaymentOrderItems(ObjectCollection $paymentComputopOrderItems): void
143
    {
144
        $authorizedStatus = $this->getFactory()->getConfig()->getOmsStatusAuthorized();
145
        foreach ($paymentComputopOrderItems as $paymentComputopOrderItem) {
146
            $paymentComputopOrderItem->setStatus($authorizedStatus);
147
            $paymentComputopOrderItem->save();
148
        }
149
    }
150
}
151