|
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 - 2016 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\Controller\Onepage; |
|
28
|
|
|
|
|
29
|
|
|
use Magento\Framework\View\Result\Page; |
|
30
|
|
|
use Payone\Core\Model\Methods\PayoneMethod; |
|
31
|
|
|
use Magento\Quote\Model\Quote; |
|
32
|
|
|
use Magento\Framework\Exception\LocalizedException; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Controller for mandate management with debit payment |
|
36
|
|
|
*/ |
|
37
|
|
|
class Debit extends \Magento\Framework\App\Action\Action |
|
38
|
|
|
{ |
|
39
|
|
|
/** |
|
40
|
|
|
* Checkout session |
|
41
|
|
|
* |
|
42
|
|
|
* @var \Magento\Checkout\Model\Session |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $checkoutSession; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* PAYONE debit request class |
|
48
|
|
|
* |
|
49
|
|
|
* @var \Payone\Core\Model\Api\Request\Managemandate |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $managemandateRequest; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Page result factory |
|
55
|
|
|
* |
|
56
|
|
|
* @var \Magento\Framework\View\Result\PageFactory |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $pageFactory; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Cart management interface |
|
62
|
|
|
* |
|
63
|
|
|
* @var \Magento\Quote\Api\CartManagementInterface |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $cartManagement; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Onepage checkout model |
|
69
|
|
|
* |
|
70
|
|
|
* @var \Magento\Checkout\Model\Type\Onepage |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $typeOnepage; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Constructor |
|
76
|
|
|
* |
|
77
|
|
|
* @param \Magento\Framework\App\Action\Context $context |
|
78
|
|
|
* @param \Magento\Checkout\Model\Session $checkoutSession |
|
79
|
|
|
* @param \Payone\Core\Model\Api\Request\Managemandate $managemandateRequest |
|
80
|
|
|
* @param \Magento\Framework\View\Result\PageFactory $pageFactory |
|
81
|
|
|
* @param \Magento\Quote\Api\CartManagementInterface $cartManagement |
|
82
|
|
|
* @param \Magento\Checkout\Model\Type\Onepage $typeOnepage |
|
83
|
|
|
*/ |
|
84
|
|
|
public function __construct( |
|
85
|
|
|
\Magento\Framework\App\Action\Context $context, |
|
86
|
|
|
\Magento\Checkout\Model\Session $checkoutSession, |
|
87
|
|
|
\Payone\Core\Model\Api\Request\Managemandate $managemandateRequest, |
|
88
|
|
|
\Magento\Framework\View\Result\PageFactory $pageFactory, |
|
89
|
|
|
\Magento\Quote\Api\CartManagementInterface $cartManagement, |
|
90
|
|
|
\Magento\Checkout\Model\Type\Onepage $typeOnepage |
|
91
|
|
|
) { |
|
92
|
|
|
parent::__construct($context); |
|
93
|
|
|
$this->checkoutSession = $checkoutSession; |
|
94
|
|
|
$this->managemandateRequest = $managemandateRequest; |
|
95
|
|
|
$this->pageFactory = $pageFactory; |
|
96
|
|
|
$this->cartManagement = $cartManagement; |
|
97
|
|
|
$this->typeOnepage = $typeOnepage; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Execute managemandate request and handle response |
|
102
|
|
|
* |
|
103
|
|
|
* @param PayoneMethod $oPayment |
|
104
|
|
|
* @param Quote $oQuote |
|
105
|
|
|
* @return array |
|
106
|
|
|
* @throws LocalizedException |
|
107
|
|
|
*/ |
|
108
|
|
|
protected function handleManagemandateRequest(PayoneMethod $oPayment, Quote $oQuote) |
|
109
|
|
|
{ |
|
110
|
|
|
$aResponse = $this->managemandateRequest->sendRequest($oPayment, $oQuote); |
|
111
|
|
|
if ($aResponse['status'] == 'ERROR') {// request was not successfull |
|
112
|
|
|
throw new LocalizedException(__($aResponse['errorcode'].' - '.$aResponse['customermessage'])); |
|
113
|
|
|
} elseif (is_array($aResponse) && array_key_exists('mandate_status', $aResponse) !== false) { |
|
114
|
|
|
// write mandate to session |
|
115
|
|
|
$this->checkoutSession->setPayoneMandate($aResponse); |
|
116
|
|
|
} |
|
117
|
|
|
return $aResponse; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* |
|
122
|
|
|
* @param PayoneMethod $oPayment |
|
123
|
|
|
* @param Quote $oQuote |
|
124
|
|
|
* @return void|Page |
|
125
|
|
|
*/ |
|
126
|
|
|
protected function handleMandate(PayoneMethod $oPayment, Quote $oQuote) |
|
127
|
|
|
{ |
|
128
|
|
|
if ($oPayment->getCustomConfigParam('sepa_mandate_enabled')) { |
|
129
|
|
|
$aMandate = $this->checkoutSession->getPayoneMandate(); |
|
130
|
|
|
if (!$aMandate) {// Is initial call, so get the mandate |
|
131
|
|
|
$aResponse = $this->handleManagemandateRequest($oPayment, $oQuote); |
|
132
|
|
|
if ($aResponse && array_key_exists('mandate_status', $aResponse) !== false && $aResponse['mandate_status'] == 'pending') { |
|
|
|
|
|
|
133
|
|
|
$oPageObject = $this->pageFactory->create(); |
|
134
|
|
|
return $oPageObject; |
|
135
|
|
|
} |
|
136
|
|
|
} elseif ($this->getRequest()->getParam('mandate_granted') == 1 && |
|
137
|
|
|
$this->getRequest()->getParam('mandate_id') != $aMandate['mandate_identification'] |
|
138
|
|
|
) {// Is return from the mandate-page with granted mandate but mandate id mismatch |
|
139
|
|
|
$this->_redirect($this->_url->getUrl('checkout')); |
|
140
|
|
|
return; |
|
141
|
|
|
} // else - mandate is granted so proceed to success page |
|
142
|
|
|
} |
|
143
|
|
|
// trigger order creation |
|
144
|
|
|
$this->cartManagement->placeOrder($oQuote->getId()); |
|
145
|
|
|
$this->_redirect($this->_url->getUrl('checkout/onepage/success')); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Handle debit checkout |
|
150
|
|
|
* Display mandate if activated |
|
151
|
|
|
* Just create the order if mandate is deactivated |
|
152
|
|
|
* Redirect to basket if quote or payment is missing |
|
153
|
|
|
* |
|
154
|
|
|
* @return void|Page |
|
155
|
|
|
*/ |
|
156
|
|
|
public function execute() |
|
157
|
|
|
{ |
|
158
|
|
|
$oQuote = $this->checkoutSession->getQuote(); |
|
159
|
|
|
$oQuote->setCheckoutMethod($this->typeOnepage->getCheckoutMethod()); |
|
160
|
|
|
|
|
161
|
|
|
$oPayment = $oQuote->getPayment()->getMethodInstance(); |
|
162
|
|
|
if (!$oQuote || !$oPayment) {// something is wrong, redirect to checkout start |
|
163
|
|
|
$this->_redirect($this->_url->getUrl('checkout')); |
|
164
|
|
|
return; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
try { |
|
168
|
|
|
return $this->handleMandate($oPayment, $oQuote); |
|
169
|
|
|
} catch (\Exception $ex) { |
|
170
|
|
|
$this->checkoutSession->setPayoneDebitError($ex->getMessage()); |
|
171
|
|
|
$oPageObject = $this->pageFactory->create(); |
|
172
|
|
|
return $oPageObject; |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.