Passed
Branch feature/ECO-965-refactoring (c9cdee)
by Andrey
06:34 queued 01:51
created

CaptureOrderTransaction::updatePaymentEntity()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 5
nop 1
dl 0
loc 30
rs 8.5806
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 SprykerEco\Shared\AmazonPay\AmazonPayConfig;
12
13
class CaptureOrderTransaction extends AbstractAmazonpayTransaction
14
{
15
    /**
16
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
17
     *
18
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer
19
     */
20 View Code Duplication
    public function execute(AmazonpayCallTransfer $amazonPayCallTransfer)
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...
21
    {
22
        if (!$this->isAllowed($amazonPayCallTransfer)) {
23
            return $amazonPayCallTransfer;
24
        }
25
26
        $this->generateCaptureReferenceId($amazonPayCallTransfer);
27
28
        $amazonPayCallTransfer = parent::execute($amazonPayCallTransfer);
29
30
        $this->updatePaymentEntity($amazonPayCallTransfer);
31
32
        return $amazonPayCallTransfer;
33
    }
34
35
    /**
36
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
37
     *
38
     * @return bool
39
     */
40
    protected function isAllowed(AmazonpayCallTransfer $amazonPayCallTransfer)
41
    {
42
        if (!in_array($amazonPayCallTransfer->getAmazonpayPayment()->getStatus(), [
43
            AmazonPayConfig::STATUS_PENDING,
44
            AmazonPayConfig::STATUS_OPEN,
45
            AmazonPayConfig::STATUS_PAYMENT_METHOD_CHANGED,
46
        ], true)) {
47
            return false;
48
        }
49
50
        if ($amazonPayCallTransfer->getAmazonpayPayment()->getCaptureDetails()
51
            && $amazonPayCallTransfer->getAmazonpayPayment()->getCaptureDetails()->getAmazonCaptureId()) {
52
            return false;
53
        }
54
55
        return true;
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->isPartialProcessing($this->paymentEntity, $amazonPayCallTransfer);
70
71
        if ($isPartialProcessing) {
72
            $this->paymentEntity = $this->paymentProcessor->duplicatePaymentEntity($this->paymentEntity);
73
        }
74
75
        $captureDetails = $this->apiResponse->getCaptureDetails();
76
77
        $amazonPayCallTransfer->getAmazonpayPayment()->setCaptureDetails($captureDetails);
78
        $this->paymentEntity->setAmazonCaptureId(
79
            $captureDetails->getAmazonCaptureId()
80
        );
81
        $this->paymentEntity->setCaptureReferenceId(
82
            $captureDetails->getCaptureReferenceId()
83
        );
84
85
        $this->paymentEntity->setStatus($captureDetails->getCaptureStatus()->getState());
86
87
        $this->paymentEntity->save();
88
89
        if ($isPartialProcessing) {
90
            $this->paymentProcessor->assignAmazonpayPaymentToItems(
91
                $this->paymentEntity,
92
                $amazonPayCallTransfer
93
            );
94
        }
95
    }
96
97
    /**
98
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
99
     *
100
     * @return void
101
     */
102
    protected function generateCaptureReferenceId(AmazonpayCallTransfer $amazonPayCallTransfer)
103
    {
104
        $amazonPayCallTransfer->getAmazonpayPayment()
105
            ->getCaptureDetails()
106
            ->setCaptureReferenceId(
107
                $this->generateOperationReferenceId($amazonPayCallTransfer)
108
            );
109
    }
110
}
111