AmazonPay::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 13
nc 1
nop 13
dl 0
loc 28
rs 9.8333
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
/**
4
 * PAYONE Magento 2 Connector is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU Lesser General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * PAYONE Magento 2 Connector is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with PAYONE Magento 2 Connector. If not, see <http://www.gnu.org/licenses/>.
16
 *
17
 * PHP version 5
18
 *
19
 * @category  Payone
20
 * @package   Payone_Magento2_Plugin
21
 * @author    FATCHIP GmbH <[email protected]>
22
 * @copyright 2003 - 2017 Payone GmbH
23
 * @license   <http://www.gnu.org/licenses/> GNU Lesser General Public License
24
 * @link      http://www.payone.de
25
 */
26
27
namespace Payone\Core\Service\V1;
28
29
use Payone\Core\Api\AmazonPayInterface;
30
use Payone\Core\Model\PayoneConfig;
31
use Magento\Checkout\Model\Type\Onepage;
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Model\Type\Onepage was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
use Magento\Customer\Model\Group;
0 ignored issues
show
Bug introduced by
The type Magento\Customer\Model\Group was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
33
34
/**
35
 * Web API model for the PAYONE addresscheck
36
 */
37
class AmazonPay implements AmazonPayInterface
38
{
39
    /**
40
     * Factory for the response object
41
     *
42
     * @var \Payone\Core\Api\Data\AmazonPayResponseInterfaceFactory
0 ignored issues
show
Bug introduced by
The type Payone\Core\Api\Data\Ama...esponseInterfaceFactory was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
     */
44
    protected $responseFactory;
45
46
    /**
47
     * Checkout session
48
     *
49
     * @var \Magento\Checkout\Model\Session
0 ignored issues
show
Bug introduced by
The type Magento\Checkout\Model\Session was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
50
     */
51
    protected $checkoutSession;
52
53
    /**
54
     * Object of getconfiguration request
55
     *
56
     * @var \Payone\Core\Model\Api\Request\Genericpayment\GetConfiguration
57
     */
58
    protected $getConfiguration;
59
60
    /**
61
     * Object of getorderreferencedetails request
62
     *
63
     * @var \Payone\Core\Model\Api\Request\Genericpayment\GetOrderReferenceDetails
64
     */
65
    protected $getOrderReferenceDetails;
66
67
    /**
68
     * Object of setorderreferencedetails request
69
     *
70
     * @var \Payone\Core\Model\Api\Request\Genericpayment\SetOrderReferenceDetails
71
     */
72
    protected $setOrderReferenceDetails;
73
74
    /**
75
     * @var \Payone\Core\Model\Api\Request\Genericpayment\CreateCheckoutSessionPayload
76
     */
77
    protected $createCheckoutSessionPayload;
78
79
    /**
80
     * Amazon Pay payment object
81
     *
82
     * @var \Payone\Core\Model\Methods\AmazonPay
83
     */
84
    protected $payment;
85
86
    /**
87
     * Amazon Pay payment object
88
     *
89
     * @var \Payone\Core\Model\Methods\AmazonPayV2
90
     */
91
    protected $paymentv2;
92
93
    /**
94
     * Cart management interface
95
     *
96
     * @var \Magento\Quote\Api\CartManagementInterface
0 ignored issues
show
Bug introduced by
The type Magento\Quote\Api\CartManagementInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
97
     */
98
    protected $cartManagement;
99
100
    /**
101
     * PAYONE order helper
102
     *
103
     * @var \Payone\Core\Helper\Order
104
     */
105
    protected $orderHelper;
106
107
    /**
108
     * PAYONE order helper
109
     *
110
     * @var \Payone\Core\Helper\Checkout
111
     */
112
    protected $checkoutHelper;
113
114
    /**
115
     * Block object of review page
116
     *
117
     * @var \Payone\Core\Block\Onepage\Review
118
     */
119
    protected $reviewBlock;
120
121
    /**
122
     * @var \Magento\Framework\App\ViewInterface
0 ignored issues
show
Bug introduced by
The type Magento\Framework\App\ViewInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
123
     */
124
    protected $view;
125
126
    /**
127
     * Constructor.
128
     *
129
     * @param \Payone\Core\Api\Data\AmazonPayResponseInterfaceFactory                    $responseFactory
130
     * @param \Magento\Checkout\Model\Session                                            $checkoutSession
131
     * @param \Payone\Core\Model\Api\Request\Genericpayment\GetConfiguration             $getConfiguration
132
     * @param \Payone\Core\Model\Api\Request\Genericpayment\GetOrderReferenceDetails     $getOrderReferenceDetails
133
     * @param \Payone\Core\Model\Api\Request\Genericpayment\CreateCheckoutSessionPayload $createCheckoutSessionPayload
134
     * @param \Payone\Core\Model\Methods\AmazonPay                                       $payment
135
     * @param \Magento\Quote\Api\CartManagementInterface                                 $cartManagement
136
     * @param \Payone\Core\Helper\Order                                                  $orderHelper
137
     * @param \Payone\Core\Helper\Checkout                                               $checkoutHelper
138
     * @param \Payone\Core\Block\Onepage\Review                                          $reviewBlock
139
     * @param \Magento\Framework\App\ViewInterface                                       $view
140
     */
141
    public function __construct(
142
        \Payone\Core\Api\Data\AmazonPayResponseInterfaceFactory $responseFactory,
143
        \Magento\Checkout\Model\Session $checkoutSession,
144
        \Payone\Core\Model\Api\Request\Genericpayment\GetConfiguration $getConfiguration,
145
        \Payone\Core\Model\Api\Request\Genericpayment\GetOrderReferenceDetails $getOrderReferenceDetails,
146
        \Payone\Core\Model\Api\Request\Genericpayment\SetOrderReferenceDetails $setOrderReferenceDetails,
147
        \Payone\Core\Model\Api\Request\Genericpayment\CreateCheckoutSessionPayload $createCheckoutSessionPayload,
148
        \Payone\Core\Model\Methods\AmazonPay $payment,
149
        \Payone\Core\Model\Methods\AmazonPayV2 $paymentv2,
150
        \Magento\Quote\Api\CartManagementInterface $cartManagement,
151
        \Payone\Core\Helper\Order $orderHelper,
152
        \Payone\Core\Helper\Checkout $checkoutHelper,
153
        \Payone\Core\Block\Onepage\Review $reviewBlock,
154
        \Magento\Framework\App\ViewInterface $view
155
    ) {
156
        $this->responseFactory = $responseFactory;
157
        $this->checkoutSession = $checkoutSession;
158
        $this->getConfiguration = $getConfiguration;
159
        $this->getOrderReferenceDetails = $getOrderReferenceDetails;
160
        $this->setOrderReferenceDetails = $setOrderReferenceDetails;
161
        $this->createCheckoutSessionPayload = $createCheckoutSessionPayload;
162
        $this->payment = $payment;
163
        $this->paymentv2 = $paymentv2;
164
        $this->cartManagement = $cartManagement;
165
        $this->orderHelper = $orderHelper;
166
        $this->checkoutHelper = $checkoutHelper;
167
        $this->reviewBlock = $reviewBlock;
168
        $this->view = $view;
169
    }
170
171
    /**
172
     * Get Amazon workorder_id from session or request
173
     *
174
     * @return string
175
     */
176
    protected function collectWorkorderId()
177
    {
178
        $sWorkorderId = $this->checkoutSession->getAmazonWorkorderId();
179
        if (empty($sWorkorderId)) {
180
            $aResult = $this->getConfiguration->sendRequest($this->payment, $this->checkoutSession->getQuote());
181
            if (isset($aResult['status']) && $aResult['status'] == 'OK' && isset($aResult['workorderid'])) {
182
                $sWorkorderId = $aResult['workorderid'];
183
                $this->checkoutSession->setAmazonWorkorderId($aResult['workorderid']);
184
            }
185
        }
186
        return $sWorkorderId;
187
    }
188
189
    /**
190
     * PAYONE addresscheck
191
     * The full class-paths must be given here otherwise the Magento 2 WebApi
192
     * cant handle this with its fake type system!
193
     *
194
     * @param  string $amazonReferenceId
195
     * @param  string $amazonAddressToken
196
     * @return \Payone\Core\Service\V1\Data\AmazonPayResponse
197
     */
198
    public function getWorkorderId($amazonReferenceId, $amazonAddressToken) {
199
        $blSuccess = false;
200
        $sWorkorderId = $this->collectWorkorderId();
201
        if (!empty($sWorkorderId)) {
202
            $blSuccess = true;
203
204
            $oQuote = $this->checkoutSession->getQuote();
205
            $aResult = $this->getOrderReferenceDetails->sendRequest($this->payment, $oQuote, $sWorkorderId, $amazonReferenceId, $amazonAddressToken);
206
            if (isset($aResult['status']) && $aResult['status'] == 'OK') {
207
                $this->checkoutSession->setAmazonAddressToken($amazonAddressToken);
208
                $this->checkoutSession->setAmazonReferenceId($amazonReferenceId);
209
210
                $oQuote = $this->orderHelper->updateAddresses($oQuote, $aResult);
211
212
                $oPayment = $oQuote->getPayment();
213
                $oPayment->setMethod(PayoneConfig::METHOD_AMAZONPAY);
214
215
                $oQuote->save();
216
217
                $aResult = $this->setOrderReferenceDetails->sendRequest($this->payment, $oQuote, $sWorkorderId, $amazonReferenceId, $amazonAddressToken);
218
                if (isset($aResult['status']) && $aResult['status'] == 'OK') {
219
                    if ($this->checkoutHelper->getCurrentCheckoutMethod($oQuote) == Onepage::METHOD_GUEST) {
220
                        $oQuote->setCustomerId(null)
221
                            ->setCustomerEmail($oQuote->getBillingAddress()->getEmail())
222
                            ->setCustomerIsGuest(true)
223
                            ->setCustomerGroupId(Group::NOT_LOGGED_IN_ID);
224
                    }
225
226
                    $oQuote->setPayment($oPayment);
227
                    $oQuote->setInventoryProcessed(false);
228
                    $oQuote->collectTotals()->save();
229
                    $this->cartManagement->placeOrder($oQuote->getId());
230
                    $oQuote->setIsActive(false)->save();
231
                }
232
            }
233
        }
234
235
        $oResponse = $this->responseFactory->create();
236
        $oResponse->setData('workorderId', $sWorkorderId);
237
        $oResponse->setData('success', $blSuccess);
238
        $this->reviewBlock->setArea('frontend');
239
        $this->view->loadLayout('payone_onepage_review', true, true, true);
240
241
        $html = $this->view->getLayout()->getBlock('payone_onepage_review')->toHtml();
242
243
        $oResponse->setData('amazonReviewHtml', $html);
244
        return $oResponse;
245
    }
246
247
    /**
248
     * Returns Amazon Pay V2 checkout session payload
249
     *
250
     * @param  string $cartId
251
     * @return \Payone\Core\Service\V1\Data\AmazonPayResponse
252
     */
253
    public function getCheckoutSessionPayload($cartId)
254
    {
255
        $blSuccess = false;
256
        $oResponse = $this->responseFactory->create();
257
258
        $oQuote = $this->checkoutSession->getQuote();
259
260
        $oPayment = $oQuote->getPayment();
261
        $oPayment->setMethod(PayoneConfig::METHOD_AMAZONPAYV2);
262
263
        $oQuote->setPayment($oPayment);
264
        $oQuote->save();
265
266
        $this->checkoutSession->setPayoneIsAmazonPayExpressPayment(true);
267
268
        $aResponse = $this->createCheckoutSessionPayload->sendRequest($this->paymentv2, $oQuote);
269
        if (isset($aResponse['status'], $aResponse['add_paydata[signature]'], $aResponse['add_paydata[payload]']) && $aResponse['status'] == 'OK') {
270
            $blSuccess = true;
271
272
            $oResponse->setData('payload', $aResponse['add_paydata[payload]']);
273
            $oResponse->setData('signature', $aResponse['add_paydata[signature]']);
274
275
            if (!empty($aResponse['workorderid'])) {
276
                $this->checkoutSession->setPayoneWorkorderId($aResponse['workorderid']);
277
                $this->checkoutSession->setPayoneQuoteComparisonString($this->checkoutHelper->getQuoteComparisonString($oQuote));
278
            }
279
        }
280
281
        if (isset($aResponse['status'], $aResponse['customermessage']) && $aResponse['status'] == 'ERROR') {
282
            $oResponse->setData('errormessage', $aResponse['customermessage']);
283
        }
284
285
        $oResponse->setData('success', $blSuccess);
286
        return $oResponse;
287
    }
288
289
    /**
290
     * Returns Amazon Pay V2 checkout session payload for APB
291
     *
292
     * @param  string $orderId
293
     * @return \Payone\Core\Service\V1\Data\AmazonPayResponse
294
     */
295
    public function getAmazonPayApbSession($orderId)
296
    {
297
        $blSuccess = false;
298
        $oResponse = $this->responseFactory->create();
299
300
        $sPayload = $this->checkoutSession->getPayoneAmazonPayPayload();
301
        $sSignature = $this->checkoutSession->getPayoneAmazonPaySignature();
302
        if (!empty($sPayload) && !empty($sSignature)) {
303
            $blSuccess = true;
304
305
            $oResponse->setData('payload', $sPayload);
306
            $oResponse->setData('signature', $sSignature);
307
        }
308
309
        $oResponse->setData('success', $blSuccess);
310
        return $oResponse;
311
    }
312
}
313