From 51dd47a89f4df5fb11c9e6f2b346acfa715f995f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D0=B0=D0=B2=D0=B5=D0=BB?= Date: Tue, 26 May 2020 17:34:31 +0300 Subject: [PATCH] corporate client fixes --- src/include/api/class-wc-retailcrm-proxy.php | 80 ++++++++++++++++--- .../api/class-wc-retailcrm-response.php | 12 +++ src/include/class-wc-retailcrm-base.php | 5 ++ src/include/class-wc-retailcrm-customers.php | 4 +- src/include/class-wc-retailcrm-history.php | 12 +-- src/include/class-wc-retailcrm-logger.php | 76 ++++++++++++++++++ src/include/class-wc-retailcrm-orders.php | 19 ++--- src/include/functions.php | 10 +++ src/retailcrm.php | 1 + 9 files changed, 185 insertions(+), 34 deletions(-) create mode 100644 src/include/class-wc-retailcrm-logger.php diff --git a/src/include/api/class-wc-retailcrm-proxy.php b/src/include/api/class-wc-retailcrm-proxy.php index 1618717..edcfcf1 100644 --- a/src/include/api/class-wc-retailcrm-proxy.php +++ b/src/include/api/class-wc-retailcrm-proxy.php @@ -16,11 +16,9 @@ if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) : { protected $retailcrm; protected $corporateEnabled; - protected $logger; public function __construct($api_url, $api_key, $api_vers = null, $corporateEnabled = false) - { - $this->logger = new WC_Logger(); + { $this->corporateEnabled = $corporateEnabled; if ( ! class_exists( 'WC_Retailcrm_Client_V4' ) ) { @@ -54,41 +52,97 @@ if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) : return $this->corporateEnabled; } + private static function reduceErrors($errors) + { + $result = ''; + + foreach ($errors as $key => $error) { + $result .= " [$key] => $error"; + } + + return $result; + } + + /** + * Response will be omitted in debug logs for those methods + * + * @return string[] + */ + private function methodsWithoutDebugResponse() + { + return array_map( + function ($val) { + return get_class($this->retailcrm) . '::' . $val; + }, + array('statusesList', 'paymentTypesList', 'deliveryTypesList', 'orderMethodsList') + ); + } + public function __call($method, $arguments) { + $result = ''; + $response = null; + $called = sprintf('%s::%s', get_class($this->retailcrm), $method); + try { + WC_Retailcrm_Logger::debug( + $called, + empty($arguments) ? '[no params]' : print_r($arguments, true) + ); + /** @var \WC_Retailcrm_Response $response */ $response = call_user_func_array(array($this->retailcrm, $method), $arguments); if (is_string($response)) { + WC_Retailcrm_Logger::debug($called, $response); return $response; } if ($response->isSuccessful()) { + // Don't print long lists in debug logs (errors while calling this will be easy to detect anyway) + // Also don't call useless array_map at all while debug mode is off. + if (retailcrm_is_debug()) { + if (in_array( + $called, + $this->methodsWithoutDebugResponse() + )) { + WC_Retailcrm_Logger::debug($called, '[request was successful, but response is omitted]'); + } else { + WC_Retailcrm_Logger::debug($called, $response->getRawResponse()); + } + } + $result = ' Ok'; } else { $result = sprintf( - $method ." : Error: [HTTP-code %s] %s", + $called ." : Error: [HTTP-code %s] %s", $response->getStatusCode(), - $response->getErrorMsg() + $response->getErrorString() ); if (isset($response['errors'])) { - foreach ($response['errors'] as $key => $error) { - $result .= " [$key] => $error"; - } + $result .= self::reduceErrors($response['errors']); } + + WC_Retailcrm_Logger::debug($called, $response->getErrorString()); + WC_Retailcrm_Logger::debug($called, $response->getRawResponse()); } - $this->logger->add('retailcrm', sprintf("[%s] %s", $method, $result)); + WC_Retailcrm_Logger::add(sprintf("[%s] %s", $called, $result)); } catch (WC_Retailcrm_Exception_Curl $exception) { - $this->logger->add('retailcrm', sprintf("[%s] %s - %s", $method, $exception->getMessage(), $result)); + WC_Retailcrm_Logger::debug(get_class($this->retailcrm).'::'.$called, $exception->getMessage()); + WC_Retailcrm_Logger::debug('', $exception->getTraceAsString()); + WC_Retailcrm_Logger::add(sprintf("[%s] %s - %s", $called, $exception->getMessage(), $result)); } catch (WC_Retailcrm_Exception_Json $exception) { - $this->logger->add('retailcrm', sprintf("[%s] %s - %s", $method, $exception->getMessage(), $result)); + WC_Retailcrm_Logger::debug(get_class($this->retailcrm).'::'.$called, $exception->getMessage()); + WC_Retailcrm_Logger::debug('', $exception->getTraceAsString()); + WC_Retailcrm_Logger::add(sprintf("[%s] %s - %s", $called, $exception->getMessage(), $result)); } catch (InvalidArgumentException $exception) { - $this->logger->add('retailcrm', sprintf("[%s] %s - %s", $method, $exception->getMessage(), $result)); + WC_Retailcrm_Logger::debug(get_class($this->retailcrm).'::'.$called, $exception->getMessage()); + WC_Retailcrm_Logger::debug('', $exception->getTraceAsString()); + WC_Retailcrm_Logger::add(sprintf("[%s] %s - %s", $called, $exception->getMessage(), $result)); } - return $response; + return !empty($response) ?: new WC_Retailcrm_Response(900, '{}'); } } endif; \ No newline at end of file diff --git a/src/include/api/class-wc-retailcrm-response.php b/src/include/api/class-wc-retailcrm-response.php index c63f3a0..b5f4bff 100644 --- a/src/include/api/class-wc-retailcrm-response.php +++ b/src/include/api/class-wc-retailcrm-response.php @@ -24,6 +24,9 @@ class WC_Retailcrm_Response implements \ArrayAccess // response assoc array protected $response; + // response raw data + protected $rawResponse; + /** * ApiResponse constructor. * @@ -35,6 +38,7 @@ class WC_Retailcrm_Response implements \ArrayAccess public function __construct($statusCode, $responseBody = null) { $this->statusCode = (int) $statusCode; + $this->rawResponse = $responseBody; if (!empty($responseBody)) { $response = json_decode($responseBody, true); @@ -192,4 +196,12 @@ class WC_Retailcrm_Response implements \ArrayAccess return ''; } + + /** + * @return mixed|null + */ + public function getRawResponse() + { + return $this->rawResponse; + } } diff --git a/src/include/class-wc-retailcrm-base.php b/src/include/class-wc-retailcrm-base.php index 494e36a..e55bd4f 100644 --- a/src/include/class-wc-retailcrm-base.php +++ b/src/include/class-wc-retailcrm-base.php @@ -96,6 +96,7 @@ if (!class_exists('WC_Retailcrm_Base')) { add_action('admin_print_footer_scripts', array($this, 'ajax_selected_order'), 99); add_action('woocommerce_created_customer', array($this, 'create_customer'), 10, 1); add_action('woocommerce_update_customer', array($this, 'update_customer'), 10, 1); + add_action('profile_update', array($this, 'update_customer'), 10, 2); add_action('wp_print_scripts', array($this, 'initialize_analytics'), 98); add_action('wp_print_scripts', array($this, 'initialize_daemon_collector'), 99); add_action('wp_print_footer_scripts', array($this, 'send_analytics'), 99); @@ -321,6 +322,10 @@ if (!class_exists('WC_Retailcrm_Base')) { return; } + if (empty($customer_id)) { + return; + } + $this->customers->updateCustomer($customer_id); } diff --git a/src/include/class-wc-retailcrm-customers.php b/src/include/class-wc-retailcrm-customers.php index 080c172..5690cf1 100644 --- a/src/include/class-wc-retailcrm-customers.php +++ b/src/include/class-wc-retailcrm-customers.php @@ -449,7 +449,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : * * @return bool|array */ - public function searchCustomer($filter) + private function searchCustomer($filter) { if (isset($filter['externalId'])) { $search = $this->retailcrm->customersGet($filter['externalId']); @@ -508,7 +508,7 @@ if (!class_exists('WC_Retailcrm_Customers')) : $customer = $this->searchCustomer(array('externalId' => $customerExternalId)); } - if (!$customer) { + if (!$customer && !empty($customerEmailOrPhone)) { $customer = $this->searchCustomer(array('email' => $customerEmailOrPhone)); } diff --git a/src/include/class-wc-retailcrm-history.php b/src/include/class-wc-retailcrm-history.php index 3fd203a..b7000e2 100644 --- a/src/include/class-wc-retailcrm-history.php +++ b/src/include/class-wc-retailcrm-history.php @@ -90,8 +90,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $this->customersHistory($this->startDateCustomers->format('Y-m-d H:i:s'), $customers_since_id); $this->ordersHistory($this->startDateOrders->format('Y-m-d H:i:s'), $orders_since_id); } catch (\Exception $exception) { - $logger = new WC_Logger(); - $logger->add('retailcrm', + WC_Retailcrm_Logger::add( sprintf("[%s] - %s", $exception->getMessage(), 'Exception in file - ' . $exception->getFile() . ' on line ' . $exception->getLine()) ); @@ -222,8 +221,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $this->update_total($wc_order); } } catch (Exception $exception) { - $logger = new WC_Logger(); - $logger->add('retailcrm', + WC_Retailcrm_Logger::add( sprintf("[%s] - %s", $exception->getMessage(), 'Exception in file - ' . $exception->getFile() . ' on line ' . $exception->getLine()) ); @@ -574,8 +572,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $companyName = ''; if ($wc_order instanceof WP_Error) { - $logger = new WC_Logger(); - $logger->add('retailcrm', sprintf( + WC_Retailcrm_Logger::add(sprintf( '[%d] error while creating order: %s', $order['id'], print_r($wc_order->get_error_messages(), true) @@ -775,8 +772,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : if (!empty($crmOrder) && isset($crmOrder['items'][$item['id']])) { $woocommerceId = $crmOrder['items'][$item['id']]['woocomerceId']; } else { - $logger = new WC_Logger(); - $logger->add('retailcrm', + WC_Retailcrm_Logger::add( sprintf( "Order externalId=`%s`: item doesn't have woocomerceId, skipping... (item id=`%s`)", $order['externalId'], diff --git a/src/include/class-wc-retailcrm-logger.php b/src/include/class-wc-retailcrm-logger.php new file mode 100644 index 0000000..90816eb --- /dev/null +++ b/src/include/class-wc-retailcrm-logger.php @@ -0,0 +1,76 @@ +add(self::HANDLE, $message, $level); + } + + /** + * Debug logging. Contains a lot of debug data like full requests & responses. + * + * @param string $method + * @param string $message + * @param string $level + */ + public static function debug($method, $message, $level = WC_Log_Levels::DEBUG) + { + if (retailcrm_is_debug()) { + if (!empty($method)) { + $message = sprintf( + '<%s> => %s', + $method, + $message + ); + } + + self::getInstance()->add(self::HANDLE . '_debug', $message, $level); + } + } + } +endif; diff --git a/src/include/class-wc-retailcrm-orders.php b/src/include/class-wc-retailcrm-orders.php index 9d038d7..a99110e 100644 --- a/src/include/class-wc-retailcrm-orders.php +++ b/src/include/class-wc-retailcrm-orders.php @@ -282,16 +282,15 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : } if ($this->retailcrm->getCorporateEnabled() && static::isCorporateOrder($wcOrder)) { - $crmCorporate = array(); - $crmCorporateList = $this->customers->searchCorporateCustomer(array( + $crmCorporate = $this->customers->searchCorporateCustomer(array( 'contactIds' => array($foundCustomerId), 'companyName' => $wcOrder->get_billing_company() - ), true); + )); - if (empty($crmCorporateList)) { - $crmCorporateList = $this->customers->searchCorporateCustomer(array( + if (empty($crmCorporate)) { + $crmCorporate = $this->customers->searchCorporateCustomer(array( 'companyName' => $wcOrder->get_billing_company() - ), true); + )); } if (empty($crmCorporate)) { @@ -575,15 +574,13 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : return; } - $handle = 'retailcrm'; - $logger = new WC_Logger(); - $logger->add($handle, $prefix); + WC_Retailcrm_Logger::add($prefix); foreach ($errors as $orderId => $error) { - $logger->add($handle, sprintf("[%d] => %s", $orderId, $error)); + WC_Retailcrm_Logger::add(sprintf("[%d] => %s", $orderId, $error)); } - $logger->add($handle, '=================================='); + WC_Retailcrm_Logger::add('=================================='); } } endif; diff --git a/src/include/functions.php b/src/include/functions.php index e255df1..c207d44 100644 --- a/src/include/functions.php +++ b/src/include/functions.php @@ -98,3 +98,13 @@ function retailcrm_get_wc_product($id, $settings) { return wc_get_product($id); } + +/** + * Returns true if either wordpress debug mode or module debugging is enabled + * + * @return bool + */ +function retailcrm_is_debug() { + return (defined('WP_DEBUG') && WP_DEBUG == true) + || (defined('RCRM_DEBUG') && RCRM_DEBUG == true); +} diff --git a/src/retailcrm.php b/src/retailcrm.php index 4ba1e3c..8c1392f 100644 --- a/src/retailcrm.php +++ b/src/retailcrm.php @@ -41,6 +41,7 @@ if (!class_exists( 'WC_Integration_Retailcrm')) : $this->load_plugin_textdomain(); if (class_exists( 'WC_Integration' )) { + require_once(dirname(__FILE__ ) . '/include/class-wc-retailcrm-logger.php'); require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-settings.php'); require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-data.php'); require_once(dirname(__FILE__ ) . '/include/abstracts/class-wc-retailcrm-abstracts-address.php');