Passed
Push — master ( a2100c...af4f13 )
by Andrey
06:37
created

PaymentController::confirmPurchaseAction()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 26
nc 7
nop 1
dl 0
loc 44
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\Yves\Amazonpay\Controller;
9
10
use Generated\Shared\Transfer\AmazonpayPaymentTransfer;
11
use SprykerEco\Yves\Amazonpay\Plugin\Provider\AmazonpayControllerProvider;
12
use Spryker\Yves\Kernel\Controller\AbstractController;
13
use Symfony\Component\HttpFoundation\JsonResponse;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * @method \SprykerEco\Yves\Amazonpay\AmazonpayFactory getFactory()
19
 * @method \SprykerEco\Client\Amazonpay\AmazonpayClient getClient()
20
 */
21
class PaymentController extends AbstractController
22
{
23
24
    const URL_PARAM_REFERENCE_ID = 'reference_id';
25
    const URL_PARAM_ACCESS_TOKEN = 'access_token';
26
    const URL_PARAM_SHIPMENT_METHOD_ID = 'shipment_method_id';
27
28
    /**
29
     * @param \Symfony\Component\HttpFoundation\Request $request
30
     *
31
     * @return array
32
     */
33
    public function checkoutAction(Request $request)
34
    {
35
        $amazonPaymentTransfer = new AmazonpayPaymentTransfer();
36
        $amazonPaymentTransfer->setOrderReferenceId($request->query->get(static::URL_PARAM_REFERENCE_ID));
37
        $amazonPaymentTransfer->setAddressConsentToken($request->query->get(static::URL_PARAM_ACCESS_TOKEN));
38
39
        $quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote();
40
        $quoteTransfer->setAmazonpayPayment($amazonPaymentTransfer);
41
        $quoteTransfer = $this->getClient()->handleCartWithAmazonpay($quoteTransfer);
42
        $this->getFactory()->getQuoteClient()->setQuote($quoteTransfer);
43
44
        return [
45
            'quoteTransfer' => $quoteTransfer,
46
        ];
47
    }
48
49
    /**
50
     * @param \Symfony\Component\HttpFoundation\Request $request
51
     *
52
     * @return \Symfony\Component\HttpFoundation\JsonResponse
53
     */
54
    public function setOrderReferenceAction(Request $request)
55
    {
56
        $quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote();
57
        $quoteTransfer->getAmazonpayPayment()->setOrderReferenceId($request->request->get(static::URL_PARAM_REFERENCE_ID));
58
59
        return new JsonResponse(['success' => true]);
60
    }
61
62
    /**
63
     * @return array
64
     */
65
    public function getShipmentMethodsAction()
66
    {
67
        $quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote();
68
        $quoteTransfer = $this->getClient()->addSelectedAddressToQuote($quoteTransfer);
69
        $this->getFactory()->getQuoteClient()->setQuote($quoteTransfer);
70
        $shipmentMethods = $this->getFactory()->getShipmentClient()->getAvailableMethods($quoteTransfer);
71
72
        return [
73
            'shipmentMethods' => $shipmentMethods->getMethods(),
74
        ];
75
    }
76
77
    /**
78
     * @param \Symfony\Component\HttpFoundation\Request $request
79
     *
80
     * @return array
81
     */
82
    public function updateShipmentMethodAction(Request $request)
83
    {
84
        $quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote();
85
        $quoteTransfer->getShipment()->setShipmentSelection(
86
            (int)$request->request->get(static::URL_PARAM_SHIPMENT_METHOD_ID)
87
        );
88
        $quoteTransfer = $this->getClient()->addSelectedShipmentMethodToQuote($quoteTransfer);
89
        $quoteTransfer = $this->getFactory()->getCalculationClient()->recalculate($quoteTransfer);
90
        $this->getFactory()->getQuoteClient()->setQuote($quoteTransfer);
91
92
        return [
93
            'quoteTransfer' => $quoteTransfer,
94
        ];
95
    }
96
97
    /**
98
     * @param \Symfony\Component\HttpFoundation\Request $request
99
     *
100
     * @return \Symfony\Component\HttpFoundation\Response
101
     */
102
    public function confirmPurchaseAction(Request $request)
103
    {
104
        $quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote();
105
106
        if (!$quoteTransfer) {
0 ignored issues
show
introduced by
The condition ! $quoteTransfer can never be false.
Loading history...
107
            return $this->getFailedRedirectResponse();
108
        }
109
110
        $quoteTransfer = $this->getClient()->confirmPurchase($quoteTransfer);
111
        $quoteTransfer = $this->getFactory()->getCalculationClient()->recalculate($quoteTransfer);
112
        $this->getFactory()->getQuoteClient()->setQuote($quoteTransfer);
113
114
        if ($quoteTransfer->getAmazonpayPayment()->getResponseHeader()->getIsSuccess()) {
115
            if (!$quoteTransfer->getAmazonpayPayment()
116
                    ->getAuthorizationDetails()
117
                    ->getAuthorizationStatus()
118
                    ->getIsDeclined()
119
            ) {
120
                $checkoutResponseTransfer = $this->getFactory()->getCheckoutClient()->placeOrder($quoteTransfer);
121
122
                if ($checkoutResponseTransfer->getIsSuccess()) {
123
                    return $this->redirectResponseInternal(AmazonpayControllerProvider::SUCCESS);
124
                }
125
126
                return new Response('Persisting Order Error');
127
            }
128
129
            if ($quoteTransfer->getAmazonpayPayment()
130
                    ->getAuthorizationDetails()
131
                    ->getAuthorizationStatus()
132
                    ->getIsPaymentMethodInvalid()
133
            ) {
134
                return $this->redirectResponseInternal(AmazonpayControllerProvider::CHANGE_PAYMENT_METHOD);
135
            } else {
136
                return $this->getFailedRedirectResponse();
137
            }
138
        }
139
140
        if ($quoteTransfer->getAmazonpayPayment()->getResponseHeader()->getConstraints()) {
141
            $this->addErrorMessage($this->getApplication()->trans('amazonpay.payment.wrong'));
142
            return $this->redirectResponseExternal($request->headers->get('referer'));
143
        }
144
145
        return $this->getFailedRedirectResponse();
146
    }
147
148
    /**
149
     * @return \Symfony\Component\HttpFoundation\RedirectResponse
150
     */
151
    protected function getFailedRedirectResponse()
152
    {
153
        $this->addErrorMessage($this->getApplication()->trans('amazonpay.payment.failed'));
0 ignored issues
show
Bug introduced by
The method trans() does not exist on Silex\Application. It seems like you code against a sub-type of Silex\Application such as Spryker\Shared\Kernel\Communication\Application or Silex\Tests\Application\TranslationApplication. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

153
        $this->addErrorMessage($this->getApplication()->/** @scrutinizer ignore-call */ trans('amazonpay.payment.failed'));
Loading history...
154
155
        return $this->redirectResponseInternal('cart');
156
    }
157
158
    /**
159
     * @param \Symfony\Component\HttpFoundation\Request $request
160
     *
161
     * @return array
162
     */
163
    public function changePaymentMethodAction(Request $request)
1 ignored issue
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

163
    public function changePaymentMethodAction(/** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
    {
165
        $quoteTransfer = $this->getFactory()->getQuoteClient()->getQuote();
166
167
        return [
168
            'quoteTransfer' => $quoteTransfer,
169
        ];
170
    }
171
172
    /**
173
     * @param \Symfony\Component\HttpFoundation\Request $request
174
     *
175
     * @return array
176
     */
177
    public function successAction(Request $request)
178
    {
179
        $this->getFactory()->getQuoteClient()->clearQuote();
180
181
        return [];
182
    }
183
184
}
185