2013-07-08 15:08:14 +04:00
|
|
|
|
<?php
|
2014-11-18 15:50:21 +03:00
|
|
|
|
|
2015-03-03 16:37:34 +03:00
|
|
|
|
namespace RetailCrm;
|
2013-07-08 15:08:14 +04:00
|
|
|
|
|
2015-03-03 16:37:34 +03:00
|
|
|
|
use RetailCrm\Response\ApiResponse;
|
|
|
|
|
use RetailCrm\Exception\CurlException;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* retailCRM API client class
|
|
|
|
|
*/
|
2013-07-08 15:08:14 +04:00
|
|
|
|
class RestApi
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
const VERSION = 'v3';
|
|
|
|
|
const METHOD_GET = 'GET';
|
|
|
|
|
const METHOD_POST = 'POST';
|
|
|
|
|
|
|
|
|
|
protected $client;
|
|
|
|
|
protected $url;
|
|
|
|
|
protected $defaultParameters;
|
2014-03-22 14:52:17 +04:00
|
|
|
|
protected $generatedAt;
|
2015-03-03 16:37:34 +03:00
|
|
|
|
|
2013-07-08 15:08:14 +04:00
|
|
|
|
/**
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* Site code
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
protected $siteCode;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Client creating
|
|
|
|
|
*
|
|
|
|
|
* @param string $url - url сайта
|
|
|
|
|
* @param string $apiKey - ключ API
|
|
|
|
|
* @param string $site - символьный код сайта
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function __construct($url, $apiKey, $site = null)
|
2013-07-08 15:08:14 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if ('/' != substr($url, strlen($url) - 1, 1)) {
|
|
|
|
|
$url .= '/';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$url = $url . 'api/' . self::VERSION;
|
|
|
|
|
|
|
|
|
|
if (false === stripos($url, 'https://')) {
|
|
|
|
|
throw new \InvalidArgumentException('API schema requires HTTPS protocol');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->url = $url;
|
|
|
|
|
$this->defaultParameters = array('apiKey' => $apiKey);
|
|
|
|
|
$this->siteCode = $site;
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Методы для работы с заказами */
|
|
|
|
|
/**
|
|
|
|
|
* Получение заказа по id
|
|
|
|
|
*
|
|
|
|
|
* @param string $id - идентификатор заказа
|
2013-07-09 19:03:56 +04:00
|
|
|
|
* @param string $by - поиск заказа по id или externalId
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param string $site - символьный код сайта
|
|
|
|
|
* @return ApiResponse - информация о заказе
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
public function ordersGet($id, $by = 'externalId', $site = null)
|
2013-07-08 15:08:14 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
$this->checkIdParameter($by);
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest("/orders/$id", self::METHOD_GET, $this->fillSite($site, array(
|
|
|
|
|
'by' => $by
|
|
|
|
|
)));
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Создание заказа
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param array $order - информация о заказе
|
|
|
|
|
* @param string $site - символьный код сайта
|
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
public function ordersCreate($order, $site = null)
|
2013-07-08 15:08:14 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!sizeof($order)) {
|
|
|
|
|
throw new \InvalidArgumentException('Parameter `order` must contains a data');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest("/orders/create", self::METHOD_POST, $this->fillSite($site, array(
|
|
|
|
|
'order' => json_encode($order)
|
|
|
|
|
)));
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Изменение заказа
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param array $order - информация о заказе
|
|
|
|
|
* @param string $by - изменение заказа по id или externalId
|
|
|
|
|
* @param string $site - символьный код сайта
|
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
public function orderEdit($order, $by = 'externalId', $site = null)
|
2013-07-08 15:08:14 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
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->makeRequest(
|
|
|
|
|
"/orders/" . $order[$by] . "/edit",
|
|
|
|
|
self::METHOD_POST,
|
|
|
|
|
$this->fillSite($site, array(
|
|
|
|
|
'order' => json_encode($order),
|
|
|
|
|
'by' => $by,
|
|
|
|
|
))
|
|
|
|
|
);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-07-29 12:29:41 +04:00
|
|
|
|
* Пакетная загрузка заказов
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*
|
|
|
|
|
* @param array $orders - массив заказов
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param string $site - символьный код сайта
|
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
public function orderUpload($orders, $site = null)
|
2013-07-08 15:08:14 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!sizeof($orders)) {
|
|
|
|
|
throw new \InvalidArgumentException('Parameter `orders` must contains array of the orders');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest("/orders/upload", self::METHOD_POST, $this->fillSite($site, array(
|
|
|
|
|
'orders' => json_encode($orders),
|
|
|
|
|
)));
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-18 22:55:52 +04:00
|
|
|
|
/**
|
|
|
|
|
* Обновление externalId у заказов с переданными id
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param array $order - массив, содержащий id и externalId заказа
|
|
|
|
|
* @return ApiResponse
|
2013-07-18 22:55:52 +04:00
|
|
|
|
*/
|
2014-03-22 14:52:17 +04:00
|
|
|
|
public function orderFixExternalIds($order)
|
2013-07-18 22:55:52 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!sizeof($order)) {
|
|
|
|
|
throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest("/orders/fix-external-ids", self::METHOD_POST, array(
|
|
|
|
|
'orders' => json_encode($order),
|
|
|
|
|
));
|
2013-07-18 22:55:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-08 15:08:14 +04:00
|
|
|
|
/**
|
|
|
|
|
* Получение последних измененных заказов
|
|
|
|
|
*
|
2014-11-18 15:50:21 +03:00
|
|
|
|
* @param \DateTime|string|int $startDate - начальная дата и время выборки (Y-m-d H:i:s)
|
|
|
|
|
* @param \DateTime|string|int $endDate - конечная дата и время выборки (Y-m-d H:i:s)
|
2013-07-08 15:08:14 +04:00
|
|
|
|
* @param int $limit - ограничение на размер выборки
|
|
|
|
|
* @param int $offset - сдвиг
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param bool $skipMyChanges
|
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
public function orderHistory(
|
|
|
|
|
$startDate = null,
|
|
|
|
|
$endDate = null,
|
|
|
|
|
$limit = 100,
|
|
|
|
|
$offset = 0,
|
|
|
|
|
$skipMyChanges = true
|
|
|
|
|
) {
|
|
|
|
|
$parameters = array();
|
|
|
|
|
|
|
|
|
|
if ($startDate) {
|
|
|
|
|
$parameters['startDate'] = $this->ensureDateTime($startDate);
|
|
|
|
|
}
|
|
|
|
|
if ($endDate) {
|
|
|
|
|
$parameters['endDate'] = $this->ensureDateTime($endDate);
|
|
|
|
|
}
|
|
|
|
|
if ($limit) {
|
|
|
|
|
$parameters['limit'] = (int) $limit;
|
|
|
|
|
}
|
|
|
|
|
if ($offset) {
|
|
|
|
|
$parameters['offset'] = (int) $offset;
|
|
|
|
|
}
|
|
|
|
|
if ($skipMyChanges) {
|
|
|
|
|
$parameters['skipMyChanges'] = (bool) $skipMyChanges;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest('/orders/history', self::METHOD_GET, $parameters);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Методы для работы с клиентами */
|
|
|
|
|
/**
|
|
|
|
|
* Получение клиента по id
|
|
|
|
|
*
|
|
|
|
|
* @param string $id - идентификатор
|
2013-07-09 19:03:56 +04:00
|
|
|
|
* @param string $by - поиск заказа по id или externalId
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param string $site - символьный код сайта
|
2013-07-08 15:08:14 +04:00
|
|
|
|
* @return array - информация о клиенте
|
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
public function customerGet($id, $by = 'externalId', $site = null)
|
2013-07-08 15:08:14 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
$this->checkIdParameter($by);
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest("/customers/$id", self::METHOD_GET, $this->fillSite($site, array(
|
|
|
|
|
'by' => $by
|
|
|
|
|
)));
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-06-20 17:59:52 +04:00
|
|
|
|
/**
|
|
|
|
|
* Получение списка клиентов в соответсвии с запросом
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param array $filter - фильтры
|
|
|
|
|
* @param int $page - страница
|
2014-06-20 17:59:52 +04:00
|
|
|
|
* @param int $limit - ограничение на размер выборки
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2014-06-20 17:59:52 +04:00
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
public function customersList(array $filter = array(), $page = null, $limit = null)
|
2014-06-20 17:59:52 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
$parameters = array();
|
|
|
|
|
|
|
|
|
|
if (sizeof($filter)) {
|
|
|
|
|
$parameters['filter'] = $filter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (null !== $page) {
|
|
|
|
|
$parameters['page'] = (int) $page;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (null !== $limit) {
|
|
|
|
|
$parameters['limit'] = (int) $limit;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest('/customers', self::METHOD_GET, $parameters);
|
|
|
|
|
}
|
2014-06-20 17:59:52 +04:00
|
|
|
|
|
2013-07-08 15:08:14 +04:00
|
|
|
|
/**
|
|
|
|
|
* Создание клиента
|
|
|
|
|
*
|
|
|
|
|
* @param array $customer - информация о клиенте
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param string $site - символьный код сайта
|
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
public function customersCreate(array $customer, $site = null)
|
2013-07-08 15:08:14 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!sizeof($customer)) {
|
|
|
|
|
throw new \InvalidArgumentException('Parameter `customer` must contains a data');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest("/customers/create", self::METHOD_POST, $this->fillSite($site, array(
|
|
|
|
|
'customer' => json_encode($customer)
|
|
|
|
|
)));
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Редактирование клиента
|
|
|
|
|
*
|
|
|
|
|
* @param array $customer - информация о клиенте
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param string $by - изменение клиента по id или externalId
|
|
|
|
|
* @param string $site - символьный код сайта
|
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
public function customerEdit($customer, $by = 'externalId', $site = null)
|
2013-07-08 15:08:14 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
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->makeRequest(
|
|
|
|
|
"/customers/" . $customer[$by] . "/edit",
|
|
|
|
|
self::METHOD_POST,
|
|
|
|
|
$this->fillSite($site, array(
|
|
|
|
|
'customer' => json_encode($customer),
|
|
|
|
|
'by' => $by,
|
|
|
|
|
)
|
|
|
|
|
));
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-29 12:29:41 +04:00
|
|
|
|
/**
|
|
|
|
|
* Пакетная загрузка клиентов
|
|
|
|
|
*
|
|
|
|
|
* @param array $customers - массив клиентов
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param string $site - символьный код сайта
|
|
|
|
|
* @return ApiResponse
|
2013-07-29 12:29:41 +04:00
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
public function customerUpload($customers, $site = null)
|
2013-07-29 12:29:41 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!sizeof($customers)) {
|
|
|
|
|
throw new \InvalidArgumentException('Parameter `customers` must contains array of the customers');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest("/customers/upload", self::METHOD_POST, $this->fillSite($site, array(
|
|
|
|
|
'customers' => json_encode($customers),
|
|
|
|
|
)));
|
2014-01-28 12:44:20 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-22 15:15:37 +04:00
|
|
|
|
/**
|
|
|
|
|
* Обновление externalId у клиентов с переданными id
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param array $customers - массив, содержащий id и externalId заказа
|
2014-03-22 15:15:37 +04:00
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function customerFixExternalIds($customers)
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!sizeof($customers)) {
|
|
|
|
|
throw new \InvalidArgumentException('Method parameter must contains at least one IDs pair');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest("/customers/fix-external-ids", self::METHOD_POST, array(
|
|
|
|
|
'customers' => json_encode($customers),
|
|
|
|
|
));
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Методы для работы со справочниками */
|
2015-03-03 16:37:34 +03:00
|
|
|
|
|
2013-07-08 15:08:14 +04:00
|
|
|
|
/**
|
|
|
|
|
* Получение списка типов доставки
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
|
|
|
|
public function deliveryTypesList()
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
return $this->makeRequest('/reference/delivery-types', self::METHOD_GET);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Редактирование типа доставки
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param array $delivery - информация о типе доставки
|
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
public function deliveryTypeEdit($delivery)
|
2013-07-08 15:08:14 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!isset($delivery['code'])) {
|
|
|
|
|
throw new \InvalidArgumentException('Data must contain "code" parameter.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest(
|
|
|
|
|
'/reference/delivery-types/' . $delivery['code'] . '/edit',
|
|
|
|
|
self::METHOD_POST,
|
|
|
|
|
array(
|
|
|
|
|
'deliveryType' => json_encode($delivery)
|
|
|
|
|
)
|
|
|
|
|
);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-22 15:08:51 +04:00
|
|
|
|
/**
|
|
|
|
|
* Получение списка служб доставки
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2014-03-22 15:08:51 +04:00
|
|
|
|
*/
|
|
|
|
|
public function deliveryServicesList()
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
return $this->makeRequest('/reference/delivery-services', self::METHOD_GET);
|
2014-03-22 15:08:51 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Редактирование службы доставки
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @param array $delivery - информация о типе доставки
|
|
|
|
|
* @return ApiResponse
|
2014-03-22 15:08:51 +04:00
|
|
|
|
*/
|
2015-03-03 16:37:34 +03:00
|
|
|
|
public function deliveryServiceEdit($delivery)
|
2014-03-22 15:08:51 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!isset($delivery['code'])) {
|
|
|
|
|
throw new \InvalidArgumentException('Data must contain "code" parameter.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest(
|
|
|
|
|
'/reference/delivery-services/' . $delivery['code'] . '/edit',
|
|
|
|
|
self::METHOD_POST,
|
|
|
|
|
array(
|
|
|
|
|
'deliveryService' => json_encode($delivery)
|
|
|
|
|
)
|
|
|
|
|
);
|
2014-03-22 15:08:51 +04:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-08 15:08:14 +04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получение списка типов оплаты
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
|
|
|
|
public function paymentTypesList()
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
return $this->makeRequest('/reference/payment-types', self::METHOD_GET);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Редактирование типа оплаты
|
|
|
|
|
*
|
|
|
|
|
* @param array $paymentType - информация о типе оплаты
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
|
|
|
|
public function paymentTypesEdit($paymentType)
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!isset($paymentType['code'])) {
|
|
|
|
|
throw new \InvalidArgumentException('Data must contain "code" parameter.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest(
|
|
|
|
|
'/reference/payment-types/' . $paymentType['code'] . '/edit',
|
|
|
|
|
self::METHOD_POST,
|
|
|
|
|
array(
|
|
|
|
|
'paymentType' => json_encode($paymentType)
|
|
|
|
|
)
|
|
|
|
|
);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получение списка статусов оплаты
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
|
|
|
|
public function paymentStatusesList()
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
return $this->makeRequest('/reference/payment-statuses', self::METHOD_GET);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Редактирование статуса оплаты
|
|
|
|
|
*
|
|
|
|
|
* @param array $paymentStatus - информация о статусе оплаты
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
|
|
|
|
public function paymentStatusesEdit($paymentStatus)
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!isset($paymentStatus['code'])) {
|
|
|
|
|
throw new \InvalidArgumentException('Data must contain "code" parameter.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest(
|
|
|
|
|
'/reference/payment-statuses/' . $paymentStatus['code'] . '/edit',
|
|
|
|
|
self::METHOD_POST,
|
|
|
|
|
array(
|
|
|
|
|
'paymentStatus' => json_encode($paymentStatus)
|
|
|
|
|
)
|
|
|
|
|
);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получение списка типов заказа
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
|
|
|
|
public function orderTypesList()
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
return $this->makeRequest('/reference/order-types', self::METHOD_GET);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Редактирование типа заказа
|
|
|
|
|
*
|
2013-07-09 19:03:56 +04:00
|
|
|
|
* @param array $orderType - информация о типе заказа
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2013-07-08 15:08:14 +04:00
|
|
|
|
*/
|
|
|
|
|
public function orderTypesEdit($orderType)
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!isset($orderType['code'])) {
|
|
|
|
|
throw new \InvalidArgumentException('Data must contain "code" parameter.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest(
|
|
|
|
|
'/reference/order-types/' . $orderType['code'] . '/edit',
|
|
|
|
|
self::METHOD_POST,
|
|
|
|
|
array(
|
|
|
|
|
'orderType' => json_encode($orderType)
|
|
|
|
|
)
|
|
|
|
|
);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
2014-03-22 14:52:17 +04:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получение списка способов оформления заказа
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2014-03-22 14:52:17 +04:00
|
|
|
|
*/
|
|
|
|
|
public function orderMethodsList()
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
return $this->makeRequest('/reference/order-methods', self::METHOD_GET);
|
2014-03-22 14:52:17 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Редактирование способа оформления заказа
|
|
|
|
|
*
|
|
|
|
|
* @param array $orderMethod - информация о способе оформления заказа
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2014-03-22 14:52:17 +04:00
|
|
|
|
*/
|
|
|
|
|
public function orderMethodsEdit($orderMethod)
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!isset($orderMethod['code'])) {
|
|
|
|
|
throw new \InvalidArgumentException('Data must contain "code" parameter.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest(
|
|
|
|
|
'/reference/order-methods/' . $orderMethod['code'] . '/edit',
|
|
|
|
|
self::METHOD_POST,
|
|
|
|
|
array(
|
|
|
|
|
'orderMethod' => json_encode($orderMethod)
|
|
|
|
|
)
|
|
|
|
|
);
|
2014-03-22 14:52:17 +04:00
|
|
|
|
}
|
|
|
|
|
|
2013-07-09 19:03:56 +04:00
|
|
|
|
/**
|
|
|
|
|
* Получение списка статусов заказа
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2013-07-09 19:03:56 +04:00
|
|
|
|
*/
|
|
|
|
|
public function orderStatusesList()
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
return $this->makeRequest('/reference/statuses', self::METHOD_GET);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Получение списка сайтов
|
|
|
|
|
*
|
|
|
|
|
* @return ApiResponse
|
|
|
|
|
*/
|
|
|
|
|
public function sitesList()
|
|
|
|
|
{
|
|
|
|
|
return $this->makeRequest('/reference/sites', self::METHOD_GET);
|
2013-07-09 19:03:56 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Редактирование статуса заказа
|
|
|
|
|
*
|
|
|
|
|
* @param array $status - информация о статусе заказа
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2013-07-09 19:03:56 +04:00
|
|
|
|
*/
|
|
|
|
|
public function orderStatusEdit($status)
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (!isset($status['code'])) {
|
|
|
|
|
throw new \InvalidArgumentException('Data must contain "code" parameter.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->makeRequest(
|
|
|
|
|
'/reference/statuses/' . $status['code'] . '/edit',
|
|
|
|
|
self::METHOD_POST,
|
|
|
|
|
array(
|
|
|
|
|
'status' => json_encode($status)
|
|
|
|
|
)
|
|
|
|
|
);
|
2013-07-09 19:03:56 +04:00
|
|
|
|
}
|
2013-07-08 15:08:14 +04:00
|
|
|
|
|
|
|
|
|
|
2013-07-09 19:03:56 +04:00
|
|
|
|
/**
|
|
|
|
|
* Получение списка групп статусов заказа
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2013-07-09 19:03:56 +04:00
|
|
|
|
*/
|
|
|
|
|
public function orderStatusGroupsList()
|
2013-07-08 15:08:14 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
return $this->makeRequest('/reference/status-groups', self::METHOD_GET);
|
2013-07-09 19:03:56 +04:00
|
|
|
|
}
|
2014-03-22 14:52:17 +04:00
|
|
|
|
|
2013-07-29 12:29:41 +04:00
|
|
|
|
/**
|
|
|
|
|
* Обновление статистики
|
|
|
|
|
*
|
2015-03-03 16:37:34 +03:00
|
|
|
|
* @return ApiResponse
|
2013-07-29 12:29:41 +04:00
|
|
|
|
*/
|
|
|
|
|
public function statisticUpdate()
|
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
return $this->makeRequest('/statistic/update', self::METHOD_GET);
|
2013-07-29 12:29:41 +04:00
|
|
|
|
}
|
2013-07-09 19:03:56 +04:00
|
|
|
|
|
2014-03-22 14:52:17 +04:00
|
|
|
|
/**
|
|
|
|
|
* @return \DateTime
|
|
|
|
|
*/
|
2014-11-18 15:50:21 +03:00
|
|
|
|
public function getGeneratedAt()
|
|
|
|
|
{
|
2014-03-22 14:52:17 +04:00
|
|
|
|
return $this->generatedAt;
|
|
|
|
|
}
|
2015-03-03 16:37:34 +03:00
|
|
|
|
|
2014-11-18 15:50:21 +03:00
|
|
|
|
protected function ensureDateTime($value)
|
|
|
|
|
{
|
|
|
|
|
if ($value instanceof \DateTime) {
|
|
|
|
|
return $value->format('Y-m-d H:i:s');
|
|
|
|
|
} elseif (is_int($value)) {
|
|
|
|
|
return date('Y-m-d H:i:s', $value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
2015-03-03 16:37:34 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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)
|
2014-03-22 14:52:17 +04:00
|
|
|
|
{
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if ($site) {
|
|
|
|
|
$params['site'] = $site;
|
|
|
|
|
} elseif ($this->siteCode) {
|
|
|
|
|
$params['site'] = $this->siteCode;
|
2014-03-22 14:52:17 +04:00
|
|
|
|
}
|
2015-03-03 16:37:34 +03:00
|
|
|
|
|
|
|
|
|
return $params;
|
2014-03-22 14:52:17 +04:00
|
|
|
|
}
|
2015-03-03 16:37:34 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Make HTTP request
|
|
|
|
|
*
|
|
|
|
|
* @param string $path
|
|
|
|
|
* @param string $method (default: 'GET')
|
|
|
|
|
* @param array $parameters (default: array())
|
|
|
|
|
* @param int $timeout
|
|
|
|
|
* @return ApiResponse
|
|
|
|
|
*/
|
|
|
|
|
public function makeRequest($path, $method, $parameters = array(), $timeout = 30)
|
|
|
|
|
{
|
|
|
|
|
$allowedMethods = array(self::METHOD_GET, self::METHOD_POST);
|
|
|
|
|
if (!in_array($method, $allowedMethods)) {
|
|
|
|
|
throw new \InvalidArgumentException(sprintf(
|
|
|
|
|
'Method "%s" is not valid. Allowed methods are %s',
|
|
|
|
|
$method,
|
|
|
|
|
implode(', ', $allowedMethods)
|
|
|
|
|
));
|
|
|
|
|
}
|
2014-03-22 14:52:17 +04:00
|
|
|
|
|
2015-03-03 16:37:34 +03:00
|
|
|
|
$parameters = array_merge($this->defaultParameters, $parameters);
|
|
|
|
|
|
|
|
|
|
$path = $this->url . $path;
|
|
|
|
|
if (self::METHOD_GET === $method && sizeof($parameters)) {
|
|
|
|
|
$path .= '?' . http_build_query($parameters);
|
|
|
|
|
}
|
2013-07-08 15:08:14 +04:00
|
|
|
|
|
|
|
|
|
$ch = curl_init();
|
2015-03-03 16:37:34 +03:00
|
|
|
|
curl_setopt($ch, CURLOPT_URL, $path);
|
|
|
|
|
curl_setopt($ch, CURLOPT_FAILONERROR, false);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
|
2015-03-03 16:37:34 +03:00
|
|
|
|
curl_setopt($ch, CURLOPT_TIMEOUT, (int) $timeout); // times out after 30s
|
|
|
|
|
// curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
|
|
|
|
|
// curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
|
2014-06-20 17:59:52 +04:00
|
|
|
|
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if (self::METHOD_POST === $method) {
|
2013-07-08 15:08:14 +04:00
|
|
|
|
curl_setopt($ch, CURLOPT_POST, true);
|
2015-03-03 16:37:34 +03:00
|
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $parameters);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
|
|
|
|
|
2015-03-03 16:37:34 +03:00
|
|
|
|
$responseBody = curl_exec($ch);
|
2014-03-22 14:52:17 +04:00
|
|
|
|
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
|
2014-03-22 14:52:17 +04:00
|
|
|
|
$errno = curl_errno($ch);
|
|
|
|
|
$error = curl_error($ch);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
curl_close($ch);
|
|
|
|
|
|
2015-03-03 16:37:34 +03:00
|
|
|
|
if ($errno) {
|
|
|
|
|
throw new CurlException($error, $errno);
|
2014-03-22 14:52:17 +04:00
|
|
|
|
}
|
2015-03-03 16:37:34 +03:00
|
|
|
|
|
|
|
|
|
$result = json_decode($responseBody, true);
|
|
|
|
|
|
2014-03-22 14:52:17 +04:00
|
|
|
|
if (isset($result['generatedAt'])) {
|
|
|
|
|
$this->generatedAt = new \DateTime($result['generatedAt']);
|
|
|
|
|
unset($result['generatedAt']);
|
|
|
|
|
}
|
2015-03-03 16:37:34 +03:00
|
|
|
|
|
|
|
|
|
return new ApiResponse($statusCode, $responseBody);
|
2013-07-08 15:08:14 +04:00
|
|
|
|
}
|
2014-06-20 17:59:52 +04:00
|
|
|
|
}
|