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\Helper; |
||
28 | |||
29 | use Magento\Quote\Model\Quote; |
||
30 | use Payone\Core\Model\Methods\PayoneMethod; |
||
31 | use Magento\Sales\Model\Order as SalesOrder; |
||
32 | use Payone\Core\Model\Methods\Ratepay\RatepayBase; |
||
33 | |||
34 | /** |
||
35 | * Helper class for everything that has to do with APIs |
||
36 | * |
||
37 | * @category Payone |
||
38 | * @package Payone_Magento2_Plugin |
||
39 | * @author FATCHIP GmbH <[email protected]> |
||
40 | * @copyright 2003 - 2016 Payone GmbH |
||
41 | * @license <http://www.gnu.org/licenses/> GNU Lesser General Public License |
||
42 | * @link http://www.payone.de |
||
43 | */ |
||
44 | class Api extends Base |
||
45 | { |
||
46 | /** |
||
47 | * PAYONE connection curl php |
||
48 | * |
||
49 | * @var \Payone\Core\Helper\Connection\CurlPhp |
||
50 | */ |
||
51 | protected $connCurlPhp; |
||
52 | |||
53 | /** |
||
54 | * PAYONE connection curl cli |
||
55 | * |
||
56 | * @var \Payone\Core\Helper\Connection\CurlCli |
||
57 | */ |
||
58 | protected $connCurlCli; |
||
59 | |||
60 | /** |
||
61 | * PAYONE connection fsockopen |
||
62 | * |
||
63 | * @var \Payone\Core\Helper\Connection\Fsockopen |
||
64 | */ |
||
65 | protected $connFsockopen; |
||
66 | |||
67 | /** |
||
68 | * Checkout session object |
||
69 | * |
||
70 | * @var \Magento\Checkout\Model\Session |
||
71 | */ |
||
72 | protected $checkoutSession; |
||
73 | |||
74 | /** |
||
75 | * Fields to copy from the request array to the order |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $requestToOrder = [ |
||
80 | 'reference' => 'payone_refnr', |
||
81 | 'request' => 'payone_authmode', |
||
82 | 'mode' => 'payone_mode', |
||
83 | 'mandate_identification' => 'payone_mandate_id', |
||
84 | 'workorderid' => 'payone_workorder_id', |
||
85 | 'add_paydata[installment_duration]' => 'payone_installment_duration', |
||
86 | 'add_paydata[shop_id]' => 'payone_ratepay_shop_id', |
||
87 | ]; |
||
88 | |||
89 | /** |
||
90 | * Fields to copy from the response to the order |
||
91 | * |
||
92 | * @var array |
||
93 | */ |
||
94 | protected $responseToOrder = [ |
||
95 | 'txid' => 'payone_txid', |
||
96 | 'mandate_identification' => 'payone_mandate_id', |
||
97 | 'clearing_reference' => 'payone_clearing_reference', |
||
98 | 'add_paydata[clearing_reference]' => 'payone_clearing_reference', |
||
99 | 'add_paydata[workorderid]' => 'payone_workorder_id', |
||
100 | 'clearing_bankaccount' => 'payone_clearing_bankaccount', |
||
101 | 'clearing_bankcode' => 'payone_clearing_bankcode', |
||
102 | 'clearing_bankcountry' => 'payone_clearing_bankcountry', |
||
103 | 'clearing_bankname' => 'payone_clearing_bankname', |
||
104 | 'clearing_bankaccountholder' => 'payone_clearing_bankaccountholder', |
||
105 | 'clearing_bankcity' => 'payone_clearing_bankcity', |
||
106 | 'clearing_bankiban' => 'payone_clearing_bankiban', |
||
107 | 'clearing_bankbic' => 'payone_clearing_bankbic', |
||
108 | 'clearing_duedate' => 'payone_clearing_duedate', |
||
109 | ]; |
||
110 | |||
111 | /** |
||
112 | * Fields to copy from the session to the order |
||
113 | * |
||
114 | * @var array |
||
115 | */ |
||
116 | protected $sessionToOrder = [ |
||
117 | 'payone_express_type' => 'payone_express_type', |
||
118 | ]; |
||
119 | |||
120 | /** |
||
121 | * Constructor |
||
122 | * |
||
123 | * @param \Magento\Framework\App\Helper\Context $context |
||
124 | * @param \Magento\Store\Model\StoreManagerInterface $storeManager |
||
125 | * @param \Payone\Core\Helper\Shop $shopHelper |
||
126 | * @param \Magento\Framework\App\State $state |
||
127 | * @param \Payone\Core\Helper\Connection\CurlPhp $connCurlPhp |
||
128 | * @param \Payone\Core\Helper\Connection\CurlCli $connCurlCli |
||
129 | * @param \Payone\Core\Helper\Connection\Fsockopen $connFsockopen |
||
130 | * @param \Magento\Checkout\Model\Session $checkoutSession |
||
131 | */ |
||
132 | public function __construct( |
||
133 | \Magento\Framework\App\Helper\Context $context, |
||
134 | \Magento\Store\Model\StoreManagerInterface $storeManager, |
||
135 | \Payone\Core\Helper\Shop $shopHelper, |
||
136 | \Magento\Framework\App\State $state, |
||
137 | \Payone\Core\Helper\Connection\CurlPhp $connCurlPhp, |
||
138 | \Payone\Core\Helper\Connection\CurlCli $connCurlCli, |
||
139 | \Payone\Core\Helper\Connection\Fsockopen $connFsockopen, |
||
140 | \Magento\Checkout\Model\Session $checkoutSession |
||
141 | ) { |
||
142 | parent::__construct($context, $storeManager, $shopHelper, $state); |
||
143 | $this->connCurlPhp = $connCurlPhp; |
||
144 | $this->connCurlCli = $connCurlCli; |
||
145 | $this->connFsockopen = $connFsockopen; |
||
146 | $this->checkoutSession = $checkoutSession; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * Check which communication possibilities are existing and send the request |
||
151 | * |
||
152 | * @param string $sRequestUrl |
||
153 | * @return array |
||
154 | */ |
||
155 | public function sendApiRequest($sRequestUrl) |
||
156 | { |
||
157 | $aParsedRequestUrl = parse_url($sRequestUrl); |
||
158 | if ($aParsedRequestUrl === false) { |
||
159 | return ["errormessage" => "Payone API request URL could not be parsed."]; |
||
160 | } |
||
161 | |||
162 | if ($this->connCurlPhp->isApplicable()) { |
||
163 | // php native curl exists so we gonna use it for requesting |
||
164 | $aResponse = $this->connCurlPhp->sendCurlPhpRequest($aParsedRequestUrl); |
||
165 | } elseif ($this->connCurlCli->isApplicable()) { |
||
166 | // cli version of curl exists on server |
||
167 | $aResponse = $this->connCurlCli->sendCurlCliRequest($aParsedRequestUrl); |
||
168 | } else { |
||
169 | // last resort => via sockets |
||
170 | $aResponse = $this->connFsockopen->sendSocketRequest($aParsedRequestUrl); |
||
171 | } |
||
172 | |||
173 | $aResponse = $this->formatOutputByResponse($aResponse); |
||
174 | |||
175 | if (!array_key_exists('status', $aResponse)) { |
||
176 | $aResponse['status'] = 'ERROR'; |
||
177 | $aResponse['errorcode'] = '0'; |
||
178 | $aResponse['customermessage'] = 'No connection to external service provider possible (timeout)'; |
||
179 | } |
||
180 | |||
181 | return $aResponse; |
||
182 | } |
||
183 | |||
184 | /** |
||
185 | * Format response to a clean output array |
||
186 | * |
||
187 | * @param array $aResponse |
||
188 | * @return array |
||
189 | */ |
||
190 | protected function formatOutputByResponse($aResponse) |
||
191 | { |
||
192 | $aOutput = []; |
||
193 | |||
194 | if (is_array($aResponse)) { // correct response existing? |
||
195 | foreach ($aResponse as $iLinenum => $sLine) { // go through line by line |
||
196 | $iPos = strpos($sLine, "="); |
||
197 | if ($iPos > 0) { // is a "=" as delimiter existing? |
||
198 | $aOutput[substr($sLine, 0, $iPos)] = trim(substr($sLine, $iPos + 1)); |
||
199 | } elseif (!empty($sLine)) { // is line not empty? |
||
200 | $aOutput[$iLinenum] = $sLine; // add the line unedited |
||
201 | } |
||
202 | } |
||
203 | } |
||
204 | |||
205 | return $aOutput; |
||
206 | } |
||
207 | |||
208 | /** |
||
209 | * Generate the request url out of the params and die api url |
||
210 | * |
||
211 | * @param array $aParameters |
||
212 | * @param string $sApiUrl |
||
213 | * @return string |
||
214 | */ |
||
215 | public function getRequestUrl($aParameters, $sApiUrl) |
||
216 | { |
||
217 | $sRequestUrl = ''; |
||
218 | foreach ($aParameters as $sKey => $mValue) { |
||
219 | if (is_array($mValue)) { // might be array |
||
220 | foreach ($mValue as $i => $sSubValue) { |
||
221 | $sRequestUrl .= "&".$sKey."[".$i."]=".urlencode($sSubValue ?? ''); |
||
222 | } |
||
223 | } else { |
||
224 | $sRequestUrl .= "&".$sKey."=".urlencode($mValue ?? ''); |
||
225 | } |
||
226 | } |
||
227 | $sRequestUrl = $sApiUrl."?".substr($sRequestUrl, 1); |
||
228 | return $sRequestUrl; |
||
229 | } |
||
230 | |||
231 | /** |
||
232 | * Copy Data to order by given map |
||
233 | * |
||
234 | * @param SalesOrder $oOrder |
||
235 | * @param array $aData |
||
236 | * @param array $aMap |
||
237 | * @return SalesOrder |
||
238 | */ |
||
239 | protected function addDataToOrder(SalesOrder $oOrder, $aData, $aMap) |
||
240 | { |
||
241 | foreach ($aMap as $sFrom => $sTo) { |
||
242 | if (isset($aData[$sFrom])) { |
||
243 | $oOrder->setData($sTo, $aData[$sFrom]); |
||
244 | } |
||
245 | } |
||
246 | return $oOrder; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Get data from session |
||
251 | * |
||
252 | * @return array |
||
253 | */ |
||
254 | protected function getSessionData() |
||
255 | { |
||
256 | $aData = []; |
||
257 | foreach ($this->sessionToOrder as $from => $to) { |
||
258 | $aData[$from] = $this->checkoutSession->getData($from, true); // get data and clear |
||
259 | } |
||
260 | return $aData; |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Add PAYONE information to the order object to be saved in the DB |
||
265 | * |
||
266 | * @param SalesOrder $oOrder |
||
267 | * @param array|false $aRequest |
||
268 | * @param array $aResponse |
||
269 | * @return void |
||
270 | */ |
||
271 | public function addPayoneOrderData(SalesOrder $oOrder, $aRequest, $aResponse) |
||
272 | { |
||
273 | $this->addDataToOrder($oOrder, $aRequest, $this->requestToOrder); |
||
274 | $this->addDataToOrder($oOrder, $aResponse, $this->responseToOrder); |
||
275 | $this->addDataToOrder($oOrder, $this->getSessionData(), $this->sessionToOrder); |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Check if invoice-data has to be added to the authorization request |
||
280 | * |
||
281 | * @param PayoneMethod $oPayment |
||
282 | * @param array|null $aPositions |
||
283 | * @return bool |
||
284 | */ |
||
285 | public function isInvoiceDataNeeded(PayoneMethod $oPayment, $aPositions = null) |
||
286 | { |
||
287 | $oInfoInstance = false; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
288 | try { |
||
289 | $oInfoInstance = $oPayment->getInfoInstance(); // using getInfoInstance when it is not set will throw an exception |
||
290 | } catch (\Exception $exc) { |
||
291 | // do nothing |
||
292 | } |
||
293 | |||
294 | $sStoreCode = null; |
||
295 | if ($oInfoInstance && $oInfoInstance->getOrder()) { |
||
296 | $sStoreCode = $oInfoInstance->getOrder()->getStore()->getCode(); |
||
297 | } |
||
298 | |||
299 | if ($oPayment instanceof RatepayBase && is_array($aPositions) && empty($aPositions)) { // empty array means products and shipping costs were deselected |
||
300 | return false; // RatePay demands that adjustment refunds without products and shipping costs are sent without basket info |
||
301 | } |
||
302 | |||
303 | $blInvoiceEnabled = (bool)$this->getConfigParam('transmit_enabled', 'invoicing', 'payone_general', $sStoreCode); // invoicing enabled? |
||
304 | if ($blInvoiceEnabled || $oPayment->needsProductInfo()) { |
||
305 | return true; // invoice data needed |
||
306 | } |
||
307 | return false; // invoice data not needed |
||
308 | } |
||
309 | |||
310 | /** |
||
311 | * Return base or display currency of the order depending on the config |
||
312 | * |
||
313 | * @param SalesOrder $oOrder |
||
314 | * @return null|string |
||
315 | */ |
||
316 | public function getCurrencyFromOrder(SalesOrder $oOrder) |
||
317 | { |
||
318 | $sCurrency = $oOrder->getBaseCurrencyCode(); |
||
319 | if ($this->getConfigParam('currency', 'global', 'payone_general', $oOrder->getStore()->getCode()) == 'display') { |
||
320 | $sCurrency = $oOrder->getOrderCurrencyCode(); |
||
321 | } |
||
322 | return $sCurrency; |
||
323 | } |
||
324 | |||
325 | /** |
||
326 | * Return base or display amount of the order depending on the config |
||
327 | * |
||
328 | * @param SalesOrder $oQuote |
||
329 | * @return float |
||
330 | */ |
||
331 | public function getOrderAmount(SalesOrder $oOrder) |
||
332 | { |
||
333 | $dAmount = $oOrder->getBaseGrandTotal(); |
||
334 | if ($this->getConfigParam('currency', 'global', 'payone_general', $oOrder->getStore()->getCode()) == 'display') { |
||
335 | $dAmount = $oOrder->getGrandTotal(); // send display amount instead of base amount |
||
336 | } |
||
337 | return $dAmount; |
||
338 | } |
||
339 | |||
340 | /** |
||
341 | * Return base or display currency of the quote depending on the config |
||
342 | * |
||
343 | * @param Quote $oQuote |
||
344 | * @return string |
||
345 | */ |
||
346 | public function getCurrencyFromQuote(Quote $oQuote) |
||
347 | { |
||
348 | $sCurrency = $oQuote->getBaseCurrencyCode(); |
||
349 | if ($this->getConfigParam('currency', 'global', 'payone_general', $oQuote->getStore()->getCode()) == 'display') { |
||
350 | $sCurrency = $oQuote->getQuoteCurrencyCode(); |
||
351 | } |
||
352 | return $sCurrency; |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * Return base or display amount of the quote depending on the config |
||
357 | * |
||
358 | * @param Quote $oQuote |
||
359 | * @return float |
||
360 | */ |
||
361 | public function getQuoteAmount(Quote $oQuote) |
||
362 | { |
||
363 | $dAmount = $oQuote->getBaseGrandTotal(); |
||
364 | if ($this->getConfigParam('currency', 'global', 'payone_general', $oQuote->getStore()->getCode()) == 'display') { |
||
365 | $dAmount = $oQuote->getGrandTotal(); // send display amount instead of base amount |
||
366 | } |
||
367 | return $dAmount; |
||
368 | } |
||
369 | } |
||
370 |