Fix history and check api version (#16)
This commit is contained in:
parent
88f502a878
commit
a03d579b9d
@ -22,8 +22,6 @@
|
||||
|
||||
class WC_Retailcrm_Client_V3
|
||||
{
|
||||
const VERSION = 'v3';
|
||||
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
@ -38,18 +36,28 @@
|
||||
* @param string $apiKey
|
||||
* @param string $site
|
||||
*/
|
||||
public function __construct($url, $apiKey, $site = null)
|
||||
public function __construct($url, $apiKey, $version = null, $site = null)
|
||||
{
|
||||
if ('/' != substr($url, strlen($url) - 1, 1)) {
|
||||
$url .= '/';
|
||||
}
|
||||
|
||||
$url = $url . 'api/' . self::VERSION;
|
||||
$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
|
||||
*
|
||||
|
@ -22,8 +22,6 @@
|
||||
|
||||
class WC_Retailcrm_Client_V4
|
||||
{
|
||||
const VERSION = 'v4';
|
||||
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
@ -40,18 +38,28 @@
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function __construct($url, $apiKey, $site = null)
|
||||
public function __construct($url, $apiKey, $version = null, $site = null)
|
||||
{
|
||||
if ('/' !== $url[strlen($url) - 1]) {
|
||||
$url .= '/';
|
||||
}
|
||||
|
||||
$url = $url . 'api/' . self::VERSION;
|
||||
$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
|
||||
*
|
||||
|
@ -22,8 +22,6 @@
|
||||
|
||||
class WC_Retailcrm_Client_V5
|
||||
{
|
||||
const VERSION = 'v5';
|
||||
|
||||
protected $client;
|
||||
|
||||
/**
|
||||
@ -41,18 +39,28 @@
|
||||
* @throws \InvalidArgumentException
|
||||
*
|
||||
*/
|
||||
public function __construct($url, $apiKey, $site = null)
|
||||
public function __construct($url, $apiKey, $version = null, $site = null)
|
||||
{
|
||||
if ('/' !== $url[strlen($url) - 1]) {
|
||||
$url .= '/';
|
||||
}
|
||||
|
||||
$url = $url . 'api/' . self::VERSION;
|
||||
$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
|
||||
*
|
||||
|
@ -14,9 +14,8 @@ if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) :
|
||||
*/
|
||||
class WC_Retailcrm_Proxy
|
||||
{
|
||||
public function __construct($api_url, $api_key, $api_vers)
|
||||
public function __construct($api_url, $api_key, $api_vers = null)
|
||||
{
|
||||
if (!$api_vers) $api_vers = 'v4';
|
||||
$this->logger = new WC_Logger();
|
||||
|
||||
if ( ! class_exists( 'WC_Retailcrm_Client_V3' ) ) {
|
||||
@ -34,23 +33,27 @@ if ( ! class_exists( 'WC_Retailcrm_Proxy' ) ) :
|
||||
if ($api_url && $api_key) {
|
||||
switch ($api_vers) {
|
||||
case 'v3':
|
||||
$this->retailcrm = new WC_Retailcrm_Client_V3($api_url, $api_key);
|
||||
$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);
|
||||
$this->retailcrm = new WC_Retailcrm_Client_V4($api_url, $api_key, $api_vers);
|
||||
break;
|
||||
|
||||
case 'v5':
|
||||
$this->retailcrm = new WC_Retailcrm_Client_V5($api_url, $api_key);
|
||||
break;
|
||||
$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);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function __call($method, $arguments)
|
||||
{
|
||||
if (!isset($this->retailcrm)) return;
|
||||
if (!isset($this->retailcrm)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$response = call_user_func_array(array($this->retailcrm, $method), $arguments);
|
||||
|
||||
|
@ -276,29 +276,35 @@ if ( ! class_exists( 'WC_Retailcrm_Base' ) ) :
|
||||
}
|
||||
|
||||
public function validate_api_version_field( $key, $value ) {
|
||||
$versionMap = array(
|
||||
'v3' => '3.0',
|
||||
'v4' => '4.0',
|
||||
'v5' => '5.0'
|
||||
);
|
||||
|
||||
$api = new WC_Retailcrm_Proxy(
|
||||
$_POST['woocommerce_integration-retailcrm_api_url'],
|
||||
$_POST['woocommerce_integration-retailcrm_api_key'],
|
||||
$value
|
||||
$_POST['woocommerce_integration-retailcrm_api_key']
|
||||
);
|
||||
|
||||
$response = $api->deliveryTypesList();
|
||||
$response = $api->apiVersions();
|
||||
|
||||
if (isset($response['errorMsg']) && $response['errorMsg'] == 'API method not found') {
|
||||
WC_Admin_Settings::add_error( esc_html__( '"Выбранная версия API недоступна"', 'woocommerce-integration-retailcrm' ) );
|
||||
} else {
|
||||
return $value;
|
||||
if ($response && $response->isSuccessful()) {
|
||||
if (!in_array($versionMap[$value], $response['versions'])) {
|
||||
WC_Admin_Settings::add_error( esc_html__( '"Выбранная версия API недоступна"', 'woocommerce-integration-retailcrm' ) );
|
||||
} else {
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function validate_api_url_field( $key, $value ) {
|
||||
$api = new WC_Retailcrm_Proxy(
|
||||
$value,
|
||||
$_POST['woocommerce_integration-retailcrm_api_key'],
|
||||
'v4'
|
||||
$_POST['woocommerce_integration-retailcrm_api_key']
|
||||
);
|
||||
|
||||
$response = $api->deliveryTypesList();
|
||||
$response = $api->apiVersions();
|
||||
|
||||
if ($response == NULL) {
|
||||
WC_Admin_Settings::add_error( esc_html__( '"Введите корректный адрес CRM"', 'woocommerce-integration-retailcrm' ) );
|
||||
|
@ -61,6 +61,9 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
|
||||
$generatedAt = $response->generatedAt;
|
||||
|
||||
foreach ($response['history'] as $record) {
|
||||
if ($record['source'] == 'api' && $record['apiKey']['current'] == true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->removeFuncsHook();
|
||||
|
||||
@ -136,6 +139,9 @@ if ( ! class_exists( 'WC_Retailcrm_History' ) ) :
|
||||
$generatedAt = $response->generatedAt;
|
||||
|
||||
foreach ($response['history'] as $record) {
|
||||
if ($record['source'] == 'api' && $record['apiKey']['current'] == true) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->removeFuncsHook();
|
||||
|
||||
|
@ -297,7 +297,7 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
|
||||
if (!empty($user_data_billing['postcode'])) $order_data['delivery']['address']['index'] = $user_data_billing['postcode'];
|
||||
if (!empty($user_data_billing['city'])) $order_data['delivery']['address']['city'] = $user_data_billing['city'];
|
||||
if (!empty($user_data_billing['country'])) $order_data['delivery']['address']['countryIso'] = $user_data_billing['country'];
|
||||
|
||||
if (!empty($user_data_billing['state'])) $order_data['delivery']['address']['region'] = $user_data_billing['state'];
|
||||
}
|
||||
|
||||
$user_data = $order->get_address('shipping');
|
||||
@ -311,12 +311,13 @@ if ( ! class_exists( 'WC_Retailcrm_Orders' ) ) :
|
||||
if (!empty($user_data['postcode'])) $order_data['delivery']['address']['index'] = $user_data['postcode'];
|
||||
if (!empty($user_data['city'])) $order_data['delivery']['address']['city'] = $user_data['city'];
|
||||
if (!empty($user_data['country'])) $order_data['delivery']['address']['countryIso'] = $user_data['country'];
|
||||
|
||||
if (!empty($user_data['state'])) $order_data['delivery']['address']['region'] = $user_data['state'];
|
||||
}
|
||||
|
||||
$order_data['delivery']['address']['text'] = sprintf(
|
||||
"%s %s %s %s",
|
||||
"%s %s %s %s %s",
|
||||
!empty($user_data_billing['postcode']) ? $user_data_billing['postcode'] : $user_data['postcode'],
|
||||
!empty($user_data_billing['state']) ? $user_data_billing['state'] : $user_data['state'],
|
||||
!empty($user_data_billing['city']) ? $user_data_billing['city'] : $user_data['city'],
|
||||
!empty($user_data_billing['address_1']) ? $user_data_billing['address_1'] : $user_data['address_1'],
|
||||
!empty($user_data_billing['address_2']) ? $user_data_billing['address_2'] : $user_data['address_2']
|
||||
|
Loading…
x
Reference in New Issue
Block a user