2014-08-15 19:36:28 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
require_once __DIR__ . '/vendor/autoload.php';
|
|
|
|
|
|
|
|
class ApiHelper
|
|
|
|
{
|
|
|
|
private $dir, $fileDate;
|
|
|
|
protected $intaroApi, $log, $settings;
|
|
|
|
|
|
|
|
public function __construct($settings) {
|
2014-08-18 18:14:04 +04:00
|
|
|
$this->dir = __DIR__ . '/../../logs/';
|
2014-08-15 19:36:28 +04:00
|
|
|
$this->fileDate = $this->dir . 'intarocrm_history.log';
|
|
|
|
$this->settings = $settings;
|
|
|
|
$this->domain = $settings['domain'];
|
|
|
|
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->log = new Monolog\Logger('intarocrm');
|
|
|
|
$this->log->pushHandler(
|
|
|
|
new Monolog\Handler\StreamHandler($this->dir . 'intarocrm_module.log', Monolog\Logger::INFO)
|
|
|
|
);
|
|
|
|
|
2014-08-15 19:36:28 +04:00
|
|
|
$this->intaroApi = new IntaroCrm\RestApi(
|
|
|
|
$settings['intarocrm_url'],
|
|
|
|
$settings['intarocrm_apikey']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-08-28 02:13:37 +04:00
|
|
|
public function processOrder($data) {
|
2014-08-15 19:36:28 +04:00
|
|
|
|
|
|
|
$order = array();
|
|
|
|
$customer = array();
|
2014-08-28 02:13:37 +04:00
|
|
|
$customers = array();
|
2014-08-15 19:36:28 +04:00
|
|
|
|
|
|
|
$payment_code = $data['payment_code'];
|
|
|
|
$delivery_code = $data['shipping_code'];
|
|
|
|
$settings = $this->settings;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$customers = $this->intaroApi->customers($data['telephone'], $data['email'], $data['lastname'], 200, 0);
|
2014-08-28 02:13:37 +04:00
|
|
|
} catch (IntaroCrm\Exception\ApiException $e) {
|
2014-08-15 19:36:28 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::customers:' . $e->getMessage());
|
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::customers:' . json_encode($data));
|
2014-08-28 02:13:37 +04:00
|
|
|
} catch (IntaroCrm\Exception\CurlException $e) {
|
2014-08-15 19:36:28 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::customers::Curl:' . $e->getMessage());
|
|
|
|
}
|
|
|
|
|
2014-08-28 02:13:37 +04:00
|
|
|
|
2014-08-15 19:36:28 +04:00
|
|
|
if(count($customers) > 0 && isset($customers[0]['externalId'])) {
|
|
|
|
$order['customerId'] = $customers[0]['externalId'];
|
|
|
|
} else {
|
2014-09-05 16:38:09 +04:00
|
|
|
$order['customerId'] = ($data['customer_id'] != '') ? $data['customer_id'] : (int) substr((microtime(true) * 10000) . mt_rand(1, 1000), 10, -1);
|
2014-08-29 19:32:23 +04:00
|
|
|
$customer['externalId'] = $order['customerId'];
|
2014-08-15 19:36:28 +04:00
|
|
|
$customer['firstName'] = $data['firstname'];
|
|
|
|
$customer['lastName'] = $data['lastname'];
|
|
|
|
$customer['email'] = $data['email'];
|
2014-08-28 18:39:02 +04:00
|
|
|
$customer['phones'] = array(array('number' => $data['telephone']));
|
2014-08-15 19:36:28 +04:00
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
$customer['address']['country'] = $data['payment_country_id'];
|
|
|
|
$customer['address']['region'] = $data['payment_zone_id'];
|
|
|
|
|
2014-08-15 19:36:28 +04:00
|
|
|
$customer['address']['text'] = implode(', ', array(
|
|
|
|
$data['payment_postcode'],
|
|
|
|
$data['payment_country'],
|
|
|
|
$data['payment_city'],
|
|
|
|
$data['payment_address_1'],
|
|
|
|
$data['payment_address_2']
|
|
|
|
));
|
|
|
|
|
|
|
|
try {
|
2014-08-28 18:39:02 +04:00
|
|
|
$this->customer = $this->intaroApi->customerEdit($customer);
|
2014-08-28 02:13:37 +04:00
|
|
|
} catch (IntaroCrm\Exception\ApiException $e) {
|
2014-08-28 18:39:02 +04:00
|
|
|
$this->customer = $e->getMessage();
|
2014-08-15 19:36:28 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage());
|
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . json_encode($order));
|
2014-08-28 02:13:37 +04:00
|
|
|
} catch (IntaroCrm\Exception\CurlException $e) {
|
2014-08-28 18:39:02 +04:00
|
|
|
$this->customer = $e->getMessage();
|
2014-08-15 19:36:28 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderCreate::Curl:' . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 18:39:02 +04:00
|
|
|
unset($customer);
|
2014-08-28 02:13:37 +04:00
|
|
|
unset($customers);
|
|
|
|
|
2014-08-15 19:36:28 +04:00
|
|
|
$order['externalId'] = $data['order_id'];
|
|
|
|
$order['firstName'] = $data['firstname'];
|
|
|
|
$order['lastName'] = $data['lastname'];
|
|
|
|
$order['email'] = $data['email'];
|
|
|
|
$order['phone'] = $data['telephone'];
|
|
|
|
$order['customerComment'] = $data['comment'];
|
|
|
|
|
2014-08-28 18:39:02 +04:00
|
|
|
$deliveryCost = 0;
|
2014-08-29 19:32:23 +04:00
|
|
|
$orderTotals = isset($data['totals']) ? $data['totals'] : $data['order_total'] ;
|
|
|
|
|
|
|
|
foreach ($orderTotals as $totals) {
|
2014-08-15 19:36:28 +04:00
|
|
|
if ($totals['code'] == 'shipping') {
|
2014-08-28 18:39:02 +04:00
|
|
|
$deliveryCost = $totals['value'];
|
2014-08-15 19:36:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$order['createdAt'] = date('Y-m-d H:i:s');
|
|
|
|
$order['paymentType'] = $settings['intarocrm_payment'][$payment_code];
|
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
$country = (isset($data['shipping_country'])) ? $data['shipping_country'] : '' ;
|
|
|
|
|
2014-08-15 19:36:28 +04:00
|
|
|
$order['delivery'] = array(
|
|
|
|
'code' => $settings['intarocrm_delivery'][$delivery_code],
|
2014-08-28 18:39:02 +04:00
|
|
|
'cost' => $deliveryCost,
|
2014-08-29 19:32:23 +04:00
|
|
|
'address' => array(
|
|
|
|
'index' => $data['shipping_postcode'],
|
|
|
|
'city' => $data['shipping_city'],
|
2014-09-03 13:03:25 +04:00
|
|
|
'country' => $data['shipping_country_id'],
|
|
|
|
'region' => $data['shipping_zone_id'],
|
2014-08-29 19:32:23 +04:00
|
|
|
'text' => implode(', ', array(
|
|
|
|
$data['shipping_postcode'],
|
2014-09-03 13:03:25 +04:00
|
|
|
$country,
|
2014-08-29 19:32:23 +04:00
|
|
|
$data['shipping_city'],
|
|
|
|
$data['shipping_address_1'],
|
|
|
|
$data['shipping_address_2']
|
|
|
|
))
|
|
|
|
)
|
2014-08-15 19:36:28 +04:00
|
|
|
);
|
|
|
|
|
2014-08-29 19:32:23 +04:00
|
|
|
$orderProducts = isset($data['products']) ? $data['products'] : $data['order_product'];
|
|
|
|
|
|
|
|
foreach ($orderProducts as $product) {
|
2014-08-15 19:36:28 +04:00
|
|
|
$order['items'][] = array(
|
|
|
|
'productId' => $product['product_id'],
|
2014-08-29 19:32:23 +04:00
|
|
|
'productName' => $product['name'],
|
2014-08-15 19:36:28 +04:00
|
|
|
'initialPrice' => $product['price'],
|
|
|
|
'quantity' => $product['quantity'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-08-29 19:32:23 +04:00
|
|
|
if (isset($data['order_status_id'])) {
|
|
|
|
$order['status'] = $data['order_status'];
|
|
|
|
}
|
|
|
|
|
2014-08-15 19:36:28 +04:00
|
|
|
try {
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->intaroApi->orderEdit($order);
|
|
|
|
} catch (IntaroCrm\Exception\ApiException $e) {
|
2014-08-15 19:36:28 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . $e->getMessage());
|
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderCreate:' . json_encode($order));
|
2014-08-28 02:13:37 +04:00
|
|
|
} catch (IntaroCrm\Exception\CurlException $e) {
|
2014-08-15 19:36:28 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderCreate::Curl:' . $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function orderHistory() {
|
|
|
|
|
2014-08-28 02:13:37 +04:00
|
|
|
$orders = array();
|
|
|
|
|
2014-08-15 19:36:28 +04:00
|
|
|
try {
|
|
|
|
$orders = $this->intaroApi->orderHistory($this->getDate());
|
|
|
|
$this->saveDate($this->intaroApi->getGeneratedAt()->format('Y-m-d H:i:s'));
|
2014-08-28 02:13:37 +04:00
|
|
|
} catch (IntaroCrm\Exception\ApiException $e) {
|
2014-08-15 19:36:28 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderHistory:' . $e->getMessage());
|
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderHistory:' . json_encode($orders));
|
|
|
|
|
|
|
|
return false;
|
2014-08-28 02:13:37 +04:00
|
|
|
} catch (IntaroCrm\Exception\CurlException $e) {
|
2014-08-15 19:36:28 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderHistory::Curl:' . $e->getMessage());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-18 18:14:04 +04:00
|
|
|
return $orders;
|
|
|
|
}
|
2014-08-15 19:36:28 +04:00
|
|
|
|
2014-08-18 18:14:04 +04:00
|
|
|
public function orderFixExternalIds($data)
|
|
|
|
{
|
|
|
|
try {
|
2014-08-28 02:13:37 +04:00
|
|
|
return $this->intaroApi->orderFixExternalIds($data);
|
|
|
|
} catch (IntaroCrm\Exception\ApiException $e) {
|
2014-08-18 18:14:04 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . $e->getMessage());
|
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . json_encode($data));
|
2014-08-15 19:36:28 +04:00
|
|
|
|
2014-08-18 18:14:04 +04:00
|
|
|
return false;
|
2014-08-28 02:13:37 +04:00
|
|
|
} catch (IntaroCrm\Exception\CurlException $e) {
|
2014-08-18 18:14:04 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds::Curl:' . $e->getMessage());
|
2014-08-15 19:36:28 +04:00
|
|
|
|
2014-08-18 18:14:04 +04:00
|
|
|
return false;
|
2014-08-15 19:36:28 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-19 07:56:17 +04:00
|
|
|
public function customerFixExternalIds($data)
|
|
|
|
{
|
|
|
|
try {
|
2014-08-28 02:13:37 +04:00
|
|
|
return $this->intaroApi->customerFixExternalIds($data);
|
|
|
|
} catch (IntaroCrm\Exception\ApiException $e) {
|
2014-08-19 07:56:17 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds:' . $e->getMessage());
|
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds:' . json_encode($data));
|
|
|
|
|
|
|
|
return false;
|
2014-08-28 02:13:37 +04:00
|
|
|
} catch (IntaroCrm\Exception\CurlException $e) {
|
2014-08-19 07:56:17 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::customerFixExternalIds::Curl:' . $e->getMessage());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-03 13:03:25 +04:00
|
|
|
public function getOrder($order_id)
|
2014-08-19 07:56:17 +04:00
|
|
|
{
|
|
|
|
try {
|
2014-09-03 13:03:25 +04:00
|
|
|
return $this->intaroApi->orderGet($order_id);
|
2014-08-28 02:13:37 +04:00
|
|
|
} catch (IntaroCrm\Exception\ApiException $e) {
|
2014-08-19 07:56:17 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . $e->getMessage());
|
2014-08-28 02:13:37 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds:' . json_encode($order));
|
2014-08-19 07:56:17 +04:00
|
|
|
|
|
|
|
return false;
|
2014-08-28 02:13:37 +04:00
|
|
|
} catch (IntaroCrm\Exception\CurlException $e) {
|
2014-08-19 07:56:17 +04:00
|
|
|
$this->log->addError('['.$this->domain.'] RestApi::orderFixExternalIds::Curl:' . $e->getMessage());
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-08-15 19:36:28 +04:00
|
|
|
private function saveDate($date) {
|
|
|
|
file_put_contents($this->fileDate, $date, LOCK_EX);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getDate() {
|
|
|
|
if (file_exists($this->fileDate)) {
|
|
|
|
$result = file_get_contents($this->fileDate);
|
|
|
|
} else {
|
|
|
|
$result = date('Y-m-d H:i:s', strtotime('-2 days', strtotime(date('Y-m-d H:i:s'))));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2014-08-20 14:48:43 +04:00
|
|
|
}
|