Total Complexity | 104 |
Total Lines | 833 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like WcPagantisGateway often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use WcPagantisGateway, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
26 | class WcPagantisGateway extends WC_Payment_Gateway |
||
|
|||
27 | { |
||
28 | const METHOD_ID = "pagantis"; |
||
29 | |||
30 | /** Orders tablename */ |
||
31 | const ORDERS_TABLE = 'cart_process'; |
||
32 | |||
33 | /** Concurrency tablename */ |
||
34 | const LOGS_TABLE = 'pagantis_logs'; |
||
35 | |||
36 | const NOT_CONFIRMED = 'No se ha podido confirmar el pago'; |
||
37 | |||
38 | const CONFIG_TABLE = 'pagantis_config'; |
||
39 | |||
40 | /** @var Array $extraConfig */ |
||
41 | public $extraConfig; |
||
42 | |||
43 | /** @var string $language */ |
||
44 | public $language; |
||
45 | |||
46 | /** |
||
47 | * WcPagantisGateway constructor. |
||
48 | */ |
||
49 | public function __construct() |
||
50 | { |
||
51 | //Mandatory vars for plugin |
||
52 | $this->id = WcPagantisGateway::METHOD_ID; |
||
53 | $this->has_fields = true; |
||
54 | $this->method_title = ucfirst($this->id); |
||
55 | |||
56 | //Useful vars |
||
57 | $this->template_path = plugin_dir_path(__FILE__) . '../templates/'; |
||
58 | $this->allowed_currencies = array("EUR"); |
||
59 | $this->mainFileLocation = dirname(plugin_dir_path(__FILE__)) . '/WC_Pagantis.php'; |
||
60 | $this->plugin_info = get_file_data($this->mainFileLocation, array('Version' => 'Version'), false); |
||
61 | $this->language = strstr(get_locale(), '_', true); |
||
62 | |||
63 | if ($this->language == 'es' || $this->language == '') { |
||
64 | $this->icon = esc_url(plugins_url('../assets/images/logopagamastarde.png', __FILE__)); |
||
65 | } else { |
||
66 | $this->icon = esc_url(plugins_url('../assets/images/logo.png', __FILE__)); |
||
67 | } |
||
68 | |||
69 | //Panel form fields |
||
70 | $this->form_fields = include(plugin_dir_path(__FILE__).'../includes/settings-pagantis.php');//Panel options |
||
71 | $this->init_settings(); |
||
72 | |||
73 | $this->extraConfig = $this->getExtraConfig(); |
||
74 | $this->title = __($this->extraConfig['PAGANTIS_TITLE'], 'pagantis'); |
||
75 | |||
76 | $this->settings['ok_url'] = ($this->extraConfig['PAGANTIS_URL_OK']!='')?$this->extraConfig['PAGANTIS_URL_OK']:$this->generateOkUrl(); |
||
77 | $this->settings['ko_url'] = ($this->extraConfig['PAGANTIS_URL_KO']!='')?$this->extraConfig['PAGANTIS_URL_KO']:$this->generateKoUrl(); |
||
78 | foreach ($this->settings as $setting_key => $setting_value) { |
||
79 | $this->$setting_key = $setting_value; |
||
80 | } |
||
81 | |||
82 | //Hooks |
||
83 | add_action('woocommerce_update_options_payment_gateways_'.$this->id, array($this,'process_admin_options')); //Save plugin options |
||
84 | add_action('admin_notices', array($this, 'pagantisCheckFields')); //Check config fields |
||
85 | add_action('woocommerce_receipt_'.$this->id, array($this, 'pagantisReceiptPage')); //Pagantis form |
||
86 | add_action('woocommerce_api_wcpagantisgateway', array($this, 'pagantisNotification')); //Json Notification |
||
87 | add_filter('woocommerce_payment_complete_order_status', array($this,'pagantisCompleteStatus'), 10, 3); |
||
88 | add_filter('load_textdomain_mofile', array($this, 'loadPagantisTranslation'), 10, 2); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @param $mofile |
||
93 | * @param $domain |
||
94 | * |
||
95 | * @return string |
||
96 | */ |
||
97 | public function loadPagantisTranslation($mofile, $domain) |
||
98 | { |
||
99 | if ('pagantis' === $domain) { |
||
100 | $mofile = WP_LANG_DIR . '/../plugins/pagantis/languages/pagantis-' . get_locale() . '.mo'; |
||
101 | } |
||
102 | return $mofile; |
||
103 | } |
||
104 | |||
105 | /*********** |
||
106 | * |
||
107 | * HOOKS |
||
108 | * |
||
109 | ***********/ |
||
110 | |||
111 | /** |
||
112 | * PANEL - Display admin panel -> Hook: woocommerce_update_options_payment_gateways_pagantis |
||
113 | */ |
||
114 | public function admin_options() |
||
115 | { |
||
116 | $template_fields = array( |
||
117 | 'panel_description' => $this->method_description, |
||
118 | 'button1_label' => __('Login to your panel', 'pagantis'), |
||
119 | 'button2_label' => __('Documentation', 'pagantis'), |
||
120 | 'logo' => $this->icon, |
||
121 | 'settings' => $this->generate_settings_html($this->form_fields, false) |
||
122 | ); |
||
123 | wc_get_template('admin_header.php', $template_fields, '', $this->template_path); |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * PANEL - Check admin panel fields -> Hook: admin_notices |
||
128 | */ |
||
129 | public function pagantisCheckFields() |
||
158 | } |
||
159 | } |
||
160 | |||
161 | |||
162 | /** |
||
163 | * CHECKOUT - Generate the pagantis form. "Return" iframe or redirect. - Hook: woocommerce_receipt_pagantis |
||
164 | * @param $order_id |
||
165 | * |
||
166 | * @throws Exception |
||
167 | */ |
||
168 | public function pagantisReceiptPage($order_id) |
||
169 | { |
||
170 | try { |
||
171 | require_once(__ROOT__.'/vendor/autoload.php'); |
||
172 | global $woocommerce; |
||
173 | $order = new WC_Order($order_id); |
||
174 | $order->set_payment_method(ucfirst($this->id)); |
||
175 | $order->save(); |
||
176 | |||
177 | if (!isset($order)) { |
||
178 | throw new Exception(_("Order not found")); |
||
179 | } |
||
180 | |||
181 | $shippingAddress = $order->get_address('shipping'); |
||
182 | $billingAddress = $order->get_address('billing'); |
||
183 | if ($shippingAddress['address_1'] == '') { |
||
184 | $shippingAddress = $billingAddress; |
||
185 | } |
||
186 | |||
187 | $national_id = $this->getNationalId($order); |
||
188 | $tax_id = $this->getTaxId($order); |
||
189 | |||
190 | $userAddress = new Address(); |
||
191 | $userAddress |
||
192 | ->setZipCode($shippingAddress['postcode']) |
||
193 | ->setFullName($shippingAddress['first_name']." ".$shippingAddress['last_name']) |
||
194 | ->setCountryCode('ES') |
||
195 | ->setCity($shippingAddress['city']) |
||
196 | ->setAddress($shippingAddress['address_1']." ".$shippingAddress['address_2']) |
||
197 | ; |
||
198 | $orderShippingAddress = new Address(); |
||
199 | $orderShippingAddress |
||
200 | ->setZipCode($shippingAddress['postcode']) |
||
201 | ->setFullName($shippingAddress['first_name']." ".$shippingAddress['last_name']) |
||
202 | ->setCountryCode('ES') |
||
203 | ->setCity($shippingAddress['city']) |
||
204 | ->setAddress($shippingAddress['address_1']." ".$shippingAddress['address_2']) |
||
205 | ->setFixPhone($shippingAddress['phone']) |
||
206 | ->setMobilePhone($shippingAddress['phone']) |
||
207 | ->setNationalId($national_id) |
||
208 | ->setTaxId($tax_id) |
||
209 | ; |
||
210 | $orderBillingAddress = new Address(); |
||
211 | $orderBillingAddress |
||
212 | ->setZipCode($billingAddress['postcode']) |
||
213 | ->setFullName($billingAddress['first_name']." ".$billingAddress['last_name']) |
||
214 | ->setCountryCode('ES') |
||
215 | ->setCity($billingAddress['city']) |
||
216 | ->setAddress($billingAddress['address_1']." ".$billingAddress['address_2']) |
||
217 | ->setFixPhone($billingAddress['phone']) |
||
218 | ->setMobilePhone($billingAddress['phone']) |
||
219 | ->setNationalId($national_id) |
||
220 | ->setTaxId($tax_id) |
||
221 | ; |
||
222 | $orderUser = new User(); |
||
223 | $orderUser |
||
224 | ->setAddress($userAddress) |
||
225 | ->setFullName($billingAddress['first_name']." ".$billingAddress['last_name']) |
||
226 | ->setBillingAddress($orderBillingAddress) |
||
227 | ->setEmail($billingAddress['email']) |
||
228 | ->setFixPhone($billingAddress['phone']) |
||
229 | ->setMobilePhone($billingAddress['phone']) |
||
230 | ->setShippingAddress($orderShippingAddress) |
||
231 | ->setNationalId($national_id) |
||
232 | ->setTaxId($tax_id) |
||
233 | ; |
||
234 | |||
235 | $previousOrders = $this->getOrders($order->get_user(), $billingAddress['email']); |
||
236 | foreach ($previousOrders as $previousOrder) { |
||
237 | $orderHistory = new OrderHistory(); |
||
238 | $orderElement = wc_get_order($previousOrder); |
||
239 | $orderCreated = $orderElement->get_date_created(); |
||
240 | $orderHistory |
||
241 | ->setAmount(intval(100 * $orderElement->get_total())) |
||
242 | ->setDate(new \DateTime($orderCreated->date('Y-m-d H:i:s'))) |
||
243 | ; |
||
244 | $orderUser->addOrderHistory($orderHistory); |
||
245 | } |
||
246 | |||
247 | $metadataOrder = new Metadata(); |
||
248 | $metadata = array( |
||
249 | 'woocommerce' => WC()->version, |
||
250 | 'pagantis' => $this->plugin_info['Version'], |
||
251 | 'php' => phpversion() |
||
252 | ); |
||
253 | foreach ($metadata as $key => $metadatum) { |
||
254 | $metadataOrder->addMetadata($key, $metadatum); |
||
255 | } |
||
256 | |||
257 | $details = new Details(); |
||
258 | $shippingCost = $order->shipping_total; |
||
259 | $details->setShippingCost(intval(strval(100 * $shippingCost))); |
||
260 | $items = $woocommerce->cart->get_cart(); |
||
261 | $promotedAmount = 0; |
||
262 | foreach ($items as $key => $item) { |
||
263 | $product = new Product(); |
||
264 | $productDescription = sprintf( |
||
265 | '%s %s %s', |
||
266 | $item['data']->get_title(), |
||
267 | $item['data']->get_description(), |
||
268 | $item['data']->get_short_description() |
||
269 | ); |
||
270 | $product |
||
271 | ->setAmount(intval(100 * $item['line_total'])) |
||
272 | ->setQuantity($item['quantity']) |
||
273 | ->setDescription($productDescription) |
||
274 | ; |
||
275 | $details->addProduct($product); |
||
276 | |||
277 | $promotedProduct = $this->isPromoted($item['product_id']); |
||
278 | if ($promotedProduct == 'true') { |
||
279 | $promotedAmount+=$product->getAmount(); |
||
280 | $promotedMessage = 'Promoted Item: ' . $product->getDescription() . |
||
281 | ' Price: ' . $item['line_total'] . |
||
282 | ' Qty: ' . $product->getQuantity() . |
||
283 | ' Item ID: ' . $item['id_product']; |
||
284 | $metadataOrder->addMetadata('promotedProduct', $promotedMessage); |
||
285 | } |
||
286 | } |
||
287 | |||
288 | $orderShoppingCart = new ShoppingCart(); |
||
289 | $orderShoppingCart |
||
290 | ->setDetails($details) |
||
291 | ->setOrderReference($order->get_id()) |
||
292 | ->setPromotedAmount($promotedAmount) |
||
293 | ->setTotalAmount(intval(strval(100 * $order->total))) |
||
294 | ; |
||
295 | $orderConfigurationUrls = new Urls(); |
||
296 | $cancelUrl = $this->getKoUrl($order); |
||
297 | $callback_arg = array( |
||
298 | 'wc-api'=>'wcpagantisgateway', |
||
299 | 'key'=>$order->get_order_key(), |
||
300 | 'order-received'=>$order->get_id()); |
||
301 | $callback_url = add_query_arg($callback_arg, home_url('/')); |
||
302 | $orderConfigurationUrls |
||
303 | ->setCancel($cancelUrl) |
||
304 | ->setKo($callback_url) |
||
305 | ->setAuthorizedNotificationCallback($callback_url) |
||
306 | ->setRejectedNotificationCallback($callback_url) |
||
307 | ->setOk($callback_url) |
||
308 | ; |
||
309 | $orderChannel = new Channel(); |
||
310 | $orderChannel |
||
311 | ->setAssistedSale(false) |
||
312 | ->setType(Channel::ONLINE) |
||
313 | ; |
||
314 | $orderConfiguration = new Configuration(); |
||
315 | |||
316 | $orderConfiguration |
||
317 | ->setChannel($orderChannel) |
||
318 | ->setUrls($orderConfigurationUrls) |
||
319 | ->setPurchaseCountry($this->language) |
||
320 | ; |
||
321 | |||
322 | $orderApiClient = new Order(); |
||
323 | $orderApiClient |
||
324 | ->setConfiguration($orderConfiguration) |
||
325 | ->setMetadata($metadataOrder) |
||
326 | ->setShoppingCart($orderShoppingCart) |
||
327 | ->setUser($orderUser) |
||
328 | ; |
||
329 | |||
330 | if ($this->pagantis_public_key=='' || $this->pagantis_private_key=='') { |
||
331 | throw new \Exception('Public and Secret Key not found'); |
||
332 | } |
||
333 | $orderClient = new Client($this->pagantis_public_key, $this->pagantis_private_key); |
||
334 | $pagantisOrder = $orderClient->createOrder($orderApiClient); |
||
335 | if ($pagantisOrder instanceof \Pagantis\OrdersApiClient\Model\Order) { |
||
336 | $url = $pagantisOrder->getActionUrls()->getForm(); |
||
337 | $this->insertRow($order->get_id(), $pagantisOrder->getId()); |
||
338 | } else { |
||
339 | throw new OrderNotFoundException(); |
||
340 | } |
||
341 | |||
342 | if ($url=="") { |
||
343 | throw new Exception(_("No ha sido posible obtener una respuesta de Pagantis")); |
||
344 | } elseif ($this->extraConfig['PAGANTIS_FORM_DISPLAY_TYPE']=='0') { |
||
345 | wp_redirect($url); |
||
346 | exit; |
||
347 | } else { |
||
348 | $template_fields = array( |
||
349 | 'url' => $url, |
||
350 | 'checkoutUrl' => $cancelUrl |
||
351 | ); |
||
352 | wc_get_template('iframe.php', $template_fields, '', $this->template_path); |
||
353 | } |
||
354 | } catch (\Exception $exception) { |
||
355 | wc_add_notice(__('Payment error ', 'pagantis') . $exception->getMessage(), 'error'); |
||
356 | $this->insertLog($exception); |
||
357 | $checkout_url = get_permalink(wc_get_page_id('checkout')); |
||
358 | wp_redirect($checkout_url); |
||
359 | exit; |
||
360 | } |
||
361 | } |
||
362 | |||
363 | /** |
||
364 | * NOTIFICATION - Endpoint for Json notification - Hook: woocommerce_api_wcpagantisgateway |
||
365 | */ |
||
366 | public function pagantisNotification() |
||
367 | { |
||
368 | try { |
||
369 | $origin = ($_SERVER['REQUEST_METHOD'] == 'POST') ? 'Notify' : 'Order'; |
||
370 | |||
371 | include_once('notifyController.php'); |
||
372 | $notify = new WcPagantisNotify(); |
||
373 | $notify->setOrigin($origin); |
||
374 | /** @var \Pagantis\ModuleUtils\Model\Response\AbstractJsonResponse $result */ |
||
375 | $result = $notify->processInformation(); |
||
376 | } catch (Exception $exception) { |
||
377 | $result['notification_message'] = $exception->getMessage(); |
||
378 | $result['notification_error'] = true; |
||
379 | } |
||
380 | |||
381 | $paymentOrder = new WC_Order($result->getMerchantOrderId()); |
||
382 | if ($paymentOrder instanceof WC_Order) { |
||
383 | $orderStatus = strtolower($paymentOrder->get_status()); |
||
384 | } else { |
||
385 | $orderStatus = 'cancelled'; |
||
386 | } |
||
387 | $acceptedStatus = array('processing', 'completed'); |
||
388 | if (in_array($orderStatus, $acceptedStatus)) { |
||
389 | $returnUrl = $this->getOkUrl($paymentOrder); |
||
390 | } else { |
||
391 | $returnUrl = $this->getKoUrl($paymentOrder); |
||
392 | } |
||
393 | |||
394 | wp_redirect($returnUrl); |
||
395 | exit; |
||
396 | } |
||
397 | |||
398 | /** |
||
399 | * After failed status, set to processing not complete -> Hook: woocommerce_payment_complete_order_status |
||
400 | * @param $status |
||
401 | * @param $order_id |
||
402 | * @param $order |
||
403 | * |
||
404 | * @return string |
||
405 | */ |
||
406 | public function pagantisCompleteStatus($status, $order_id, $order) |
||
407 | { |
||
408 | if ($order->get_payment_method() == WcPagantisGateway::METHOD_ID) { |
||
409 | if ($order->get_status() == 'failed') { |
||
410 | $status = 'processing'; |
||
411 | } elseif ($order->get_status() == 'pending' && $status=='completed') { |
||
412 | $status = 'processing'; |
||
413 | } |
||
414 | } |
||
415 | |||
416 | return $status; |
||
417 | } |
||
418 | |||
419 | /*********** |
||
420 | * |
||
421 | * REDEFINED FUNCTIONS |
||
422 | * |
||
423 | ***********/ |
||
424 | |||
425 | /** |
||
426 | * CHECKOUT - Check if payment method is available (called by woocommerce, can't apply cammel caps) |
||
427 | * @return bool |
||
428 | */ |
||
429 | public function is_available() |
||
430 | { |
||
431 | $locale = strtolower(strstr(get_locale(), '_', true)); |
||
432 | $allowedCountries = unserialize($this->extraConfig['PAGANTIS_ALLOWED_COUNTRIES']); |
||
433 | $allowedCountry = (in_array(strtolower($locale), $allowedCountries)); |
||
434 | if ($this->enabled==='yes' && $this->pagantis_public_key!='' && $this->pagantis_private_key!='' && |
||
435 | (int)$this->get_order_total()>$this->extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT'] && $allowedCountry) { |
||
436 | return true; |
||
437 | } |
||
438 | |||
439 | return false; |
||
440 | } |
||
441 | |||
442 | /** |
||
443 | * CHECKOUT - Checkout + admin panel title(method_title - get_title) (called by woocommerce,can't apply cammel caps) |
||
444 | * @return string |
||
445 | */ |
||
446 | public function get_title() |
||
447 | { |
||
448 | return __($this->extraConfig['PAGANTIS_TITLE'], 'pagantis'); |
||
449 | } |
||
450 | |||
451 | /** |
||
452 | * CHECKOUT - Called after push pagantis button on checkout(called by woocommerce, can't apply cammel caps |
||
453 | * @param $order_id |
||
454 | * @return array |
||
455 | */ |
||
456 | public function process_payment($order_id) |
||
457 | { |
||
458 | try { |
||
459 | $order = new WC_Order($order_id); |
||
460 | |||
461 | $redirectUrl = $order->get_checkout_payment_url(true); //pagantisReceiptPage function |
||
462 | if (strpos($redirectUrl, 'order-pay=')===false) { |
||
463 | $redirectUrl.="&order-pay=".$order_id; |
||
464 | } |
||
465 | |||
466 | return array( |
||
467 | 'result' => 'success', |
||
468 | 'redirect' => $redirectUrl |
||
469 | ); |
||
470 | |||
471 | } catch (Exception $e) { |
||
472 | wc_add_notice(__('Payment error ', 'pagantis') . $e->getMessage(), 'error'); |
||
473 | return array(); |
||
474 | } |
||
475 | } |
||
476 | |||
477 | /** |
||
478 | * CHECKOUT - simulator (called by woocommerce, can't apply cammel caps) |
||
479 | */ |
||
480 | public function payment_fields() |
||
481 | { |
||
482 | $locale = strtolower(strstr(get_locale(), '_', true)); |
||
483 | $allowedCountries = unserialize($this->extraConfig['PAGANTIS_ALLOWED_COUNTRIES']); |
||
484 | $allowedCountry = (in_array(strtolower($locale), $allowedCountries)); |
||
485 | $promotedAmount = $this->getPromotedAmount(); |
||
486 | |||
487 | $template_fields = array( |
||
488 | 'public_key' => $this->pagantis_public_key, |
||
489 | 'total' => WC()->session->cart_totals['total'], |
||
490 | 'enabled' => $this->settings['enabled'], |
||
491 | 'min_installments' => $this->extraConfig['PAGANTIS_DISPLAY_MIN_AMOUNT'], |
||
492 | 'simulator_enabled' => $this->settings['pagantis_simulator'], |
||
493 | 'locale' => $locale, |
||
494 | 'allowedCountry' => $allowedCountry, |
||
495 | 'simulator_type' => $this->extraConfig['PAGANTIS_SIMULATOR_DISPLAY_TYPE'], |
||
496 | 'promoted_amount' => $promotedAmount |
||
497 | ); |
||
498 | wc_get_template('checkout_description.php', $template_fields, '', $this->template_path); |
||
499 | } |
||
500 | |||
501 | /*********** |
||
502 | * |
||
503 | * UTILS FUNCTIONS |
||
504 | * |
||
505 | ***********/ |
||
506 | |||
507 | /** |
||
508 | * PANEL KO_URL FIELD |
||
509 | * CHECKOUT PAGE => ?page_id=91 // ORDER-CONFIRMATION PAGE => ?page_id=91&order-pay=<order_id>&key=<order_key> |
||
510 | */ |
||
511 | private function generateOkUrl() |
||
512 | { |
||
513 | return $this->generateUrl($this->get_return_url()); |
||
514 | } |
||
515 | |||
516 | /** |
||
517 | * PANEL OK_URL FIELD |
||
518 | */ |
||
519 | private function generateKoUrl() |
||
520 | { |
||
521 | return $this->generateUrl(get_permalink(wc_get_page_id('checkout'))); |
||
522 | } |
||
523 | |||
524 | /** |
||
525 | * Replace empty space by {{var}} |
||
526 | * @param $url |
||
527 | * |
||
528 | * @return string |
||
529 | */ |
||
530 | private function generateUrl($url) |
||
531 | { |
||
532 | $parsed_url = parse_url($url); |
||
533 | if ($parsed_url !== false) { |
||
534 | $parsed_url['query'] = !isset($parsed_url['query']) ? '' : $parsed_url['query']; |
||
535 | parse_str($parsed_url['query'], $arrayParams); |
||
536 | foreach ($arrayParams as $keyParam => $valueParam) { |
||
537 | if ($valueParam=='') { |
||
538 | $arrayParams[$keyParam] = '{{'.$keyParam.'}}'; |
||
539 | } |
||
540 | } |
||
541 | $parsed_url['query'] = http_build_query($arrayParams); |
||
542 | $return_url = $this->unparseUrl($parsed_url); |
||
543 | return urldecode($return_url); |
||
544 | } else { |
||
545 | return $url; |
||
546 | } |
||
547 | } |
||
548 | |||
549 | /** |
||
550 | * Replace {{}} by vars values inside ok_url |
||
551 | * @param $order |
||
552 | * |
||
553 | * @return string |
||
554 | */ |
||
555 | private function getOkUrl($order) |
||
556 | { |
||
557 | return $this->getKeysUrl($order, $this->ok_url); |
||
558 | } |
||
559 | |||
560 | /** |
||
561 | * Replace {{}} by vars values inside ko_url |
||
562 | * @param $order |
||
563 | * |
||
564 | * @return string |
||
565 | */ |
||
566 | private function getKoUrl($order) |
||
567 | { |
||
568 | return $this->getKeysUrl($order, $this->ko_url); |
||
569 | } |
||
570 | |||
571 | /** |
||
572 | * Replace {{}} by vars values |
||
573 | * @param $order |
||
574 | * @param $url |
||
575 | * |
||
576 | * @return string |
||
577 | */ |
||
578 | private function getKeysUrl($order, $url) |
||
579 | { |
||
580 | $defaultFields = (get_class($order)=='WC_Order') ? |
||
581 | array('order-received'=>$order->get_id(), 'key'=>$order->get_order_key()) : |
||
582 | array(); |
||
583 | |||
584 | $parsedUrl = parse_url($url); |
||
585 | if ($parsedUrl !== false) { |
||
586 | //Replace parameters from url |
||
587 | $parsedUrl['query'] = $this->getKeysParametersUrl($parsedUrl['query'], $defaultFields); |
||
588 | |||
589 | //Replace path from url |
||
590 | $parsedUrl['path'] = $this->getKeysPathUrl($parsedUrl['path'], $defaultFields); |
||
591 | |||
592 | $returnUrl = $this->unparseUrl($parsedUrl); |
||
593 | return $returnUrl; |
||
594 | } |
||
595 | return $url; |
||
596 | } |
||
597 | |||
598 | /** |
||
599 | * Replace {{}} by vars values inside parameters |
||
600 | * @param $queryString |
||
601 | * @param $defaultFields |
||
602 | * |
||
603 | * @return string |
||
604 | */ |
||
605 | private function getKeysParametersUrl($queryString, $defaultFields) |
||
606 | { |
||
607 | parse_str(html_entity_decode($queryString), $arrayParams); |
||
608 | $commonKeys = array_intersect_key($arrayParams, $defaultFields); |
||
609 | if (count($commonKeys)) { |
||
610 | $arrayResult = array_merge($arrayParams, $defaultFields); |
||
611 | } else { |
||
612 | $arrayResult = $arrayParams; |
||
613 | } |
||
614 | return urldecode(http_build_query($arrayResult)); |
||
615 | } |
||
616 | |||
617 | /** |
||
618 | * Replace {{}} by vars values inside path |
||
619 | * @param $pathString |
||
620 | * @param $defaultFields |
||
621 | * |
||
622 | * @return string |
||
623 | */ |
||
624 | private function getKeysPathUrl($pathString, $defaultFields) |
||
625 | { |
||
626 | $arrayParams = explode("/", $pathString); |
||
627 | foreach ($arrayParams as $keyParam => $valueParam) { |
||
628 | preg_match('#\{{.*?}\}#', $valueParam, $match); |
||
629 | if (count($match)) { |
||
630 | $key = str_replace(array('{{','}}'), array('',''), $match[0]); |
||
631 | $arrayParams[$keyParam] = $defaultFields[$key]; |
||
632 | } |
||
633 | } |
||
634 | return implode('/', $arrayParams); |
||
635 | } |
||
636 | |||
637 | /** |
||
638 | * Replace {{var}} by empty space |
||
639 | * @param $parsed_url |
||
640 | * |
||
641 | * @return string |
||
642 | */ |
||
643 | private function unparseUrl($parsed_url) |
||
644 | { |
||
645 | $scheme = isset($parsed_url['scheme']) ? $parsed_url['scheme'] . '://' : ''; |
||
646 | $host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; |
||
647 | $port = isset($parsed_url['port']) ? ':' . $parsed_url['port'] : ''; |
||
648 | $query = isset($parsed_url['query']) ? '?' . $parsed_url['query'] : ''; |
||
649 | $fragment = isset($parsed_url['fragment']) ? '#' . $parsed_url['fragment'] : ''; |
||
650 | $path = $parsed_url['path']; |
||
651 | return $scheme . $host . $port . $path . $query . $fragment; |
||
652 | } |
||
653 | |||
654 | /** |
||
655 | * Get the orders of a customer |
||
656 | * @param $current_user |
||
657 | * @param $billingEmail |
||
658 | * |
||
659 | * @return mixed |
||
660 | */ |
||
661 | private function getOrders($current_user, $billingEmail) |
||
662 | { |
||
663 | $sign_up = ''; |
||
664 | $total_orders = 0; |
||
665 | $total_amt = 0; |
||
666 | $refund_amt = 0; |
||
667 | $total_refunds = 0; |
||
668 | $partial_refunds = 0; |
||
669 | if ($current_user->user_login) { |
||
670 | $is_guest = "false"; |
||
671 | $sign_up = substr($current_user->user_registered, 0, 10); |
||
672 | $customer_orders = get_posts(array( |
||
673 | 'numberposts' => - 1, |
||
674 | 'meta_key' => '_customer_user', |
||
675 | 'meta_value' => $current_user->ID, |
||
676 | 'post_type' => array( 'shop_order' ), |
||
677 | 'post_status' => array( 'wc-completed', 'wc-processing', 'wc-refunded' ), |
||
678 | )); |
||
679 | } else { |
||
680 | $is_guest = "true"; |
||
681 | $customer_orders = get_posts(array( |
||
682 | 'numberposts' => - 1, |
||
683 | 'meta_key' => '_billing_email', |
||
684 | 'meta_value' => $billingEmail, |
||
685 | 'post_type' => array( 'shop_order' ), |
||
686 | 'post_status' => array( 'wc-completed', 'wc-processing', 'wc-refunded'), |
||
687 | )); |
||
688 | foreach ($customer_orders as $customer_order) { |
||
689 | if (trim($sign_up)=='' || |
||
690 | strtotime(substr($customer_order->post_date, 0, 10)) <= strtotime($sign_up)) { |
||
691 | $sign_up = substr($customer_order->post_date, 0, 10); |
||
692 | } |
||
693 | } |
||
694 | } |
||
695 | |||
696 | return $customer_orders; |
||
697 | } |
||
698 | |||
699 | |||
700 | /** |
||
701 | * @param $orderId |
||
702 | * @param $pagantisOrderId |
||
703 | * |
||
704 | * @throws Exception |
||
705 | */ |
||
706 | private function insertRow($orderId, $pagantisOrderId) |
||
707 | { |
||
708 | global $wpdb; |
||
709 | $this->checkDbTable(); |
||
710 | $tableName = $wpdb->prefix.self::ORDERS_TABLE; |
||
711 | |||
712 | //Check if id exists |
||
713 | $resultsSelect = $wpdb->get_results("select * from $tableName where id='$orderId'"); |
||
714 | $countResults = count($resultsSelect); |
||
715 | if ($countResults == 0) { |
||
716 | $wpdb->insert( |
||
717 | $tableName, |
||
718 | array('id' => $orderId, 'order_id' => $pagantisOrderId), |
||
719 | array('%d', '%s') |
||
720 | ); |
||
721 | } else { |
||
722 | $wpdb->update( |
||
723 | $tableName, |
||
724 | array('order_id' => $pagantisOrderId), |
||
725 | array('id' => $orderId), |
||
726 | array('%s'), |
||
727 | array('%d') |
||
728 | ); |
||
729 | } |
||
730 | } |
||
731 | |||
732 | /** |
||
733 | * Check if orders table exists |
||
734 | */ |
||
735 | private function checkDbTable() |
||
736 | { |
||
737 | global $wpdb; |
||
738 | $tableName = $wpdb->prefix.self::ORDERS_TABLE; |
||
739 | |||
740 | if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) { |
||
741 | $charset_collate = $wpdb->get_charset_collate(); |
||
742 | $sql = "CREATE TABLE $tableName ( id int, order_id varchar(50), wc_order_id varchar(50), |
||
743 | UNIQUE KEY id (id)) $charset_collate"; |
||
744 | |||
745 | require_once(ABSPATH.'wp-admin/includes/upgrade.php'); |
||
746 | dbDelta($sql); |
||
747 | } |
||
748 | } |
||
749 | |||
750 | /** |
||
751 | * @return array |
||
752 | */ |
||
753 | private function getExtraConfig() |
||
754 | { |
||
755 | global $wpdb; |
||
756 | $tableName = $wpdb->prefix.self::CONFIG_TABLE; |
||
757 | $response = array(); |
||
758 | $dbResult = $wpdb->get_results("select config, value from $tableName", ARRAY_A); |
||
759 | foreach ($dbResult as $value) { |
||
760 | $response[$value['config']] = $value['value']; |
||
761 | } |
||
762 | |||
763 | return $response; |
||
764 | } |
||
765 | |||
766 | /** |
||
767 | * @param $order |
||
768 | * |
||
769 | * @return null |
||
770 | */ |
||
771 | private function getNationalId($order) |
||
772 | { |
||
773 | foreach ((array)$order->get_meta_data() as $mdObject) { |
||
774 | $data = $mdObject->get_data(); |
||
775 | if ($data['key'] == 'vat_number') { |
||
776 | return $data['value']; |
||
777 | } |
||
778 | } |
||
779 | |||
780 | return null; |
||
781 | } |
||
782 | |||
783 | /** |
||
784 | * @param $order |
||
785 | * |
||
786 | * @return mixed |
||
787 | */ |
||
788 | private function getTaxId($order) |
||
789 | { |
||
790 | foreach ((array)$order->get_meta_data() as $mdObject) { |
||
791 | $data = $mdObject->get_data(); |
||
792 | if ($data['key'] == 'billing_cfpiva') { |
||
793 | return $data['value']; |
||
794 | } |
||
795 | } |
||
796 | } |
||
797 | |||
798 | /** |
||
799 | * @param null $exception |
||
800 | * @param null $message |
||
801 | */ |
||
802 | private function insertLog($exception = null, $message = null) |
||
803 | { |
||
804 | global $wpdb; |
||
805 | $this->checkDbLogTable(); |
||
806 | $logEntry = new LogEntry(); |
||
807 | if ($exception instanceof \Exception) { |
||
808 | $logEntry = $logEntry->error($exception); |
||
809 | } else { |
||
810 | $logEntry = $logEntry->info($message); |
||
811 | } |
||
812 | $tableName = $wpdb->prefix.self::LOGS_TABLE; |
||
813 | $wpdb->insert($tableName, array('log' => $logEntry->toJson())); |
||
814 | } |
||
815 | /** |
||
816 | * Check if logs table exists |
||
817 | */ |
||
818 | private function checkDbLogTable() |
||
819 | { |
||
820 | global $wpdb; |
||
821 | $tableName = $wpdb->prefix.self::LOGS_TABLE; |
||
822 | if ($wpdb->get_var("SHOW TABLES LIKE '$tableName'") != $tableName) { |
||
823 | $charset_collate = $wpdb->get_charset_collate(); |
||
824 | $sql = "CREATE TABLE $tableName ( id int NOT NULL AUTO_INCREMENT, log text NOT NULL, |
||
825 | createdAt timestamp DEFAULT CURRENT_TIMESTAMP, UNIQUE KEY id (id)) $charset_collate"; |
||
826 | require_once(ABSPATH.'wp-admin/includes/upgrade.php'); |
||
827 | dbDelta($sql); |
||
828 | } |
||
829 | return; |
||
830 | } |
||
831 | |||
832 | /** |
||
833 | * @param $product_id |
||
834 | * |
||
835 | * @return string |
||
836 | */ |
||
837 | private function isPromoted($product_id) |
||
841 | } |
||
842 | |||
843 | /** |
||
844 | * @return int |
||
845 | */ |
||
846 | private function getPromotedAmount() |
||
847 | { |
||
848 | global $woocommerce; |
||
849 | $items = $woocommerce->cart->get_cart(); |
||
850 | $promotedAmount = 0; |
||
851 | foreach ($items as $key => $item) { |
||
852 | $promotedProduct = $this->isPromoted($item['product_id']); |
||
853 | if ($promotedProduct == 'true') { |
||
854 | $promotedAmount+=$item['line_total']; |
||
855 | } |
||
856 | } |
||
857 | |||
858 | return $promotedAmount; |
||
859 | } |
||
860 | } |
||
861 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths