Completed
Push — master ( f02bf1...5bd582 )
by Oleksandr
25s queued 11s
created

AuthorizeTransaction   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 49
dl 0
loc 128
rs 10
c 3
b 1
f 0
wmc 13

7 Methods

Rating   Name   Duplication   Size   Complexity  
A isStateDeclined() 0 10 1
A updateAuthorizationReferenceId() 0 6 1
A isInvalidPaymentMethod() 0 6 1
B updatePaymentEntity() 0 35 7
A isPartialProcessing() 0 3 1
A buildErrorMessage() 0 7 1
A execute() 0 9 1
1
<?php
2
3
/**
4
 * Apache OSL-2
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\AmazonPay\Business\Payment\Handler\Transaction;
9
10
use Generated\Shared\Transfer\AmazonpayCallTransfer;
1 ignored issue
show
Bug introduced by
The type Generated\Shared\Transfer\AmazonpayCallTransfer 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\AmazonPay\Persistence\SpyPaymentAmazonpay;
1 ignored issue
show
Bug introduced by
The type Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay 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 SprykerEco\Shared\AmazonPay\AmazonPayConfig;
13
14
class AuthorizeTransaction extends AbstractAmazonpayTransaction
15
{
16
    /**
17
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
18
     *
19
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer
20
     */
21
    public function execute(AmazonpayCallTransfer $amazonPayCallTransfer)
22
    {
23
        $this->updateAuthorizationReferenceId($amazonPayCallTransfer);
24
25
        $amazonPayCallTransfer = parent::execute($amazonPayCallTransfer);
26
27
        $this->updatePaymentEntity($amazonPayCallTransfer);
28
29
        return $amazonPayCallTransfer;
30
    }
31
32
    /**
33
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
34
     *
35
     * @return string
36
     */
37
    protected function buildErrorMessage(AmazonpayCallTransfer $amazonpayCallTransfer)
38
    {
39
        return AmazonPayConfig::PREFIX_AMAZONPAY_PAYMENT_ERROR .
40
            $amazonpayCallTransfer->getAmazonpayPayment()
41
                ->getAuthorizationDetails()
42
                ->getAuthorizationStatus()
43
                ->getReasonCode();
44
    }
45
46
    /**
47
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
48
     *
49
     * @return void
50
     */
51
    protected function updateAuthorizationReferenceId(AmazonpayCallTransfer $amazonPayCallTransfer)
52
    {
53
        $authReferenceId = $this->generateOperationReferenceId($amazonPayCallTransfer);
54
        $amazonPayCallTransfer->getAmazonpayPayment()
55
            ->getAuthorizationDetails()
56
            ->setAuthorizationReferenceId($authReferenceId);
57
    }
58
59
    /**
60
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
61
     *
62
     * @return void
63
     */
64
    protected function updatePaymentEntity(AmazonpayCallTransfer $amazonPayCallTransfer)
65
    {
66
        if (!$this->isPaymentSuccess($amazonPayCallTransfer)) {
67
            $this->paymentEntity->setStatus(AmazonPayConfig::STATUS_DECLINED);
68
            $this->paymentEntity->save();
69
70
            return;
71
        }
72
73
        $isPartialProcessing = $this->paymentEntity !== null && $this->isPartialProcessing($this->paymentEntity, $amazonPayCallTransfer);
74
75
        if ($isPartialProcessing) {
76
            $this->paymentEntity = $this->paymentProcessor->duplicatePaymentEntity($this->paymentEntity);
77
        }
78
79
        $authorizationDetails = $amazonPayCallTransfer->getAmazonpayPayment()->getAuthorizationDetails();
80
81
        if ($this->isStateDeclined($authorizationDetails->getAuthorizationStatus()->getState())) {
82
            $amazonPayCallTransfer->getAmazonpayPayment()->getResponseHeader()
83
                ->setIsSuccess(false)
84
                ->setIsInvalidPaymentMethod($this->isInvalidPaymentMethod($amazonPayCallTransfer))
85
                ->setErrorMessage($this->buildErrorMessage($amazonPayCallTransfer));
86
        }
87
88
        if ($this->paymentEntity !== null) {
89
            $this->paymentEntity->setStatus($authorizationDetails->getAuthorizationStatus()->getState());
90
            $this->paymentEntity->setAuthorizationReferenceId($authorizationDetails->getAuthorizationReferenceId());
91
            $this->paymentEntity->setAmazonAuthorizationId($authorizationDetails->getAmazonAuthorizationId());
92
            $this->paymentEntity->save();
93
        }
94
95
        if ($isPartialProcessing) {
96
            $this->paymentProcessor->assignAmazonpayPaymentToItems(
97
                $this->paymentEntity,
98
                $amazonPayCallTransfer
99
            );
100
        }
101
    }
102
103
    /**
104
     * @param string $stateName
105
     *
106
     * @return bool
107
     */
108
    protected function isStateDeclined($stateName)
109
    {
110
        return in_array($stateName, [
111
            AmazonPayConfig::STATUS_DECLINED,
112
            AmazonPayConfig::STATUS_TRANSACTION_TIMED_OUT,
113
            AmazonPayConfig::STATUS_CANCELLED,
114
            AmazonPayConfig::STATUS_SUSPENDED,
115
            AmazonPayConfig::STATUS_EXPIRED,
116
            AmazonPayConfig::STATUS_PAYMENT_METHOD_INVALID,
117
        ], true);
118
    }
119
120
    /**
121
     * @param \Orm\Zed\AmazonPay\Persistence\SpyPaymentAmazonpay $paymentAmazonPayEntity
122
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
123
     *
124
     * @return bool
125
     */
126
    protected function isPartialProcessing(SpyPaymentAmazonpay $paymentAmazonPayEntity, AmazonpayCallTransfer $amazonPayCallTransfer): bool
127
    {
128
        return false;
129
    }
130
131
    /**
132
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
133
     *
134
     * @return bool
135
     */
136
    protected function isInvalidPaymentMethod(AmazonpayCallTransfer $amazonPayCallTransfer)
137
    {
138
        return ($amazonPayCallTransfer->getAmazonpayPayment()
139
                ->getAuthorizationDetails()
140
                ->getAuthorizationStatus()
141
                ->getReasonCode() === AmazonPayConfig::REASON_CODE_PAYMENT_METHOD_INVALID);
142
    }
143
}
144