Completed
Push — master ( 13ff69...56b1e7 )
by Oleksandr
12s
created

WidgetController::resetAmazonPaymentInQuote()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 8
rs 10
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\Yves\AmazonPay\Controller;
9
10
use Generated\Shared\Transfer\QuoteTransfer;
11
use Spryker\Yves\Kernel\Controller\AbstractController;
12
use SprykerEco\Shared\AmazonPay\AmazonPayConfig;
13
14
/**
15
 * @method \SprykerEco\Yves\AmazonPay\AmazonPayFactory getFactory()
16
 */
17
class WidgetController extends AbstractController
18
{
19
    const ADDRESS_BOOK_MODE = 'addressBookMode';
20
    const AMAZON_PAY_CONFIG = 'amazonpayConfig';
21
    const LOGOUT = 'logout';
22
    const ORDER_REFERENCE = 'orderReferenceId';
23
24
    /**
25
     * @return array
26
     */
27
    public function payButtonAction()
28
    {
29
        $isLogout = $this->isLogout();
30
31
        if ($isLogout) {
32
            $this->resetAmazonPaymentInQuote();
33
        }
34
35
        return [
36
            static::AMAZON_PAY_CONFIG => $this->getAmazonPayConfig(),
37
            static::LOGOUT => $isLogout,
38
        ];
39
    }
40
41
    /**
42
     * @return void
43
     */
44
    protected function resetAmazonPaymentInQuote()
45
    {
46
        $quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote();
47
48
        $quoteTransfer->setAmazonpayPayment(null);
49
        $this->getFactory()
50
            ->getQuoteClient()
51
            ->setQuote($quoteTransfer);
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function checkoutWidgetAction()
58
    {
59
        $quoteTransfer = $this->getFactory()
60
            ->getQuoteClient()
61
            ->getQuote();
62
63
        $data = [
64
            static::AMAZON_PAY_CONFIG => $this->getAmazonPayConfig(),
65
        ];
66
67
        if ($this->isAmazonPaymentInvalid($quoteTransfer)) {
68
            $data[static::ORDER_REFERENCE] = $this->getAmazonPaymentOrderReferenceId($quoteTransfer);
69
            $data[static::ADDRESS_BOOK_MODE] = AmazonPayConfig::DISPLAY_MODE_READONLY;
70
        }
71
72
        return $data;
73
    }
74
75
    /**
76
     * @return array
77
     */
78
    public function walletWidgetAction()
79
    {
80
        return [
81
            static::AMAZON_PAY_CONFIG => $this->getAmazonPayConfig(),
82
        ];
83
    }
84
85
    /**
86
     * @return int
87
     */
88
    protected function isLogout()
89
    {
90
        $quote = $this->getFactory()->getQuoteClient()->getQuote();
91
92
        $isLogout = $quote->getAmazonpayPayment()
93
            && $quote->getAmazonpayPayment()->getResponseHeader()
94
            && !$quote->getAmazonpayPayment()->getResponseHeader()->getIsSuccess();
95
96
        if ($isLogout) {
97
            return 1;
98
        }
99
100
        return 0;
101
    }
102
103
    /**
104
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
105
     *
106
     * @return null|string
107
     */
108
    protected function getAmazonPaymentOrderReferenceId(QuoteTransfer $quoteTransfer)
109
    {
110
        if ($quoteTransfer->getAmazonpayPayment() !== null && $quoteTransfer->getAmazonpayPayment()->getOrderReferenceId() !== null) {
111
            return $quoteTransfer->getAmazonpayPayment()->getOrderReferenceId();
112
        }
113
114
        return null;
115
    }
116
117
    /**
118
     * @return \SprykerEco\Shared\AmazonPay\AmazonPayConfigInterface
119
     */
120
    protected function getAmazonPayConfig()
121
    {
122
        return $this->getFactory()->createAmazonPayConfig();
123
    }
124
125
    /**
126
     * @param \Generated\Shared\Transfer\QuoteTransfer $quoteTransfer
127
     *
128
     * @return bool
129
     */
130
    protected function isAmazonPaymentInvalid(QuoteTransfer $quoteTransfer)
131
    {
132
        if ($quoteTransfer->getAmazonpayPayment()->getResponseHeader() !== null
133
            && $quoteTransfer->getAmazonpayPayment()->getResponseHeader()->getIsInvalidPaymentMethod()) {
134
            return true;
135
        }
136
137
        return false;
138
    }
139
}
140