895 lines
31 KiB
PHP
Raw Normal View History

2014-04-30 03:51:52 +04:00
<?php
2017-09-01 10:14:13 +02:00
/**
* @author Retail Driver LCC
* @copyright RetailCRM
* @license GPL
2018-10-24 16:15:28 +03:00
<<<<<<< 4dfc7b8d1acbf9bdc33ac6484f0d7f4171e774d6
2019-01-16 13:11:34 +03:00
* @version 2.2.9
2018-10-24 16:15:28 +03:00
=======
* @version 2.2.5
>>>>>>> v2.2.5
2017-09-01 10:14:13 +02:00
* @link https://retailcrm.ru
*
*/
if (function_exists('date_default_timezone_set') && function_exists('date_default_timezone_get')) {
date_default_timezone_set(@date_default_timezone_get());
2015-07-21 15:21:12 +03:00
}
if (!defined('_PS_VERSION_')) {
exit;
2014-04-30 03:51:52 +04:00
}
require_once(dirname(__FILE__) . '/bootstrap.php');
class RetailCRM extends Module
{
2018-05-28 17:09:31 +03:00
public $api = false;
2018-05-29 13:15:18 +03:00
public $default_lang;
public $default_currency;
public $default_country;
public $apiUrl;
public $apiKey;
public $apiVersion;
public $psVersion;
public $log;
public $confirmUninstall;
public $reference;
2018-05-28 17:09:31 +03:00
private $use_new_hooks = true;
2017-09-01 10:14:13 +02:00
public function __construct()
{
$this->name = 'retailcrm';
$this->tab = 'export';
2019-01-16 13:11:34 +03:00
$this->version = '2.2.9';
$this->author = 'Retail Driver LCC';
$this->displayName = $this->l('RetailCRM');
$this->description = $this->l('Integration module for RetailCRM');
$this->confirmUninstall = $this->l('Are you sure you want to uninstall?');
$this->default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
$this->default_currency = (int)Configuration::get('PS_CURRENCY_DEFAULT');
$this->default_country = (int)Configuration::get('PS_COUNTRY_DEFAULT');
$this->apiUrl = Configuration::get('RETAILCRM_ADDRESS');
$this->apiKey = Configuration::get('RETAILCRM_API_TOKEN');
$this->apiVersion = Configuration::get('RETAILCRM_API_VERSION');
$this->ps_versions_compliancy = array('min' => '1.5', 'max' => _PS_VERSION_);
2017-09-01 10:14:13 +02:00
$this->psVersion = Tools::substr(_PS_VERSION_, 0, 3);
$this->log = _PS_ROOT_DIR_ . '/retailcrm.log';
2017-09-01 10:14:13 +02:00
$this->module_key = '149c765c6cddcf35e1f13ea6c71e9fa5';
2017-09-01 10:14:13 +02:00
if ($this->psVersion == '1.6') {
$this->bootstrap = true;
$this->use_new_hooks = false;
}
if ($this->apiUrl && $this->apiKey) {
$this->api = new RetailcrmProxy($this->apiUrl, $this->apiKey, $this->log, $this->apiVersion);
$this->reference = new RetailcrmReferences($this->api);
}
parent::__construct();
}
2017-09-01 10:14:13 +02:00
public function install()
{
return (
parent::install() &&
$this->registerHook('newOrder') &&
$this->registerHook('actionOrderStatusPostUpdate') &&
$this->registerHook('actionPaymentConfirmation') &&
$this->registerHook('actionCustomerAccountAdd') &&
$this->registerHook('actionOrderEdited') &&
2018-10-24 16:15:28 +03:00
($this->use_new_hooks ? $this->registerHook('actionCustomerAccountUpdate') : true) &&
($this->use_new_hooks ? $this->registerHook('actionValidateCustomerAddressForm') : true)
);
}
2017-09-01 10:14:13 +02:00
public function uninstall()
{
$api = new RetailcrmProxy(
Configuration::get('RETAILCRM_ADDRESS'),
Configuration::get('RETAILCRM_API_TOKEN'),
_PS_ROOT_DIR_ . '/retailcrm.log',
Configuration::get('RETAILCRM_API_VERSION')
);
$clientId = Configuration::get('RETAILCRM_CLIENT_ID');
$this->integrationModule($api, $clientId, Configuration::get('RETAILCRM_API_VERSION'), false);
return parent::uninstall() &&
Configuration::deleteByName('RETAILCRM_ADDRESS') &&
Configuration::deleteByName('RETAILCRM_API_TOKEN') &&
Configuration::deleteByName('RETAILCRM_API_STATUS') &&
Configuration::deleteByName('RETAILCRM_API_DELIVERY') &&
Configuration::deleteByName('RETAILCRM_LAST_SYNC') &&
Configuration::deleteByName('RETAILCRM_API_VERSION') &&
Configuration::deleteByName('RETAILCRM_LAST_CUSTOMERS_SYNC') &&
Configuration::deleteByName('RETAILCRM_LAST_ORDERS_SYNC');
}
public function getContent()
{
$output = null;
$address = Configuration::get('RETAILCRM_ADDRESS');
$token = Configuration::get('RETAILCRM_API_TOKEN');
$version = Configuration::get('RETAILCRM_API_VERSION');
if (Tools::isSubmit('submit' . $this->name)) {
2017-09-01 10:14:13 +02:00
$address = (string)(Tools::getValue('RETAILCRM_ADDRESS'));
$token = (string)(Tools::getValue('RETAILCRM_API_TOKEN'));
$version = (string)(Tools::getValue('RETAILCRM_API_VERSION'));
$delivery = json_encode(Tools::getValue('RETAILCRM_API_DELIVERY'));
$status = json_encode(Tools::getValue('RETAILCRM_API_STATUS'));
$payment = json_encode(Tools::getValue('RETAILCRM_API_PAYMENT'));
$deliveryDefault = json_encode(Tools::getValue('RETAILCRM_API_DELIVERY_DEFAULT'));
$paymentDefault = json_encode(Tools::getValue('RETAILCRM_API_PAYMENT_DEFAULT'));
$statusExport = (string)(Tools::getValue('RETAILCRM_STATUS_EXPORT'));
$clientId = Configuration::get('RETAILCRM_CLIENT_ID');
$settings = array(
2017-09-01 10:14:13 +02:00
'address' => $address,
'token' => $token,
'version' => $version,
'clientId' => $clientId
);
$output .= $this->validateForm($settings, $output);
2018-09-03 16:22:56 +03:00
if ($output === '') {
Configuration::updateValue('RETAILCRM_ADDRESS', $address);
Configuration::updateValue('RETAILCRM_API_TOKEN', $token);
Configuration::updateValue('RETAILCRM_API_VERSION', $version);
Configuration::updateValue('RETAILCRM_API_DELIVERY', $delivery);
Configuration::updateValue('RETAILCRM_API_STATUS', $status);
Configuration::updateValue('RETAILCRM_API_PAYMENT', $payment);
Configuration::updateValue('RETAILCRM_API_DELIVERY_DEFAULT', $deliveryDefault);
Configuration::updateValue('RETAILCRM_API_PAYMENT_DEFAULT', $paymentDefault);
Configuration::updateValue('RETAILCRM_STATUS_EXPORT', $statusExport);
$output .= $this->displayConfirmation($this->l('Settings updated'));
}
if ($version == 5 && $this->isRegisteredInHook('actionPaymentCCAdd') == 0) {
$this->registerHook('actionPaymentCCAdd');
} elseif ($version == 4 && $this->isRegisteredInHook('actionPaymentCCAdd') == 1) {
$hook_id = Hook::getIdByName('actionPaymentCCAdd');
$this->unregisterHook($hook_id);
}
}
if ($address && $token) {
$this->api = new RetailcrmProxy($address, $token, $this->log, $version);
$this->reference = new RetailcrmReferences($this->api);
}
$output .= $this->displayConfirmation(
$this->l('Timezone settings must be identical to both of your crm and shop') .
2018-09-03 16:22:56 +03:00
"<a target=\"_blank\" href=\"$address/admin/settings#t-main\">$address/admin/settings#t-main</a>"
);
2018-05-28 17:09:31 +03:00
$this->display(__FILE__, 'retailcrm.tpl');
return $output . $this->displayForm();
}
public function displayForm()
{
$this->displayConfirmation($this->l('Settings updated'));
$default_lang = $this->default_lang;
$apiVersions = array(
array(
'option_id' => '4',
'name' => 'v4'
),
array(
'option_id' => '5',
'name' => 'v5'
)
);
2017-09-01 10:14:13 +02:00
$fields_form = array();
/*
* Network connection form
*/
$fields_form[0]['form'] = array(
'legend' => array(
'title' => $this->l('Network connection'),
),
'input' => array(
array(
'type' => 'select',
'name' => 'RETAILCRM_API_VERSION',
'label' => $this->l('API version'),
'options' => array(
'query' => $apiVersions,
2017-09-01 10:14:13 +02:00
'id' => 'option_id',
'name' => 'name'
)
),
array(
'type' => 'text',
'label' => $this->l('CRM address'),
'name' => 'RETAILCRM_ADDRESS',
'size' => 20,
'required' => true
),
array(
'type' => 'text',
'label' => $this->l('CRM token'),
'name' => 'RETAILCRM_API_TOKEN',
'size' => 20,
'required' => true
)
),
'submit' => array(
'title' => $this->l('Save'),
'class' => 'button'
)
);
if ($this->api) {
/*
* Delivery
*/
$fields_form[1]['form'] = array(
'legend' => array('title' => $this->l('Delivery')),
'input' => $this->reference->getDeliveryTypes(),
);
/*
* Order status
*/
$fields_form[2]['form'] = array(
'legend' => array('title' => $this->l('Order statuses')),
'input' => $this->reference->getStatuses(),
);
/*
* Payment
*/
$fields_form[3]['form'] = array(
'legend' => array('title' => $this->l('Payment types')),
'input' => $this->reference->getPaymentTypes(),
);
/*
* Default
*/
$fields_form[4]['form'] = array(
2017-06-16 16:13:25 +03:00
'legend' => array('title' => $this->l('Default')),
2017-09-01 10:14:13 +02:00
'input' => $this->reference->getPaymentAndDeliveryForDefault(
array($this->l('Delivery method'), $this->l('Payment type'))
),
);
/*
* Status in export
*/
$fields_form[5]['form'] = array(
'legend' => array('title' => $this->l('Default status')),
'input' => array(array(
'type' => 'select',
'name' => 'RETAILCRM_STATUS_EXPORT',
'label' => $this->l('Default status in export'),
'options' => array(
'query' => $this->reference->getStatuseDefaultExport(),
'id' => 'id_option',
'name' => 'name'
)
)),
);
}
/*
* Diplay forms
*/
$helper = new HelperForm();
$helper->module = $this;
$helper->name_controller = $this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->currentIndex = AdminController::$currentIndex . '&configure=' . $this->name;
$helper->default_form_language = $default_lang;
$helper->allow_employee_form_lang = $default_lang;
$helper->title = $this->displayName;
$helper->show_toolbar = true;
$helper->toolbar_scroll = true;
$helper->submit_action = 'submit' . $this->name;
$helper->toolbar_btn = array(
'save' =>
array(
'desc' => $this->l('Save'),
'href' => sprintf(
"%s&configure=%s&save%s&token=%s",
AdminController::$currentIndex,
$this->name,
$this->name,
Tools::getAdminTokenLite('AdminModules')
)
),
'back' => array(
'href' => AdminController::$currentIndex . '&token=' . Tools::getAdminTokenLite('AdminModules'),
'desc' => $this->l('Back to list')
)
);
$helper->fields_value['RETAILCRM_ADDRESS'] = Configuration::get('RETAILCRM_ADDRESS');
$helper->fields_value['RETAILCRM_API_TOKEN'] = Configuration::get('RETAILCRM_API_TOKEN');
$helper->fields_value['RETAILCRM_API_VERSION'] = Configuration::get('RETAILCRM_API_VERSION');
$helper->fields_value['RETAILCRM_STATUS_EXPORT'] = Configuration::get('RETAILCRM_STATUS_EXPORT');
$deliverySettings = Configuration::get('RETAILCRM_API_DELIVERY');
if (isset($deliverySettings) && $deliverySettings != '') {
$deliveryTypes = json_decode($deliverySettings);
if ($deliveryTypes) {
foreach ($deliveryTypes as $idx => $delivery) {
$name = 'RETAILCRM_API_DELIVERY[' . $idx . ']';
$helper->fields_value[$name] = $delivery;
}
}
}
$statusSettings = Configuration::get('RETAILCRM_API_STATUS');
if (isset($statusSettings) && $statusSettings != '') {
$statusTypes = json_decode($statusSettings);
if ($statusTypes) {
foreach ($statusTypes as $idx => $status) {
$name = 'RETAILCRM_API_STATUS[' . $idx . ']';
$helper->fields_value[$name] = $status;
}
}
}
$paymentSettings = Configuration::get('RETAILCRM_API_PAYMENT');
if (isset($paymentSettings) && $paymentSettings != '') {
$paymentTypes = json_decode($paymentSettings);
if ($paymentTypes) {
foreach ($paymentTypes as $idx => $payment) {
$name = 'RETAILCRM_API_PAYMENT[' . $idx . ']';
$helper->fields_value[$name] = $payment;
}
}
}
$paymentSettingsDefault = Configuration::get('RETAILCRM_API_PAYMENT_DEFAULT');
if (isset($paymentSettingsDefault) && $paymentSettingsDefault != '') {
$paymentTypesDefault = json_decode($paymentSettingsDefault);
if ($paymentTypesDefault) {
$name = 'RETAILCRM_API_PAYMENT_DEFAULT';
$helper->fields_value[$name] = $paymentTypesDefault;
}
}
$deliverySettingsDefault = Configuration::get('RETAILCRM_API_DELIVERY_DEFAULT');
if (isset($deliverySettingsDefault) && $deliverySettingsDefault != '') {
$deliveryTypesDefault = json_decode($deliverySettingsDefault);
if ($deliveryTypesDefault) {
$name = 'RETAILCRM_API_DELIVERY_DEFAULT';
$helper->fields_value[$name] = $deliveryTypesDefault;
}
}
return $helper->generateForm($fields_form);
}
public function hookActionCustomerAccountAdd($params)
{
2018-05-28 17:09:31 +03:00
$customer = $params['newCustomer'];
$customerSend = array(
'externalId' => $customer->id,
'firstName' => $customer->firstname,
'lastName' => $customer->lastname,
'email' => $customer->email,
'createdAt' => $customer->date_add
);
2018-05-28 17:09:31 +03:00
$this->api->customersCreate($customerSend);
return $customerSend;
}
// this hook added in 1.7
public function hookActionCustomerAccountUpdate($params)
{
2018-05-28 17:09:31 +03:00
$customer = $params['customer'];
2018-10-30 17:07:14 +03:00
2018-05-28 17:09:31 +03:00
$customerSend = array(
'externalId' => $customer->id,
'firstName' => $customer->firstname,
'lastName' => $customer->lastname,
'email' => $customer->email,
'birthday' => $customer->birthday
);
2018-05-28 17:09:31 +03:00
2018-10-30 17:07:14 +03:00
$addreses = $customer->getAddresses($this->default_lang);
$address = array_shift($addreses);
if (!empty($address)){
if (is_object($address)) {
$address = $this->addressParse($address);
} else {
$address = new Address($address['id_address']);
$address = $this->addressParse($address);
}
$customerSend = array_merge($customerSend, $address['customer']);
}
2018-10-24 16:15:28 +03:00
if (isset($params['cart'])){
$address = $this->addressParse($params['cart']);
2018-10-24 16:34:14 +03:00
$customerSend = array_merge($customerSend, $address['customer']);
2018-10-24 16:15:28 +03:00
}
$customerSend = array_merge($customerSend, $address['customer']);
2018-05-28 17:09:31 +03:00
$this->api->customersEdit($customerSend);
return $customerSend;
}
2018-10-24 16:15:28 +03:00
// this hook added in 1.7
public function hookActionValidateCustomerAddressForm($params)
{
$customer = new Customer($params['cart']->id_customer);
$customerAddress = array('customer' => $customer, 'cart' => $params['cart']);
return $this->hookActionCustomerAccountUpdate($customerAddress);
}
public function hookNewOrder($params)
{
return $this->hookActionOrderStatusPostUpdate($params);
}
public function hookActionPaymentConfirmation($params)
{
2018-05-28 17:09:31 +03:00
if ($this->apiVersion == 4) {
$this->api->ordersEdit(
array(
'externalId' => $params['id_order'],
'paymentStatus' => 'paid'
)
);
}
return $this->hookActionOrderStatusPostUpdate($params);
}
public function hookActionOrderEdited($params)
2017-09-01 10:14:13 +02:00
{
2018-08-30 11:37:18 +03:00
$order = array(
'externalId' => $params['order']->id,
'firstName' => $params['customer']->firstname,
'lastName' => $params['customer']->lastname,
'email' => $params['customer']->email,
'createdAt' => $params['order']->date_add,
'delivery' => array('cost' => $params['order']->total_shipping)
);
2018-05-28 17:09:31 +03:00
if ($this->apiVersion != 5) {
$order['discount'] = $params['order']->total_discounts;
} else {
$order['discountManualAmount'] = $params['order']->total_discounts;
}
$orderdb = new Order($params['order']->id);
2018-01-12 14:05:04 +03:00
$comment = $orderdb->getFirstMessage();
if ($comment !== false) {
$order['customerComment'] = $comment;
}
2018-09-03 16:22:56 +03:00
2018-01-12 14:05:04 +03:00
unset($comment);
foreach ($orderdb->getProducts() as $item) {
if (isset($item['product_attribute_id']) && $item['product_attribute_id'] > 0) {
$productId = $item['product_id'] . '#' . $item['product_attribute_id'];
} else {
$productId = $item['product_id'];
}
$order['items'][] = array(
'initialPrice' => $item['unit_price_tax_incl'],
'quantity' => $item['product_quantity'],
'offer' => array('externalId' => $productId),
'productName' => $item['product_name'],
);
}
$order['customer']['externalId'] = $params['order']->id_customer;
$this->api->ordersEdit($order);
2018-05-28 17:09:31 +03:00
return $order;
}
2018-10-24 16:15:28 +03:00
private function addressParse($address)
{
$addressCollection = $address->getAddressCollection();
$address = array_shift($addressCollection);
if ($address instanceof Address) {
$postcode = $address->postcode;
$city = $address->city;
$addres_line = sprintf("%s %s", $address->address1, $address->address2);
$countryIso = CountryCore::getIsoById($address->id_country);
}
if (!empty($postcode)) {
$customer['address']['index'] = $postcode;
$order['delivery']['address']['index'] = $postcode;
}
if (!empty($city)) {
$customer['address']['city'] = $city;
$order['delivery']['address']['city'] = $city;
}
if (!empty($addres_line)) {
$customer['address']['text'] = $addres_line;
$order['delivery']['address']['text'] = $addres_line;
}
if (!empty($countryIso)) {
$order['countryIso'] = $countryIso;
$customer['address']['countryIso'] = $countryIso;
}
$phones = $this->getPhone($address);
$order = array_merge($order, $phones['order']);
$customer = array_merge($customer, $phones['customer']);
$addressArray = array('order' => $order, 'customer' => $customer);
return $addressArray;
}
private function getPhone($address)
{
if (!empty($address->phone_mobile)){
$order['phone'] = $address->phone_mobile;
$customer['phones'][] = array('number'=> $address->phone_mobile);
}
if (!empty($address->phone)){
$order['additionalPhone'] = $address->phone;
$customer['phones'][] = array('number'=> $address->phone);
}
if (!isset($order['phone']) && !empty($order['additionalPhone'])){
$order['phone'] = $order['additionalPhone'];
unset($order['additionalPhone']);
}
$phonesArray = array('customer' => $customer, 'order' => $order);
return $phonesArray;
}
public function hookActionOrderStatusPostUpdate($params)
{
$delivery = json_decode(Configuration::get('RETAILCRM_API_DELIVERY'), true);
$payment = json_decode(Configuration::get('RETAILCRM_API_PAYMENT'), true);
$status = json_decode(Configuration::get('RETAILCRM_API_STATUS'), true);
if (isset($params['orderStatus'])) {
$customer = array(
'externalId' => $params['customer']->id,
'lastName' => $params['customer']->lastname,
'firstName' => $params['customer']->firstname,
'email' => $params['customer']->email,
'createdAt' => $params['customer']->date_add
);
$order = array(
'externalId' => $params['order']->id,
'firstName' => $params['customer']->firstname,
'lastName' => $params['customer']->lastname,
'email' => $params['customer']->email,
'createdAt' => $params['order']->date_add,
'delivery' => array('cost' => $params['order']->total_shipping)
);
2018-05-28 17:09:31 +03:00
if ($this->apiVersion != 5) {
$order['discount'] = $params['order']->total_discounts;
} else {
$order['discountManualAmount'] = $params['order']->total_discounts;
}
2018-05-28 17:09:31 +03:00
$cart = $params['cart'];
2018-10-24 16:15:28 +03:00
$addressCollection = $cart->getAddressCollection();
$address = array_shift($addressCollection);
2018-10-30 17:07:14 +03:00
$address = $this->addressParse($address);
2018-10-24 16:15:28 +03:00
2018-10-30 17:07:14 +03:00
$customer = array_merge($customer, $address['customer']);
$order = array_merge($order, $address['order']);
2018-08-30 11:37:18 +03:00
$comment = $params['order']->getFirstMessage();
2018-08-31 13:40:16 +03:00
if ($comment !== false) {
$order['customerComment'] = $comment;
}
2018-05-28 17:09:31 +03:00
foreach ($cart->getProducts() as $item) {
2017-09-01 10:14:13 +02:00
if (isset($item['id_product_attribute']) && $item['id_product_attribute'] > 0) {
$productId = $item['id_product'] . '#' . $item['id_product_attribute'];
} else {
$productId = $item['id_product'];
}
if ($item['attributes']) {
$arProp = array();
$count = 0;
$arAttr = explode(",", $item['attributes']);
2017-09-01 10:14:13 +02:00
foreach ($arAttr as $valAttr) {
$arItem = explode(":", $valAttr);
if ($arItem[0] && $arItem[1]) {
$arProp[$count]['name'] = trim($arItem[0]);
$arProp[$count]['value'] = trim($arItem[1]);
}
$count++;
}
}
2018-05-28 17:09:31 +03:00
$orderItem = array(
'initialPrice' => !empty($item['rate'])
? $item['price'] + ($item['price'] * $item['rate'] / 100)
: $item['price'],
'quantity' => $item['quantity'],
'offer' => array('externalId' => $productId),
2018-05-28 17:09:31 +03:00
'productName' => $item['name']
);
2018-05-28 17:09:31 +03:00
if (isset($arProp)) {
$orderItem['properties'] = $arProp;
}
$order['items'][] = $orderItem;
unset($arAttr);
unset($count);
unset($arProp);
}
$deliveryCode = $params['order']->id_carrier;
if (array_key_exists($deliveryCode, $delivery) && !empty($delivery[$deliveryCode])) {
$order['delivery']['code'] = $delivery[$deliveryCode];
}
if (Module::getInstanceByName('advancedcheckout') === false) {
2017-09-01 10:14:13 +02:00
$paymentCode = $params['order']->module;
} else {
$paymentCode = $params['order']->payment;
}
2018-05-30 09:58:53 +03:00
2018-05-28 17:09:31 +03:00
if ($this->apiVersion != 5) {
if (array_key_exists($paymentCode, $payment) && !empty($payment[$paymentCode])) {
$order['paymentType'] = $payment[$paymentCode];
}
} else {
2018-05-28 17:09:31 +03:00
$paymentSend = array(
'externalId' => $params['order']->id .'#'. $params['order']->reference,
'amount' => $params['order']->total_paid,
'type' => $payment[$paymentCode] ? $payment[$paymentCode] : ''
);
}
2018-05-28 17:09:31 +03:00
if (isset($paymentSend)) {
$order['payments'][] = $paymentSend;
2017-09-01 10:14:13 +02:00
}
$statusCode = $params['orderStatus']->id;
if (array_key_exists($statusCode, $status) && !empty($status[$statusCode])) {
$order['status'] = $status[$statusCode];
} else {
$order['status'] = 'new';
}
$customerCheck = $this->api->customersGet($customer['externalId']);
if ($customerCheck === false) {
$this->api->customersCreate($customer);
}
$order['customer']['externalId'] = $customer['externalId'];
2018-09-03 16:22:56 +03:00
$this->api->ordersCreate($order);
2018-05-28 17:09:31 +03:00
return $order;
2017-09-01 10:14:13 +02:00
} elseif (isset($params['newOrderStatus'])) {
$statusCode = $params['newOrderStatus']->id;
if (array_key_exists($statusCode, $status) && !empty($status[$statusCode])) {
$orderStatus = $status[$statusCode];
}
if (isset($orderStatus)) {
$this->api->ordersEdit(
array(
'externalId' => $params['id_order'],
'status' => $orderStatus
)
);
2018-05-28 17:09:31 +03:00
return $orderStatus;
}
}
2018-05-28 17:09:31 +03:00
return false;
}
public function hookActionPaymentCCAdd($params)
{
$order_id = Order::getOrderByCartId($params['cart']->id);
$payments = $this->reference->getSystemPaymentModules();
$paymentCRM = json_decode(Configuration::get('RETAILCRM_API_PAYMENT'), true);
foreach ($payments as $valPay) {
if ($valPay['name'] == $params['paymentCC']->payment_method) {
$payCode = $valPay['code'];
}
}
if (array_key_exists($payCode, $paymentCRM) && !empty($paymentCRM[$payCode])) {
$payment = $paymentCRM[$payCode];
}
2018-05-28 17:09:31 +03:00
$response = $this->api->ordersGet($order_id);
2017-09-27 11:54:20 +02:00
if ($response !== false) {
$orderCRM = $response['order'];
2018-05-28 17:09:31 +03:00
if ($orderCRM && $orderCRM['payments']) {
foreach ($orderCRM['payments'] as $orderPayment) {
if ($orderPayment['type'] == $payment) {
$updatePayment = $orderPayment;
$updatePayment['amount'] = $params['paymentCC']->amount;
$updatePayment['paidAt'] = $params['paymentCC']->date_add;
if ($params['paymentCC']->amount == $orderCRM['totalSumm']) {
$updatePayment['status'] = 'paid';
}
}
}
}
}
if (isset($updatePayment)) {
$this->api->ordersPaymentEdit($updatePayment);
2018-05-28 17:09:31 +03:00
return $updatePayment;
} else {
2018-05-28 17:09:31 +03:00
$createPayment = array(
'externalId' => $params['paymentCC']->id,
'amount' => $params['paymentCC']->amount,
'paidAt' => $params['paymentCC']->date_add,
'type' => $payment,
'status' => 'paid',
'order' => array(
'externalId' => $order_id,
),
);
2018-05-28 17:09:31 +03:00
$this->api->ordersPaymentCreate($createPayment);
return $createPayment;
}
2018-05-28 17:09:31 +03:00
return false;
}
2017-09-01 10:14:13 +02:00
private function validateCrmAddress($address)
{
if (preg_match("/https:\/\/(.*).retailcrm.ru/", $address) === 1) {
return true;
2017-09-01 10:14:13 +02:00
}
return false;
}
2017-09-01 10:14:13 +02:00
private function validateApiVersion($settings)
{
$api = new RetailcrmProxy(
$settings['address'],
$settings['token'],
_PS_ROOT_DIR_ . '/retailcrm.log',
$settings['version']
);
2017-09-01 10:14:13 +02:00
$response = $api->deliveryTypesList();
2017-09-27 11:54:20 +02:00
if ($response !== false) {
if (!$settings['clientId']) {
2018-10-24 16:35:07 +03:00
$clientId = uniqid();
$result = $this->integrationModule($api, $clientId, $settings['version']);
if ($result) {
Configuration::updateValue('RETAILCRM_CLIENT_ID', $clientId);
}
}
2017-09-01 10:14:13 +02:00
return true;
}
return false;
}
2017-09-01 10:14:13 +02:00
private function validateForm($settings, $output)
{
if (!$this->validateCrmAddress($settings['address']) || !Validate::isGenericName($settings['address'])) {
$output .= $this->displayError($this->l('Invalid or empty crm address'));
} elseif (!$settings['token'] || $settings['token'] == '') {
$output .= $this->displayError($this->l('Invalid or empty crm api token'));
} elseif (!$this->validateApiVersion($settings)) {
$output .= $this->displayError($this->l('The selected version of the API is unavailable'));
}
return $output;
}
/**
* Activate/deactivate module in marketplace retailCRM
*
* @param \RetailcrmProxy $apiClient
* @param string $clientId
* @param string $apiVersion
* @param boolean $active
*
* @return boolean
*/
private function integrationModule($apiClient, $clientId, $apiVersion, $active = true)
{
$scheme = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
$logo = 'https://s3.eu-central-1.amazonaws.com/retailcrm-billing/images/5b845ce986911-prestashop2.svg';
2018-10-24 16:35:07 +03:00
$integrationCode = 'prestashop';
$name = 'PrestaShop';
$accountUrl = $scheme . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
2018-10-25 13:37:56 +03:00
if ($apiVersion == '4') {
$configuration = array(
'name' => $name,
2018-10-24 16:35:07 +03:00
'code' => $integrationCode . '-' . $clientId,
'logo' => $logo,
'configurationUrl' => $accountUrl,
'active' => $active
);
$response = $apiClient->marketplaceSettingsEdit($configuration);
} else {
$configuration = array(
'clientId' => $clientId,
2018-10-24 16:35:07 +03:00
'code' => $integrationCode . '-' . $clientId,
'integrationCode' => $integrationCode,
'active' => $active,
'name' => $name,
'logo' => $logo,
'accountUrl' => $accountUrl
);
$response = $apiClient->integrationModulesEdit($configuration);
}
if (!$response) {
return false;
}
if ($response->isSuccessful()) {
return true;
}
return false;
}
}