updatePayment()   A
last analyzed

Complexity

Conditions 6
Paths 17

Size

Total Lines 26
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 17
nop 1
dl 0
loc 26
rs 9.2222
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
use SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface;
13
use SprykerEco\Zed\AmazonPay\Business\Api\Adapter\CallAdapterInterface;
14
use SprykerEco\Zed\AmazonPay\Business\Order\PaymentProcessorInterface;
15
use SprykerEco\Zed\AmazonPay\Business\Order\RefundOrderInterface;
16
use SprykerEco\Zed\AmazonPay\Business\Payment\Handler\Transaction\Logger\TransactionLoggerInterface;
17
18
class UpdateOrderRefundStatusTransaction extends AbstractAmazonpayTransaction
19
{
20
    /**
21
     * @var \SprykerEco\Zed\AmazonPay\Business\Order\RefundOrderInterface
22
     */
23
    protected $refundOrderModel;
24
25
    /**
26
     * @param \SprykerEco\Zed\AmazonPay\Business\Api\Adapter\CallAdapterInterface $executionAdapter
27
     * @param \SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface $config
28
     * @param \SprykerEco\Zed\AmazonPay\Business\Payment\Handler\Transaction\Logger\TransactionLoggerInterface $transactionLogger
29
     * @param \SprykerEco\Zed\AmazonPay\Business\Order\PaymentProcessorInterface $paymentProcessor
30
     * @param \SprykerEco\Zed\AmazonPay\Business\Order\RefundOrderInterface $refundOrderModel
31
     */
32
    public function __construct(
33
        CallAdapterInterface $executionAdapter,
34
        AmazonPayConfigInterface $config,
35
        TransactionLoggerInterface $transactionLogger,
36
        PaymentProcessorInterface $paymentProcessor,
37
        RefundOrderInterface $refundOrderModel
38
    ) {
39
40
        parent::__construct($executionAdapter, $config, $transactionLogger, $paymentProcessor);
41
42
        $this->refundOrderModel = $refundOrderModel;
43
    }
44
45
    /**
46
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
47
     *
48
     * @return \Generated\Shared\Transfer\AmazonpayCallTransfer
49
     */
50
    public function execute(AmazonpayCallTransfer $amazonPayCallTransfer)
51
    {
52
        if (!$this->isAllowed($amazonPayCallTransfer)) {
53
            return $amazonPayCallTransfer;
54
        }
55
56
        $amazonPayCallTransfer = parent::execute($amazonPayCallTransfer);
57
58
        $this->updatePayment($amazonPayCallTransfer);
59
60
        return $amazonPayCallTransfer;
61
    }
62
63
    /**
64
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
65
     *
66
     * @return bool
67
     */
68
    protected function isAllowed(AmazonpayCallTransfer $amazonPayCallTransfer)
69
    {
70
        return !empty($amazonPayCallTransfer->getAmazonpayPayment()->getRefundDetails()->getAmazonRefundId());
71
    }
72
73
    /**
74
     * @param \Generated\Shared\Transfer\AmazonpayCallTransfer $amazonPayCallTransfer
75
     *
76
     * @return void
77
     */
78
    protected function updatePayment(AmazonpayCallTransfer $amazonPayCallTransfer)
79
    {
80
        if (!$this->apiResponse->getResponseHeader()->getIsSuccess()) {
81
            return;
82
        }
83
84
        $isPartialProcessing = $this->isPartialProcessing($this->paymentEntity, $amazonPayCallTransfer);
85
86
        if ($isPartialProcessing) {
87
            $this->paymentEntity = $this->paymentProcessor->duplicatePaymentEntity($this->paymentEntity);
88
        }
89
90
        $status = $this->apiResponse->getRefundDetails()->getRefundStatus()->getState();
91
92
        $refundIsRequired = ($status === AmazonPayConfig::STATUS_COMPLETED
93
            && $this->paymentEntity->getStatus() !== $status);
94
95
        $this->paymentEntity->setStatus($status);
96
        $this->paymentEntity->save();
97
98
        if ($isPartialProcessing) {
99
            $this->paymentProcessor->assignAmazonpayPaymentToItems($this->paymentEntity, $amazonPayCallTransfer);
100
        }
101
102
        if ($refundIsRequired) {
103
            $this->refundOrderModel->refundPayment($this->paymentEntity);
104
        }
105
    }
106
}
107