diff --git a/intaro.intarocrm/classes/general/Exception/ApiException.php b/intaro.intarocrm/classes/general/Exception/ApiException.php new file mode 100644 index 00000000..75e4d8d0 --- /dev/null +++ b/intaro.intarocrm/classes/general/Exception/ApiException.php @@ -0,0 +1,6 @@ += $pSize) { - $customers = $api->customerUpload($resCustomers); - // error pushing customers - if ($api->getStatusCode() != 201) { - //handle err - //self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError()); + try { + $customers = $api->customerUpload($resCustomers); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', + $e->getCode() . ': ' . $e->getMessage() + ); - if ($api->getStatusCode() != 460) // some orders were sent - return false; // in pack mode return errors + if($e->getCode() != 201) + if($e->getCode() != 460) + return false; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + + return false; } - $orders = $api->orderUpload($resOrders); + try { + $orders = $api->orderUpload($resOrders); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', + $e->getCode() . ': ' . $e->getMessage() + ); - // error pushing orders - if ($api->getStatusCode() != 201) { - //handle err - self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); + if($e->getCode() != 201) + if($e->getCode() != 460) + return false; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); - if ($api->getStatusCode() != 460) // some orders were sent - return false; // in pack mode return errors + return false; } if ($lastOrderId) @@ -137,26 +156,44 @@ class ICrmOrderActions } } if (!empty($resOrders)) { - $customers = $api->customerUpload($resCustomers); + try { + $customers = $api->customerUpload($resCustomers); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', + $e->getCode() . ': ' . $e->getMessage() + ); - // error pushing customers - if ($api->getStatusCode() != 201) { - //handle err - //self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError()); + if($e->getCode() != 201) + if($e->getCode() != 460) + return false; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); - if ($api->getStatusCode() != 460) // some orders were sent - return false; // in pack mode return errors + return false; } - $orders = $api->orderUpload($resOrders); + try { + $orders = $api->orderUpload($resOrders); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', + $e->getCode() . ': ' . $e->getMessage() + ); - // error pushing orders - if ($api->getStatusCode() != 201) { - //handle err - self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); + if($e->getCode() != 201) + if($e->getCode() != 460) + return false; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); - if ($api->getStatusCode() != 460) // some orders were sent - return false; // in pack mode return errors + return false; } } @@ -188,26 +225,44 @@ class ICrmOrderActions $recOrders[] = $arOrder['ID']; if ($orderCount >= $pSize) { - $customers = $api->customerUpload($resCustomers); + try { + $customers = $api->customerUpload($resCustomers); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', + $e->getCode() . ': ' . $e->getMessage() + ); - // error pushing customers - if ($api->getStatusCode() != 201) { - //handle err - //self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError()); + if($e->getCode() != 201) + if($e->getCode() != 460) + return false; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); - if ($api->getStatusCode() != 460) // some orders were sent - return false; // in pack mode return errors + return false; } - $orders = $api->orderUpload($resOrders); + try { + $orders = $api->orderUpload($resOrders); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', + $e->getCode() . ': ' . $e->getMessage() + ); - // error pushing orders - if ($api->getStatusCode() != 201) { - //handle err - self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); + if($e->getCode() != 201) + if($e->getCode() != 460) + return false; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); - if ($api->getStatusCode() != 460) // some orders were sent - return false; // in pack mode return errors + return false; } if (!empty($recOrders)) { @@ -219,26 +274,44 @@ class ICrmOrderActions } } if (!empty($resOrders)) { - $customers = $api->customerUpload($resCustomers); + try { + $customers = $api->customerUpload($resCustomers); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', + $e->getCode() . ': ' . $e->getMessage() + ); - // error pushing customers - if ($api->getStatusCode() != 201) { - //handle err - //self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload', $api->getLastError()); + if($e->getCode() != 201) + if($e->getCode() != 460) + return false; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::customerUpload::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); - if ($api->getStatusCode() != 460) // some orders were sent - return false; // in pack mode return errors + return false; } - $orders = $api->orderUpload($resOrders); + try { + $orders = $api->orderUpload($resOrders); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', + $e->getCode() . ': ' . $e->getMessage() + ); - // error pushing orders - if ($api->getStatusCode() != 201) { - //handle err - self::eventLog('ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload', $api->getLastError()); + if($e->getCode() != 201) + if($e->getCode() != 460) + return false; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::uploadOrders', 'IntaroCrm\RestApi::orderUpload::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); - if ($api->getStatusCode() != 460) // some orders were sent - return false; // in pack mode return errors + return false; } } @@ -368,9 +441,25 @@ class ICrmOrderActions $dateStart = $dateStart->format('Y-m-d H:i:s'); } - $dateFinish = new \DateTime(); + try { + $orderHistory = $api->orderHistory($dateStart); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderHistory', + $e->getCode() . ': ' . $e->getMessage() + ); - $orderHistory = $api->orderHistory($dateStart); + return true; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderHistory::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + + return true; + } + + $dateFinish = $api->getGeneratedAt(); $GLOBALS['INTARO_CRM_FROM_HISTORY'] = true; @@ -447,11 +536,21 @@ class ICrmOrderActions $order['customer']['externalId'] = $registeredUserID; } - $api->customerFixExternalIds(array(array('id' => $order['customer']['id'], 'externalId' => $order['customer']['externalId']))); + try { + $api->customerFixExternalIds(array(array('id' => $order['customer']['id'], 'externalId' => $order['customer']['externalId']))); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::customerFixExternalIds', + $e->getCode() . ': ' . $e->getMessage() + ); + + continue; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::customerFixExternalIds::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); - if ($api->getStatusCode() != 200) { - //handle err - write log & continue - self::eventLog('ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::customerFixExternalIds', $api->getLastError()); continue; } @@ -480,11 +579,21 @@ class ICrmOrderActions if(isset($GLOBALS['ICRM_ACCOUNT_NUMBER'])) unset($GLOBALS['ICRM_ACCOUNT_NUMBER']); - $api->orderFixExternalIds(array(array('id' => $order['id'], 'externalId' => $order['externalId']))); + try { + $api->orderFixExternalIds(array(array('id' => $order['id'], 'externalId' => $order['externalId']))); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderFixExternalIds', + $e->getCode() . ': ' . $e->getMessage() + ); + + continue; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderFixExternalIds::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); - if ($api->getStatusCode() != 200) { - //handle err - write log & continue - self::eventLog('ICrmOrderActions::orderHistory', 'IntaroCrm\RestApi::orderFixExternalIds', $api->getLastError()); continue; } } @@ -985,14 +1094,24 @@ class ICrmOrderActions 'createdAt' => $createdAt )); - if($send) - $customer = $api->customerEdit($customer); + if($send) { + try { + $customer = $api->customerEdit($customer); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::orderCreate', 'IntaroCrm\RestApi::customerEdit', + $e->getCode() . ': ' . $e->getMessage() + ); - // error pushing customer - if (!$customer) { - //handle err - self::eventLog('ICrmOrderActions::orderCreate', 'IntaroCrm\RestApi::customerEdit', $api->getLastError()); - return false; + return false; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::orderCreate', 'IntaroCrm\RestApi::customerEdit::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + + return false; + } } // delivery types @@ -1166,8 +1285,25 @@ class ICrmOrderActions $resOrder = self::clearArr($resOrder); - if($send) - return $api->orderEdit($resOrder); + if($send) { + try { + return $api->orderEdit($resOrder); + } catch (\IntaroCrm\Exception\ApiException $e) { + self::eventLog( + 'ICrmOrderActions::orderCreate', 'IntaroCrm\RestApi::orderEdit', + $e->getCode() . ': ' . $e->getMessage() + ); + + return false; + } catch (\IntaroCrm\Exception\CurlException $e) { + self::eventLog( + 'ICrmOrderActions::orderCreate', 'IntaroCrm\RestApi::orderEdit::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + + return false; + } + } return array( 'order' => $resOrder, diff --git a/intaro.intarocrm/classes/general/RestApi.php b/intaro.intarocrm/classes/general/RestApi.php index 3200b2db..c7f8ce81 100644 --- a/intaro.intarocrm/classes/general/RestApi.php +++ b/intaro.intarocrm/classes/general/RestApi.php @@ -5,10 +5,9 @@ class RestApi { protected $apiUrl; protected $apiKey; - protected $apiVersion = '1'; + protected $apiVersion = '2'; + protected $generatedAt; - protected $response; - protected $statusCode; protected $parameters; /** @@ -22,35 +21,6 @@ class RestApi $this->parameters = array('apiKey' => $this->apiKey); } - - public function getStatusCode() - { - return $this->statusCode; - } - - /* Получение кода статуса и сообщения об ошибке */ - public function getLastError() - { - if (isset($this->response['errorMsg']) && isset($this->response['errors'])) - { - $result = $this->statusCode . ' ' . $this->response['errorMsg']; - foreach ($this->response['errors'] as $error) - $result .= ' ' . $error; - } - elseif (isset($this->response['errorMsg'])) - $result = $this->statusCode . ' ' . $this->response['errorMsg']; - else - $result = null; - return $result; - } - - /* Псообщения об ошибке */ - public function getLastErrorMessage() - { - return $this->response['errorMsg']; - } - - /* Методы для работы с заказами */ /** * Получение заказа по id @@ -122,12 +92,12 @@ class RestApi /** * Обновление externalId у заказов с переданными id * - * @param array $orders - массив, содержащий id и externalId заказа + * @param array $orders- массив, содержащий id и externalId заказа * @return array */ - public function orderFixExternalIds($orders) + public function orderFixExternalIds($order) { - $dataJson = json_encode($orders); + $dataJson = json_encode($order); $this->parameters['orders'] = $dataJson; $url = $this->apiUrl.'orders/fix-external-ids'; @@ -241,22 +211,6 @@ class RestApi return $result; } - /** - * Обновление externalId у клиентов с переданными id - * - * @param array $customers- массив, содержащий id и externalId заказа - * @return array - */ - public function customerFixExternalIds($customers) - { - $dataJson = json_encode($customers); - $this->parameters['customers'] = $dataJson; - - $url = $this->apiUrl.'customers/fix-external-ids'; - $result = $this->curlRequest($url, 'POST'); - return $result; - } - /** * Удаление клиента * @@ -329,34 +283,6 @@ class RestApi return $result; } - /** - * Получение списка служб доставки - * - * @return array - массив типов доставки - */ - public function deliveryServicesList() - { - $url = $this->apiUrl.'reference/delivery-services'; - $result = $this->curlRequest($url); - return $result; - } - - /** - * Редактирование службы доставки - * - * @param array $deliveryService - информация о типе доставки - * @return array - */ - public function deliveryServiceEdit($deliveryService) - { - $dataJson = json_encode($deliveryService); - $this->parameters['deliveryService'] = $dataJson; - - $url = $this->apiUrl.'reference/delivery-services/'.$deliveryService['code'].'/edit'; - $result = $this->curlRequest($url, 'POST'); - return $result; - } - /** * Получение списка типов оплаты @@ -444,6 +370,35 @@ class RestApi return $result; } + + /** + * Получение списка способов оформления заказа + * + * @return array - массив способов оформления заказа + */ + public function orderMethodsList() + { + $url = $this->apiUrl.'reference/order-methods'; + $result = $this->curlRequest($url); + return $result; + } + + /** + * Редактирование способа оформления заказа + * + * @param array $orderMethod - информация о способе оформления заказа + * @return array + */ + public function orderMethodsEdit($orderMethod) + { + $dataJson = json_encode($orderMethod); + $this->parameters['orderMethod'] = $dataJson; + + $url = $this->apiUrl.'reference/order-methods/'.$orderMethod['code'].'/edit'; + $result = $this->curlRequest($url, 'POST'); + return $result; + } + /** * Получение списка статусов заказа * @@ -484,7 +439,7 @@ class RestApi $result = $this->curlRequest($url); return $result; } - + /** * Обновление статистики * @@ -497,6 +452,36 @@ class RestApi return $result; } + /** + * @return \DateTime + */ + public function getGeneratedAt() { + return $this->generatedAt; + } + + protected function getErrorMessage($response) + { + $str = ''; + if (isset($response['message'])) + $str = $response['message']; + elseif (isset($response[0]['message'])) + $str = $response[0]['message']; + elseif (isset($response['error']) && isset($response['error']['message'])) + $str = $response['error']['message']; + elseif (isset($response['errorMsg'])) + $str = $response['errorMsg']; + + if (isset($response['errors']) && sizeof($response['errors'])) { + foreach ($response['errors'] as $error) + $str .= '. ' . $error; + } + + if (!strlen($str)) + return 'Application Error'; + + return $str; + } + protected function curlRequest($url, $method = 'GET', $format = 'json') { if ($method == 'GET' && !is_null($this->parameters)) @@ -516,28 +501,34 @@ class RestApi } $response = curl_exec($ch); - $this->statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); unset($this->parameters); /* Сброс массива с параметрами */ $this->parameters = array('apiKey' => $this->apiKey); - if (curl_errno($ch)) - { - $this->response = array('errorMsg' => 'Curl error: ' . curl_error($ch)); - return null; - } + $errno = curl_errno($ch); + $error = curl_error($ch); curl_close($ch); - $result = (array)json_decode($response, true); - $this->response = $result; - if ($result['success'] == false) - return null; + if ($errno) + throw new Exception\CurlException($error, $errno); + + $result = json_decode($response, true); + + if ($statusCode >= 400 || isset($result['success']) && $result['success'] === false) { + throw new Exception\ApiException($this->getErrorMessage($result), $statusCode); + } + + if (isset($result['generatedAt'])) { + $this->generatedAt = new \DateTime($result['generatedAt']); + unset($result['generatedAt']); + } unset($result['success']); + if (count($result) == 0) return true; + return reset($result); } -} - -?> \ No newline at end of file +} \ No newline at end of file diff --git a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php index df3eed8f..c12dcb53 100644 --- a/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php +++ b/intaro.intarocrm/classes/general/events/ICrmOrderEvent.php @@ -212,12 +212,20 @@ class ICrmOrderEvent { 'managerComment' => $arOrder['COMMENTS'] ); } - - $api->orderEdit($order); - - // error pushing order - if ($api->getStatusCode() != 201) - ICrmOrderActions::eventLog('ICrmOrderEvent::onSaleCancelOrder', 'IntaroCrm\RestApi::orderEdit', $api->getLastError()); + + try { + $api->orderEdit($order); + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'ICrmOrderEvent::onSaleCancelOrder', 'IntaroCrm\RestApi::orderEdit', + $e->getCode() . ': ' . $e->getMessage() + ); + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'ICrmOrderEvent::onSaleCancelOrder', 'IntaroCrm\RestApi::orderEdit::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + } return true; } @@ -268,9 +276,19 @@ class ICrmOrderEvent { $api->orderEdit($order); - // error pushing order - if ($api->getStatusCode() != 201) - ICrmOrderActions::eventLog('ICrmOrderEvent::onSalePayOrder', 'IntaroCrm\RestApi::orderEdit', $api->getLastError()); + try { + $api->orderEdit($order); + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'ICrmOrderEvent::onSalePayOrder', 'IntaroCrm\RestApi::orderEdit', + $e->getCode() . ': ' . $e->getMessage() + ); + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'ICrmOrderEvent::onSalePayOrder', 'IntaroCrm\RestApi::orderEdit::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + } return true; } diff --git a/intaro.intarocrm/include.php b/intaro.intarocrm/include.php index 933ef5fa..d836cd19 100755 --- a/intaro.intarocrm/include.php +++ b/intaro.intarocrm/include.php @@ -2,9 +2,11 @@ CModule::AddAutoloadClasses( 'intaro.intarocrm', // module name array ( - 'IntaroCrm\RestApi' => 'classes/general/RestApi.php', - 'ICrmOrderActions' => 'classes/general/ICrmOrderActions.php', - 'ICMLLoader' => 'classes/general/ICMLLoader.php', - 'ICrmOrderEvent' => 'classes/general/events/ICrmOrderEvent.php' + 'IntaroCrm\RestApi' => 'classes/general/RestApi.php', + 'ICrmOrderActions' => 'classes/general/ICrmOrderActions.php', + 'ICMLLoader' => 'classes/general/ICMLLoader.php', + 'ICrmOrderEvent' => 'classes/general/events/ICrmOrderEvent.php', + 'IntaroCrm\Exception\ApiException' => 'classes/general/Exception/ApiException.php', + 'IntaroCrm\Exception\CurlException' => 'classes/general/Exception/CurlException.php' ) ); \ No newline at end of file diff --git a/intaro.intarocrm/install/index.php b/intaro.intarocrm/install/index.php index 9c7388b8..e4b0ec2a 100755 --- a/intaro.intarocrm/install/index.php +++ b/intaro.intarocrm/install/index.php @@ -196,20 +196,48 @@ class intaro_intarocrm extends CModule { $this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key); //prepare crm lists - $arResult['orderTypesList'] = $this->INTARO_CRM_API->orderTypesList(); + try { + $arResult['orderTypesList'] = $this->INTARO_CRM_API->orderTypesList(); + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::orderTypesList', + $e->getCode() . ': ' . $e->getMessage() + ); + + $APPLICATION->RestartBuffer(); + header('Content-Type: application/x-javascript; charset=' . LANG_CHARSET); + die(json_encode(array("success" => false))); + + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::orderTypesList::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); - if ((int) $this->INTARO_CRM_API->getStatusCode() != 200) { $APPLICATION->RestartBuffer(); header('Content-Type: application/x-javascript; charset=' . LANG_CHARSET); die(json_encode(array("success" => false))); } - $arResult['deliveryTypesList'] = $this->INTARO_CRM_API->deliveryTypesList(); - $arResult['deliveryServicesList'] = $this->INTARO_CRM_API->deliveryServicesList(); - $arResult['paymentTypesList'] = $this->INTARO_CRM_API->paymentTypesList(); - $arResult['paymentStatusesList'] = $this->INTARO_CRM_API->paymentStatusesList(); // --statuses - $arResult['paymentList'] = $this->INTARO_CRM_API->orderStatusesList(); - $arResult['paymentGroupList'] = $this->INTARO_CRM_API->orderStatusGroupsList(); // -- statuses groups + try { + $arResult['deliveryTypesList'] = $this->INTARO_CRM_API->deliveryTypesList(); + $arResult['deliveryServicesList'] = $this->INTARO_CRM_API->deliveryServicesList(); + $arResult['paymentTypesList'] = $this->INTARO_CRM_API->paymentTypesList(); + $arResult['paymentStatusesList'] = $this->INTARO_CRM_API->paymentStatusesList(); // --statuses + $arResult['paymentList'] = $this->INTARO_CRM_API->orderStatusesList(); + $arResult['paymentGroupList'] = $this->INTARO_CRM_API->orderStatusGroupsList(); // -- statuses groups + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::*List', + $e->getCode() . ': ' . $e->getMessage() + ); + + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::*List::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + } //bitrix orderTypesList -- personTypes $dbOrderTypesList = CSalePersonType::GetList( array( @@ -480,15 +508,33 @@ class intaro_intarocrm extends CModule { $this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key); - $this->INTARO_CRM_API->paymentStatusesList(); + try { + $this->INTARO_CRM_API->paymentStatusesList(); + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::paymentStatusesList', + $e->getCode() . ': ' . $e->getMessage() + ); - //check connection & apiKey valid - if ((int) $this->INTARO_CRM_API->getStatusCode() != 200) { - $arResult['errCode'] = 'ERR_' . $this->INTARO_CRM_API->getStatusCode(); + $arResult['errCode'] = 'ERR_' . $e->getCode(); $APPLICATION->IncludeAdminFile( - GetMessage('MODULE_INSTALL_TITLE'), - $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php' + GetMessage('MODULE_INSTALL_TITLE'), + $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php' + ); + + return; + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::paymentStatusesList::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + + $arResult['errCode'] = 'ERR_' . $e->getCode(); + + $APPLICATION->IncludeAdminFile( + GetMessage('MODULE_INSTALL_TITLE'), + $_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/' . $this->MODULE_ID . '/install/step1.php' ); return; @@ -499,14 +545,26 @@ class intaro_intarocrm extends CModule { COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_SITES, serialize($orderSites)); //prepare crm lists - $arResult['orderTypesList'] = $this->INTARO_CRM_API->orderTypesList(); - $arResult['deliveryTypesList'] = $this->INTARO_CRM_API->deliveryTypesList(); - $arResult['deliveryServicesList'] = $this->INTARO_CRM_API->deliveryServicesList(); - $arResult['paymentTypesList'] = $this->INTARO_CRM_API->paymentTypesList(); - $arResult['paymentStatusesList'] = $this->INTARO_CRM_API->paymentStatusesList(); // --statuses - $arResult['paymentList'] = $this->INTARO_CRM_API->orderStatusesList(); - $arResult['paymentGroupList'] = $this->INTARO_CRM_API->orderStatusGroupsList(); // -- statuses groups + try { + $arResult['orderTypesList'] = $this->INTARO_CRM_API->orderTypesList(); + $arResult['deliveryTypesList'] = $this->INTARO_CRM_API->deliveryTypesList(); + $arResult['deliveryServicesList'] = $this->INTARO_CRM_API->deliveryServicesList(); + $arResult['paymentTypesList'] = $this->INTARO_CRM_API->paymentTypesList(); + $arResult['paymentStatusesList'] = $this->INTARO_CRM_API->paymentStatusesList(); // --statuses + $arResult['paymentList'] = $this->INTARO_CRM_API->orderStatusesList(); + $arResult['paymentGroupList'] = $this->INTARO_CRM_API->orderStatusGroupsList(); // -- statuses groups + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::*List', + $e->getCode() . ': ' . $e->getMessage() + ); + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::*List::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + } //bitrix orderTypesList -- personTypes $dbOrderTypesList = CSalePersonType::GetList( array( @@ -691,21 +749,27 @@ class intaro_intarocrm extends CModule { $deliveryTypesArr[$arDeliveryTypesList['ID']] = $resultDeliveryTypeId; // send to crm - $this->INTARO_CRM_API->deliveryTypeEdit(ICrmOrderActions::clearArr(array( - 'code' => $resultDeliveryTypeId, - 'name' => ICrmOrderActions::toJSON($arDeliveryTypesList['NAME']), - 'defaultCost' => $arDeliveryTypesList['PRICE'], - 'description' => ICrmOrderActions::toJSON($arDeliveryTypesList['DESCRIPTION']), - 'paymentTypes' => '' - ))); + try { + $this->INTARO_CRM_API->deliveryTypeEdit(ICrmOrderActions::clearArr(array( + 'code' => $resultDeliveryTypeId, + 'name' => ICrmOrderActions::toJSON($arDeliveryTypesList['NAME']), + 'defaultCost' => $arDeliveryTypesList['PRICE'], + 'description' => ICrmOrderActions::toJSON($arDeliveryTypesList['DESCRIPTION']), + 'paymentTypes' => '' + ))); + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::deliveryTypeEdit', + $e->getCode() . ': ' . $e->getMessage() + ); - // error pushing dt - if ($this->INTARO_CRM_API->getStatusCode() != 200) { - if ($this->INTARO_CRM_API->getStatusCode() != 201) { - //handle err - ICrmOrderActions::eventLog('install/index.php', 'IntaroCrm\RestApi::deliveryTypeEdit', $this->INTARO_CRM_API->getLastError()); - } + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::deliveryTypeEdit::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); } + } while ($arDeliveryTypesList = $dbDeliveryTypesList->Fetch()); } @@ -715,37 +779,47 @@ class intaro_intarocrm extends CModule { $deliveryTypesArr[$arDeliveryServicesList['SID']] = $arDeliveryServicesList['SID']; // send to crm - $this->INTARO_CRM_API->deliveryTypeEdit(ICrmOrderActions::clearArr(array( - 'code' => $arDeliveryServicesList['SID'], - 'name' => ICrmOrderActions::toJSON($arDeliveryServicesList['NAME']), - 'defaultCost' => 0, - 'description' => ICrmOrderActions::toJSON($arDeliveryTypesList['DESCRIPTION']), - 'paymentTypes' => '' - ))); + try { + $this->INTARO_CRM_API->deliveryTypeEdit(ICrmOrderActions::clearArr(array( + 'code' => $arDeliveryServicesList['SID'], + 'name' => ICrmOrderActions::toJSON($arDeliveryServicesList['NAME']), + 'defaultCost' => 0, + 'description' => ICrmOrderActions::toJSON($arDeliveryTypesList['DESCRIPTION']), + 'paymentTypes' => '' + ))); + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::deliveryTypeEdit', + $e->getCode() . ': ' . $e->getMessage() + ); - // error pushing dt - if ($this->INTARO_CRM_API->getStatusCode() != 200) { - if ($this->INTARO_CRM_API->getStatusCode() != 201) { - //handle err - ICrmOrderActions::eventLog('install/index.php', 'IntaroCrm\RestApi::deliveryTypeEdit', $this->INTARO_CRM_API->getLastError()); - } + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::deliveryTypeEdit::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); } foreach($arDeliveryServicesList['PROFILES'] as $id => $profile) { // send to crm - $this->INTARO_CRM_API->deliveryServiceEdit(ICrmOrderActions::clearArr(array( - 'code' => $arDeliveryServicesList['SID'] . '-' . $id, - 'name' => ICrmOrderActions::toJSON($profile['TITLE']), - 'deliveryType' => $arDeliveryServicesList['SID'] - ))); + try { + $this->INTARO_CRM_API->deliveryServiceEdit(ICrmOrderActions::clearArr(array( + 'code' => $arDeliveryServicesList['SID'] . '-' . $id, + 'name' => ICrmOrderActions::toJSON($profile['TITLE']), + 'deliveryType' => $arDeliveryServicesList['SID'] + ))); + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::deliveryServiceEdit', + $e->getCode() . ': ' . $e->getMessage() + ); - // error pushing dt - if ($this->INTARO_CRM_API->getStatusCode() != 200) { - if ($this->INTARO_CRM_API->getStatusCode() != 201) { - //handle err - ICrmOrderActions::eventLog('install/index.php', 'IntaroCrm\RestApi::deliveryServiceEdit', $this->INTARO_CRM_API->getLastError()); - } + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::deliveryServiceEdit::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); } } @@ -1159,8 +1233,20 @@ class intaro_intarocrm extends CModule { $api_host = COption::GetOptionString($this->MODULE_ID, $this->CRM_API_HOST_OPTION, 0); $api_key = COption::GetOptionString($this->MODULE_ID, $this->CRM_API_KEY_OPTION, 0); $this->INTARO_CRM_API = new \IntaroCrm\RestApi($api_host, $api_key); - $this->INTARO_CRM_API->statisticUpdate(); + try { + $this->INTARO_CRM_API->statisticUpdate(); + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::statisticUpdate', + $e->getCode() . ': ' . $e->getMessage() + ); + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/install/index.php', 'IntaroCrm\RestApi::statisticUpdate::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + } // in fin order COption::SetOptionString($this->MODULE_ID, $this->CRM_ORDER_HISTORY_DATE, date('Y-m-d H:i:s')); diff --git a/intaro.intarocrm/install/version.php b/intaro.intarocrm/install/version.php index aa8ea92d..33bfa071 100644 --- a/intaro.intarocrm/install/version.php +++ b/intaro.intarocrm/install/version.php @@ -1,5 +1,5 @@ "1.0.2", - "VERSION_DATE" => "2014-03-14 20:56:00", + "VERSION" => "1.0.3", + "VERSION_DATE" => "2014-03-22 14:51:00", ); \ No newline at end of file diff --git a/intaro.intarocrm/options.php b/intaro.intarocrm/options.php index 74e6e980..3262d127 100755 --- a/intaro.intarocrm/options.php +++ b/intaro.intarocrm/options.php @@ -101,13 +101,27 @@ if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && (strtolower($_SERVER['HTTP_X_RE $api = new IntaroCrm\RestApi($api_host, $api_key); - $api->paymentStatusesList(); + try { + $api->paymentStatusesList(); + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/options.php', 'IntaroCrm\RestApi::paymentStatusesList', + $e->getCode() . ': ' . $e->getMessage() + ); - //check connection & apiKey valid - if ((int) $api->getStatusCode() != 200) { $APPLICATION->RestartBuffer(); header('Content-Type: application/x-javascript; charset=' . LANG_CHARSET); - die(json_encode(array('success' => false, 'errMsg' => $api->getStatusCode()))); + die(json_encode(array('success' => false, 'errMsg' => $e->getCode()))); + + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/options.php', 'IntaroCrm\RestApi::paymentStatusesList::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + + $APPLICATION->RestartBuffer(); + header('Content-Type: application/x-javascript; charset=' . LANG_CHARSET); + die(json_encode(array('success' => false, 'errMsg' => $e->getCode()))); } $optionsDelivTypes = unserialize(COption::GetOptionString($mid, $CRM_DELIVERY_TYPES_ARR, 0)); @@ -134,18 +148,23 @@ if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && (strtolower($_SERVER['HTTP_X_RE foreach($arDeliveryServicesList['PROFILES'] as $id => $profile) { // send to crm - $api->deliveryServiceEdit(ICrmOrderActions::clearArr(array( - 'code' => $arDeliveryServicesList['SID'] . '-' . $id, - 'name' => ICrmOrderActions::toJSON($profile['TITLE']), - 'deliveryType' => $arDeliveryServicesList['SID'] - ))); + try { + $api->deliveryServiceEdit(ICrmOrderActions::clearArr(array( + 'code' => $arDeliveryServicesList['SID'] . '-' . $id, + 'name' => ICrmOrderActions::toJSON($profile['TITLE']), + 'deliveryType' => $arDeliveryServicesList['SID'] + ))); + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/options.php', 'IntaroCrm\RestApi::deliveryServiceEdit', + $e->getCode() . ': ' . $e->getMessage() + ); - // error pushing dt - if ($api->getStatusCode() != 200) { - if ($api->getStatusCode() != 201) { - //handle err - ICrmOrderActions::eventLog('options.php', 'IntaroCrm\RestApi::deliveryServiceEdit', $api->getLastError()); - } + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/options.php', 'IntaroCrm\RestApi::deliveryServiceEdit::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); } } @@ -170,17 +189,29 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) { if($api_host && $api_key) { $api = new IntaroCrm\RestApi($api_host, $api_key); - - $api->paymentStatusesList(); - - //check connection & apiKey valid - if((int) $api->getStatusCode() != 200) { - $uri .= '&errc=ERR_' . $api->getStatusCode(); + try { + $api->paymentStatusesList(); + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/options.php', 'IntaroCrm\RestApi::paymentStatusesList', + $e->getCode() . ': ' . $e->getMessage() + ); + + $uri .= '&errc=ERR_' . $e->getCode(); LocalRedirect($uri); - } else { - COption::SetOptionString($mid, 'api_host', $api_host); - COption::SetOptionString($mid, 'api_key', $api_key); - } + + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/options.php', 'IntaroCrm\RestApi::paymentStatusesList::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + + $uri .= '&errc=ERR_' . $e->getCode(); + LocalRedirect($uri); + } + + COption::SetOptionString($mid, 'api_host', $api_host); + COption::SetOptionString($mid, 'api_key', $api_key); } //bitrix orderTypesList -- personTypes @@ -349,17 +380,30 @@ if (isset($_POST['Update']) && ($_POST['Update'] == 'Y')) { $arResult['arSites'][] = $ar; //prepare crm lists - $arResult['orderTypesList'] = $api->orderTypesList(); - $arResult['deliveryTypesList'] = $api->deliveryTypesList(); - $arResult['deliveryServicesList'] = $api->deliveryServicesList(); - $arResult['paymentTypesList'] = $api->paymentTypesList(); - $arResult['paymentStatusesList'] = $api->paymentStatusesList(); // --statuses - $arResult['paymentList'] = $api->orderStatusesList(); - $arResult['paymentGroupList'] = $api->orderStatusGroupsList(); // -- statuses groups - - //check connection & apiKey valid - if ((int) $api->getStatusCode() != 200) - echo CAdminMessage::ShowMessage(GetMessage('ERR_' . $api->getStatusCode())); + try { + $arResult['orderTypesList'] = $api->orderTypesList(); + $arResult['deliveryTypesList'] = $api->deliveryTypesList(); + $arResult['deliveryServicesList'] = $api->deliveryServicesList(); + $arResult['paymentTypesList'] = $api->paymentTypesList(); + $arResult['paymentStatusesList'] = $api->paymentStatusesList(); // --statuses + $arResult['paymentList'] = $api->orderStatusesList(); + $arResult['paymentGroupList'] = $api->orderStatusGroupsList(); // -- statuses groups + } catch (\IntaroCrm\Exception\ApiException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/options.php', 'IntaroCrm\RestApi::*List', + $e->getCode() . ': ' . $e->getMessage() + ); + + echo CAdminMessage::ShowMessage(GetMessage('ERR_' . $e->getCode())); + + } catch (\IntaroCrm\Exception\CurlException $e) { + ICrmOrderActions::eventLog( + 'intaro.crm/options.php', 'IntaroCrm\RestApi::*List::CurlException', + $e->getCode() . ': ' . $e->getMessage() + ); + + echo CAdminMessage::ShowMessage(GetMessage('ERR_' . $e->getCode())); + } //bitrix orderTypesList -- personTypes $dbOrderTypesList = CSalePersonType::GetList(