diff --git a/woo-retailcrm/include/api/class-wc-retailcrm-client-v3.php b/woo-retailcrm/include/api/class-wc-retailcrm-client-v3.php deleted file mode 100644 index 5d1527a..0000000 --- a/woo-retailcrm/include/api/class-wc-retailcrm-client-v3.php +++ /dev/null @@ -1,836 +0,0 @@ - - * @license https://opensource.org/licenses/MIT MIT License - * @link http://retailcrm.ru/docs/Developers/ApiVersion3 - */ - - if ( ! class_exists( 'WC_Retailcrm_Request' ) ) { - include_once( __DIR__ . '/class-wc-retailcrm-request.php' ); - } - - if ( ! class_exists( 'WC_Retailcrm_Response' ) ) { - include_once( __DIR__ . '/class-wc-retailcrm-response.php' ); - } - - class WC_Retailcrm_Client_V3 - { - protected $client; - - /** - * Site code - */ - protected $siteCode; - - /** - * Client creating - * - * @param string $url - * @param string $apiKey - * @param string $site - */ - public function __construct($url, $apiKey, $version = null, $site = null) - { - if ('/' != substr($url, strlen($url) - 1, 1)) { - $url .= '/'; - } - - $url = $version == null ? $url . 'api' : $url . 'api/' . $version; - - $this->client = new WC_Retailcrm_Request($url, array('apiKey' => $apiKey)); - $this->siteCode = $site; - } - - /** - * Returns api versions list - * - * @return WC_Retailcrm_Response - */ - public function apiVersions() - { - return $this->client->makeRequest('/api-versions', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Create a order - * - * @param array $order - * @param string $site (default: null) - * @return WC_Retailcrm_Response - */ - public function ordersCreate(array $order, $site = null) - { - if (!sizeof($order)) { - throw new InvalidArgumentException('Parameter `order` must contains a data'); - } - - return $this->client->makeRequest("/orders/create", WC_Retailcrm_Request::METHOD_POST, $this->fillSite($site, array( - 'order' => json_encode($order) - ))); - } - - /** - * Edit a order - * - * @param array $order - * @param string $by - * @param string $site (default: null) - * @return WC_Retailcrm_Response - */ - public function ordersEdit(array $order, $by = 'externalId', $site = null) - { - if (!sizeof($order)) { - throw new InvalidArgumentException('Parameter `order` must contains a data'); - } - - $this->checkIdParameter($by); - - if (!isset($order[$by])) { - throw new InvalidArgumentException(sprintf('Order array must contain the "%s" parameter.', $by)); - } - - return $this->client->makeRequest( - "/orders/" . $order[$by] . "/edit", - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array( - 'order' => json_encode($order), - 'by' => $by, - )) - ); - } - - /** - * Upload array of the orders - * - * @param array $orders - * @param string $site (default: null) - * @return WC_Retailcrm_Response - */ - public function ordersUpload(array $orders, $site = null) - { - if (!sizeof($orders)) { - throw new InvalidArgumentException('Parameter `orders` must contains array of the orders'); - } - - return $this->client->makeRequest("/orders/upload", WC_Retailcrm_Request::METHOD_POST, $this->fillSite($site, array( - 'orders' => json_encode($orders), - ))); - } - - /** - * Get order by id or externalId - * - * @param string $id - * @param string $by (default: 'externalId') - * @param string $site (default: null) - * @return WC_Retailcrm_Response - */ - public function ordersGet($id, $by = 'externalId', $site = null) - { - $this->checkIdParameter($by); - - return $this->client->makeRequest("/orders/$id", WC_Retailcrm_Request::METHOD_GET, $this->fillSite($site, array( - 'by' => $by - ))); - } - - /** - * Returns a orders history - * - * @param DateTime $startDate (default: null) - * @param DateTime $endDate (default: null) - * @param int $limit (default: 100) - * @param int $offset (default: 0) - * @param bool $skipMyChanges (default: true) - * @return WC_Retailcrm_Response - */ - public function ordersHistory( - DateTime $startDate = null, - DateTime $endDate = null, - $limit = 100, - $offset = 0, - $skipMyChanges = true - ) { - $parameters = array(); - - if ($startDate) { - $parameters['startDate'] = $startDate->format('Y-m-d H:i:s'); - } - if ($endDate) { - $parameters['endDate'] = $endDate->format('Y-m-d H:i:s'); - } - if ($limit) { - $parameters['limit'] = (int) $limit; - } - if ($offset) { - $parameters['offset'] = (int) $offset; - } - if ($skipMyChanges) { - $parameters['skipMyChanges'] = (bool) $skipMyChanges; - } - - return $this->client->makeRequest('/orders/history', WC_Retailcrm_Request::METHOD_GET, $parameters); - } - - /** - * Returns filtered orders list - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * @return WC_Retailcrm_Response - */ - public function ordersList(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (sizeof($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest('/orders', WC_Retailcrm_Request::METHOD_GET, $parameters); - } - - /** - * Returns statuses of the orders - * - * @param array $ids (default: array()) - * @param array $externalIds (default: array()) - * @return WC_Retailcrm_Response - */ - public function ordersStatuses(array $ids = array(), array $externalIds = array()) - { - $parameters = array(); - - if (sizeof($ids)) { - $parameters['ids'] = $ids; - } - if (sizeof($externalIds)) { - $parameters['externalIds'] = $externalIds; - } - - return $this->client->makeRequest('/orders/statuses', WC_Retailcrm_Request::METHOD_GET, $parameters); - } - - /** - * Save order IDs' (id and externalId) association in the CRM - * - * @param array $ids - * @return WC_Retailcrm_Response - */ - public function ordersFixExternalIds(array $ids) - { - if (!sizeof($ids)) { - throw new InvalidArgumentException('Method parameter must contains at least one IDs pair'); - } - - return $this->client->makeRequest("/orders/fix-external-ids", WC_Retailcrm_Request::METHOD_POST, array( - 'orders' => json_encode($ids), - )); - } - - /** - * Get orders assembly history - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * @return WC_Retailcrm_Response - */ - public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (sizeof($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest('/orders/packs/history', WC_Retailcrm_Request::METHOD_GET, $parameters); - } - - /** - * Create a customer - * - * @param array $customer - * @param string $site (default: null) - * @return WC_Retailcrm_Response - */ - public function customersCreate(array $customer, $site = null) - { - if (!sizeof($customer)) { - throw new InvalidArgumentException('Parameter `customer` must contains a data'); - } - - return $this->client->makeRequest("/customers/create", WC_Retailcrm_Request::METHOD_POST, $this->fillSite($site, array( - 'customer' => json_encode($customer) - ))); - } - - /** - * Edit a customer - * - * @param array $customer - * @param string $by (default: 'externalId') - * @param string $site (default: null) - * @return WC_Retailcrm_Response - */ - public function customersEdit(array $customer, $by = 'externalId', $site = null) - { - if (!sizeof($customer)) { - throw new InvalidArgumentException('Parameter `customer` must contains a data'); - } - - $this->checkIdParameter($by); - - if (!isset($customer[$by])) { - throw new InvalidArgumentException(sprintf('Customer array must contain the "%s" parameter.', $by)); - } - - return $this->client->makeRequest( - "/customers/" . $customer[$by] . "/edit", - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite( - $site, - array( - 'customer' => json_encode($customer), - 'by' => $by - ) - ) - ); - } - - /** - * Upload array of the customers - * - * @param array $customers - * @param string $site (default: null) - * @return WC_Retailcrm_Response - */ - public function customersUpload(array $customers, $site = null) - { - if (!sizeof($customers)) { - throw new InvalidArgumentException('Parameter `customers` must contains array of the customers'); - } - - return $this->client->makeRequest("/customers/upload", WC_Retailcrm_Request::METHOD_POST, $this->fillSite($site, array( - 'customers' => json_encode($customers), - ))); - } - - /** - * Get customer by id or externalId - * - * @param string $id - * @param string $by (default: 'externalId') - * @param string $site (default: null) - * @return WC_Retailcrm_Response - */ - public function customersGet($id, $by = 'externalId', $site = null) - { - $this->checkIdParameter($by); - - return $this->client->makeRequest("/customers/$id", WC_Retailcrm_Request::METHOD_GET, $this->fillSite($site, array( - 'by' => $by - ))); - } - - /** - * Returns filtered customers list - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * @return WC_Retailcrm_Response - */ - public function customersList(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (sizeof($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest('/customers', WC_Retailcrm_Request::METHOD_GET, $parameters); - } - - /** - * Save customer IDs' (id and externalId) association in the CRM - * - * @param array $ids - * @return WC_Retailcrm_Response - */ - public function customersFixExternalIds(array $ids) - { - if (!sizeof($ids)) { - throw new InvalidArgumentException('Method parameter must contains at least one IDs pair'); - } - - return $this->client->makeRequest("/customers/fix-external-ids", WC_Retailcrm_Request::METHOD_POST, array( - 'customers' => json_encode($ids), - )); - } - - /** - * Get purchace prices & stock balance - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * @param string $site (default: null) - * @return WC_Retailcrm_Response - */ - public function storeInventories(array $filter = array(), $page = null, $limit = null, $site = null) - { - $parameters = array(); - - if (sizeof($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest('/store/inventories', WC_Retailcrm_Request::METHOD_GET, $this->fillSite($site, $parameters)); - } - - /** - * Upload store inventories - * - * @param array $offers - * @param string $site (default: null) - * @return WC_Retailcrm_Response - */ - public function storeInventoriesUpload(array $offers, $site = null) - { - if (!sizeof($offers)) { - throw new InvalidArgumentException('Parameter `offers` must contains array of the customers'); - } - - return $this->client->makeRequest( - "/store/inventories/upload", - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('offers' => json_encode($offers))) - ); - } - - /** - * Returns deliveryServices list - * - * @return WC_Retailcrm_Response - */ - public function deliveryServicesList() - { - return $this->client->makeRequest('/reference/delivery-services', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns deliveryTypes list - * - * @return WC_Retailcrm_Response - */ - public function deliveryTypesList() - { - return $this->client->makeRequest('/reference/delivery-types', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns orderMethods list - * - * @return WC_Retailcrm_Response - */ - public function orderMethodsList() - { - return $this->client->makeRequest('/reference/order-methods', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns orderTypes list - * - * @return WC_Retailcrm_Response - */ - public function orderTypesList() - { - return $this->client->makeRequest('/reference/order-types', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns paymentStatuses list - * - * @return WC_Retailcrm_Response - */ - public function paymentStatusesList() - { - return $this->client->makeRequest('/reference/payment-statuses', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns paymentTypes list - * - * @return WC_Retailcrm_Response - */ - public function paymentTypesList() - { - return $this->client->makeRequest('/reference/payment-types', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns productStatuses list - * - * @return WC_Retailcrm_Response - */ - public function productStatusesList() - { - return $this->client->makeRequest('/reference/product-statuses', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns statusGroups list - * - * @return WC_Retailcrm_Response - */ - public function statusGroupsList() - { - return $this->client->makeRequest('/reference/status-groups', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns statuses list - * - * @return WC_Retailcrm_Response - */ - public function statusesList() - { - return $this->client->makeRequest('/reference/statuses', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns sites list - * - * @return WC_Retailcrm_Response - */ - public function sitesList() - { - return $this->client->makeRequest('/reference/sites', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns stores list - * - * @return WC_Retailcrm_Response - */ - public function storesList() - { - return $this->client->makeRequest('/reference/stores', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Edit deliveryService - * - * @param array $data delivery service data - * @return WC_Retailcrm_Response - */ - public function deliveryServicesEdit(array $data) - { - if (!isset($data['code'])) { - throw new InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/delivery-services/' . $data['code'] . '/edit', - WC_Retailcrm_Request::METHOD_POST, - array( - 'deliveryService' => json_encode($data) - ) - ); - } - - /** - * Edit deliveryType - * - * @param array $data delivery type data - * @return WC_Retailcrm_Response - */ - public function deliveryTypesEdit(array $data) - { - if (!isset($data['code'])) { - throw new InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/delivery-types/' . $data['code'] . '/edit', - WC_Retailcrm_Request::METHOD_POST, - array( - 'deliveryType' => json_encode($data) - ) - ); - } - - /** - * Edit orderMethod - * - * @param array $data order method data - * @return WC_Retailcrm_Response - */ - public function orderMethodsEdit(array $data) - { - if (!isset($data['code'])) { - throw new InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/order-methods/' . $data['code'] . '/edit', - WC_Retailcrm_Request::METHOD_POST, - array( - 'orderMethod' => json_encode($data) - ) - ); - } - - /** - * Edit orderType - * - * @param array $data order type data - * @return WC_Retailcrm_Response - */ - public function orderTypesEdit(array $data) - { - if (!isset($data['code'])) { - throw new InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/order-types/' . $data['code'] . '/edit', - WC_Retailcrm_Request::METHOD_POST, - array( - 'orderType' => json_encode($data) - ) - ); - } - - /** - * Edit paymentStatus - * - * @param array $data payment status data - * @return WC_Retailcrm_Response - */ - public function paymentStatusesEdit(array $data) - { - if (!isset($data['code'])) { - throw new InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/payment-statuses/' . $data['code'] . '/edit', - WC_Retailcrm_Request::METHOD_POST, - array( - 'paymentStatus' => json_encode($data) - ) - ); - } - - /** - * Edit paymentType - * - * @param array $data payment type data - * @return WC_Retailcrm_Response - */ - public function paymentTypesEdit(array $data) - { - if (!isset($data['code'])) { - throw new InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/payment-types/' . $data['code'] . '/edit', - WC_Retailcrm_Request::METHOD_POST, - array( - 'paymentType' => json_encode($data) - ) - ); - } - - /** - * Edit productStatus - * - * @param array $data product status data - * @return WC_Retailcrm_Response - */ - public function productStatusesEdit(array $data) - { - if (!isset($data['code'])) { - throw new InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/product-statuses/' . $data['code'] . '/edit', - WC_Retailcrm_Request::METHOD_POST, - array( - 'productStatus' => json_encode($data) - ) - ); - } - - /** - * Edit order status - * - * @param array $data status data - * @return WC_Retailcrm_Response - */ - public function statusesEdit(array $data) - { - if (!isset($data['code'])) { - throw new InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/statuses/' . $data['code'] . '/edit', - WC_Retailcrm_Request::METHOD_POST, - array( - 'status' => json_encode($data) - ) - ); - } - - /** - * Edit site - * - * @param array $data site data - * @return WC_Retailcrm_Response - */ - public function sitesEdit(array $data) - { - if (!isset($data['code'])) { - throw new InvalidArgumentException('Data must contain "code" parameter.'); - } - - return $this->client->makeRequest( - '/reference/sites/' . $data['code'] . '/edit', - WC_Retailcrm_Request::METHOD_POST, - array( - 'site' => json_encode($data) - ) - ); - } - - /** - * Edit store - * - * @param array $data site data - * @return WC_Retailcrm_Response - */ - public function storesEdit(array $data) - { - if (!isset($data['code'])) { - throw new InvalidArgumentException('Data must contain "code" parameter.'); - } - - if (!isset($data['name'])) { - throw new InvalidArgumentException('Data must contain "name" parameter.'); - } - - return $this->client->makeRequest( - '/reference/stores/' . $data['code'] . '/edit', - WC_Retailcrm_Request::METHOD_POST, - array( - 'store' => json_encode($data) - ) - ); - } - - /** - * Update CRM basic statistic - * - * @return WC_Retailcrm_Response - */ - public function statisticUpdate() - { - return $this->client->makeRequest('/statistic/update', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Return current site - * - * @return string - */ - public function getSite() - { - return $this->siteCode; - } - - /** - * Set site - * - * @param string $site - * @return void - */ - public function setSite($site) - { - $this->siteCode = $site; - } - - /** - * Check ID parameter - * - * @param string $by - * @return bool - */ - protected function checkIdParameter($by) - { - $allowedForBy = array('externalId', 'id'); - if (!in_array($by, $allowedForBy)) { - throw new InvalidArgumentException(sprintf( - 'Value "%s" for parameter "by" is not valid. Allowed values are %s.', - $by, - implode(', ', $allowedForBy) - )); - } - - return true; - } - - /** - * Fill params by site value - * - * @param string $site - * @param array $params - * @return array - */ - protected function fillSite($site, array $params) - { - if ($site) { - $params['site'] = $site; - } elseif ($this->siteCode) { - $params['site'] = $this->siteCode; - } - - return $params; - } - } diff --git a/woo-retailcrm/include/api/class-wc-retailcrm-client-v4.php b/woo-retailcrm/include/api/class-wc-retailcrm-client-v4.php index 08d4ab6..1cce88f 100644 --- a/woo-retailcrm/include/api/class-wc-retailcrm-client-v4.php +++ b/woo-retailcrm/include/api/class-wc-retailcrm-client-v4.php @@ -1,1868 +1,1868 @@ - * @license https://opensource.org/licenses/MIT MIT License - * @link http://retailcrm.ru/docs/Developers/ApiVersion4 - */ - - if ( ! class_exists( 'WC_Retailcrm_Request' ) ) { - include_once( __DIR__ . '/class-wc-retailcrm-request.php' ); - } - - if ( ! class_exists( 'WC_Retailcrm_Response' ) ) { - include_once( __DIR__ . '/class-wc-retailcrm-response.php' ); - } - - class WC_Retailcrm_Client_V4 - { - protected $client; - - /** - * Site code - */ - protected $siteCode; - - /** - * Client creating - * - * @param string $url api url - * @param string $apiKey api key - * @param string $site site code - * - * @throws \InvalidArgumentException - */ - public function __construct($url, $apiKey, $version = null, $site = null) - { - if ('/' !== $url[strlen($url) - 1]) { - $url .= '/'; - } - - $url = $version == null ? $url . 'api' : $url . 'api/' . $version; - - $this->client = new WC_Retailcrm_Request($url, array('apiKey' => $apiKey)); - $this->siteCode = $site; - } - - /** - * Returns api versions list - * - * @return WC_Retailcrm_Response - */ - public function apiVersions() - { - return $this->client->makeRequest('/api-versions', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns users list - * - * @param array $filter - * @param null $page - * @param null $limit - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function usersList(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/users', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Get user groups - * - * @param null $page - * @param null $limit - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * - * @return WC_Retailcrm_Response - */ - public function usersGroups($page = null, $limit = null) - { - $parameters = array(); - - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/user-groups', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Returns user data - * - * @param integer $id user ID - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function usersGet($id) - { - return $this->client->makeRequest("/users/$id", WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns filtered orders list - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersList(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/orders', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Create a order - * - * @param array $order order data - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersCreate(array $order, $site = null) - { - if (!count($order)) { - throw new \InvalidArgumentException( - 'Parameter `order` must contains a data' - ); - } - - return $this->client->makeRequest( - '/orders/create', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('order' => json_encode($order))) - ); - } - - /** - * Save order IDs' (id and externalId) association in the CRM - * - * @param array $ids order identificators - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersFixExternalIds(array $ids) - { - if (! count($ids)) { - throw new \InvalidArgumentException( - 'Method parameter must contains at least one IDs pair' - ); - } - - return $this->client->makeRequest( - '/orders/fix-external-ids', - WC_Retailcrm_Request::METHOD_POST, - array('orders' => json_encode($ids) - ) - ); - } - - /** - * Returns statuses of the orders - * - * @param array $ids (default: array()) - * @param array $externalIds (default: array()) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersStatuses(array $ids = array(), array $externalIds = array()) - { - $parameters = array(); - - if (count($ids)) { - $parameters['ids'] = $ids; - } - if (count($externalIds)) { - $parameters['externalIds'] = $externalIds; - } - - return $this->client->makeRequest( - '/orders/statuses', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Upload array of the orders - * - * @param array $orders array of orders - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersUpload(array $orders, $site = null) - { - if (!count($orders)) { - throw new \InvalidArgumentException( - 'Parameter `orders` must contains array of the orders' - ); - } - - return $this->client->makeRequest( - '/orders/upload', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('orders' => json_encode($orders))) - ); - } - - /** - * Get order by id or externalId - * - * @param string $id order identificator - * @param string $by (default: 'externalId') - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersGet($id, $by = 'externalId', $site = null) - { - $this->checkIdParameter($by); - - return $this->client->makeRequest( - "/orders/$id", - WC_Retailcrm_Request::METHOD_GET, - $this->fillSite($site, array('by' => $by)) - ); - } - - /** - * Edit a order - * - * @param array $order order data - * @param string $by (default: 'externalId') - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersEdit(array $order, $by = 'externalId', $site = null) - { - if (!count($order)) { - throw new \InvalidArgumentException( - 'Parameter `order` must contains a data' - ); - } - - $this->checkIdParameter($by); - - if (!array_key_exists($by, $order)) { - throw new \InvalidArgumentException( - sprintf('Order array must contain the "%s" parameter.', $by) - ); - } - - return $this->client->makeRequest( - sprintf('/orders/%s/edit', $order[$by]), - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite( - $site, - array('order' => json_encode($order), 'by' => $by) - ) - ); - } - - /** - * Get orders history - * @param array $filter - * @param null $page - * @param null $limit - * - * @return WC_Retailcrm_Response - */ - public function ordersHistory(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/orders/history', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Returns filtered customers list - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersList(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/customers', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Create a customer - * - * @param array $customer customer data - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersCreate(array $customer, $site = null) - { - if (! count($customer)) { - throw new \InvalidArgumentException( - 'Parameter `customer` must contains a data' - ); - } - - return $this->client->makeRequest( - '/customers/create', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('customer' => json_encode($customer))) - ); - } - - /** - * Save customer IDs' (id and externalId) association in the CRM - * - * @param array $ids ids mapping - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersFixExternalIds(array $ids) - { - if (! count($ids)) { - throw new \InvalidArgumentException( - 'Method parameter must contains at least one IDs pair' - ); - } - - return $this->client->makeRequest( - '/customers/fix-external-ids', - WC_Retailcrm_Request::METHOD_POST, - array('customers' => json_encode($ids)) - ); - } - - /** - * Upload array of the customers - * - * @param array $customers array of customers - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersUpload(array $customers, $site = null) - { - if (! count($customers)) { - throw new \InvalidArgumentException( - 'Parameter `customers` must contains array of the customers' - ); - } - - return $this->client->makeRequest( - '/customers/upload', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('customers' => json_encode($customers))) - ); - } - - /** - * Get customer by id or externalId - * - * @param string $id customer identificator - * @param string $by (default: 'externalId') - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersGet($id, $by = 'externalId', $site = null) - { - $this->checkIdParameter($by); - - return $this->client->makeRequest( - "/customers/$id", - WC_Retailcrm_Request::METHOD_GET, - $this->fillSite($site, array('by' => $by)) - ); - } - - /** - * Edit a customer - * - * @param array $customer customer data - * @param string $by (default: 'externalId') - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersEdit(array $customer, $by = 'externalId', $site = null) - { - if (!count($customer)) { - throw new \InvalidArgumentException( - 'Parameter `customer` must contains a data' - ); - } - - $this->checkIdParameter($by); - - if (!array_key_exists($by, $customer)) { - throw new \InvalidArgumentException( - sprintf('Customer array must contain the "%s" parameter.', $by) - ); - } - - return $this->client->makeRequest( - sprintf('/customers/%s/edit', $customer[$by]), - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite( - $site, - array('customer' => json_encode($customer), 'by' => $by) - ) - ); - } - - /** - * Get customers history - * @param array $filter - * @param null $page - * @param null $limit - * - * @return WC_Retailcrm_Response - */ - public function customersHistory(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/customers/history', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Get orders assembly list - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPacksList(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/orders/packs', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Create orders assembly - * - * @param array $pack pack data - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPacksCreate(array $pack, $site = null) - { - if (!count($pack)) { - throw new \InvalidArgumentException( - 'Parameter `pack` must contains a data' - ); - } - - return $this->client->makeRequest( - '/orders/packs/create', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('pack' => json_encode($pack))) - ); - } - - /** - * Get orders assembly history - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/orders/packs/history', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Get orders assembly by id - * - * @param string $id pack identificator - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPacksGet($id) - { - if (empty($id)) { - throw new \InvalidArgumentException('Parameter `id` must be set'); - } - - return $this->client->makeRequest( - "/orders/packs/$id", - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Delete orders assembly by id - * - * @param string $id pack identificator - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPacksDelete($id) - { - if (empty($id)) { - throw new \InvalidArgumentException('Parameter `id` must be set'); - } - - return $this->client->makeRequest( - sprintf('/orders/packs/%s/delete', $id), - WC_Retailcrm_Request::METHOD_POST - ); - } - - /** - * Edit orders assembly - * - * @param array $pack pack data - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPacksEdit(array $pack, $site = null) - { - if (!count($pack) || empty($pack['id'])) { - throw new \InvalidArgumentException( - 'Parameter `pack` must contains a data & pack `id` must be set' - ); - } - - return $this->client->makeRequest( - sprintf('/orders/packs/%s/edit', $pack['id']), - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('pack' => json_encode($pack))) - ); - } - - /** - * Get purchace prices & stock balance - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function storeInventories(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/store/inventories', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Get store settings - * - * @param string $code get settings code - * - * @return WC_Retailcrm_Response - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function storeSettingsGet($code) - { - if (empty($code)) { - throw new \InvalidArgumentException('Parameter `code` must be set'); - } - - return $this->client->makeRequest( - "/store/setting/$code", - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit store configuration - * - * @param array $configuration - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function storeSettingsEdit(array $configuration) - { - if (!count($configuration) || empty($configuration['code'])) { - throw new \InvalidArgumentException( - 'Parameter `configuration` must contains a data & configuration `code` must be set' - ); - } - - return $this->client->makeRequest( - sprintf('/store/setting/%s/edit', $configuration['code']), - WC_Retailcrm_Request::METHOD_POST, - array('configuration' => json_encode($configuration)) - ); - } - - /** - * Upload store inventories - * - * @param array $offers offers data - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function storeInventoriesUpload(array $offers, $site = null) - { - if (!count($offers)) { - throw new \InvalidArgumentException( - 'Parameter `offers` must contains array of the offers' - ); - } - - return $this->client->makeRequest( - '/store/inventories/upload', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('offers' => json_encode($offers))) - ); - } - - /** - * Upload store prices - * - * @param array $prices prices data - * @param string $site default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function storePricesUpload(array $prices, $site = null) - { - if (!count($prices)) { - throw new \InvalidArgumentException( - 'Parameter `prices` must contains array of the prices' - ); - } - - return $this->client->makeRequest( - '/store/prices/upload', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('prices' => json_encode($prices))) - ); - } - - /** - * Get products - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function storeProducts(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/store/products', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Get delivery settings - * - * @param string $code - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function deliverySettingsGet($code) - { - if (empty($code)) { - throw new \InvalidArgumentException('Parameter `code` must be set'); - } - - return $this->client->makeRequest( - "/delivery/generic/setting/$code", - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit delivery configuration - * - * @param array $configuration - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function deliverySettingsEdit(array $configuration) - { - if (!count($configuration) || empty($configuration['code'])) { - throw new \InvalidArgumentException( - 'Parameter `configuration` must contains a data & configuration `code` must be set' - ); - } - - return $this->client->makeRequest( - sprintf('/delivery/generic/setting/%s/edit', $configuration['code']), - WC_Retailcrm_Request::METHOD_POST, - array('configuration' => json_encode($configuration)) - ); - } - - /** - * Delivery tracking update - * - * @param string $code - * @param array $statusUpdate - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function deliveryTracking($code, array $statusUpdate) - { - if (empty($code)) { - throw new \InvalidArgumentException('Parameter `code` must be set'); - } - - if (!count($statusUpdate)) { - throw new \InvalidArgumentException( - 'Parameter `statusUpdate` must contains a data' - ); - } - - return $this->client->makeRequest( - sprintf('/delivery/generic/%s/tracking', $code), - WC_Retailcrm_Request::METHOD_POST, - array('statusUpdate' => json_encode($statusUpdate)) - ); - } - - /** - * Returns available county list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function countriesList() - { - return $this->client->makeRequest( - '/reference/countries', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Returns deliveryServices list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function deliveryServicesList() - { - return $this->client->makeRequest( - '/reference/delivery-services', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit deliveryService - * - * @param array $data delivery service data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function deliveryServicesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/delivery-services/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('deliveryService' => json_encode($data)) - ); - } - - /** - * Returns deliveryTypes list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function deliveryTypesList() - { - return $this->client->makeRequest( - '/reference/delivery-types', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit deliveryType - * - * @param array $data delivery type data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function deliveryTypesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/delivery-types/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('deliveryType' => json_encode($data)) - ); - } - - /** - * Returns orderMethods list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function orderMethodsList() - { - return $this->client->makeRequest( - '/reference/order-methods', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit orderMethod - * - * @param array $data order method data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function orderMethodsEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/order-methods/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('orderMethod' => json_encode($data)) - ); - } - - /** - * Returns orderTypes list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function orderTypesList() - { - return $this->client->makeRequest( - '/reference/order-types', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit orderType - * - * @param array $data order type data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function orderTypesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/order-types/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('orderType' => json_encode($data)) - ); - } - - /** - * Returns paymentStatuses list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function paymentStatusesList() - { - return $this->client->makeRequest( - '/reference/payment-statuses', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit paymentStatus - * - * @param array $data payment status data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function paymentStatusesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/payment-statuses/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('paymentStatus' => json_encode($data)) - ); - } - - /** - * Returns paymentTypes list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function paymentTypesList() - { - return $this->client->makeRequest( - '/reference/payment-types', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit paymentType - * - * @param array $data payment type data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function paymentTypesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/payment-types/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('paymentType' => json_encode($data)) - ); - } - - /** - * Returns productStatuses list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function productStatusesList() - { - return $this->client->makeRequest( - '/reference/product-statuses', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit productStatus - * - * @param array $data product status data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function productStatusesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/product-statuses/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('productStatus' => json_encode($data)) - ); - } - - /** - * Returns sites list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function sitesList() - { - return $this->client->makeRequest( - '/reference/sites', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit site - * - * @param array $data site data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function sitesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/sites/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('site' => json_encode($data)) - ); - } - - /** - * Returns statusGroups list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function statusGroupsList() - { - return $this->client->makeRequest( - '/reference/status-groups', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Returns statuses list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function statusesList() - { - return $this->client->makeRequest( - '/reference/statuses', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit order status - * - * @param array $data status data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function statusesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/statuses/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('status' => json_encode($data)) - ); - } - - /** - * Returns stores list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function storesList() - { - return $this->client->makeRequest( - '/reference/stores', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit store - * - * @param array $data site data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function storesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - if (!array_key_exists('name', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "name" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/stores/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('store' => json_encode($data)) - ); - } - - /** - * Get prices types - * - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function pricesTypes() - { - return $this->client->makeRequest( - '/reference/price-types', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit price type - * - * @param array $data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function pricesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - if (!array_key_exists('name', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "name" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/price-types/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('priceType' => json_encode($data)) - ); - } - - /** - * Get telephony settings - * - * @param string $code - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function telephonySettingsGet($code) - { - if (empty($code)) { - throw new \InvalidArgumentException('Parameter `code` must be set'); - } - - return $this->client->makeRequest( - "/telephony/setting/$code", - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit telephony settings - * - * @param string $code symbolic code - * @param string $clientId client id - * @param boolean $active telephony activity - * @param mixed $name service name - * @param mixed $makeCallUrl service init url - * @param mixed $image service logo url(svg file) - * - * @param array $additionalCodes - * @param array $externalPhones - * @param bool $allowEdit - * @param bool $inputEventSupported - * @param bool $outputEventSupported - * @param bool $hangupEventSupported - * @param bool $changeUserStatusUrl - * - * @return WC_Retailcrm_Response - */ - public function telephonySettingsEdit( - $code, - $clientId, - $active = false, - $name = false, - $makeCallUrl = false, - $image = false, - $additionalCodes = array(), - $externalPhones = array(), - $allowEdit = false, - $inputEventSupported = false, - $outputEventSupported = false, - $hangupEventSupported = false, - $changeUserStatusUrl = false - ) - { - if (!isset($code)) { - throw new \InvalidArgumentException('Code must be set'); - } - - $parameters['code'] = $code; - - if (!isset($clientId)) { - throw new \InvalidArgumentException('client id must be set'); - } - - $parameters['clientId'] = $clientId; - - if (!isset($active)) { - $parameters['active'] = false; - } else { - $parameters['active'] = $active; - } - - if (!isset($name)) { - throw new \InvalidArgumentException('name must be set'); - } - - if (isset($name)) { - $parameters['name'] = $name; - } - - if (isset($makeCallUrl)) { - $parameters['makeCallUrl'] = $makeCallUrl; - } - - if (isset($image)) { - $parameters['image'] = $image; - } - - if (isset($additionalCodes)) { - $parameters['additionalCodes'] = $additionalCodes; - } - - if (isset($externalPhones)) { - $parameters['externalPhones'] = $externalPhones; - } - - if (isset($allowEdit)) { - $parameters['allowEdit'] = $allowEdit; - } - - if (isset($inputEventSupported)) { - $parameters['inputEventSupported'] = $inputEventSupported; - } - - if (isset($outputEventSupported)) { - $parameters['outputEventSupported'] = $outputEventSupported; - } - - if (isset($hangupEventSupported)) { - $parameters['hangupEventSupported'] = $hangupEventSupported; - } - - if (isset($changeUserStatusUrl)) { - $parameters['changeUserStatusUrl'] = $changeUserStatusUrl; - } - - return $this->client->makeRequest( - "/telephony/setting/$code/edit", - WC_Retailcrm_Request::METHOD_POST, - array('configuration' => json_encode($parameters)) - ); - } - - /** - * Call event - * - * @param string $phone phone number - * @param string $type call type - * @param array $codes - * @param string $hangupStatus - * @param string $externalPhone - * @param array $webAnalyticsData - * - * @return WC_Retailcrm_Response - * @internal param string $code additional phone code - * @internal param string $status call status - * - */ - public function telephonyCallEvent( - $phone, - $type, - $codes, - $hangupStatus, - $externalPhone = null, - $webAnalyticsData = array() - ) - { - if (!isset($phone)) { - throw new \InvalidArgumentException('Phone number must be set'); - } - - if (!isset($type)) { - throw new \InvalidArgumentException('Type must be set (in|out|hangup)'); - } - - if (empty($codes)) { - throw new \InvalidArgumentException('Codes array must be set'); - } - - $parameters['phone'] = $phone; - $parameters['type'] = $type; - $parameters['codes'] = $codes; - $parameters['hangupStatus'] = $hangupStatus; - $parameters['callExternalId'] = $externalPhone; - $parameters['webAnalyticsData'] = $webAnalyticsData; - - - return $this->client->makeRequest( - '/telephony/call/event', - WC_Retailcrm_Request::METHOD_POST, - array('event' => json_encode($parameters)) - ); - } - - /** - * Upload calls - * - * @param array $calls calls data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function telephonyCallsUpload(array $calls) - { - if (!count($calls)) { - throw new \InvalidArgumentException( - 'Parameter `calls` must contains array of the calls' - ); - } - - return $this->client->makeRequest( - '/telephony/calls/upload', - WC_Retailcrm_Request::METHOD_POST, - array('calls' => json_encode($calls)) - ); - } - - /** - * Get call manager - * - * @param string $phone phone number - * @param bool $details detailed information - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function telephonyCallManager($phone, $details) - { - if (!isset($phone)) { - throw new \InvalidArgumentException('Phone number must be set'); - } - - $parameters['phone'] = $phone; - $parameters['details'] = isset($details) ? $details : 0; - - return $this->client->makeRequest( - '/telephony/manager', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Edit marketplace configuration - * - * @param array $configuration - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function marketplaceSettingsEdit(array $configuration) - { - if (!count($configuration) || empty($configuration['code'])) { - throw new \InvalidArgumentException( - 'Parameter `configuration` must contains a data & configuration `code` must be set' - ); - } - - return $this->client->makeRequest( - sprintf('/marketplace/external/setting/%s/edit', $configuration['code']), - WC_Retailcrm_Request::METHOD_POST, - array('configuration' => json_encode($configuration)) - ); - } - - /** - * Update CRM basic statistic - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function statisticUpdate() - { - return $this->client->makeRequest( - '/statistic/update', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Return current site - * - * @return string - */ - public function getSite() - { - return $this->siteCode; - } - - /** - * Set site - * - * @param string $site site code - * - * @return void - */ - public function setSite($site) - { - $this->siteCode = $site; - } - - /** - * Check ID parameter - * - * @param string $by identify by - * - * @throws \InvalidArgumentException - * - * @return bool - */ - protected function checkIdParameter($by) - { - $allowedForBy = array( - 'externalId', - 'id' - ); - - if (!in_array($by, $allowedForBy, false)) { - throw new \InvalidArgumentException( - sprintf( - 'Value "%s" for "by" param is not valid. Allowed values are %s.', - $by, - implode(', ', $allowedForBy) - ) - ); - } - - return true; - } - - /** - * Fill params by site value - * - * @param string $site site code - * @param array $params input parameters - * - * @return array - */ - protected function fillSite($site, array $params) - { - if ($site) { - $params['site'] = $site; - } elseif ($this->siteCode) { - $params['site'] = $this->siteCode; - } - - return $params; - } - } +/** + * PHP version 5.3 + * + * Request class + * + * @category Integration + * @package WC_Retailcrm_Client + * @author RetailCRM + * @license https://opensource.org/licenses/MIT MIT License + * @link http://retailcrm.ru/docs/Developers/ApiVersion4 + */ + +if ( ! class_exists( 'WC_Retailcrm_Request' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-request.php' ); +} + +if ( ! class_exists( 'WC_Retailcrm_Response' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-response.php' ); +} + +class WC_Retailcrm_Client_V4 +{ + protected $client; + + /** + * Site code + */ + protected $siteCode; + + /** + * Client creating + * + * @param string $url api url + * @param string $apiKey api key + * @param string $site site code + * + * @throws \InvalidArgumentException + */ + public function __construct($url, $apiKey, $version = null, $site = null) + { + if ('/' !== $url[strlen($url) - 1]) { + $url .= '/'; + } + + $url = $version == null ? $url . 'api' : $url . 'api/' . $version; + + $this->client = new WC_Retailcrm_Request($url, array('apiKey' => $apiKey)); + $this->siteCode = $site; + } + + /** + * Returns api versions list + * + * @return WC_Retailcrm_Response + */ + public function apiVersions() + { + return $this->client->makeRequest('/api-versions', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns users list + * + * @param array $filter + * @param null $page + * @param null $limit + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function usersList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/users', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get user groups + * + * @param null $page + * @param null $limit + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * + * @return WC_Retailcrm_Response + */ + public function usersGroups($page = null, $limit = null) + { + $parameters = array(); + + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/user-groups', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Returns user data + * + * @param integer $id user ID + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function usersGet($id) + { + return $this->client->makeRequest("/users/$id", WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns filtered orders list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/orders', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create a order + * + * @param array $order order data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersCreate(array $order, $site = null) + { + if (!count($order)) { + throw new \InvalidArgumentException( + 'Parameter `order` must contains a data' + ); + } + + return $this->client->makeRequest( + '/orders/create', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('order' => json_encode($order))) + ); + } + + /** + * Save order IDs' (id and externalId) association in the CRM + * + * @param array $ids order identificators + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersFixExternalIds(array $ids) + { + if (! count($ids)) { + throw new \InvalidArgumentException( + 'Method parameter must contains at least one IDs pair' + ); + } + + return $this->client->makeRequest( + '/orders/fix-external-ids', + WC_Retailcrm_Request::METHOD_POST, + array('orders' => json_encode($ids) + ) + ); + } + + /** + * Returns statuses of the orders + * + * @param array $ids (default: array()) + * @param array $externalIds (default: array()) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersStatuses(array $ids = array(), array $externalIds = array()) + { + $parameters = array(); + + if (count($ids)) { + $parameters['ids'] = $ids; + } + if (count($externalIds)) { + $parameters['externalIds'] = $externalIds; + } + + return $this->client->makeRequest( + '/orders/statuses', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Upload array of the orders + * + * @param array $orders array of orders + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersUpload(array $orders, $site = null) + { + if (!count($orders)) { + throw new \InvalidArgumentException( + 'Parameter `orders` must contains array of the orders' + ); + } + + return $this->client->makeRequest( + '/orders/upload', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('orders' => json_encode($orders))) + ); + } + + /** + * Get order by id or externalId + * + * @param string $id order identificator + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersGet($id, $by = 'externalId', $site = null) + { + $this->checkIdParameter($by); + + return $this->client->makeRequest( + "/orders/$id", + WC_Retailcrm_Request::METHOD_GET, + $this->fillSite($site, array('by' => $by)) + ); + } + + /** + * Edit a order + * + * @param array $order order data + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersEdit(array $order, $by = 'externalId', $site = null) + { + if (!count($order)) { + throw new \InvalidArgumentException( + 'Parameter `order` must contains a data' + ); + } + + $this->checkIdParameter($by); + + if (!array_key_exists($by, $order)) { + throw new \InvalidArgumentException( + sprintf('Order array must contain the "%s" parameter.', $by) + ); + } + + return $this->client->makeRequest( + sprintf('/orders/%s/edit', $order[$by]), + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array('order' => json_encode($order), 'by' => $by) + ) + ); + } + + /** + * Get orders history + * @param array $filter + * @param null $page + * @param null $limit + * + * @return WC_Retailcrm_Response + */ + public function ordersHistory(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/orders/history', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Returns filtered customers list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/customers', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create a customer + * + * @param array $customer customer data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersCreate(array $customer, $site = null) + { + if (! count($customer)) { + throw new \InvalidArgumentException( + 'Parameter `customer` must contains a data' + ); + } + + return $this->client->makeRequest( + '/customers/create', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('customer' => json_encode($customer))) + ); + } + + /** + * Save customer IDs' (id and externalId) association in the CRM + * + * @param array $ids ids mapping + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersFixExternalIds(array $ids) + { + if (! count($ids)) { + throw new \InvalidArgumentException( + 'Method parameter must contains at least one IDs pair' + ); + } + + return $this->client->makeRequest( + '/customers/fix-external-ids', + WC_Retailcrm_Request::METHOD_POST, + array('customers' => json_encode($ids)) + ); + } + + /** + * Upload array of the customers + * + * @param array $customers array of customers + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersUpload(array $customers, $site = null) + { + if (! count($customers)) { + throw new \InvalidArgumentException( + 'Parameter `customers` must contains array of the customers' + ); + } + + return $this->client->makeRequest( + '/customers/upload', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('customers' => json_encode($customers))) + ); + } + + /** + * Get customer by id or externalId + * + * @param string $id customer identificator + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersGet($id, $by = 'externalId', $site = null) + { + $this->checkIdParameter($by); + + return $this->client->makeRequest( + "/customers/$id", + WC_Retailcrm_Request::METHOD_GET, + $this->fillSite($site, array('by' => $by)) + ); + } + + /** + * Edit a customer + * + * @param array $customer customer data + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersEdit(array $customer, $by = 'externalId', $site = null) + { + if (!count($customer)) { + throw new \InvalidArgumentException( + 'Parameter `customer` must contains a data' + ); + } + + $this->checkIdParameter($by); + + if (!array_key_exists($by, $customer)) { + throw new \InvalidArgumentException( + sprintf('Customer array must contain the "%s" parameter.', $by) + ); + } + + return $this->client->makeRequest( + sprintf('/customers/%s/edit', $customer[$by]), + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array('customer' => json_encode($customer), 'by' => $by) + ) + ); + } + + /** + * Get customers history + * @param array $filter + * @param null $page + * @param null $limit + * + * @return WC_Retailcrm_Response + */ + public function customersHistory(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/customers/history', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get orders assembly list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/orders/packs', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create orders assembly + * + * @param array $pack pack data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksCreate(array $pack, $site = null) + { + if (!count($pack)) { + throw new \InvalidArgumentException( + 'Parameter `pack` must contains a data' + ); + } + + return $this->client->makeRequest( + '/orders/packs/create', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('pack' => json_encode($pack))) + ); + } + + /** + * Get orders assembly history + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/orders/packs/history', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get orders assembly by id + * + * @param string $id pack identificator + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksGet($id) + { + if (empty($id)) { + throw new \InvalidArgumentException('Parameter `id` must be set'); + } + + return $this->client->makeRequest( + "/orders/packs/$id", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Delete orders assembly by id + * + * @param string $id pack identificator + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksDelete($id) + { + if (empty($id)) { + throw new \InvalidArgumentException('Parameter `id` must be set'); + } + + return $this->client->makeRequest( + sprintf('/orders/packs/%s/delete', $id), + WC_Retailcrm_Request::METHOD_POST + ); + } + + /** + * Edit orders assembly + * + * @param array $pack pack data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksEdit(array $pack, $site = null) + { + if (!count($pack) || empty($pack['id'])) { + throw new \InvalidArgumentException( + 'Parameter `pack` must contains a data & pack `id` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/orders/packs/%s/edit', $pack['id']), + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('pack' => json_encode($pack))) + ); + } + + /** + * Get purchace prices & stock balance + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storeInventories(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/store/inventories', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get store settings + * + * @param string $code get settings code + * + * @return WC_Retailcrm_Response + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function storeSettingsGet($code) + { + if (empty($code)) { + throw new \InvalidArgumentException('Parameter `code` must be set'); + } + + return $this->client->makeRequest( + "/store/setting/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit store configuration + * + * @param array $configuration + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function storeSettingsEdit(array $configuration) + { + if (!count($configuration) || empty($configuration['code'])) { + throw new \InvalidArgumentException( + 'Parameter `configuration` must contains a data & configuration `code` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/store/setting/%s/edit', $configuration['code']), + WC_Retailcrm_Request::METHOD_POST, + array('configuration' => json_encode($configuration)) + ); + } + + /** + * Upload store inventories + * + * @param array $offers offers data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storeInventoriesUpload(array $offers, $site = null) + { + if (!count($offers)) { + throw new \InvalidArgumentException( + 'Parameter `offers` must contains array of the offers' + ); + } + + return $this->client->makeRequest( + '/store/inventories/upload', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('offers' => json_encode($offers))) + ); + } + + /** + * Upload store prices + * + * @param array $prices prices data + * @param string $site default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storePricesUpload(array $prices, $site = null) + { + if (!count($prices)) { + throw new \InvalidArgumentException( + 'Parameter `prices` must contains array of the prices' + ); + } + + return $this->client->makeRequest( + '/store/prices/upload', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('prices' => json_encode($prices))) + ); + } + + /** + * Get products + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storeProducts(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/store/products', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get delivery settings + * + * @param string $code + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliverySettingsGet($code) + { + if (empty($code)) { + throw new \InvalidArgumentException('Parameter `code` must be set'); + } + + return $this->client->makeRequest( + "/delivery/generic/setting/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit delivery configuration + * + * @param array $configuration + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function deliverySettingsEdit(array $configuration) + { + if (!count($configuration) || empty($configuration['code'])) { + throw new \InvalidArgumentException( + 'Parameter `configuration` must contains a data & configuration `code` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/delivery/generic/setting/%s/edit', $configuration['code']), + WC_Retailcrm_Request::METHOD_POST, + array('configuration' => json_encode($configuration)) + ); + } + + /** + * Delivery tracking update + * + * @param string $code + * @param array $statusUpdate + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function deliveryTracking($code, array $statusUpdate) + { + if (empty($code)) { + throw new \InvalidArgumentException('Parameter `code` must be set'); + } + + if (!count($statusUpdate)) { + throw new \InvalidArgumentException( + 'Parameter `statusUpdate` must contains a data' + ); + } + + return $this->client->makeRequest( + sprintf('/delivery/generic/%s/tracking', $code), + WC_Retailcrm_Request::METHOD_POST, + array('statusUpdate' => json_encode($statusUpdate)) + ); + } + + /** + * Returns available county list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function countriesList() + { + return $this->client->makeRequest( + '/reference/countries', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Returns deliveryServices list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliveryServicesList() + { + return $this->client->makeRequest( + '/reference/delivery-services', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit deliveryService + * + * @param array $data delivery service data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliveryServicesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/delivery-services/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('deliveryService' => json_encode($data)) + ); + } + + /** + * Returns deliveryTypes list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliveryTypesList() + { + return $this->client->makeRequest( + '/reference/delivery-types', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit deliveryType + * + * @param array $data delivery type data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliveryTypesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/delivery-types/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('deliveryType' => json_encode($data)) + ); + } + + /** + * Returns orderMethods list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function orderMethodsList() + { + return $this->client->makeRequest( + '/reference/order-methods', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit orderMethod + * + * @param array $data order method data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function orderMethodsEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/order-methods/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('orderMethod' => json_encode($data)) + ); + } + + /** + * Returns orderTypes list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function orderTypesList() + { + return $this->client->makeRequest( + '/reference/order-types', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit orderType + * + * @param array $data order type data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function orderTypesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/order-types/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('orderType' => json_encode($data)) + ); + } + + /** + * Returns paymentStatuses list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function paymentStatusesList() + { + return $this->client->makeRequest( + '/reference/payment-statuses', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit paymentStatus + * + * @param array $data payment status data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function paymentStatusesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/payment-statuses/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('paymentStatus' => json_encode($data)) + ); + } + + /** + * Returns paymentTypes list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function paymentTypesList() + { + return $this->client->makeRequest( + '/reference/payment-types', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit paymentType + * + * @param array $data payment type data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function paymentTypesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/payment-types/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('paymentType' => json_encode($data)) + ); + } + + /** + * Returns productStatuses list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function productStatusesList() + { + return $this->client->makeRequest( + '/reference/product-statuses', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit productStatus + * + * @param array $data product status data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function productStatusesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/product-statuses/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('productStatus' => json_encode($data)) + ); + } + + /** + * Returns sites list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function sitesList() + { + return $this->client->makeRequest( + '/reference/sites', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit site + * + * @param array $data site data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function sitesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/sites/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('site' => json_encode($data)) + ); + } + + /** + * Returns statusGroups list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function statusGroupsList() + { + return $this->client->makeRequest( + '/reference/status-groups', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Returns statuses list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function statusesList() + { + return $this->client->makeRequest( + '/reference/statuses', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit order status + * + * @param array $data status data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function statusesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/statuses/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('status' => json_encode($data)) + ); + } + + /** + * Returns stores list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storesList() + { + return $this->client->makeRequest( + '/reference/stores', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit store + * + * @param array $data site data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + if (!array_key_exists('name', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "name" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/stores/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('store' => json_encode($data)) + ); + } + + /** + * Get prices types + * + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function pricesTypes() + { + return $this->client->makeRequest( + '/reference/price-types', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit price type + * + * @param array $data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function pricesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + if (!array_key_exists('name', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "name" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/price-types/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('priceType' => json_encode($data)) + ); + } + + /** + * Get telephony settings + * + * @param string $code + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function telephonySettingsGet($code) + { + if (empty($code)) { + throw new \InvalidArgumentException('Parameter `code` must be set'); + } + + return $this->client->makeRequest( + "/telephony/setting/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit telephony settings + * + * @param string $code symbolic code + * @param string $clientId client id + * @param boolean $active telephony activity + * @param mixed $name service name + * @param mixed $makeCallUrl service init url + * @param mixed $image service logo url(svg file) + * + * @param array $additionalCodes + * @param array $externalPhones + * @param bool $allowEdit + * @param bool $inputEventSupported + * @param bool $outputEventSupported + * @param bool $hangupEventSupported + * @param bool $changeUserStatusUrl + * + * @return WC_Retailcrm_Response + */ + public function telephonySettingsEdit( + $code, + $clientId, + $active = false, + $name = false, + $makeCallUrl = false, + $image = false, + $additionalCodes = array(), + $externalPhones = array(), + $allowEdit = false, + $inputEventSupported = false, + $outputEventSupported = false, + $hangupEventSupported = false, + $changeUserStatusUrl = false + ) + { + if (!isset($code)) { + throw new \InvalidArgumentException('Code must be set'); + } + + $parameters['code'] = $code; + + if (!isset($clientId)) { + throw new \InvalidArgumentException('client id must be set'); + } + + $parameters['clientId'] = $clientId; + + if (!isset($active)) { + $parameters['active'] = false; + } else { + $parameters['active'] = $active; + } + + if (!isset($name)) { + throw new \InvalidArgumentException('name must be set'); + } + + if (isset($name)) { + $parameters['name'] = $name; + } + + if (isset($makeCallUrl)) { + $parameters['makeCallUrl'] = $makeCallUrl; + } + + if (isset($image)) { + $parameters['image'] = $image; + } + + if (isset($additionalCodes)) { + $parameters['additionalCodes'] = $additionalCodes; + } + + if (isset($externalPhones)) { + $parameters['externalPhones'] = $externalPhones; + } + + if (isset($allowEdit)) { + $parameters['allowEdit'] = $allowEdit; + } + + if (isset($inputEventSupported)) { + $parameters['inputEventSupported'] = $inputEventSupported; + } + + if (isset($outputEventSupported)) { + $parameters['outputEventSupported'] = $outputEventSupported; + } + + if (isset($hangupEventSupported)) { + $parameters['hangupEventSupported'] = $hangupEventSupported; + } + + if (isset($changeUserStatusUrl)) { + $parameters['changeUserStatusUrl'] = $changeUserStatusUrl; + } + + return $this->client->makeRequest( + "/telephony/setting/$code/edit", + WC_Retailcrm_Request::METHOD_POST, + array('configuration' => json_encode($parameters)) + ); + } + + /** + * Call event + * + * @param string $phone phone number + * @param string $type call type + * @param array $codes + * @param string $hangupStatus + * @param string $externalPhone + * @param array $webAnalyticsData + * + * @return WC_Retailcrm_Response + * @internal param string $code additional phone code + * @internal param string $status call status + * + */ + public function telephonyCallEvent( + $phone, + $type, + $codes, + $hangupStatus, + $externalPhone = null, + $webAnalyticsData = array() + ) + { + if (!isset($phone)) { + throw new \InvalidArgumentException('Phone number must be set'); + } + + if (!isset($type)) { + throw new \InvalidArgumentException('Type must be set (in|out|hangup)'); + } + + if (empty($codes)) { + throw new \InvalidArgumentException('Codes array must be set'); + } + + $parameters['phone'] = $phone; + $parameters['type'] = $type; + $parameters['codes'] = $codes; + $parameters['hangupStatus'] = $hangupStatus; + $parameters['callExternalId'] = $externalPhone; + $parameters['webAnalyticsData'] = $webAnalyticsData; + + + return $this->client->makeRequest( + '/telephony/call/event', + WC_Retailcrm_Request::METHOD_POST, + array('event' => json_encode($parameters)) + ); + } + + /** + * Upload calls + * + * @param array $calls calls data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function telephonyCallsUpload(array $calls) + { + if (!count($calls)) { + throw new \InvalidArgumentException( + 'Parameter `calls` must contains array of the calls' + ); + } + + return $this->client->makeRequest( + '/telephony/calls/upload', + WC_Retailcrm_Request::METHOD_POST, + array('calls' => json_encode($calls)) + ); + } + + /** + * Get call manager + * + * @param string $phone phone number + * @param bool $details detailed information + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function telephonyCallManager($phone, $details) + { + if (!isset($phone)) { + throw new \InvalidArgumentException('Phone number must be set'); + } + + $parameters['phone'] = $phone; + $parameters['details'] = isset($details) ? $details : 0; + + return $this->client->makeRequest( + '/telephony/manager', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Edit marketplace configuration + * + * @param array $configuration + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function marketplaceSettingsEdit(array $configuration) + { + if (!count($configuration) || empty($configuration['code'])) { + throw new \InvalidArgumentException( + 'Parameter `configuration` must contains a data & configuration `code` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/marketplace/external/setting/%s/edit', $configuration['code']), + WC_Retailcrm_Request::METHOD_POST, + array('configuration' => json_encode($configuration)) + ); + } + + /** + * Update CRM basic statistic + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function statisticUpdate() + { + return $this->client->makeRequest( + '/statistic/update', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Return current site + * + * @return string + */ + public function getSite() + { + return $this->siteCode; + } + + /** + * Set site + * + * @param string $site site code + * + * @return void + */ + public function setSite($site) + { + $this->siteCode = $site; + } + + /** + * Check ID parameter + * + * @param string $by identify by + * + * @throws \InvalidArgumentException + * + * @return bool + */ + protected function checkIdParameter($by) + { + $allowedForBy = array( + 'externalId', + 'id' + ); + + if (!in_array($by, $allowedForBy, false)) { + throw new \InvalidArgumentException( + sprintf( + 'Value "%s" for "by" param is not valid. Allowed values are %s.', + $by, + implode(', ', $allowedForBy) + ) + ); + } + + return true; + } + + /** + * Fill params by site value + * + * @param string $site site code + * @param array $params input parameters + * + * @return array + */ + protected function fillSite($site, array $params) + { + if ($site) { + $params['site'] = $site; + } elseif ($this->siteCode) { + $params['site'] = $this->siteCode; + } + + return $params; + } +} diff --git a/woo-retailcrm/include/api/class-wc-retailcrm-client-v5.php b/woo-retailcrm/include/api/class-wc-retailcrm-client-v5.php index 5a54a2a..e298daf 100644 --- a/woo-retailcrm/include/api/class-wc-retailcrm-client-v5.php +++ b/woo-retailcrm/include/api/class-wc-retailcrm-client-v5.php @@ -1,2381 +1,2381 @@ + * @license https://opensource.org/licenses/MIT MIT License + * @link http://retailcrm.ru/docs/Developers/ApiVersion5 + */ + +if ( ! class_exists( 'WC_Retailcrm_Request' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-request.php' ); +} + +if ( ! class_exists( 'WC_Retailcrm_Response' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-response.php' ); +} + +class WC_Retailcrm_Client_V5 +{ + protected $client; + /** - * PHP version 5.3 - * - * Request class - * - * @category Integration - * @package WC_Retailcrm_Client - * @author RetailCRM - * @license https://opensource.org/licenses/MIT MIT License - * @link http://retailcrm.ru/docs/Developers/ApiVersion5 + * Site code */ + protected $siteCode; - if ( ! class_exists( 'WC_Retailcrm_Request' ) ) { - include_once( __DIR__ . '/class-wc-retailcrm-request.php' ); - } - - if ( ! class_exists( 'WC_Retailcrm_Response' ) ) { - include_once( __DIR__ . '/class-wc-retailcrm-response.php' ); - } - - class WC_Retailcrm_Client_V5 + /** + * Client creating + * + * @param string $url api url + * @param string $apiKey api key + * @param string $site site code + * + * @throws \InvalidArgumentException + * + */ + public function __construct($url, $apiKey, $version = null, $site = null) { - protected $client; - - /** - * Site code - */ - protected $siteCode; - - /** - * Client creating - * - * @param string $url api url - * @param string $apiKey api key - * @param string $site site code - * - * @throws \InvalidArgumentException - * - */ - public function __construct($url, $apiKey, $version = null, $site = null) - { - if ('/' !== $url[strlen($url) - 1]) { - $url .= '/'; - } - - $url = $version == null ? $url . 'api' : $url . 'api/' . $version; - - $this->client = new WC_Retailcrm_Request($url, array('apiKey' => $apiKey)); - $this->siteCode = $site; - } - - /** - * Returns api versions list - * - * @return WC_Retailcrm_Response - */ - public function apiVersions() - { - return $this->client->makeRequest('/api-versions', WC_Retailcrm_Request::METHOD_GET); - } - - /** - * Returns users list - * - * @param array $filter - * @param null $page - * @param null $limit - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function usersList(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/users', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Returns user data - * - * @param integer $id user ID - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function usersGet($id) - { - return $this->client->makeRequest("/users/$id", WC_Retailcrm_Request::METHOD_GET); + if ('/' !== $url[strlen($url) - 1]) { + $url .= '/'; } - /** - * Change user status - * - * @param integer $id user ID - * @param string $status user status - * - * @return WC_Retailcrm_Response - */ - public function usersStatus($id, $status) - { - $statuses = array("free", "busy", "dinner", "break"); - - if (empty($status) || !in_array($status, $statuses)) { - throw new \InvalidArgumentException( - 'Parameter `status` must be not empty & must be equal one of these values: free|busy|dinner|break' - ); - } - - return $this->client->makeRequest( - "/users/$id/status", - WC_Retailcrm_Request::METHOD_POST, - array('status' => $status) - ); - } - - /** - * Get segments list - * - * @param array $filter - * @param null $limit - * @param null $page - * - * @return WC_Retailcrm_Response - */ - public function segmentsList(array $filter = array(), $limit = null, $page = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/segments', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Get custom fields list - * - * @param array $filter - * @param null $limit - * @param null $page - * - * @return WC_Retailcrm_Response - */ - public function customFieldsList(array $filter = array(), $limit = null, $page = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/custom-fields', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Create custom field - * - * @param $entity - * @param $customField - * - * @return WC_Retailcrm_Response - */ - public function customFieldsCreate($entity, $customField) - { - if (!count($customField) || - empty($customField['code']) || - empty($customField['name']) || - empty($customField['type']) - ) { - throw new \InvalidArgumentException( - 'Parameter `customField` must contain a data & fields `code`, `name` & `type` must be set' - ); - } - - if (empty($entity) || $entity != 'customer' || $entity != 'order') { - throw new \InvalidArgumentException( - 'Parameter `entity` must contain a data & value must be `order` or `customer`' - ); - } - - return $this->client->makeRequest( - "/custom-fields/$entity/create", - WC_Retailcrm_Request::METHOD_POST, - array('customField' => json_encode($customField)) - ); - } - - /** - * Edit custom field - * - * @param $entity - * @param $customField - * - * @return WC_Retailcrm_Response - */ - public function customFieldsEdit($entity, $customField) - { - if (!count($customField) || empty($customField['code'])) { - throw new \InvalidArgumentException( - 'Parameter `customField` must contain a data & fields `code` must be set' - ); - } - - if (empty($entity) || $entity != 'customer' || $entity != 'order') { - throw new \InvalidArgumentException( - 'Parameter `entity` must contain a data & value must be `order` or `customer`' - ); - } - - return $this->client->makeRequest( - "/custom-fields/$entity/edit/{$customField['code']}", - WC_Retailcrm_Request::METHOD_POST, - array('customField' => json_encode($customField)) - ); - } - - /** - * Get custom field - * - * @param $entity - * @param $code - * - * @return WC_Retailcrm_Response - */ - public function customFieldsGet($entity, $code) - { - if (empty($code)) { - throw new \InvalidArgumentException( - 'Parameter `code` must be not empty' - ); - } - - if (empty($entity) || $entity != 'customer' || $entity != 'order') { - throw new \InvalidArgumentException( - 'Parameter `entity` must contain a data & value must be `order` or `customer`' - ); - } - - return $this->client->makeRequest( - "/custom-fields/$entity/$code", - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Get custom dictionaries list - * - * @param array $filter - * @param null $limit - * @param null $page - * - * @return WC_Retailcrm_Response - */ - public function customDictionariesList(array $filter = array(), $limit = null, $page = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/custom-fields/dictionaries', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Create custom dictionary - * - * @param $customDictionary - * - * @return WC_Retailcrm_Response - */ - public function customDictionariesCreate($customDictionary) - { - if (!count($customDictionary) || - empty($customDictionary['code']) || - empty($customDictionary['elements']) - ) { - throw new \InvalidArgumentException( - 'Parameter `dictionary` must contain a data & fields `code` & `elemets` must be set' - ); - } - - return $this->client->makeRequest( - "/custom-fields/dictionaries/{$customDictionary['code']}/create", - WC_Retailcrm_Request::METHOD_POST, - array('customDictionary' => json_encode($customDictionary)) - ); - } - - /** - * Edit custom dictionary - * - * @param $customDictionary - * - * @return WC_Retailcrm_Response - */ - public function customDictionariesEdit($customDictionary) - { - if (!count($customDictionary) || - empty($customDictionary['code']) || - empty($customDictionary['elements']) - ) { - throw new \InvalidArgumentException( - 'Parameter `dictionary` must contain a data & fields `code` & `elemets` must be set' - ); - } - - return $this->client->makeRequest( - "/custom-fields/dictionaries/{$customDictionary['code']}/edit", - WC_Retailcrm_Request::METHOD_POST, - array('customDictionary' => json_encode($customDictionary)) - ); - } - - /** - * Get custom dictionary - * - * @param $code - * - * @return WC_Retailcrm_Response - */ - public function customDictionariesGet($code) - { - if (empty($code)) { - throw new \InvalidArgumentException( - 'Parameter `code` must be not empty' - ); - } - - return $this->client->makeRequest( - "/custom-fields/dictionaries/$code", - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Returns filtered orders list - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersList(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/orders', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Create a order - * - * @param array $order order data - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersCreate(array $order, $site = null) - { - if (!count($order)) { - throw new \InvalidArgumentException( - 'Parameter `order` must contains a data' - ); - } - - return $this->client->makeRequest( - '/orders/create', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('order' => json_encode($order))) - ); - } - - /** - * Save order IDs' (id and externalId) association in the CRM - * - * @param array $ids order identificators - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersFixExternalIds(array $ids) - { - if (! count($ids)) { - throw new \InvalidArgumentException( - 'Method parameter must contains at least one IDs pair' - ); - } - - return $this->client->makeRequest( - '/orders/fix-external-ids', - WC_Retailcrm_Request::METHOD_POST, - array('orders' => json_encode($ids) - ) - ); - } - - /** - * Returns statuses of the orders - * - * @param array $ids (default: array()) - * @param array $externalIds (default: array()) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersStatuses(array $ids = array(), array $externalIds = array()) - { - $parameters = array(); - - if (count($ids)) { - $parameters['ids'] = $ids; - } - if (count($externalIds)) { - $parameters['externalIds'] = $externalIds; - } - - return $this->client->makeRequest( - '/orders/statuses', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Upload array of the orders - * - * @param array $orders array of orders - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersUpload(array $orders, $site = null) - { - if (!count($orders)) { - throw new \InvalidArgumentException( - 'Parameter `orders` must contains array of the orders' - ); - } - - return $this->client->makeRequest( - '/orders/upload', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('orders' => json_encode($orders))) - ); - } - - /** - * Get order by id or externalId - * - * @param string $id order identificator - * @param string $by (default: 'externalId') - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersGet($id, $by = 'externalId', $site = null) - { - $this->checkIdParameter($by); - - return $this->client->makeRequest( - "/orders/$id", - WC_Retailcrm_Request::METHOD_GET, - $this->fillSite($site, array('by' => $by)) - ); - } - - /** - * Edit a order - * - * @param array $order order data - * @param string $by (default: 'externalId') - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersEdit(array $order, $by = 'externalId', $site = null) - { - if (!count($order)) { - throw new \InvalidArgumentException( - 'Parameter `order` must contains a data' - ); - } - - $this->checkIdParameter($by); - - if (!array_key_exists($by, $order)) { - throw new \InvalidArgumentException( - sprintf('Order array must contain the "%s" parameter.', $by) - ); - } - - return $this->client->makeRequest( - sprintf('/orders/%s/edit', $order[$by]), - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite( - $site, - array('order' => json_encode($order), 'by' => $by) - ) - ); - } - - /** - * Get orders history - * @param array $filter - * @param null $page - * @param null $limit - * - * @return WC_Retailcrm_Response - */ - public function ordersHistory(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/orders/history', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Combine orders - * - * @param string $technique - * @param array $order - * @param array $resultOrder - * - * @return WC_Retailcrm_Response - */ - public function ordersCombine($order, $resultOrder, $technique = 'ours') - { - $techniques = array('ours', 'summ', 'theirs'); - - if (!count($order) || !count($resultOrder)) { - throw new \InvalidArgumentException( - 'Parameters `order` & `resultOrder` must contains a data' - ); - } - - if (!in_array($technique, $techniques)) { - throw new \InvalidArgumentException( - 'Parameter `technique` must be on of ours|summ|theirs' - ); - } - - return $this->client->makeRequest( - '/orders/combine', - WC_Retailcrm_Request::METHOD_POST, - array( - 'technique' => $technique, - 'order' => json_encode($order), - 'resultOrder' => json_encode($resultOrder) - ) - ); - } - - /** - * Create an order payment - * - * @param array $payment order data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPaymentCreate(array $payment) - { - if (!count($payment)) { - throw new \InvalidArgumentException( - 'Parameter `payment` must contains a data' - ); - } - - return $this->client->makeRequest( - '/orders/payments/create', - WC_Retailcrm_Request::METHOD_POST, - array('payment' => json_encode($payment)) - ); - } - - /** - * Edit an order payment - * - * @param array $payment order data - * @param string $by by key - * @param null $site site code - * - * @return WC_Retailcrm_Response - */ - public function ordersPaymentEdit(array $payment, $by = 'externalId', $site = null) - { - if (!count($payment)) { - throw new \InvalidArgumentException( - 'Parameter `payment` must contains a data' - ); - } - - $this->checkIdParameter($by); - - if (!array_key_exists($by, $payment)) { - throw new \InvalidArgumentException( - sprintf('Order array must contain the "%s" parameter.', $by) - ); - } - - return $this->client->makeRequest( - sprintf('/orders/payments/%s/edit', $payment[$by]), - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite( - $site, - array('payment' => json_encode($payment), 'by' => $by) - ) - ); - } - - /** - * Edit an order payment - * - * @param string $id payment id - * - * @return WC_Retailcrm_Response - */ - public function ordersPaymentDelete($id) - { - if (!$id) { - throw new \InvalidArgumentException( - 'Parameter `id` must be set' - ); - } - - return $this->client->makeRequest( - sprintf('/orders/payments/%s/delete', $id), - WC_Retailcrm_Request::METHOD_POST - ); - } - - /** - * Returns filtered customers list - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersList(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/customers', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Create a customer - * - * @param array $customer customer data - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersCreate(array $customer, $site = null) - { - if (! count($customer)) { - throw new \InvalidArgumentException( - 'Parameter `customer` must contains a data' - ); - } - - return $this->client->makeRequest( - '/customers/create', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('customer' => json_encode($customer))) - ); - } - - /** - * Save customer IDs' (id and externalId) association in the CRM - * - * @param array $ids ids mapping - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersFixExternalIds(array $ids) - { - if (! count($ids)) { - throw new \InvalidArgumentException( - 'Method parameter must contains at least one IDs pair' - ); - } - - return $this->client->makeRequest( - '/customers/fix-external-ids', - WC_Retailcrm_Request::METHOD_POST, - array('customers' => json_encode($ids)) - ); - } - - /** - * Upload array of the customers - * - * @param array $customers array of customers - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersUpload(array $customers, $site = null) - { - if (! count($customers)) { - throw new \InvalidArgumentException( - 'Parameter `customers` must contains array of the customers' - ); - } - - return $this->client->makeRequest( - '/customers/upload', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('customers' => json_encode($customers))) - ); - } - - /** - * Get customer by id or externalId - * - * @param string $id customer identificator - * @param string $by (default: 'externalId') - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersGet($id, $by = 'externalId', $site = null) - { - $this->checkIdParameter($by); - - return $this->client->makeRequest( - "/customers/$id", - WC_Retailcrm_Request::METHOD_GET, - $this->fillSite($site, array('by' => $by)) - ); - } - - /** - * Edit a customer - * - * @param array $customer customer data - * @param string $by (default: 'externalId') - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersEdit(array $customer, $by = 'externalId', $site = null) - { - if (!count($customer)) { - throw new \InvalidArgumentException( - 'Parameter `customer` must contains a data' - ); - } - - $this->checkIdParameter($by); - - if (!array_key_exists($by, $customer)) { - throw new \InvalidArgumentException( - sprintf('Customer array must contain the "%s" parameter.', $by) - ); - } - - return $this->client->makeRequest( - sprintf('/customers/%s/edit', $customer[$by]), - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite( - $site, - array('customer' => json_encode($customer), 'by' => $by) - ) - ); - } - - /** - * Get customers history - * @param array $filter - * @param null $page - * @param null $limit - * - * @return WC_Retailcrm_Response - */ - public function customersHistory(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/customers/history', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Combine customers - * - * @param array $customers - * @param array $resultCustomer - * - * @return WC_Retailcrm_Response - */ - public function customersCombine(array $customers, $resultCustomer) - { - - if (!count($customers) || !count($resultCustomer)) { - throw new \InvalidArgumentException( - 'Parameters `customers` & `resultCustomer` must contains a data' - ); - } - - return $this->client->makeRequest( - '/customers/combine', - WC_Retailcrm_Request::METHOD_POST, - array( - 'customers' => json_encode($customers), - 'resultCustomer' => json_encode($resultCustomer) - ) - ); - } - - /** - * Returns filtered customers notes list - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersNotesList(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - return $this->client->makeRequest( - '/customers/notes', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Create customer note - * - * @param array $note (default: array()) - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersNotesCreate($note, $site = null) - { - if (empty($note['customer']['id']) && empty($note['customer']['externalId'])) { - throw new \InvalidArgumentException( - 'Customer identifier must be set' - ); - } - return $this->client->makeRequest( - '/customers/notes/create', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('note' => json_encode($note))) - ); - } - - /** - * Delete customer note - * - * @param integer $id - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function customersNotesDelete($id) - { - if (empty($id)) { - throw new \InvalidArgumentException( - 'Note id must be set' - ); - } - return $this->client->makeRequest( - "/customers/notes/$id/delete", - WC_Retailcrm_Request::METHOD_POST - ); - } - - /** - * Get orders assembly list - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPacksList(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/orders/packs', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Create orders assembly - * - * @param array $pack pack data - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPacksCreate(array $pack, $site = null) - { - if (!count($pack)) { - throw new \InvalidArgumentException( - 'Parameter `pack` must contains a data' - ); - } - - return $this->client->makeRequest( - '/orders/packs/create', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('pack' => json_encode($pack))) - ); - } - - /** - * Get orders assembly history - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/orders/packs/history', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Get orders assembly by id - * - * @param string $id pack identificator - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPacksGet($id) - { - if (empty($id)) { - throw new \InvalidArgumentException('Parameter `id` must be set'); - } - - return $this->client->makeRequest( - "/orders/packs/$id", - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Delete orders assembly by id - * - * @param string $id pack identificator - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPacksDelete($id) - { - if (empty($id)) { - throw new \InvalidArgumentException('Parameter `id` must be set'); - } - - return $this->client->makeRequest( - sprintf('/orders/packs/%s/delete', $id), - WC_Retailcrm_Request::METHOD_POST - ); - } - - /** - * Edit orders assembly - * - * @param array $pack pack data - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function ordersPacksEdit(array $pack, $site = null) - { - if (!count($pack) || empty($pack['id'])) { - throw new \InvalidArgumentException( - 'Parameter `pack` must contains a data & pack `id` must be set' - ); - } - - return $this->client->makeRequest( - sprintf('/orders/packs/%s/edit', $pack['id']), - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('pack' => json_encode($pack))) - ); - } - - /** - * Get tasks list - * - * @param array $filter - * @param null $limit - * @param null $page - * - * @return WC_Retailcrm_Response - */ - public function tasksList(array $filter = array(), $limit = null, $page = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } + $url = $version == null ? $url . 'api' : $url . 'api/' . $version; - return $this->client->makeRequest( - '/tasks', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Create task - * - * @param array $task - * @param null $site - * - * @return WC_Retailcrm_Response - * - */ - public function tasksCreate($task, $site = null) - { - if (!count($task)) { - throw new \InvalidArgumentException( - 'Parameter `task` must contain a data' - ); - } - - return $this->client->makeRequest( - "/tasks/create", - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite( - $site, - array('task' => json_encode($task)) - ) - ); - } - - /** - * Edit task - * - * @param array $task - * @param null $site - * - * @return WC_Retailcrm_Response - * - */ - public function tasksEdit($task, $site = null) - { - if (!count($task)) { - throw new \InvalidArgumentException( - 'Parameter `task` must contain a data' - ); - } - - return $this->client->makeRequest( - "/tasks/{$task['id']}/edit", - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite( - $site, - array('task' => json_encode($task)) - ) - ); - } - - /** - * Get custom dictionary - * - * @param $id - * - * @return WC_Retailcrm_Response - */ - public function tasksGet($id) - { - if (empty($id)) { - throw new \InvalidArgumentException( - 'Parameter `id` must be not empty' - ); - } - - return $this->client->makeRequest( - "/tasks/$id", - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Get products groups - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function storeProductsGroups(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/store/product-groups', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Get purchace prices & stock balance - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function storeInventories(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/store/inventories', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Get store settings - * - * @param string $code get settings code - * - * @return WC_Retailcrm_Response - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function storeSettingsGet($code) - { - if (empty($code)) { - throw new \InvalidArgumentException('Parameter `code` must be set'); - } - - return $this->client->makeRequest( - "/store/setting/$code", - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit store configuration - * - * @param array $configuration - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function storeSettingsEdit(array $configuration) - { - if (!count($configuration) || empty($configuration['code'])) { - throw new \InvalidArgumentException( - 'Parameter `configuration` must contains a data & configuration `code` must be set' - ); - } - - return $this->client->makeRequest( - sprintf('/store/setting/%s/edit', $configuration['code']), - WC_Retailcrm_Request::METHOD_POST, - $configuration - ); - } - - /** - * Upload store inventories - * - * @param array $offers offers data - * @param string $site (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function storeInventoriesUpload(array $offers, $site = null) - { - if (!count($offers)) { - throw new \InvalidArgumentException( - 'Parameter `offers` must contains array of the offers' - ); - } - - return $this->client->makeRequest( - '/store/inventories/upload', - WC_Retailcrm_Request::METHOD_POST, - $this->fillSite($site, array('offers' => json_encode($offers))) - ); - } - - /** - * Get products - * - * @param array $filter (default: array()) - * @param int $page (default: null) - * @param int $limit (default: null) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function storeProducts(array $filter = array(), $page = null, $limit = null) - { - $parameters = array(); - - if (count($filter)) { - $parameters['filter'] = $filter; - } - if (null !== $page) { - $parameters['page'] = (int) $page; - } - if (null !== $limit) { - $parameters['limit'] = (int) $limit; - } - - return $this->client->makeRequest( - '/store/products', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Get delivery settings - * - * @param string $code - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function deliverySettingsGet($code) - { - if (empty($code)) { - throw new \InvalidArgumentException('Parameter `code` must be set'); - } - - return $this->client->makeRequest( - "/delivery/generic/setting/$code", - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit delivery configuration - * - * @param array $configuration - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function deliverySettingsEdit(array $configuration) - { - if (!count($configuration) || empty($configuration['code'])) { - throw new \InvalidArgumentException( - 'Parameter `configuration` must contains a data & configuration `code` must be set' - ); - } - - return $this->client->makeRequest( - sprintf('/delivery/generic/setting/%s/edit', $configuration['code']), - WC_Retailcrm_Request::METHOD_POST, - array('configuration' => json_encode($configuration)) - ); - } - - /** - * Delivery tracking update - * - * @param string $code - * @param array $statusUpdate - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function deliveryTracking($code, array $statusUpdate) - { - if (empty($code)) { - throw new \InvalidArgumentException('Parameter `code` must be set'); - } - - if (!count($statusUpdate)) { - throw new \InvalidArgumentException( - 'Parameter `statusUpdate` must contains a data' - ); - } - - return $this->client->makeRequest( - sprintf('/delivery/generic/%s/tracking', $code), - WC_Retailcrm_Request::METHOD_POST, - $statusUpdate - ); - } - - /** - * Returns available county list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function countriesList() - { - return $this->client->makeRequest( - '/reference/countries', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Returns deliveryServices list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function deliveryServicesList() - { - return $this->client->makeRequest( - '/reference/delivery-services', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit deliveryService - * - * @param array $data delivery service data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function deliveryServicesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/delivery-services/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('deliveryService' => json_encode($data)) - ); - } - - /** - * Returns deliveryTypes list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function deliveryTypesList() - { - return $this->client->makeRequest( - '/reference/delivery-types', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit deliveryType - * - * @param array $data delivery type data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function deliveryTypesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/delivery-types/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('deliveryType' => json_encode($data)) - ); - } - - /** - * Returns orderMethods list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function orderMethodsList() - { - return $this->client->makeRequest( - '/reference/order-methods', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit orderMethod - * - * @param array $data order method data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function orderMethodsEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/order-methods/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('orderMethod' => json_encode($data)) - ); - } - - /** - * Returns orderTypes list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function orderTypesList() - { - return $this->client->makeRequest( - '/reference/order-types', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit orderType - * - * @param array $data order type data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function orderTypesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/order-types/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('orderType' => json_encode($data)) - ); - } - - /** - * Returns paymentStatuses list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function paymentStatusesList() - { - return $this->client->makeRequest( - '/reference/payment-statuses', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit paymentStatus - * - * @param array $data payment status data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function paymentStatusesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/payment-statuses/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('paymentStatus' => json_encode($data)) - ); - } - - /** - * Returns paymentTypes list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function paymentTypesList() - { - return $this->client->makeRequest( - '/reference/payment-types', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit paymentType - * - * @param array $data payment type data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function paymentTypesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/payment-types/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('paymentType' => json_encode($data)) - ); - } - - /** - * Returns productStatuses list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function productStatusesList() - { - return $this->client->makeRequest( - '/reference/product-statuses', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit productStatus - * - * @param array $data product status data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function productStatusesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/product-statuses/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('productStatus' => json_encode($data)) - ); - } - - /** - * Returns sites list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function sitesList() - { - return $this->client->makeRequest( - '/reference/sites', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit site - * - * @param array $data site data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function sitesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/sites/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('site' => json_encode($data)) - ); - } - - /** - * Returns statusGroups list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function statusGroupsList() - { - return $this->client->makeRequest( - '/reference/status-groups', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Returns statuses list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function statusesList() - { - return $this->client->makeRequest( - '/reference/statuses', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit order status - * - * @param array $data status data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function statusesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/statuses/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('status' => json_encode($data)) - ); - } - - /** - * Returns stores list - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function storesList() - { - return $this->client->makeRequest( - '/reference/stores', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit store - * - * @param array $data site data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function storesEdit(array $data) - { - if (!array_key_exists('code', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "code" parameter.' - ); - } - - if (!array_key_exists('name', $data)) { - throw new \InvalidArgumentException( - 'Data must contain "name" parameter.' - ); - } - - return $this->client->makeRequest( - sprintf('/reference/stores/%s/edit', $data['code']), - WC_Retailcrm_Request::METHOD_POST, - array('store' => json_encode($data)) - ); - } - - /** - * Get telephony settings - * - * @param string $code - * - * @throws WC_Retailcrm_Exception_Json - * @throws WC_Retailcrm_Exception_Curl - * @throws \InvalidArgumentException - * - * @return WC_Retailcrm_Response - */ - public function telephonySettingsGet($code) - { - if (empty($code)) { - throw new \InvalidArgumentException('Parameter `code` must be set'); - } - - return $this->client->makeRequest( - "/telephony/setting/$code", - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Edit telephony settings - * - * @param string $code symbolic code - * @param string $clientId client id - * @param boolean $active telephony activity - * @param mixed $name service name - * @param mixed $makeCallUrl service init url - * @param mixed $image service logo url(svg file) - * - * @param array $additionalCodes - * @param array $externalPhones - * @param bool $allowEdit - * @param bool $inputEventSupported - * @param bool $outputEventSupported - * @param bool $hangupEventSupported - * @param bool $changeUserStatusUrl - * - * @return WC_Retailcrm_Response - */ - public function telephonySettingsEdit( - $code, - $clientId, - $active = false, - $name = false, - $makeCallUrl = false, - $image = false, - $additionalCodes = array(), - $externalPhones = array(), - $allowEdit = false, - $inputEventSupported = false, - $outputEventSupported = false, - $hangupEventSupported = false, - $changeUserStatusUrl = false - ) - { - if (!isset($code)) { - throw new \InvalidArgumentException('Code must be set'); - } - - $parameters['code'] = $code; - - if (!isset($clientId)) { - throw new \InvalidArgumentException('client id must be set'); - } - - $parameters['clientId'] = $clientId; - - if (!isset($active)) { - $parameters['active'] = false; - } else { - $parameters['active'] = $active; - } - - if (!isset($name)) { - throw new \InvalidArgumentException('name must be set'); - } - - if (isset($name)) { - $parameters['name'] = $name; - } - - if (isset($makeCallUrl)) { - $parameters['makeCallUrl'] = $makeCallUrl; - } - - if (isset($image)) { - $parameters['image'] = $image; - } - - if (isset($additionalCodes)) { - $parameters['additionalCodes'] = $additionalCodes; - } - - if (isset($externalPhones)) { - $parameters['externalPhones'] = $externalPhones; - } - - if (isset($allowEdit)) { - $parameters['allowEdit'] = $allowEdit; - } - - if (isset($inputEventSupported)) { - $parameters['inputEventSupported'] = $inputEventSupported; - } - - if (isset($outputEventSupported)) { - $parameters['outputEventSupported'] = $outputEventSupported; - } - - if (isset($hangupEventSupported)) { - $parameters['hangupEventSupported'] = $hangupEventSupported; - } - - if (isset($changeUserStatusUrl)) { - $parameters['changeUserStatusUrl'] = $changeUserStatusUrl; - } - - return $this->client->makeRequest( - "/telephony/setting/$code/edit", - WC_Retailcrm_Request::METHOD_POST, - array('configuration' => json_encode($parameters)) - ); - } - - /** - * Call event - * - * @param string $phone phone number - * @param string $type call type - * @param array $codes - * @param string $hangupStatus - * @param string $externalPhone - * @param array $webAnalyticsData - * - * @return WC_Retailcrm_Response - * @internal param string $code additional phone code - * @internal param string $status call status - * - */ - public function telephonyCallEvent( - $phone, - $type, - $codes, - $hangupStatus, - $externalPhone = null, - $webAnalyticsData = array() - ) - { - if (!isset($phone)) { - throw new \InvalidArgumentException('Phone number must be set'); - } - - if (!isset($type)) { - throw new \InvalidArgumentException('Type must be set (in|out|hangup)'); - } - - if (empty($codes)) { - throw new \InvalidArgumentException('Codes array must be set'); - } - - $parameters['phone'] = $phone; - $parameters['type'] = $type; - $parameters['codes'] = $codes; - $parameters['hangupStatus'] = $hangupStatus; - $parameters['callExternalId'] = $externalPhone; - $parameters['webAnalyticsData'] = $webAnalyticsData; - - - return $this->client->makeRequest( - '/telephony/call/event', - WC_Retailcrm_Request::METHOD_POST, - array('event' => json_encode($parameters)) - ); - } - - /** - * Upload calls - * - * @param array $calls calls data - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function telephonyCallsUpload(array $calls) - { - if (!count($calls)) { - throw new \InvalidArgumentException( - 'Parameter `calls` must contains array of the calls' - ); - } - - return $this->client->makeRequest( - '/telephony/calls/upload', - WC_Retailcrm_Request::METHOD_POST, - array('calls' => json_encode($calls)) - ); - } - - /** - * Get call manager - * - * @param string $phone phone number - * @param bool $details detailed information - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function telephonyCallManager($phone, $details) - { - if (!isset($phone)) { - throw new \InvalidArgumentException('Phone number must be set'); - } - - $parameters['phone'] = $phone; - $parameters['details'] = isset($details) ? $details : 0; - - return $this->client->makeRequest( - '/telephony/manager', - WC_Retailcrm_Request::METHOD_GET, - $parameters - ); - } - - /** - * Update CRM basic statistic - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * @throws WC_Retailcrm_Exception_Json - * - * @return WC_Retailcrm_Response - */ - public function statisticUpdate() - { - return $this->client->makeRequest( - '/statistic/update', - WC_Retailcrm_Request::METHOD_GET - ); - } - - /** - * Return current site - * - * @return string - */ - public function getSite() - { - return $this->siteCode; - } - - /** - * Set site - * - * @param string $site site code - * - * @return void - */ - public function setSite($site) - { - $this->siteCode = $site; - } - - /** - * Check ID parameter - * - * @param string $by identify by - * - * @throws \InvalidArgumentException - * - * @return bool - */ - protected function checkIdParameter($by) - { - $allowedForBy = array( - 'externalId', - 'id' - ); - - if (!in_array($by, $allowedForBy, false)) { - throw new \InvalidArgumentException( - sprintf( - 'Value "%s" for "by" param is not valid. Allowed values are %s.', - $by, - implode(', ', $allowedForBy) - ) - ); - } - - return true; - } - - /** - * Fill params by site value - * - * @param string $site site code - * @param array $params input parameters - * - * @return array - */ - protected function fillSite($site, array $params) - { - if ($site) { - $params['site'] = $site; - } elseif ($this->siteCode) { - $params['site'] = $this->siteCode; - } - - return $params; - } + $this->client = new WC_Retailcrm_Request($url, array('apiKey' => $apiKey)); + $this->siteCode = $site; } + + /** + * Returns api versions list + * + * @return WC_Retailcrm_Response + */ + public function apiVersions() + { + return $this->client->makeRequest('/api-versions', WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Returns users list + * + * @param array $filter + * @param null $page + * @param null $limit + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function usersList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/users', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Returns user data + * + * @param integer $id user ID + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function usersGet($id) + { + return $this->client->makeRequest("/users/$id", WC_Retailcrm_Request::METHOD_GET); + } + + /** + * Change user status + * + * @param integer $id user ID + * @param string $status user status + * + * @return WC_Retailcrm_Response + */ + public function usersStatus($id, $status) + { + $statuses = array("free", "busy", "dinner", "break"); + + if (empty($status) || !in_array($status, $statuses)) { + throw new \InvalidArgumentException( + 'Parameter `status` must be not empty & must be equal one of these values: free|busy|dinner|break' + ); + } + + return $this->client->makeRequest( + "/users/$id/status", + WC_Retailcrm_Request::METHOD_POST, + array('status' => $status) + ); + } + + /** + * Get segments list + * + * @param array $filter + * @param null $limit + * @param null $page + * + * @return WC_Retailcrm_Response + */ + public function segmentsList(array $filter = array(), $limit = null, $page = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/segments', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get custom fields list + * + * @param array $filter + * @param null $limit + * @param null $page + * + * @return WC_Retailcrm_Response + */ + public function customFieldsList(array $filter = array(), $limit = null, $page = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/custom-fields', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create custom field + * + * @param $entity + * @param $customField + * + * @return WC_Retailcrm_Response + */ + public function customFieldsCreate($entity, $customField) + { + if (!count($customField) || + empty($customField['code']) || + empty($customField['name']) || + empty($customField['type']) + ) { + throw new \InvalidArgumentException( + 'Parameter `customField` must contain a data & fields `code`, `name` & `type` must be set' + ); + } + + if (empty($entity) || $entity != 'customer' || $entity != 'order') { + throw new \InvalidArgumentException( + 'Parameter `entity` must contain a data & value must be `order` or `customer`' + ); + } + + return $this->client->makeRequest( + "/custom-fields/$entity/create", + WC_Retailcrm_Request::METHOD_POST, + array('customField' => json_encode($customField)) + ); + } + + /** + * Edit custom field + * + * @param $entity + * @param $customField + * + * @return WC_Retailcrm_Response + */ + public function customFieldsEdit($entity, $customField) + { + if (!count($customField) || empty($customField['code'])) { + throw new \InvalidArgumentException( + 'Parameter `customField` must contain a data & fields `code` must be set' + ); + } + + if (empty($entity) || $entity != 'customer' || $entity != 'order') { + throw new \InvalidArgumentException( + 'Parameter `entity` must contain a data & value must be `order` or `customer`' + ); + } + + return $this->client->makeRequest( + "/custom-fields/$entity/edit/{$customField['code']}", + WC_Retailcrm_Request::METHOD_POST, + array('customField' => json_encode($customField)) + ); + } + + /** + * Get custom field + * + * @param $entity + * @param $code + * + * @return WC_Retailcrm_Response + */ + public function customFieldsGet($entity, $code) + { + if (empty($code)) { + throw new \InvalidArgumentException( + 'Parameter `code` must be not empty' + ); + } + + if (empty($entity) || $entity != 'customer' || $entity != 'order') { + throw new \InvalidArgumentException( + 'Parameter `entity` must contain a data & value must be `order` or `customer`' + ); + } + + return $this->client->makeRequest( + "/custom-fields/$entity/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Get custom dictionaries list + * + * @param array $filter + * @param null $limit + * @param null $page + * + * @return WC_Retailcrm_Response + */ + public function customDictionariesList(array $filter = array(), $limit = null, $page = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/custom-fields/dictionaries', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create custom dictionary + * + * @param $customDictionary + * + * @return WC_Retailcrm_Response + */ + public function customDictionariesCreate($customDictionary) + { + if (!count($customDictionary) || + empty($customDictionary['code']) || + empty($customDictionary['elements']) + ) { + throw new \InvalidArgumentException( + 'Parameter `dictionary` must contain a data & fields `code` & `elemets` must be set' + ); + } + + return $this->client->makeRequest( + "/custom-fields/dictionaries/{$customDictionary['code']}/create", + WC_Retailcrm_Request::METHOD_POST, + array('customDictionary' => json_encode($customDictionary)) + ); + } + + /** + * Edit custom dictionary + * + * @param $customDictionary + * + * @return WC_Retailcrm_Response + */ + public function customDictionariesEdit($customDictionary) + { + if (!count($customDictionary) || + empty($customDictionary['code']) || + empty($customDictionary['elements']) + ) { + throw new \InvalidArgumentException( + 'Parameter `dictionary` must contain a data & fields `code` & `elemets` must be set' + ); + } + + return $this->client->makeRequest( + "/custom-fields/dictionaries/{$customDictionary['code']}/edit", + WC_Retailcrm_Request::METHOD_POST, + array('customDictionary' => json_encode($customDictionary)) + ); + } + + /** + * Get custom dictionary + * + * @param $code + * + * @return WC_Retailcrm_Response + */ + public function customDictionariesGet($code) + { + if (empty($code)) { + throw new \InvalidArgumentException( + 'Parameter `code` must be not empty' + ); + } + + return $this->client->makeRequest( + "/custom-fields/dictionaries/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Returns filtered orders list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/orders', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create a order + * + * @param array $order order data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersCreate(array $order, $site = null) + { + if (!count($order)) { + throw new \InvalidArgumentException( + 'Parameter `order` must contains a data' + ); + } + + return $this->client->makeRequest( + '/orders/create', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('order' => json_encode($order))) + ); + } + + /** + * Save order IDs' (id and externalId) association in the CRM + * + * @param array $ids order identificators + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersFixExternalIds(array $ids) + { + if (! count($ids)) { + throw new \InvalidArgumentException( + 'Method parameter must contains at least one IDs pair' + ); + } + + return $this->client->makeRequest( + '/orders/fix-external-ids', + WC_Retailcrm_Request::METHOD_POST, + array('orders' => json_encode($ids) + ) + ); + } + + /** + * Returns statuses of the orders + * + * @param array $ids (default: array()) + * @param array $externalIds (default: array()) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersStatuses(array $ids = array(), array $externalIds = array()) + { + $parameters = array(); + + if (count($ids)) { + $parameters['ids'] = $ids; + } + if (count($externalIds)) { + $parameters['externalIds'] = $externalIds; + } + + return $this->client->makeRequest( + '/orders/statuses', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Upload array of the orders + * + * @param array $orders array of orders + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersUpload(array $orders, $site = null) + { + if (!count($orders)) { + throw new \InvalidArgumentException( + 'Parameter `orders` must contains array of the orders' + ); + } + + return $this->client->makeRequest( + '/orders/upload', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('orders' => json_encode($orders))) + ); + } + + /** + * Get order by id or externalId + * + * @param string $id order identificator + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersGet($id, $by = 'externalId', $site = null) + { + $this->checkIdParameter($by); + + return $this->client->makeRequest( + "/orders/$id", + WC_Retailcrm_Request::METHOD_GET, + $this->fillSite($site, array('by' => $by)) + ); + } + + /** + * Edit a order + * + * @param array $order order data + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersEdit(array $order, $by = 'externalId', $site = null) + { + if (!count($order)) { + throw new \InvalidArgumentException( + 'Parameter `order` must contains a data' + ); + } + + $this->checkIdParameter($by); + + if (!array_key_exists($by, $order)) { + throw new \InvalidArgumentException( + sprintf('Order array must contain the "%s" parameter.', $by) + ); + } + + return $this->client->makeRequest( + sprintf('/orders/%s/edit', $order[$by]), + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array('order' => json_encode($order), 'by' => $by) + ) + ); + } + + /** + * Get orders history + * @param array $filter + * @param null $page + * @param null $limit + * + * @return WC_Retailcrm_Response + */ + public function ordersHistory(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/orders/history', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Combine orders + * + * @param string $technique + * @param array $order + * @param array $resultOrder + * + * @return WC_Retailcrm_Response + */ + public function ordersCombine($order, $resultOrder, $technique = 'ours') + { + $techniques = array('ours', 'summ', 'theirs'); + + if (!count($order) || !count($resultOrder)) { + throw new \InvalidArgumentException( + 'Parameters `order` & `resultOrder` must contains a data' + ); + } + + if (!in_array($technique, $techniques)) { + throw new \InvalidArgumentException( + 'Parameter `technique` must be on of ours|summ|theirs' + ); + } + + return $this->client->makeRequest( + '/orders/combine', + WC_Retailcrm_Request::METHOD_POST, + array( + 'technique' => $technique, + 'order' => json_encode($order), + 'resultOrder' => json_encode($resultOrder) + ) + ); + } + + /** + * Create an order payment + * + * @param array $payment order data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPaymentCreate(array $payment) + { + if (!count($payment)) { + throw new \InvalidArgumentException( + 'Parameter `payment` must contains a data' + ); + } + + return $this->client->makeRequest( + '/orders/payments/create', + WC_Retailcrm_Request::METHOD_POST, + array('payment' => json_encode($payment)) + ); + } + + /** + * Edit an order payment + * + * @param array $payment order data + * @param string $by by key + * @param null $site site code + * + * @return WC_Retailcrm_Response + */ + public function ordersPaymentEdit(array $payment, $by = 'externalId', $site = null) + { + if (!count($payment)) { + throw new \InvalidArgumentException( + 'Parameter `payment` must contains a data' + ); + } + + $this->checkIdParameter($by); + + if (!array_key_exists($by, $payment)) { + throw new \InvalidArgumentException( + sprintf('Order array must contain the "%s" parameter.', $by) + ); + } + + return $this->client->makeRequest( + sprintf('/orders/payments/%s/edit', $payment[$by]), + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array('payment' => json_encode($payment), 'by' => $by) + ) + ); + } + + /** + * Edit an order payment + * + * @param string $id payment id + * + * @return WC_Retailcrm_Response + */ + public function ordersPaymentDelete($id) + { + if (!$id) { + throw new \InvalidArgumentException( + 'Parameter `id` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/orders/payments/%s/delete', $id), + WC_Retailcrm_Request::METHOD_POST + ); + } + + /** + * Returns filtered customers list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/customers', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create a customer + * + * @param array $customer customer data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersCreate(array $customer, $site = null) + { + if (! count($customer)) { + throw new \InvalidArgumentException( + 'Parameter `customer` must contains a data' + ); + } + + return $this->client->makeRequest( + '/customers/create', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('customer' => json_encode($customer))) + ); + } + + /** + * Save customer IDs' (id and externalId) association in the CRM + * + * @param array $ids ids mapping + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersFixExternalIds(array $ids) + { + if (! count($ids)) { + throw new \InvalidArgumentException( + 'Method parameter must contains at least one IDs pair' + ); + } + + return $this->client->makeRequest( + '/customers/fix-external-ids', + WC_Retailcrm_Request::METHOD_POST, + array('customers' => json_encode($ids)) + ); + } + + /** + * Upload array of the customers + * + * @param array $customers array of customers + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersUpload(array $customers, $site = null) + { + if (! count($customers)) { + throw new \InvalidArgumentException( + 'Parameter `customers` must contains array of the customers' + ); + } + + return $this->client->makeRequest( + '/customers/upload', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('customers' => json_encode($customers))) + ); + } + + /** + * Get customer by id or externalId + * + * @param string $id customer identificator + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersGet($id, $by = 'externalId', $site = null) + { + $this->checkIdParameter($by); + + return $this->client->makeRequest( + "/customers/$id", + WC_Retailcrm_Request::METHOD_GET, + $this->fillSite($site, array('by' => $by)) + ); + } + + /** + * Edit a customer + * + * @param array $customer customer data + * @param string $by (default: 'externalId') + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersEdit(array $customer, $by = 'externalId', $site = null) + { + if (!count($customer)) { + throw new \InvalidArgumentException( + 'Parameter `customer` must contains a data' + ); + } + + $this->checkIdParameter($by); + + if (!array_key_exists($by, $customer)) { + throw new \InvalidArgumentException( + sprintf('Customer array must contain the "%s" parameter.', $by) + ); + } + + return $this->client->makeRequest( + sprintf('/customers/%s/edit', $customer[$by]), + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array('customer' => json_encode($customer), 'by' => $by) + ) + ); + } + + /** + * Get customers history + * @param array $filter + * @param null $page + * @param null $limit + * + * @return WC_Retailcrm_Response + */ + public function customersHistory(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/customers/history', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Combine customers + * + * @param array $customers + * @param array $resultCustomer + * + * @return WC_Retailcrm_Response + */ + public function customersCombine(array $customers, $resultCustomer) + { + + if (!count($customers) || !count($resultCustomer)) { + throw new \InvalidArgumentException( + 'Parameters `customers` & `resultCustomer` must contains a data' + ); + } + + return $this->client->makeRequest( + '/customers/combine', + WC_Retailcrm_Request::METHOD_POST, + array( + 'customers' => json_encode($customers), + 'resultCustomer' => json_encode($resultCustomer) + ) + ); + } + + /** + * Returns filtered customers notes list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersNotesList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + return $this->client->makeRequest( + '/customers/notes', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create customer note + * + * @param array $note (default: array()) + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersNotesCreate($note, $site = null) + { + if (empty($note['customer']['id']) && empty($note['customer']['externalId'])) { + throw new \InvalidArgumentException( + 'Customer identifier must be set' + ); + } + return $this->client->makeRequest( + '/customers/notes/create', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('note' => json_encode($note))) + ); + } + + /** + * Delete customer note + * + * @param integer $id + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function customersNotesDelete($id) + { + if (empty($id)) { + throw new \InvalidArgumentException( + 'Note id must be set' + ); + } + return $this->client->makeRequest( + "/customers/notes/$id/delete", + WC_Retailcrm_Request::METHOD_POST + ); + } + + /** + * Get orders assembly list + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksList(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/orders/packs', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create orders assembly + * + * @param array $pack pack data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksCreate(array $pack, $site = null) + { + if (!count($pack)) { + throw new \InvalidArgumentException( + 'Parameter `pack` must contains a data' + ); + } + + return $this->client->makeRequest( + '/orders/packs/create', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('pack' => json_encode($pack))) + ); + } + + /** + * Get orders assembly history + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksHistory(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/orders/packs/history', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get orders assembly by id + * + * @param string $id pack identificator + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksGet($id) + { + if (empty($id)) { + throw new \InvalidArgumentException('Parameter `id` must be set'); + } + + return $this->client->makeRequest( + "/orders/packs/$id", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Delete orders assembly by id + * + * @param string $id pack identificator + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksDelete($id) + { + if (empty($id)) { + throw new \InvalidArgumentException('Parameter `id` must be set'); + } + + return $this->client->makeRequest( + sprintf('/orders/packs/%s/delete', $id), + WC_Retailcrm_Request::METHOD_POST + ); + } + + /** + * Edit orders assembly + * + * @param array $pack pack data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function ordersPacksEdit(array $pack, $site = null) + { + if (!count($pack) || empty($pack['id'])) { + throw new \InvalidArgumentException( + 'Parameter `pack` must contains a data & pack `id` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/orders/packs/%s/edit', $pack['id']), + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('pack' => json_encode($pack))) + ); + } + + /** + * Get tasks list + * + * @param array $filter + * @param null $limit + * @param null $page + * + * @return WC_Retailcrm_Response + */ + public function tasksList(array $filter = array(), $limit = null, $page = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/tasks', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Create task + * + * @param array $task + * @param null $site + * + * @return WC_Retailcrm_Response + * + */ + public function tasksCreate($task, $site = null) + { + if (!count($task)) { + throw new \InvalidArgumentException( + 'Parameter `task` must contain a data' + ); + } + + return $this->client->makeRequest( + "/tasks/create", + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array('task' => json_encode($task)) + ) + ); + } + + /** + * Edit task + * + * @param array $task + * @param null $site + * + * @return WC_Retailcrm_Response + * + */ + public function tasksEdit($task, $site = null) + { + if (!count($task)) { + throw new \InvalidArgumentException( + 'Parameter `task` must contain a data' + ); + } + + return $this->client->makeRequest( + "/tasks/{$task['id']}/edit", + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite( + $site, + array('task' => json_encode($task)) + ) + ); + } + + /** + * Get custom dictionary + * + * @param $id + * + * @return WC_Retailcrm_Response + */ + public function tasksGet($id) + { + if (empty($id)) { + throw new \InvalidArgumentException( + 'Parameter `id` must be not empty' + ); + } + + return $this->client->makeRequest( + "/tasks/$id", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Get products groups + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storeProductsGroups(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/store/product-groups', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get purchace prices & stock balance + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storeInventories(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/store/inventories', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get store settings + * + * @param string $code get settings code + * + * @return WC_Retailcrm_Response + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function storeSettingsGet($code) + { + if (empty($code)) { + throw new \InvalidArgumentException('Parameter `code` must be set'); + } + + return $this->client->makeRequest( + "/store/setting/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit store configuration + * + * @param array $configuration + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function storeSettingsEdit(array $configuration) + { + if (!count($configuration) || empty($configuration['code'])) { + throw new \InvalidArgumentException( + 'Parameter `configuration` must contains a data & configuration `code` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/store/setting/%s/edit', $configuration['code']), + WC_Retailcrm_Request::METHOD_POST, + $configuration + ); + } + + /** + * Upload store inventories + * + * @param array $offers offers data + * @param string $site (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storeInventoriesUpload(array $offers, $site = null) + { + if (!count($offers)) { + throw new \InvalidArgumentException( + 'Parameter `offers` must contains array of the offers' + ); + } + + return $this->client->makeRequest( + '/store/inventories/upload', + WC_Retailcrm_Request::METHOD_POST, + $this->fillSite($site, array('offers' => json_encode($offers))) + ); + } + + /** + * Get products + * + * @param array $filter (default: array()) + * @param int $page (default: null) + * @param int $limit (default: null) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storeProducts(array $filter = array(), $page = null, $limit = null) + { + $parameters = array(); + + if (count($filter)) { + $parameters['filter'] = $filter; + } + if (null !== $page) { + $parameters['page'] = (int) $page; + } + if (null !== $limit) { + $parameters['limit'] = (int) $limit; + } + + return $this->client->makeRequest( + '/store/products', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Get delivery settings + * + * @param string $code + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliverySettingsGet($code) + { + if (empty($code)) { + throw new \InvalidArgumentException('Parameter `code` must be set'); + } + + return $this->client->makeRequest( + "/delivery/generic/setting/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit delivery configuration + * + * @param array $configuration + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function deliverySettingsEdit(array $configuration) + { + if (!count($configuration) || empty($configuration['code'])) { + throw new \InvalidArgumentException( + 'Parameter `configuration` must contains a data & configuration `code` must be set' + ); + } + + return $this->client->makeRequest( + sprintf('/delivery/generic/setting/%s/edit', $configuration['code']), + WC_Retailcrm_Request::METHOD_POST, + array('configuration' => json_encode($configuration)) + ); + } + + /** + * Delivery tracking update + * + * @param string $code + * @param array $statusUpdate + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function deliveryTracking($code, array $statusUpdate) + { + if (empty($code)) { + throw new \InvalidArgumentException('Parameter `code` must be set'); + } + + if (!count($statusUpdate)) { + throw new \InvalidArgumentException( + 'Parameter `statusUpdate` must contains a data' + ); + } + + return $this->client->makeRequest( + sprintf('/delivery/generic/%s/tracking', $code), + WC_Retailcrm_Request::METHOD_POST, + $statusUpdate + ); + } + + /** + * Returns available county list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function countriesList() + { + return $this->client->makeRequest( + '/reference/countries', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Returns deliveryServices list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliveryServicesList() + { + return $this->client->makeRequest( + '/reference/delivery-services', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit deliveryService + * + * @param array $data delivery service data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliveryServicesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/delivery-services/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('deliveryService' => json_encode($data)) + ); + } + + /** + * Returns deliveryTypes list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliveryTypesList() + { + return $this->client->makeRequest( + '/reference/delivery-types', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit deliveryType + * + * @param array $data delivery type data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function deliveryTypesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/delivery-types/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('deliveryType' => json_encode($data)) + ); + } + + /** + * Returns orderMethods list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function orderMethodsList() + { + return $this->client->makeRequest( + '/reference/order-methods', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit orderMethod + * + * @param array $data order method data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function orderMethodsEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/order-methods/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('orderMethod' => json_encode($data)) + ); + } + + /** + * Returns orderTypes list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function orderTypesList() + { + return $this->client->makeRequest( + '/reference/order-types', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit orderType + * + * @param array $data order type data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function orderTypesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/order-types/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('orderType' => json_encode($data)) + ); + } + + /** + * Returns paymentStatuses list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function paymentStatusesList() + { + return $this->client->makeRequest( + '/reference/payment-statuses', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit paymentStatus + * + * @param array $data payment status data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function paymentStatusesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/payment-statuses/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('paymentStatus' => json_encode($data)) + ); + } + + /** + * Returns paymentTypes list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function paymentTypesList() + { + return $this->client->makeRequest( + '/reference/payment-types', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit paymentType + * + * @param array $data payment type data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function paymentTypesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/payment-types/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('paymentType' => json_encode($data)) + ); + } + + /** + * Returns productStatuses list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function productStatusesList() + { + return $this->client->makeRequest( + '/reference/product-statuses', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit productStatus + * + * @param array $data product status data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function productStatusesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/product-statuses/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('productStatus' => json_encode($data)) + ); + } + + /** + * Returns sites list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function sitesList() + { + return $this->client->makeRequest( + '/reference/sites', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit site + * + * @param array $data site data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function sitesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/sites/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('site' => json_encode($data)) + ); + } + + /** + * Returns statusGroups list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function statusGroupsList() + { + return $this->client->makeRequest( + '/reference/status-groups', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Returns statuses list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function statusesList() + { + return $this->client->makeRequest( + '/reference/statuses', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit order status + * + * @param array $data status data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function statusesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/statuses/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('status' => json_encode($data)) + ); + } + + /** + * Returns stores list + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storesList() + { + return $this->client->makeRequest( + '/reference/stores', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit store + * + * @param array $data site data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function storesEdit(array $data) + { + if (!array_key_exists('code', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "code" parameter.' + ); + } + + if (!array_key_exists('name', $data)) { + throw new \InvalidArgumentException( + 'Data must contain "name" parameter.' + ); + } + + return $this->client->makeRequest( + sprintf('/reference/stores/%s/edit', $data['code']), + WC_Retailcrm_Request::METHOD_POST, + array('store' => json_encode($data)) + ); + } + + /** + * Get telephony settings + * + * @param string $code + * + * @throws WC_Retailcrm_Exception_Json + * @throws WC_Retailcrm_Exception_Curl + * @throws \InvalidArgumentException + * + * @return WC_Retailcrm_Response + */ + public function telephonySettingsGet($code) + { + if (empty($code)) { + throw new \InvalidArgumentException('Parameter `code` must be set'); + } + + return $this->client->makeRequest( + "/telephony/setting/$code", + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Edit telephony settings + * + * @param string $code symbolic code + * @param string $clientId client id + * @param boolean $active telephony activity + * @param mixed $name service name + * @param mixed $makeCallUrl service init url + * @param mixed $image service logo url(svg file) + * + * @param array $additionalCodes + * @param array $externalPhones + * @param bool $allowEdit + * @param bool $inputEventSupported + * @param bool $outputEventSupported + * @param bool $hangupEventSupported + * @param bool $changeUserStatusUrl + * + * @return WC_Retailcrm_Response + */ + public function telephonySettingsEdit( + $code, + $clientId, + $active = false, + $name = false, + $makeCallUrl = false, + $image = false, + $additionalCodes = array(), + $externalPhones = array(), + $allowEdit = false, + $inputEventSupported = false, + $outputEventSupported = false, + $hangupEventSupported = false, + $changeUserStatusUrl = false + ) + { + if (!isset($code)) { + throw new \InvalidArgumentException('Code must be set'); + } + + $parameters['code'] = $code; + + if (!isset($clientId)) { + throw new \InvalidArgumentException('client id must be set'); + } + + $parameters['clientId'] = $clientId; + + if (!isset($active)) { + $parameters['active'] = false; + } else { + $parameters['active'] = $active; + } + + if (!isset($name)) { + throw new \InvalidArgumentException('name must be set'); + } + + if (isset($name)) { + $parameters['name'] = $name; + } + + if (isset($makeCallUrl)) { + $parameters['makeCallUrl'] = $makeCallUrl; + } + + if (isset($image)) { + $parameters['image'] = $image; + } + + if (isset($additionalCodes)) { + $parameters['additionalCodes'] = $additionalCodes; + } + + if (isset($externalPhones)) { + $parameters['externalPhones'] = $externalPhones; + } + + if (isset($allowEdit)) { + $parameters['allowEdit'] = $allowEdit; + } + + if (isset($inputEventSupported)) { + $parameters['inputEventSupported'] = $inputEventSupported; + } + + if (isset($outputEventSupported)) { + $parameters['outputEventSupported'] = $outputEventSupported; + } + + if (isset($hangupEventSupported)) { + $parameters['hangupEventSupported'] = $hangupEventSupported; + } + + if (isset($changeUserStatusUrl)) { + $parameters['changeUserStatusUrl'] = $changeUserStatusUrl; + } + + return $this->client->makeRequest( + "/telephony/setting/$code/edit", + WC_Retailcrm_Request::METHOD_POST, + array('configuration' => json_encode($parameters)) + ); + } + + /** + * Call event + * + * @param string $phone phone number + * @param string $type call type + * @param array $codes + * @param string $hangupStatus + * @param string $externalPhone + * @param array $webAnalyticsData + * + * @return WC_Retailcrm_Response + * @internal param string $code additional phone code + * @internal param string $status call status + * + */ + public function telephonyCallEvent( + $phone, + $type, + $codes, + $hangupStatus, + $externalPhone = null, + $webAnalyticsData = array() + ) + { + if (!isset($phone)) { + throw new \InvalidArgumentException('Phone number must be set'); + } + + if (!isset($type)) { + throw new \InvalidArgumentException('Type must be set (in|out|hangup)'); + } + + if (empty($codes)) { + throw new \InvalidArgumentException('Codes array must be set'); + } + + $parameters['phone'] = $phone; + $parameters['type'] = $type; + $parameters['codes'] = $codes; + $parameters['hangupStatus'] = $hangupStatus; + $parameters['callExternalId'] = $externalPhone; + $parameters['webAnalyticsData'] = $webAnalyticsData; + + + return $this->client->makeRequest( + '/telephony/call/event', + WC_Retailcrm_Request::METHOD_POST, + array('event' => json_encode($parameters)) + ); + } + + /** + * Upload calls + * + * @param array $calls calls data + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function telephonyCallsUpload(array $calls) + { + if (!count($calls)) { + throw new \InvalidArgumentException( + 'Parameter `calls` must contains array of the calls' + ); + } + + return $this->client->makeRequest( + '/telephony/calls/upload', + WC_Retailcrm_Request::METHOD_POST, + array('calls' => json_encode($calls)) + ); + } + + /** + * Get call manager + * + * @param string $phone phone number + * @param bool $details detailed information + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function telephonyCallManager($phone, $details) + { + if (!isset($phone)) { + throw new \InvalidArgumentException('Phone number must be set'); + } + + $parameters['phone'] = $phone; + $parameters['details'] = isset($details) ? $details : 0; + + return $this->client->makeRequest( + '/telephony/manager', + WC_Retailcrm_Request::METHOD_GET, + $parameters + ); + } + + /** + * Update CRM basic statistic + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * @throws WC_Retailcrm_Exception_Json + * + * @return WC_Retailcrm_Response + */ + public function statisticUpdate() + { + return $this->client->makeRequest( + '/statistic/update', + WC_Retailcrm_Request::METHOD_GET + ); + } + + /** + * Return current site + * + * @return string + */ + public function getSite() + { + return $this->siteCode; + } + + /** + * Set site + * + * @param string $site site code + * + * @return void + */ + public function setSite($site) + { + $this->siteCode = $site; + } + + /** + * Check ID parameter + * + * @param string $by identify by + * + * @throws \InvalidArgumentException + * + * @return bool + */ + protected function checkIdParameter($by) + { + $allowedForBy = array( + 'externalId', + 'id' + ); + + if (!in_array($by, $allowedForBy, false)) { + throw new \InvalidArgumentException( + sprintf( + 'Value "%s" for "by" param is not valid. Allowed values are %s.', + $by, + implode(', ', $allowedForBy) + ) + ); + } + + return true; + } + + /** + * Fill params by site value + * + * @param string $site site code + * @param array $params input parameters + * + * @return array + */ + protected function fillSite($site, array $params) + { + if ($site) { + $params['site'] = $site; + } elseif ($this->siteCode) { + $params['site'] = $this->siteCode; + } + + return $params; + } +} diff --git a/woo-retailcrm/include/api/class-wc-retailcrm-exception-curl.php b/woo-retailcrm/include/api/class-wc-retailcrm-exception-curl.php index 9d741e1..177cd3b 100644 --- a/woo-retailcrm/include/api/class-wc-retailcrm-exception-curl.php +++ b/woo-retailcrm/include/api/class-wc-retailcrm-exception-curl.php @@ -1,16 +1,16 @@ - * @license https://opensource.org/licenses/MIT MIT License - * @link http://retailcrm.ru/docs/Developers/ApiVersion4 - */ - class WC_Retailcrm_Exception_Curl extends \RuntimeException - { - } +/** + * PHP version 5.3 + * + * WC_Retailcrm_Exception_Curl class + * + * @category RetailCRM + * @package WC_Retailcrm_Exception_Curl + * @author RetailCRM + * @license https://opensource.org/licenses/MIT MIT License + * @link http://retailcrm.ru/docs/Developers/ApiVersion4 + */ +class WC_Retailcrm_Exception_Curl extends \RuntimeException +{ +} diff --git a/woo-retailcrm/include/api/class-wc-retailcrm-exception-json.php b/woo-retailcrm/include/api/class-wc-retailcrm-exception-json.php index 9f8f16c..403adf2 100644 --- a/woo-retailcrm/include/api/class-wc-retailcrm-exception-json.php +++ b/woo-retailcrm/include/api/class-wc-retailcrm-exception-json.php @@ -1,16 +1,16 @@ - * @license https://opensource.org/licenses/MIT MIT License - */ +/** + * PHP version 5.3 + * + * WC_Retailcrm_Exception_Json class + * + * @category RetailCRM + * @package WC_Retailcrm_Exception_Json + * @author RetailCRM + * @license https://opensource.org/licenses/MIT MIT License + */ - class WC_Retailcrm_Exception_Json extends \DomainException - { - } +class WC_Retailcrm_Exception_Json extends \DomainException +{ +} diff --git a/woo-retailcrm/include/api/class-wc-retailcrm-proxy.php b/woo-retailcrm/include/api/class-wc-retailcrm-proxy.php index 48ec0e9..6be9439 100644 --- a/woo-retailcrm/include/api/class-wc-retailcrm-proxy.php +++ b/woo-retailcrm/include/api/class-wc-retailcrm-proxy.php @@ -21,10 +21,6 @@ if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) : { $this->logger = new WC_Logger(); - if ( ! class_exists( 'WC_Retailcrm_Client_V3' ) ) { - include_once( __DIR__ . '/class-wc-retailcrm-client-v3.php' ); - } - if ( ! class_exists( 'WC_Retailcrm_Client_V4' ) ) { include_once( __DIR__ . '/class-wc-retailcrm-client-v4.php' ); } @@ -34,9 +30,6 @@ if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) : } switch ($api_vers) { - case 'v3': - $this->retailcrm = new WC_Retailcrm_Client_V3($api_url, $api_key, $api_vers); - break; case 'v4': $this->retailcrm = new WC_Retailcrm_Client_V4($api_url, $api_key, $api_vers); break; @@ -44,7 +37,7 @@ if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) : $this->retailcrm = new WC_Retailcrm_Client_V5($api_url, $api_key, $api_vers); break; case null: - $this->retailcrm = new WC_Retailcrm_Client_V3($api_url, $api_key, $api_vers); + $this->retailcrm = new WC_Retailcrm_Client_V4($api_url, $api_key, $api_vers); break; } } diff --git a/woo-retailcrm/include/api/class-wc-retailcrm-request.php b/woo-retailcrm/include/api/class-wc-retailcrm-request.php index 53ce65c..a70420c 100644 --- a/woo-retailcrm/include/api/class-wc-retailcrm-request.php +++ b/woo-retailcrm/include/api/class-wc-retailcrm-request.php @@ -1,117 +1,116 @@ - * @license https://opensource.org/licenses/MIT MIT License - */ +/** + * PHP version 5.3 + * + * Request class + * + * @category Integration + * @package WC_Retailcrm_Request + * @author RetailCRM + * @license https://opensource.org/licenses/MIT MIT License + */ - if ( ! class_exists( 'WC_Retailcrm_Exception_Curl' ) ) { - include_once( __DIR__ . '/class-wc-retailcrm-exception-curl.php' ); - } +if ( ! class_exists( 'WC_Retailcrm_Exception_Curl' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-exception-curl.php' ); +} - if ( ! class_exists( 'WC_Retailcrm_Response' ) ) { - include_once( __DIR__ . '/class-wc-retailcrm-response.php' ); - } +if ( ! class_exists( 'WC_Retailcrm_Response' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-response.php' ); +} - class WC_Retailcrm_Request - { - const METHOD_GET = 'GET'; - const METHOD_POST = 'POST'; +class WC_Retailcrm_Request +{ + const METHOD_GET = 'GET'; + const METHOD_POST = 'POST'; - protected $url; - protected $defaultParameters; + protected $url; + protected $defaultParameters; - /** - * Client constructor. - * - * @param string $url api url - * @param array $defaultParameters array of parameters - * - * @throws \InvalidArgumentException - */ - public function __construct($url, array $defaultParameters = array()) - { - if (false === stripos($url, 'https://')) { - throw new \InvalidArgumentException( - 'API schema requires HTTPS protocol' - ); - } + /** + * Client constructor. + * + * @param string $url api url + * @param array $defaultParameters array of parameters + * + * @throws \InvalidArgumentException + */ + public function __construct($url, array $defaultParameters = array()) + { + if (false === stripos($url, 'https://')) { + throw new \InvalidArgumentException( + 'API schema requires HTTPS protocol' + ); + } - $this->url = $url; - $this->defaultParameters = $defaultParameters; - } + $this->url = $url; + $this->defaultParameters = $defaultParameters; + } - /** - * Make HTTP request - * - * @param string $path request url - * @param string $method (default: 'GET') - * @param array $parameters (default: array()) - * - * @SuppressWarnings(PHPMD.ExcessiveParameterList) - * - * @throws \InvalidArgumentException - * @throws WC_Retailcrm_Exception_Curl - * - * @return WC_Retailcrm_Response - */ - public function makeRequest( - $path, - $method, - array $parameters = array() - ) { - $allowedMethods = array(self::METHOD_GET, self::METHOD_POST); + /** + * Make HTTP request + * + * @param string $path request url + * @param string $method (default: 'GET') + * @param array $parameters (default: array()) + * + * @SuppressWarnings(PHPMD.ExcessiveParameterList) + * + * @throws \InvalidArgumentException + * @throws WC_Retailcrm_Exception_Curl + * + * @return WC_Retailcrm_Response + */ + public function makeRequest( + $path, + $method, + array $parameters = array() + ) { + $allowedMethods = array(self::METHOD_GET, self::METHOD_POST); - if (!in_array($method, $allowedMethods, false)) { - throw new \InvalidArgumentException( - sprintf( - 'Method "%s" is not valid. Allowed methods are %s', - $method, - implode(', ', $allowedMethods) - ) - ); - } + if (!in_array($method, $allowedMethods, false)) { + throw new \InvalidArgumentException( + sprintf( + 'Method "%s" is not valid. Allowed methods are %s', + $method, + implode(', ', $allowedMethods) + ) + ); + } - $parameters = array_merge($this->defaultParameters, $parameters); + $parameters = array_merge($this->defaultParameters, $parameters); - $url = $this->url . $path; + $url = $this->url . $path; - if (self::METHOD_GET === $method && count($parameters)) { - $url .= '?' . http_build_query($parameters, '', '&'); - } + if (self::METHOD_GET === $method && count($parameters)) { + $url .= '?' . http_build_query($parameters, '', '&'); + } - $curlHandler = curl_init(); - curl_setopt($curlHandler, CURLOPT_URL, $url); - curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1); - curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, 1); - curl_setopt($curlHandler, CURLOPT_FAILONERROR, false); - curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false); - curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, false); - curl_setopt($curlHandler, CURLOPT_TIMEOUT, 30); - curl_setopt($curlHandler, CURLOPT_CONNECTTIMEOUT, 30); + $curlHandler = curl_init(); + curl_setopt($curlHandler, CURLOPT_URL, $url); + curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curlHandler, CURLOPT_FAILONERROR, false); + curl_setopt($curlHandler, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curlHandler, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($curlHandler, CURLOPT_TIMEOUT, 30); + curl_setopt($curlHandler, CURLOPT_CONNECTTIMEOUT, 30); - if (self::METHOD_POST === $method) { - curl_setopt($curlHandler, CURLOPT_POST, true); - curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters); - } + if (self::METHOD_POST === $method) { + curl_setopt($curlHandler, CURLOPT_POST, true); + curl_setopt($curlHandler, CURLOPT_POSTFIELDS, $parameters); + } - $responseBody = curl_exec($curlHandler); - $statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); - $errno = curl_errno($curlHandler); - $error = curl_error($curlHandler); + $responseBody = curl_exec($curlHandler); + $statusCode = curl_getinfo($curlHandler, CURLINFO_HTTP_CODE); + $errno = curl_errno($curlHandler); + $error = curl_error($curlHandler); - curl_close($curlHandler); + curl_close($curlHandler); - if ($errno) { - throw new WC_Retailcrm_Exception_Curl($error, $errno); - } + if ($errno) { + throw new WC_Retailcrm_Exception_Curl($error, $errno); + } - return new WC_Retailcrm_Response($statusCode, $responseBody); - } - } + return new WC_Retailcrm_Response($statusCode, $responseBody); + } +} diff --git a/woo-retailcrm/include/api/class-wc-retailcrm-response.php b/woo-retailcrm/include/api/class-wc-retailcrm-response.php index 3372e48..e9e8ed9 100644 --- a/woo-retailcrm/include/api/class-wc-retailcrm-response.php +++ b/woo-retailcrm/include/api/class-wc-retailcrm-response.php @@ -1,169 +1,169 @@ - * @license https://opensource.org/licenses/MIT MIT License - */ +/** + * PHP version 5.3 + * + * Response class + * + * @category Integration + * @package WC_Retailcrm_Response + * @author RetailCRM + * @license https://opensource.org/licenses/MIT MIT License + */ - if ( ! class_exists( 'WC_Retailcrm_Exception_Json' ) ) { - include_once( __DIR__ . '/class-wc-retailcrm-exception-json.php' ); - } +if ( ! class_exists( 'WC_Retailcrm_Exception_Json' ) ) { + include_once( __DIR__ . '/class-wc-retailcrm-exception-json.php' ); +} - class WC_Retailcrm_Response implements \ArrayAccess - { - // HTTP response status code - protected $statusCode; +class WC_Retailcrm_Response implements \ArrayAccess +{ + // HTTP response status code + protected $statusCode; - // response assoc array - protected $response; + // response assoc array + protected $response; - /** - * ApiResponse constructor. - * - * @param int $statusCode HTTP status code - * @param mixed $responseBody HTTP body - * - * @throws WC_Retailcrm_Exception_Json - */ - public function __construct($statusCode, $responseBody = null) - { - $this->statusCode = (int) $statusCode; + /** + * ApiResponse constructor. + * + * @param int $statusCode HTTP status code + * @param mixed $responseBody HTTP body + * + * @throws WC_Retailcrm_Exception_Json + */ + public function __construct($statusCode, $responseBody = null) + { + $this->statusCode = (int) $statusCode; - if (!empty($responseBody)) { - $response = json_decode($responseBody, true); + if (!empty($responseBody)) { + $response = json_decode($responseBody, true); - if (!$response && JSON_ERROR_NONE !== ($error = json_last_error())) { - throw new WC_Retailcrm_Exception_Json( - "Invalid JSON in the API response body. Error code #$error", - $error - ); - } + if (!$response && JSON_ERROR_NONE !== ($error = json_last_error())) { + throw new WC_Retailcrm_Exception_Json( + "Invalid JSON in the API response body. Error code #$error", + $error + ); + } - $this->response = $response; - } - } + $this->response = $response; + } + } - /** - * Return HTTP response status code - * - * @return int - */ - public function getStatusCode() - { - return $this->statusCode; - } + /** + * Return HTTP response status code + * + * @return int + */ + public function getStatusCode() + { + return $this->statusCode; + } - /** - * HTTP request was successful - * - * @return bool - */ - public function isSuccessful() - { - return $this->statusCode < 400; - } + /** + * HTTP request was successful + * + * @return bool + */ + public function isSuccessful() + { + return $this->statusCode < 400; + } - /** - * Allow to access for the property throw class method - * - * @param string $name method name - * @param mixed $arguments method parameters - * - * @throws \InvalidArgumentException - * - * @return mixed - */ - public function __call($name, $arguments) - { - // convert getSomeProperty to someProperty - $propertyName = strtolower(substr($name, 3, 1)) . substr($name, 4); + /** + * Allow to access for the property throw class method + * + * @param string $name method name + * @param mixed $arguments method parameters + * + * @throws \InvalidArgumentException + * + * @return mixed + */ + public function __call($name, $arguments) + { + // convert getSomeProperty to someProperty + $propertyName = strtolower(substr($name, 3, 1)) . substr($name, 4); - if (!isset($this->response[$propertyName])) { - throw new \InvalidArgumentException("Method \"$name\" not found"); - } + if (!isset($this->response[$propertyName])) { + throw new \InvalidArgumentException("Method \"$name\" not found"); + } - return $this->response[$propertyName]; - } + return $this->response[$propertyName]; + } - /** - * Allow to access for the property throw object property - * - * @param string $name property name - * - * @throws \InvalidArgumentException - * - * @return mixed - */ - public function __get($name) - { - if (!isset($this->response[$name])) { - throw new \InvalidArgumentException("Property \"$name\" not found"); - } + /** + * Allow to access for the property throw object property + * + * @param string $name property name + * + * @throws \InvalidArgumentException + * + * @return mixed + */ + public function __get($name) + { + if (!isset($this->response[$name])) { + throw new \InvalidArgumentException("Property \"$name\" not found"); + } - return $this->response[$name]; - } + return $this->response[$name]; + } - /** - * Offset set - * - * @param mixed $offset offset - * @param mixed $value value - * - * @throws \BadMethodCallException - * @return void - */ - public function offsetSet($offset, $value) - { - throw new \BadMethodCallException('This activity not allowed'); - } + /** + * Offset set + * + * @param mixed $offset offset + * @param mixed $value value + * + * @throws \BadMethodCallException + * @return void + */ + public function offsetSet($offset, $value) + { + throw new \BadMethodCallException('This activity not allowed'); + } - /** - * Offset unset - * - * @param mixed $offset offset - * - * @throws \BadMethodCallException - * @return void - */ - public function offsetUnset($offset) - { - throw new \BadMethodCallException('This call not allowed'); - } + /** + * Offset unset + * + * @param mixed $offset offset + * + * @throws \BadMethodCallException + * @return void + */ + public function offsetUnset($offset) + { + throw new \BadMethodCallException('This call not allowed'); + } - /** - * Check offset - * - * @param mixed $offset offset - * - * @return bool - */ - public function offsetExists($offset) - { - return isset($this->response[$offset]); - } + /** + * Check offset + * + * @param mixed $offset offset + * + * @return bool + */ + public function offsetExists($offset) + { + return isset($this->response[$offset]); + } - /** - * Get offset - * - * @param mixed $offset offset - * - * @throws \InvalidArgumentException - * - * @return mixed - */ - public function offsetGet($offset) - { - if (!isset($this->response[$offset])) { - throw new \InvalidArgumentException("Property \"$offset\" not found"); - } + /** + * Get offset + * + * @param mixed $offset offset + * + * @throws \InvalidArgumentException + * + * @return mixed + */ + public function offsetGet($offset) + { + if (!isset($this->response[$offset])) { + throw new \InvalidArgumentException("Property \"$offset\" not found"); + } - return $this->response[$offset]; - } - } + return $this->response[$offset]; + } +} diff --git a/woo-retailcrm/include/class-wc-retailcrm-base.php b/woo-retailcrm/include/class-wc-retailcrm-base.php index 91f951f..d43a1dd 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-base.php +++ b/woo-retailcrm/include/class-wc-retailcrm-base.php @@ -311,8 +311,8 @@ if ( ! class_exists( 'WC_Retailcrm_Base' ) ) : public function validate_api_version_field( $key, $value ) { $post = $this->get_post_data(); + $versionMap = array( - 'v3' => '3.0', 'v4' => '4.0', 'v5' => '5.0' ); diff --git a/woo-retailcrm/include/class-wc-retailcrm-history.php b/woo-retailcrm/include/class-wc-retailcrm-history.php index ddd2519..64b879a 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-history.php +++ b/woo-retailcrm/include/class-wc-retailcrm-history.php @@ -143,295 +143,7 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : $this->removeFuncsHook(); try { - if ($record['field'] == 'status' && !empty($record['newValue']) && !empty($record['oldValue'])) { - $newStatus = $record['newValue']['code']; - if (!empty($options[$newStatus]) && !empty($record['order']['externalId'])) { - $order = new WC_Order($record['order']['externalId']); - $order->update_status($options[$newStatus]); - } - } - - elseif($record['field'] == 'order_product' && $record['newValue']) { - $product = wc_get_product($record['item']['offer']['externalId']); - $order = new WC_Order($record['order']['externalId']); - $order->add_product($product, $record['item']['quantity']); - - $this->update_total($order); - } - - elseif($record['field'] == 'order_product.quantity' && $record['newValue']) { - - $order = new WC_Order($record['order']['externalId']); - $product = wc_get_product($record['item']['offer']['externalId']); - $items = $order->get_items(); - - foreach ($items as $order_item_id => $item) { - if ($item['variation_id'] != 0 ) { - $offer_id = $item['variation_id']; - } else { - $offer_id = $item['product_id']; - } - if ($offer_id == $record['item']['offer']['externalId']) { - wc_delete_order_item($order_item_id); - $order->add_product($product, $record['newValue']); - $this->update_total($order); - } - } - } - - elseif ($record['field'] == 'order_product' && !$record['newValue']) { - $order = new WC_Order($record['order']['externalId']); - $items = $order->get_items(); - - foreach ($items as $order_item_id => $item) { - if ($item['variation_id'] != 0 ) { - $offer_id = $item['variation_id']; - } else { - $offer_id = $item['product_id']; - } - if ($offer_id == $record['item']['offer']['externalId']) { - wc_delete_order_item($order_item_id); - $this->update_total($order); - } - } - } - - elseif ($record['field'] == 'delivery_type') { - $newValue = $record['newValue']['code']; - - if (!empty($options[$newValue]) && !empty($record['order']['externalId'])) { - if (isset($options[$newValue])) { - $order = new WC_Order($record['order']['externalId']); - $items = $order->get_items('shipping'); - $item_id = $this->getShippingItemId($items); - $crmOrder = $this->retailcrm->ordersGet($record['order']['externalId']); - $shipping_methods = get_wc_shipping_methods(true); - - if (isset($shipping_methods[$options[$newValue]])) { - $method_id = $options[$newValue]; - } else { - $method_id = explode(':', $options[$newValue]); - $method_id = $method_id[0]; - $shipping_method = $shipping_methods[$method_id]['shipping_methods'][$options[$newValue]]; - } - - if ( is_object($crmOrder)) { - if ($crmOrder->isSuccessful()) { - $deliveryCost = isset($crmOrder['order']['delivery']['cost']) ? $crmOrder['order']['delivery']['cost'] : 0; - } - } - - $args = array( - 'method_id' => $options[$newValue], - 'method_title' => isset($shipping_method) ? $shipping_method['title'] : $shipping_methods[$options[$newValue]]['name'], - 'total' => $deliveryCost - ); - - $item = $order->get_item((int)$item_id); - $item->set_order_id((int)$order->get_id()); - $item->set_props($args); - $item->save(); - } - - $updateOrder = new WC_Order((int)$order->get_id()); - $this->update_total($updateOrder); - } - } - - elseif ($record['field'] == 'delivery_address.region') { - $order = new WC_Order($record['order']['externalId']); - $order->set_shipping_state($record['newValue']); - } - - elseif ($record['field'] == 'delivery_address.city') { - $order = new WC_Order($record['order']['externalId']); - $order->set_shipping_city($record['newValue']); - } - - elseif ($record['field'] == 'delivery_address.street') { - $order = new WC_Order($record['order']['externalId']); - $order->set_shipping_address_1($record['newValue']); - } - - elseif ($record['field'] == 'delivery_address.building') { - $order = new WC_Order($record['order']['externalId']); - $order->set_shipping_address_2($record['newValue']); - } - - elseif ($record['field'] == 'payment_type') { - $order = new WC_Order($record['order']['externalId']); - $newValue = $record['newValue']['code']; - if (!empty($options[$newValue]) && !empty($record['order']['externalId'])) { - $payment = new WC_Payment_Gateways(); - $payment_types = $payment->get_available_payment_gateways(); - if (isset($payment_types[$options[$newValue]])) { - update_post_meta($order->get_id(), '_payment_method', $payment->id); - } - } - } - - elseif ($record['field'] == 'payments') { - $response = $this->retailcrm->ordersGet($record['order']['externalId']); - - if ($response->isSuccessful()) { - $order_data = $response['order']; - $order = new WC_Order($record['order']['externalId']); - $payment = new WC_Payment_Gateways(); - $payment_types = $payment->get_available_payment_gateways(); - - if (count($order_data['payments']) == 1) { - $paymentType = end($order_data['payments']); - if (isset($payment_types[$options[$paymentType['type']]])) { - $payment = $payment_types[$options[$paymentType['type']]]; - update_post_meta($order->get_id(), '_payment_method', $payment->id); - } - } else { - foreach ($order_data['payments'] as $payment_data) { - if (isset($payment_data['externalId'])) { - $paymentType = $payment_data; - } - } - - if (!isset($paymentType)) { - $paymentType = $order_data['payments'][0]; - } - - if (isset($payment_types[$options[$paymentType['type']]])) { - update_post_meta($order->get_id(), '_payment_method', $payment->id); - } - } - } - } - - elseif (isset($record['created']) && - $record['created'] == 1 && - !isset($record['order']['externalId'])) { - - $args = array( - 'status' => $options[$record['order']['status']], - 'customer_id' => isset($record['order']['customer']['externalId']) ? - $record['order']['customer']['externalId'] : - null - ); - - $order_record = $record['order']; - $order_data = wc_create_order($args); - $order = new WC_Order($order_data->id); - - $address_shipping = array( - 'first_name' => $order_record['firstName'], - 'last_name' => isset($order_record['lastName']) ? $order_record['lastName'] : '', - 'company' => '', - 'email' => isset($order_record['email']) ? $order_record['email'] : '', - 'phone' => isset($order_record['phone']) ? $order_record['phone'] : '', - 'address_1' => isset($order_record['delivery']['address']['text']) ? $order_record['delivery']['address']['text'] : '', - 'address_2' => '', - 'city' => isset($order_record['delivery']['address']['city']) ? $order_record['delivery']['address']['city'] : '', - 'state' => isset($order_record['delivery']['address']['region']) ? $order_record['delivery']['address']['region'] : '', - 'postcode' => isset($order_record['delivery']['address']['postcode']) ? $order_record['delivery']['address']['postcode'] : '', - 'country' => $order_record['delivery']['address']['countryIso'] - ); - $address_billing = array( - 'first_name' => $order_record['customer']['firstName'], - 'last_name' => isset($order_record['customer']['lastName']) ? $order_record['customer']['lastName'] : '', - 'company' => '', - 'email' => isset($order_record['customer']['email']) ? $order_record['customer']['email'] : '', - 'phone' => isset($order_record['customer'][0]['number']) ? $order_record['customer'][0]['number'] : '', - 'address_1' => isset($order_record['customer']['address']['text']) ? $order_record['customer']['address']['text'] : '', - 'address_2' => '', - 'city' => isset($order_record['customer']['address']['city']) ? $order_record['customer']['address']['city'] : '', - 'state' => isset($order_record['customer']['address']['region']) ? $order_record['customer']['address']['region'] : '', - 'postcode' => isset($order_record['customer']['address']['postcode']) ? $order_record['customer']['address']['postcode'] : '', - 'country' => $order_record['customer']['address']['countryIso'] - ); - - if ($this->retailcrm_settings['api_version'] == 'v5') { - if ($order_record['payments']) { - $payment = new WC_Payment_Gateways(); - - if (count($order_record['payments']) == 1) { - $payment_types = $payment->get_available_payment_gateways(); - $paymentType = end($order_record['payments']); - - if (isset($payment_types[$options[$paymentType['type']]])) { - $order->set_payment_method($payment_types[$options[$paymentType['type']]]); - } - } - } - } else { - if ($order_record['paymentType']) { - $payment = new WC_Payment_Gateways(); - $payment_types = $payment->get_available_payment_gateways(); - if (isset($payment_types[$options[$order_record['paymentType']]])) { - $order->set_payment_method($payment_types[$options[$order_record['paymentType']]]); - } - } - } - - $order->set_address($address_billing, 'billing'); - $order->set_address($address_shipping, 'shipping'); - $product_data = isset($order_record['items']) ? $order_record['items'] : array(); - - if ($product_data) { - foreach ($product_data as $product) { - $order->add_product(wc_get_product($product['offer']['externalId']), $product['quantity']); - } - } - - if (array_key_exists('delivery', $order_record)) { - $deliveryCode = isset($order_record['delivery']['code']) ? $order_record['delivery']['code'] : false; - - if ($deliveryCode && isset($options[$deliveryCode])) { - $delivery = explode(':', $options[$deliveryCode]); - - if (isset($delivery[1])) { - $instance_id = $delivery[1]; - } - } - - if (isset($instance_id)) { - $wc_shipping = WC_Shipping_Zones::get_shipping_method($instance_id); - $shipping_method_title = $wc_shipping->method_title; - $shipping_method_id = $options[$deliveryCode]; - $shipping_total = $order_record['delivery']['cost']; - } else { - $wc_shipping = new WC_Shipping(); - $wc_shipping_types = $wc_shipping->get_shipping_methods(); - - foreach ($wc_shipping_types as $shipping_type) { - if ($shipping_type->id == $options[$deliveryCode]) { - $shipping_method_id = $shipping_type->id; - $shipping_method_title = $shipping_type->method_title; - $shipping_total = $order_record['delivery']['cost']; - } - } - } - - if (version_compare(get_option('woocommerce_db_version'), '3.0', '<' )) { - $shipping_rate = new WC_Shipping_Rate($shipping_method_id, isset($shipping_method_title) ? $shipping_method_title : '', isset($shipping_total) ? floatval($shipping_total) : 0, array(), $shipping_method_id); - $order->add_shipping($shipping_rate); - } else { - $shipping = new WC_Order_Item_Shipping(); - $shipping->set_props( array( - 'method_title' => $shipping_method_title, - 'method_id' => $shipping_method_id, - 'total' => wc_format_decimal($shipping_total), - 'order_id' => $order->id - ) ); - $shipping->save(); - $order->add_item( $shipping ); - } - } - - $this->update_total($order); - - $ids[] = array( - 'id' => (int)$order_record['id'], - 'externalId' => (int)$order_data->id - ); - - $this->retailcrm->ordersFixExternalIds($ids); - } + $this->orderEdit($record, $options); } catch (Exception $exception) { $logger = new WC_Logger(); $logger->add('retailcrm', sprintf("[%s] - %s", $exception->getMessage(), 'Exception in file - ' . $exception->getFile() . ' on line ' . $exception->getLine())); @@ -497,6 +209,298 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) : } } + protected function orderEdit($record, $options) + { + if ($record['field'] == 'status' && !empty($record['newValue']) && !empty($record['oldValue'])) { + $newStatus = $record['newValue']['code']; + if (!empty($options[$newStatus]) && !empty($record['order']['externalId'])) { + $order = new WC_Order($record['order']['externalId']); + $order->update_status($options[$newStatus]); + } + } + + elseif($record['field'] == 'order_product' && $record['newValue']) { + $product = wc_get_product($record['item']['offer']['externalId']); + $order = new WC_Order($record['order']['externalId']); + $order->add_product($product, $record['item']['quantity']); + + $this->update_total($order); + } + + elseif($record['field'] == 'order_product.quantity' && $record['newValue']) { + $order = new WC_Order($record['order']['externalId']); + $product = wc_get_product($record['item']['offer']['externalId']); + $items = $order->get_items(); + + foreach ($items as $order_item_id => $item) { + if ($item['variation_id'] != 0 ) { + $offer_id = $item['variation_id']; + } else { + $offer_id = $item['product_id']; + } + if ($offer_id == $record['item']['offer']['externalId']) { + wc_delete_order_item($order_item_id); + $order->add_product($product, $record['newValue']); + $this->update_total($order); + } + } + } + + elseif ($record['field'] == 'order_product' && !$record['newValue']) { + $order = new WC_Order($record['order']['externalId']); + $items = $order->get_items(); + + foreach ($items as $order_item_id => $item) { + if ($item['variation_id'] != 0 ) { + $offer_id = $item['variation_id']; + } else { + $offer_id = $item['product_id']; + } + if ($offer_id == $record['item']['offer']['externalId']) { + wc_delete_order_item($order_item_id); + $this->update_total($order); + } + } + } + + elseif ($record['field'] == 'delivery_type') { + $newValue = $record['newValue']['code']; + + if (!empty($options[$newValue]) && !empty($record['order']['externalId'])) { + if (isset($options[$newValue])) { + $order = new WC_Order($record['order']['externalId']); + $items = $order->get_items('shipping'); + $item_id = $this->getShippingItemId($items); + $crmOrder = $this->retailcrm->ordersGet($record['order']['externalId']); + $shipping_methods = get_wc_shipping_methods(true); + + if (isset($shipping_methods[$options[$newValue]])) { + $method_id = $options[$newValue]; + } else { + $method_id = explode(':', $options[$newValue]); + $method_id = $method_id[0]; + $shipping_method = $shipping_methods[$method_id]['shipping_methods'][$options[$newValue]]; + } + + if ( is_object($crmOrder)) { + if ($crmOrder->isSuccessful()) { + $deliveryCost = isset($crmOrder['order']['delivery']['cost']) ? $crmOrder['order']['delivery']['cost'] : 0; + } + } + + $args = array( + 'method_id' => $options[$newValue], + 'method_title' => isset($shipping_method) ? $shipping_method['title'] : $shipping_methods[$options[$newValue]]['name'], + 'total' => $deliveryCost + ); + + $item = $order->get_item((int)$item_id); + $item->set_order_id((int)$order->get_id()); + $item->set_props($args); + $item->save(); + } + + $updateOrder = new WC_Order((int)$order->get_id()); + $this->update_total($updateOrder); + } + } + + elseif ($record['field'] == 'delivery_address.region') { + $order = new WC_Order($record['order']['externalId']); + $order->set_shipping_state($record['newValue']); + } + + elseif ($record['field'] == 'delivery_address.city') { + $order = new WC_Order($record['order']['externalId']); + $order->set_shipping_city($record['newValue']); + } + + elseif ($record['field'] == 'delivery_address.street') { + $order = new WC_Order($record['order']['externalId']); + $order->set_shipping_address_1($record['newValue']); + } + + elseif ($record['field'] == 'delivery_address.building') { + $order = new WC_Order($record['order']['externalId']); + $order->set_shipping_address_2($record['newValue']); + } + + elseif ($record['field'] == 'payment_type') { + $order = new WC_Order($record['order']['externalId']); + $newValue = $record['newValue']['code']; + if (!empty($options[$newValue]) && !empty($record['order']['externalId'])) { + $payment = new WC_Payment_Gateways(); + $payment_types = $payment->get_available_payment_gateways(); + if (isset($payment_types[$options[$newValue]])) { + update_post_meta($order->get_id(), '_payment_method', $payment->id); + } + } + } + + elseif ($record['field'] == 'payments') { + $response = $this->retailcrm->ordersGet($record['order']['externalId']); + + if ($response->isSuccessful()) { + $order_data = $response['order']; + $order = new WC_Order($record['order']['externalId']); + $payment = new WC_Payment_Gateways(); + $payment_types = $payment->get_available_payment_gateways(); + + if (count($order_data['payments']) == 1) { + $paymentType = end($order_data['payments']); + if (isset($payment_types[$options[$paymentType['type']]])) { + $payment = $payment_types[$options[$paymentType['type']]]; + update_post_meta($order->get_id(), '_payment_method', $payment->id); + } + } else { + foreach ($order_data['payments'] as $payment_data) { + if (isset($payment_data['externalId'])) { + $paymentType = $payment_data; + } + } + + if (!isset($paymentType)) { + $paymentType = $order_data['payments'][0]; + } + + if (isset($payment_types[$options[$paymentType['type']]])) { + update_post_meta($order->get_id(), '_payment_method', $payment->id); + } + } + } + } + + elseif (isset($record['created']) && + $record['created'] == 1 && + !isset($record['order']['externalId'])) { + + $args = array( + 'status' => $options[$record['order']['status']], + 'customer_id' => isset($record['order']['customer']['externalId']) ? + $record['order']['customer']['externalId'] : + null + ); + + $order_record = $record['order']; + $order_data = wc_create_order($args); + $order = new WC_Order($order_data->id); + + $address_shipping = array( + 'first_name' => $order_record['firstName'], + 'last_name' => isset($order_record['lastName']) ? $order_record['lastName'] : '', + 'company' => '', + 'email' => isset($order_record['email']) ? $order_record['email'] : '', + 'phone' => isset($order_record['phone']) ? $order_record['phone'] : '', + 'address_1' => isset($order_record['delivery']['address']['text']) ? $order_record['delivery']['address']['text'] : '', + 'address_2' => '', + 'city' => isset($order_record['delivery']['address']['city']) ? $order_record['delivery']['address']['city'] : '', + 'state' => isset($order_record['delivery']['address']['region']) ? $order_record['delivery']['address']['region'] : '', + 'postcode' => isset($order_record['delivery']['address']['postcode']) ? $order_record['delivery']['address']['postcode'] : '', + 'country' => $order_record['delivery']['address']['countryIso'] + ); + $address_billing = array( + 'first_name' => $order_record['customer']['firstName'], + 'last_name' => isset($order_record['customer']['lastName']) ? $order_record['customer']['lastName'] : '', + 'company' => '', + 'email' => isset($order_record['customer']['email']) ? $order_record['customer']['email'] : '', + 'phone' => isset($order_record['customer'][0]['number']) ? $order_record['customer'][0]['number'] : '', + 'address_1' => isset($order_record['customer']['address']['text']) ? $order_record['customer']['address']['text'] : '', + 'address_2' => '', + 'city' => isset($order_record['customer']['address']['city']) ? $order_record['customer']['address']['city'] : '', + 'state' => isset($order_record['customer']['address']['region']) ? $order_record['customer']['address']['region'] : '', + 'postcode' => isset($order_record['customer']['address']['postcode']) ? $order_record['customer']['address']['postcode'] : '', + 'country' => $order_record['customer']['address']['countryIso'] + ); + + if ($this->retailcrm_settings['api_version'] == 'v5') { + if ($order_record['payments']) { + $payment = new WC_Payment_Gateways(); + + if (count($order_record['payments']) == 1) { + $payment_types = $payment->get_available_payment_gateways(); + $paymentType = end($order_record['payments']); + + if (isset($payment_types[$options[$paymentType['type']]])) { + $order->set_payment_method($payment_types[$options[$paymentType['type']]]); + } + } + } + } else { + if ($order_record['paymentType']) { + $payment = new WC_Payment_Gateways(); + $payment_types = $payment->get_available_payment_gateways(); + if (isset($payment_types[$options[$order_record['paymentType']]])) { + $order->set_payment_method($payment_types[$options[$order_record['paymentType']]]); + } + } + } + + $order->set_address($address_billing, 'billing'); + $order->set_address($address_shipping, 'shipping'); + $product_data = isset($order_record['items']) ? $order_record['items'] : array(); + + if ($product_data) { + foreach ($product_data as $product) { + $order->add_product(wc_get_product($product['offer']['externalId']), $product['quantity']); + } + } + + if (array_key_exists('delivery', $order_record)) { + $deliveryCode = isset($order_record['delivery']['code']) ? $order_record['delivery']['code'] : false; + + if ($deliveryCode && isset($options[$deliveryCode])) { + $delivery = explode(':', $options[$deliveryCode]); + + if (isset($delivery[1])) { + $instance_id = $delivery[1]; + } + } + + if (isset($instance_id)) { + $wc_shipping = WC_Shipping_Zones::get_shipping_method($instance_id); + $shipping_method_title = $wc_shipping->method_title; + $shipping_method_id = $options[$deliveryCode]; + $shipping_total = $order_record['delivery']['cost']; + } else { + $wc_shipping = new WC_Shipping(); + $wc_shipping_types = $wc_shipping->get_shipping_methods(); + + foreach ($wc_shipping_types as $shipping_type) { + if ($shipping_type->id == $options[$deliveryCode]) { + $shipping_method_id = $shipping_type->id; + $shipping_method_title = $shipping_type->method_title; + $shipping_total = $order_record['delivery']['cost']; + } + } + } + + if (version_compare(get_option('woocommerce_db_version'), '3.0', '<' )) { + $shipping_rate = new WC_Shipping_Rate($shipping_method_id, isset($shipping_method_title) ? $shipping_method_title : '', isset($shipping_total) ? floatval($shipping_total) : 0, array(), $shipping_method_id); + $order->add_shipping($shipping_rate); + } else { + $shipping = new WC_Order_Item_Shipping(); + $shipping->set_props( array( + 'method_title' => $shipping_method_title, + 'method_id' => $shipping_method_id, + 'total' => wc_format_decimal($shipping_total), + 'order_id' => $order->id + ) ); + $shipping->save(); + $order->add_item( $shipping ); + } + } + + $this->update_total($order); + + $ids[] = array( + 'id' => (int)$order_record['id'], + 'externalId' => (int)$order_data->id + ); + + $this->retailcrm->ordersFixExternalIds($ids); + } + } + protected function getShippingItemId($items) { if ($items) { diff --git a/woo-retailcrm/include/class-wc-retailcrm-orders.php b/woo-retailcrm/include/class-wc-retailcrm-orders.php index acf272a..e5b5e0b 100644 --- a/woo-retailcrm/include/class-wc-retailcrm-orders.php +++ b/woo-retailcrm/include/class-wc-retailcrm-orders.php @@ -277,10 +277,11 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : * process to combine order data * * @param int $order_id - * + * @param boolean $update + * * @return array $order_data */ - public function processOrder($order_id) + public function processOrder($order_id, $update = false) { if ( !$order_id ){ return; @@ -303,8 +304,8 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $shipping = end($order->get_items( 'shipping' )); $shipping_code = explode(':', $shipping['method_id']); - if (isset($this->retailcrm_settings[$shipping])) { - $shipping_method = $shipping; + if (isset($this->retailcrm_settings[$shipping['method_id']])) { + $shipping_method = $shipping['method_id']; } else { $shipping_method = $shipping_code[0]; } @@ -369,7 +370,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : foreach ($order->get_items() as $item) { $uid = ($item['variation_id'] > 0) ? $item['variation_id'] : $item['product_id'] ; $_product = wc_get_product($uid); - $price = wc_get_price_including_tax($_product); + $price = round($item['line_subtotal'] + $item['line_subtotal_tax'], 2); if ($_product) { $product_price = $item->get_total() ? $item->get_total() / $item->get_quantity() : 0; @@ -384,10 +385,10 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : 'quantity' => $item['qty'], ); - if ($this->retailcrm_settings['api_version'] == 'v5') { - $order_item['discountManualAmount'] = $discount_price; - } elseif ($this->retailcrm_settings['api_version'] == 'v4') { - $order_item['discount'] = $discount_price; + if ($this->retailcrm_settings['api_version'] == 'v5' && round($discount_price, 2)) { + $order_item['discountManualAmount'] = round($discount_price, 2); + } elseif ($this->retailcrm_settings['api_version'] == 'v4' && round($discount_price, 2)) { + $order_item['discount'] = round($discount_price, 2); } } @@ -419,12 +420,24 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $payment['paidAt'] = trim($pay_date->date('Y-m-d H:i:s')); } - $order_data['payments'][] = $payment; + if (!$update) { + $order_data['payments'][] = $payment; + } else { + $this->editPayment($payment); + } } return $order_data; } + /** + * Create payment in CRM + * + * @param WC_Order $order + * @param int $order_id + * + * @return void + */ protected function createPayment($order, $order_id) { $payment = array( @@ -452,16 +465,35 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) : $this->retailcrm->ordersPaymentCreate($payment); } + /** + * Edit payment in CRM + * + * @param array $payment + * + * @return void + */ + protected function editPayment($payment) + { + $this->retailcrm->ordersPaymentEdit($payment); + } + + /** + * Edit order in CRM + * + * @param int $order_id + * + * @return void + */ public function updateOrder($order_id) { - $order = $this->processOrder($order_id); + $order = $this->processOrder($order_id, true); $response = $this->retailcrm->ordersEdit($order); - $order = new WC_Order($order_id); + $orderWc = new WC_Order($order_id); if ($response->isSuccessful()) { - $this->orderUpdatePaymentType($order_id, $order->payment_method); + $this->orderUpdatePaymentType($order_id, $orderWc->payment_method); } } } diff --git a/woo-retailcrm/retailcrm.php b/woo-retailcrm/retailcrm.php index add5549..708659a 100644 --- a/woo-retailcrm/retailcrm.php +++ b/woo-retailcrm/retailcrm.php @@ -1,6 +1,6 @@