Passed
Branch feature/ECO-573-per-item-proce... (652e5f)
by Andrey
03:36
created

AuthorizeTransaction::execute()   C

Complexity

Conditions 7
Paths 33

Size

Total Lines 42
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 25
nc 33
nop 1
dl 0
loc 42
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;
11
use Generated\Shared\Transfer\AmazonpayStatusTransfer;
12
use SprykerEco\Shared\Amazonpay\AmazonpayConstants;
13
14
class AuthorizeTransaction extends AbstractAmazonpayTransaction
15
{
16
17
    /**
18
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
19
     *
20
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer
21
     */
22
    public function execute(AmazonpayCallTransfer $amazonpayCallTransfer)
23
    {
24
        $authReferenceId = $this->generateOperationReferenceId($amazonpayCallTransfer);
25
        $amazonpayCallTransfer->getAmazonpayPayment()
26
            ->getAuthorizationDetails()
27
            ->setAuthorizationReferenceId($authReferenceId);
28
29
        $amazonpayCallTransfer = parent::execute($amazonpayCallTransfer);
30
31
        if (!$amazonpayCallTransfer->getAmazonpayPayment()->getResponseHeader()->getIsSuccess()) {
32
            return $amazonpayCallTransfer;
33
        }
34
35
        $isPartialProcessing = $this->paymentEntity && $this->isPartialProcessing($this->paymentEntity, $amazonpayCallTransfer);
36
37
        if ($isPartialProcessing) {
38
            $this->paymentEntity = $this->duplicatePaymentEntity($this->paymentEntity);
39
        }
40
41
        $amazonpayCallTransfer->getAmazonpayPayment()->setAuthorizationDetails(
42
            $this->apiResponse->getAuthorizationDetails()
43
        );
44
45
        $statusDetails = $amazonpayCallTransfer->getAmazonpayPayment()
46
            ->getAuthorizationDetails()
47
            ->getAuthorizationStatus();
48
        if ($statusDetails->getIsDeclined()) {
49
            $amazonpayCallTransfer->getAmazonpayPayment()->getResponseHeader()
50
                ->setIsSuccess(false)
51
                ->setErrorCode($this->buildErrorCode($amazonpayCallTransfer));
52
        }
53
54
        if ($this->paymentEntity) {
55
            $this->paymentEntity->setStatus($this->getStatus($statusDetails));
56
            $this->paymentEntity->save();
57
        }
58
59
        if ($isPartialProcessing) {
60
            $this->assignAmazonpayPaymentToItemsIfNew($this->paymentEntity, $amazonpayCallTransfer);
61
        }
62
63
        return $amazonpayCallTransfer;
64
    }
65
66
    /**
67
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonpayCallTransfer
68
     *
69
     * @return string
70
     */
71
    protected function buildErrorCode(AmazonpayCallTransfer $amazonpayCallTransfer)
72
    {
73
        return AmazonpayConstants::PREFIX_AMAZONPAY_PAYMENT_ERROR .
74
        $amazonpayCallTransfer->getAmazonpayPayment()
75
            ->getAuthorizationDetails()
76
            ->getAuthorizationStatus()
77
            ->getReasonCode();
78
    }
79
80
    /**
81
     * @param \Generated\Shared\Transfer\AmazonpayStatusTransfer $statusDetails
82
     *
83
     * @return string
84
     */
85 View Code Duplication
    protected function getStatus(AmazonpayStatusTransfer $statusDetails)
1 ignored issue
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...
86
    {
87
        if ($statusDetails->getIsOpen()) {
88
            return AmazonpayConstants::OMS_STATUS_AUTH_OPEN;
89
        }
90
91
        if ($statusDetails->getIsPending()) {
92
            return AmazonpayConstants::OMS_STATUS_AUTH_PENDING;
93
        }
94
95
        if ($statusDetails->getIsSuspended()) {
96
            return AmazonpayConstants::OMS_STATUS_AUTH_SUSPENDED;
97
        }
98
99
        return AmazonpayConstants::OMS_STATUS_AUTH_DECLINED;
100
    }
101
102
}
103