Passed
Pull Request — dev (#9)
by Andrey
07:03 queued 03:30
created

AuthorizeTransaction::updatePaymentEntity()   C

Complexity

Conditions 7
Paths 33

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 33
nop 1
dl 0
loc 31
rs 6.7272
c 0
b 0
f 0
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 SprykerEco\Shared\AmazonPay\AmazonPayConfig;
12
13
class AuthorizeTransaction extends AbstractAmazonpayTransaction
14
{
15
    /**
16
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
17
     *
18
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer
19
     */
20
    public function execute(AmazonpayCallTransfer $amazonPayCallTransfer)
21
    {
22
        $this->updateAuthorizationReferenceId($amazonPayCallTransfer);
23
24
        $amazonPayCallTransfer = parent::execute($amazonPayCallTransfer);
25
26
        $this->updatePaymentEntity($amazonPayCallTransfer);
27
28
        return $amazonPayCallTransfer;
29
    }
30
31
    /**
32
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
33
     *
34
     * @return string
35
     */
36
    protected function buildErrorMessage(AmazonpayCallTransfer $amazonpayCallTransfer)
37
    {
38
        return AmazonPayConfig::PREFIX_AMAZONPAY_PAYMENT_ERROR .
39
            $amazonpayCallTransfer->getAmazonpayPayment()
40
                ->getAuthorizationDetails()
41
                ->getAuthorizationStatus()
42
                ->getReasonCode();
43
    }
44
45
    /**
46
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
47
     *
48
     * @return void
49
     */
50
    protected function updateAuthorizationReferenceId(AmazonpayCallTransfer $amazonPayCallTransfer)
51
    {
52
        $authReferenceId = $this->generateOperationReferenceId($amazonPayCallTransfer);
53
        $amazonPayCallTransfer->getAmazonpayPayment()
54
            ->getAuthorizationDetails()
55
            ->setAuthorizationReferenceId($authReferenceId);
56
    }
57
58
    /**
59
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
60
     *
61
     * @return void
62
     */
63
    protected function updatePaymentEntity(AmazonpayCallTransfer $amazonPayCallTransfer)
64
    {
65
        if (!$this->isPaymentSuccess($amazonPayCallTransfer)) {
66
            return;
67
        }
68
69
        $isPartialProcessing = $this->paymentEntity && $this->isPartialProcessing($this->paymentEntity, $amazonPayCallTransfer);
70
71
        if ($isPartialProcessing) {
72
            $this->paymentEntity = $this->paymentProcessor->duplicatePaymentEntity($this->paymentEntity);
73
        }
74
75
        $statusDetails = $amazonPayCallTransfer->getAmazonpayPayment()
76
            ->getAuthorizationDetails()
77
            ->getAuthorizationStatus();
78
79
        if ($this->isStateDeclined($statusDetails->getState())) {
80
            $amazonPayCallTransfer->getAmazonpayPayment()->getResponseHeader()
81
                ->setIsSuccess(false)
82
                ->setErrorMessage($this->buildErrorMessage($amazonPayCallTransfer));
83
        }
84
85
        if ($this->paymentEntity) {
86
            $this->paymentEntity->setStatus($statusDetails->getState());
87
            $this->paymentEntity->save();
88
        }
89
90
        if ($isPartialProcessing) {
91
            $this->paymentProcessor->assignAmazonpayPaymentToItems(
92
                $this->paymentEntity,
93
                $amazonPayCallTransfer
94
            );
95
        }
96
    }
97
98
    /**
99
     * @param string $stateName
100
     *
101
     * @return bool
102
     */
103 View Code Duplication
    protected function isStateDeclined($stateName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        return in_array($stateName, [
106
            AmazonPayConfig::STATUS_DECLINED,
107
            AmazonPayConfig::STATUS_TRANSACTION_TIMED_OUT,
108
            AmazonPayConfig::STATUS_CANCELLED,
109
            AmazonPayConfig::STATUS_SUSPENDED,
110
            AmazonPayConfig::STATUS_EXPIRED,
111
            AmazonPayConfig::STATUS_PAYMENT_METHOD_INVALID,
112
        ], true);
113
    }
114
}
115