OpenCart 🇺🇦

Класс Amazon { ... }

Название свойства Описание
Amazon::$token private mixed $token
Amazon::$encPass private mixed $encPass
Amazon::$encSalt private mixed $encSalt
Amazon::$server private mixed $server = 'http://uk-amazon.openbaypro.com/';
Amazon::$registry private mixed $registry
Тип Возвр. Описание
public mixed __construct ( $registry )
Исходный код метода:
$this->registry = $registry;

$this->token   = $registry->get('config')->get('openbay_amazon_token');
$this->encPass = $registry->get('config')->get('openbay_amazon_enc_string1');
$this->encSalt = $registry->get('config')->get('openbay_amazon_enc_string2');

public mixed __get ( $name )
Исходный код метода:
return $this->registry->get($name);
public mixed orderNew ( $orderId )
Исходный код метода:
if ($this->config->get('amazon_status') != 1) {
	return;
}

/* Is called from front-end? */
if (!defined('HTTPS_CATALOG')) {
	$this->load->model('openbay/amazon_order');
	$amazonOrderId = $this->model_openbay_amazon_order->getAmazonOrderId($orderId);

	$this->load->library('log');
	$logger = new Log('amazon_stocks.log');
	$logger->write('orderNew() called with order id: ' . $orderId);

	//Stock levels update
	if ($this->openbay->addonLoad('openstock')) {
		$logger->write('openStock found installed.');

		$osProducts = $this->osProducts($orderId);
		$logger->write(print_r($osProducts, true));
		$quantityData = array();
		foreach ($osProducts as $osProduct) {
			$amazonSkuRows = $this->getLinkedSkus($osProduct['pid'], $osProduct['var']);
			foreach($amazonSkuRows as $amazonSkuRow) {
				$quantityData[$amazonSkuRow['amazon_sku']] = $osProduct['qty_left'];
			}
		}
		if(!empty($quantityData)) {
			$logger->write('Updating quantities with data: ' . print_r($quantityData, true));
			$this->updateQuantities($quantityData);
		} else {
			$logger->write('No quantity data need to be posted.');
		}
	} else {
		$orderedProducts = $this->getOrderdProducts($orderId);
		$orderedProductIds = array();
		foreach($orderedProducts as $orderedProduct) {
			$orderedProductIds[] = $orderedProduct['product_id'];
		}
		$this->putStockUpdateBulk($orderedProductIds);
	}
	$logger->write('orderNew() exiting');
}
public mixed productUpdateListen ( $productId, $data )
Исходный код метода:
$logger = new Log('amazon_stocks.log');
$logger->write('productUpdateListen called for product id: ' . $productId);

if ($this->openbay->addonLoad('openstock') && (isset($data['has_option']) && $data['has_option'] == 1)) {
	$logger->write('openStock found installed and product has options.');
	$quantityData = array();
	foreach($data['product_option_stock'] as $optStock) {
		$amazonSkuRows = $this->getLinkedSkus($productId, $optStock['var']);
		foreach($amazonSkuRows as $amazonSkuRow) {
			$quantityData[$amazonSkuRow['amazon_sku']] = $optStock['stock'];
		}
	}
	if(!empty($quantityData)) {
		$logger->write('Updating quantities with data: ' . print_r($quantityData, true));
		$this->updateQuantities($quantityData);
	} else {
		$logger->write('No quantity data need to be posted.');
	}

} else {
	$this->putStockUpdateBulk(array($productId));
}
$logger->write('productUpdateListen() exiting');
public mixed updateOrder ( $orderId, $orderStatusString, $courier_id = '', $courierFromList = true, $tracking_no = '' )
Исходный код метода:
if ($this->config->get('amazon_status') != 1) {
	return;
}

/* Is called from admin? */
if (!defined('HTTPS_CATALOG')) {
	return;
}

$amazonOrder = $this->getOrder($orderId);

if(!$amazonOrder) {
	return;
}

$amazonOrderId = $amazonOrder['amazon_order_id'];


$log = new Log('amazon.log');
$log->write("Order's $amazonOrderId status changed to $orderStatusString");


$this->load->model('openbay/amazon');
$amazonOrderProducts = $this->model_openbay_amazon->getAmazonOrderedProducts($orderId);


$requestNode = new SimpleXMLElement('<Request/>');

$requestNode->addChild('AmazonOrderId', $amazonOrderId);
$requestNode->addChild('Status', $orderStatusString);

if(!empty($courier_id)) {
	if($courierFromList) {
		$requestNode->addChild('CourierId', $courier_id);
	} else {
		$requestNode->addChild('CourierOther', $courier_id);
	}
	$requestNode->addChild('TrackingNo', $tracking_no);
}

$orderItemsNode = $requestNode->addChild('OrderItems');

foreach ($amazonOrderProducts as $product) {
	$newOrderItem = $orderItemsNode->addChild('OrderItem');
	$newOrderItem->addChild('ItemId', htmlspecialchars($product['amazon_order_item_id']));
	$newOrderItem->addChild('Quantity', (int)$product['quantity']);
}

$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->loadXML($requestNode->asXML());
$doc->formatOutput = true;

$this->model_openbay_amazon->updateAmazonOrderTracking($orderId, $courier_id, $courierFromList, !empty($courier_id) ? $tracking_no : '');
$log->write('Request: ' . $doc->saveXML());
$response = $this->callWithResponse('order/update2', $doc->saveXML(), false);
$log->write("Response for Order's status update: $response");
public mixed getCategoryTemplates ( )
Исходный код метода:
$result = $this->callWithResponse("productv2/RequestTemplateList");
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}
public mixed registerInsertion ( $data )
Исходный код метода:
$result = $this->callWithResponse("productv2/RegisterInsertionRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}
public mixed insertProduct ( $data )
Исходный код метода:
$result = $this->callWithResponse("productv2/InsertProductRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}
public mixed updateQuantities ( $data )
Исходный код метода:
$result = $this->callWithResponse("product/UpdateQuantityRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}
public mixed getStockUpdatesStatus ( $data )
Исходный код метода:
$result = $this->callWithResponse("status/StockUpdates", $data);
if(isset($result)) {
	return $result;
} else {
	return false;
}
public mixed callNoResponse ( $method, $data = array(), $isJson = true )
Исходный код метода:
if  ($isJson) {
	$argString = json_encode($data);
} else {
	$argString = $data;
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$crypt = $this->encrypt($argString, $token, true);

$defaults = array(
	CURLOPT_POST => 1,
	CURLOPT_HEADER => 0,
	CURLOPT_URL => $this->server . $method,
	CURLOPT_USERAGENT => 'OpenBay Pro for Amazon/Opencart',
	CURLOPT_FRESH_CONNECT => 1,
	CURLOPT_RETURNTRANSFER => 1,
	CURLOPT_FORBID_REUSE => 1,
	CURLOPT_TIMEOUT => 2,
	CURLOPT_SSL_VERIFYPEER => 0,
	CURLOPT_SSL_VERIFYHOST => 0,
	CURLOPT_POSTFIELDS => 'token=' . $this->token . '&data=' . rawurlencode($crypt),
);
$ch = curl_init();

curl_setopt_array($ch, $defaults);

curl_exec($ch);

curl_close($ch);
public mixed callWithResponse ( $method, $data = array(), $isJson = true )
Исходный код метода:
if  ($isJson) {
	$argString = json_encode($data);
} else {
	$argString = $data;
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$crypt = $this->encrypt($argString, $token, true);

$defaults = array(
	CURLOPT_POST            => 1,
	CURLOPT_HEADER          => 0,
	CURLOPT_URL             => $this->server . $method,
	CURLOPT_USERAGENT       => 'OpenBay Pro for Amazon/Opencart',
	CURLOPT_FRESH_CONNECT   => 1,
	CURLOPT_RETURNTRANSFER  => 1,
	CURLOPT_FORBID_REUSE    => 1,
	CURLOPT_TIMEOUT         => 30,
	CURLOPT_SSL_VERIFYPEER  => 0,
	CURLOPT_SSL_VERIFYHOST  => 0,
	CURLOPT_POSTFIELDS      => 'token=' . $this->token . '&data=' . rawurlencode($crypt),
);
$ch = curl_init();

curl_setopt_array($ch, $defaults);

$response = curl_exec($ch);

curl_close($ch);

return $response;
public mixed decryptArgs ( $crypt, $isBase64 = true )
Исходный код метода:
if ($isBase64) {
	$crypt = base64_decode($crypt, true);
	if (!$crypt) {
		return false;
	}
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$data = $this->decrypt($crypt, $token);

return $data;
private mixed encrypt ( $msg, $k, $base64 = false )
Исходный код метода:
if (!$td = mcrypt_module_open('rijndael-256', '', 'ctr', ''))
	return false;

$iv = mcrypt_create_iv(32, MCRYPT_RAND);

if (mcrypt_generic_init($td, $k, $iv) !== 0)
	return false;

$msg = mcrypt_generic($td, $msg);
$msg = $iv . $msg;
$mac = $this->pbkdf2($msg, $k, 1000, 32);
$msg .= $mac;

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

if ($base64) {
	$msg = base64_encode($msg);
}

return $msg;
private mixed decrypt ( $msg, $k, $base64 = false )
Исходный код метода:
if ($base64) {
	$msg = base64_decode($msg);
}

if (!$td = mcrypt_module_open('rijndael-256', '', 'ctr', '')) {
	return false;
}

$iv = substr($msg, 0, 32);
$mo = strlen($msg) - 32;
$em = substr($msg, $mo);
$msg = substr($msg, 32, strlen($msg) - 64);
$mac = $this->pbkdf2($iv . $msg, $k, 1000, 32);

if ($em !== $mac) {
	return false;
}

if (mcrypt_generic_init($td, $k, $iv) !== 0) {
	return false;
}

$msg = mdecrypt_generic($td, $msg);
$msg = unserialize($msg);

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

return $msg;
private mixed pbkdf2 ( $p, $s, $c, $kl, $a = 'sha256' )
Исходный код метода:
$hl = strlen(hash($a, null, true));
$kb = ceil($kl / $hl);
$dk = '';

for ($block = 1; $block <= $kb; $block++) {

	$ib = $b = hash_hmac($a, $s . pack('N', $block), $p, true);

	for ($i = 1; $i < $c; $i++)
		$ib ^= ($b = hash_hmac($a, $b, $p, true));

	$dk .= $ib;
}

return substr($dk, 0, $kl);
public mixed getServer ( )
Исходный код метода:
return $this->server;
public mixed putStockUpdateBulk ( $productIdArray, $endInactive = false )
Исходный код метода:
Salt;
rivate $server = 'http://uk-amazon.openbaypro.com/';
rivate $registry;

ublic function __construct($registry) {
$this->registry = $registry;

$this->token   = $registry->get('config')->get('openbay_amazon_token');
$this->encPass = $registry->get('config')->get('openbay_amazon_enc_string1');
$this->encSalt = $registry->get('config')->get('openbay_amazon_enc_string2');



ublic function __get($name) {
return $this->registry->get($name);


ublic function orderNew($orderId) {
if ($this->config->get('amazon_status') != 1) {
	return;
}

/* Is called from front-end? */
if (!defined('HTTPS_CATALOG')) {
	$this->load->model('openbay/amazon_order');
	$amazonOrderId = $this->model_openbay_amazon_order->getAmazonOrderId($orderId);

	$this->load->library('log');
	$logger = new Log('amazon_stocks.log');
	$logger->write('orderNew() called with order id: ' . $orderId);

	//Stock levels update
	if ($this->openbay->addonLoad('openstock')) {
		$logger->write('openStock found installed.');

		$osProducts = $this->osProducts($orderId);
		$logger->write(print_r($osProducts, true));
		$quantityData = array();
		foreach ($osProducts as $osProduct) {
			$amazonSkuRows = $this->getLinkedSkus($osProduct['pid'], $osProduct['var']);
			foreach($amazonSkuRows as $amazonSkuRow) {
				$quantityData[$amazonSkuRow['amazon_sku']] = $osProduct['qty_left'];
			}
		}
		if(!empty($quantityData)) {
			$logger->write('Updating quantities with data: ' . print_r($quantityData, true));
			$this->updateQuantities($quantityData);
		} else {
			$logger->write('No quantity data need to be posted.');
		}
	} else {
		$orderedProducts = $this->getOrderdProducts($orderId);
		$orderedProductIds = array();
		foreach($orderedProducts as $orderedProduct) {
			$orderedProductIds[] = $orderedProduct['product_id'];
		}
		$this->putStockUpdateBulk($orderedProductIds);
	}
	$logger->write('orderNew() exiting');
}


ublic function productUpdateListen($productId, $data) {
$logger = new Log('amazon_stocks.log');
$logger->write('productUpdateListen called for product id: ' . $productId);

if ($this->openbay->addonLoad('openstock') && (isset($data['has_option']) && $data['has_option'] == 1)) {
	$logger->write('openStock found installed and product has options.');
	$quantityData = array();
	foreach($data['product_option_stock'] as $optStock) {
		$amazonSkuRows = $this->getLinkedSkus($productId, $optStock['var']);
		foreach($amazonSkuRows as $amazonSkuRow) {
			$quantityData[$amazonSkuRow['amazon_sku']] = $optStock['stock'];
		}
	}
	if(!empty($quantityData)) {
		$logger->write('Updating quantities with data: ' . print_r($quantityData, true));
		$this->updateQuantities($quantityData);
	} else {
		$logger->write('No quantity data need to be posted.');
	}

} else {
	$this->putStockUpdateBulk(array($productId));
}
$logger->write('productUpdateListen() exiting');


ublic function updateOrder($orderId, $orderStatusString, $courier_id = '', $courierFromList = true, $tracking_no = '') {

if ($this->config->get('amazon_status') != 1) {
	return;
}

/* Is called from admin? */
if (!defined('HTTPS_CATALOG')) {
	return;
}

$amazonOrder = $this->getOrder($orderId);

if(!$amazonOrder) {
	return;
}

$amazonOrderId = $amazonOrder['amazon_order_id'];


$log = new Log('amazon.log');
$log->write("Order's $amazonOrderId status changed to $orderStatusString");


$this->load->model('openbay/amazon');
$amazonOrderProducts = $this->model_openbay_amazon->getAmazonOrderedProducts($orderId);


$requestNode = new SimpleXMLElement('<Request/>');

$requestNode->addChild('AmazonOrderId', $amazonOrderId);
$requestNode->addChild('Status', $orderStatusString);

if(!empty($courier_id)) {
	if($courierFromList) {
		$requestNode->addChild('CourierId', $courier_id);
	} else {
		$requestNode->addChild('CourierOther', $courier_id);
	}
	$requestNode->addChild('TrackingNo', $tracking_no);
}

$orderItemsNode = $requestNode->addChild('OrderItems');

foreach ($amazonOrderProducts as $product) {
	$newOrderItem = $orderItemsNode->addChild('OrderItem');
	$newOrderItem->addChild('ItemId', htmlspecialchars($product['amazon_order_item_id']));
	$newOrderItem->addChild('Quantity', (int)$product['quantity']);
}

$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->loadXML($requestNode->asXML());
$doc->formatOutput = true;

$this->model_openbay_amazon->updateAmazonOrderTracking($orderId, $courier_id, $courierFromList, !empty($courier_id) ? $tracking_no : '');
$log->write('Request: ' . $doc->saveXML());
$response = $this->callWithResponse('order/update2', $doc->saveXML(), false);
$log->write("Response for Order's status update: $response");


ublic function getCategoryTemplates() {
$result = $this->callWithResponse("productv2/RequestTemplateList");
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function registerInsertion($data) {
$result = $this->callWithResponse("productv2/RegisterInsertionRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function insertProduct($data) {
$result = $this->callWithResponse("productv2/InsertProductRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function updateQuantities($data) {
$result = $this->callWithResponse("product/UpdateQuantityRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function getStockUpdatesStatus($data) {
$result = $this->callWithResponse("status/StockUpdates", $data);
if(isset($result)) {
	return $result;
} else {
	return false;
}


ublic function callNoResponse($method, $data = array(), $isJson = true) {
if  ($isJson) {
	$argString = json_encode($data);
} else {
	$argString = $data;
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$crypt = $this->encrypt($argString, $token, true);

$defaults = array(
	CURLOPT_POST => 1,
	CURLOPT_HEADER => 0,
	CURLOPT_URL => $this->server . $method,
	CURLOPT_USERAGENT => 'OpenBay Pro for Amazon/Opencart',
	CURLOPT_FRESH_CONNECT => 1,
	CURLOPT_RETURNTRANSFER => 1,
	CURLOPT_FORBID_REUSE => 1,
	CURLOPT_TIMEOUT => 2,
	CURLOPT_SSL_VERIFYPEER => 0,
	CURLOPT_SSL_VERIFYHOST => 0,
	CURLOPT_POSTFIELDS => 'token=' . $this->token . '&data=' . rawurlencode($crypt),
);
$ch = curl_init();

curl_setopt_array($ch, $defaults);

curl_exec($ch);

curl_close($ch);


ublic function callWithResponse($method, $data = array(), $isJson = true) {
if  ($isJson) {
	$argString = json_encode($data);
} else {
	$argString = $data;
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$crypt = $this->encrypt($argString, $token, true);

$defaults = array(
	CURLOPT_POST            => 1,
	CURLOPT_HEADER          => 0,
	CURLOPT_URL             => $this->server . $method,
	CURLOPT_USERAGENT       => 'OpenBay Pro for Amazon/Opencart',
	CURLOPT_FRESH_CONNECT   => 1,
	CURLOPT_RETURNTRANSFER  => 1,
	CURLOPT_FORBID_REUSE    => 1,
	CURLOPT_TIMEOUT         => 30,
	CURLOPT_SSL_VERIFYPEER  => 0,
	CURLOPT_SSL_VERIFYHOST  => 0,
	CURLOPT_POSTFIELDS      => 'token=' . $this->token . '&data=' . rawurlencode($crypt),
);
$ch = curl_init();

curl_setopt_array($ch, $defaults);

$response = curl_exec($ch);

curl_close($ch);

return $response;


ublic function decryptArgs($crypt, $isBase64 = true) {
if ($isBase64) {
	$crypt = base64_decode($crypt, true);
	if (!$crypt) {
		return false;
	}
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$data = $this->decrypt($crypt, $token);

return $data;


rivate function encrypt($msg, $k, $base64 = false) {
if (!$td = mcrypt_module_open('rijndael-256', '', 'ctr', ''))
	return false;

$iv = mcrypt_create_iv(32, MCRYPT_RAND);

if (mcrypt_generic_init($td, $k, $iv) !== 0)
	return false;

$msg = mcrypt_generic($td, $msg);
$msg = $iv . $msg;
$mac = $this->pbkdf2($msg, $k, 1000, 32);
$msg .= $mac;

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

if ($base64) {
	$msg = base64_encode($msg);
}

return $msg;


rivate function decrypt($msg, $k, $base64 = false) {
if ($base64) {
	$msg = base64_decode($msg);
}

if (!$td = mcrypt_module_open('rijndael-256', '', 'ctr', '')) {
	return false;
}

$iv = substr($msg, 0, 32);
$mo = strlen($msg) - 32;
$em = substr($msg, $mo);
$msg = substr($msg, 32, strlen($msg) - 64);
$mac = $this->pbkdf2($iv . $msg, $k, 1000, 32);

if ($em !== $mac) {
	return false;
}

if (mcrypt_generic_init($td, $k, $iv) !== 0) {
	return false;
}

$msg = mdecrypt_generic($td, $msg);
$msg = unserialize($msg);

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

return $msg;


rivate function pbkdf2($p, $s, $c, $kl, $a = 'sha256') {
$hl = strlen(hash($a, null, true));
$kb = ceil($kl / $hl);
$dk = '';

for ($block = 1; $block <= $kb; $block++) {

	$ib = $b = hash_hmac($a, $s . pack('N', $block), $p, true);

	for ($i = 1; $i < $c; $i++)
		$ib ^= ($b = hash_hmac($a, $b, $p, true));

	$dk .= $ib;
}

return substr($dk, 0, $kl);


ublic function getServer() {
return $this->server;


ublic function putStockUpdateBulk($productIdArray, $endInactive = false){
$this->load->library('log');
$logger = new Log('amazon_stocks.log');
$logger->write('Updating stock using putStockUpdateBulk()');
$quantityData = array();
foreach($productIdArray as $productId) {
	$amazonRows = $this->getLinkedSkus($productId);
	foreach($amazonRows as $amazonRow) {
		$productRow = $this->db->query("SELECT quantity, status FROM `" . DB_PREFIX . "product`
			WHERE `product_id` = '" . (int)$productId . "'")->row;

		if(!empty($productRow)) {
			if($endInactive && $productRow['status'] == '0') {
				$quantityData[$amazonRow['amazon_sku']] = 0;
			} else {
				$quantityData[$amazonRow['amazon_sku']] = $productRow['quantity'];
			}
		}
	}
}
if(!empty($quantityData)) {
	$logger->write('Quantity data to be sent:' . print_r($quantityData, true));
	$response = $this->updateQuantities($quantityData);
	$logger->write('Submit to API. Response: ' . print_r($response, true));
} else {
	$logger->write('No quantity data need to be posted.');
}


ublic function getLinkedSkus($productId, $var='') {
return $this->db->query("SELECT `amazon_sku` FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . (int)$productId . "' AND `var` = '" . $this->db->escape($var) . "'")->rows;


ublic function getOrderdProducts($orderId) {
return $this->db->query("SELECT `op`.`product_id`, `p`.`quantity` as `quantity_left`
	FROM `" . DB_PREFIX . "order_product` as `op`
	LEFT JOIN `" . DB_PREFIX . "product` as `p`
	ON `p`.`product_id` = `op`.`product_id`
	WHERE `op`.`order_id` = '" . (int)$orderId . "'
	")->rows;


ublic function osProducts($order_id){
$order_product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'");

$passArray = array();
foreach ($order_product_query->rows as $order_product) {
	$product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product` WHERE `product_id` = '".(int)$order_product['product_id']."' LIMIT 1");

	if (!empty($product_query->row)) {
		if (isset($product_query->row['has_option']) && ($product_query->row['has_option'] == 1)) {
			$pOption_query = $this->db->query("
				SELECT `oo`.`product_option_value_id`
				FROM `" . DB_PREFIX . "order_option` `oo`
					LEFT JOIN `" . DB_PREFIX . "product_option_value` `pov` ON (`pov`.`product_option_value_id` = `oo`.`product_option_value_id`)
					LEFT JOIN `" . DB_PREFIX . "option` `o` ON (`o`.`option_id` = `pov`.`option_id`)
				WHERE `oo`.`order_product_id` = '" . (int)$order_product['order_product_id'] . "'
				AND `oo`.`order_id` = '" . (int)$order_id . "'
				AND ((`o`.`type` = 'radio') OR (`o`.`type` = 'select') OR (`o`.`type` = 'image'))
				ORDER BY `oo`.`order_option_id`
				ASC");

			if ($pOption_query->num_rows != 0) {
				$pOptions = array();
				foreach ($pOption_query->rows as $pOptionRow) {
					$pOptions[] = $pOptionRow['product_option_value_id'];
				}

				$var = implode(':', $pOptions);
				$qtyLeftRow = $this->db->query("SELECT `stock` FROM `" . DB_PREFIX . "product_option_relation` WHERE `product_id` = '" . (int)$order_product['product_id'] . "' AND `var` = '" . $this->db->escape($var) . "'")->row;

				if(empty($qtyLeftRow)) {
					$qtyLeftRow['stock'] = 0;
				}

				$passArray[] = array('pid' => $order_product['product_id'], 'qty_left' => $qtyLeftRow['stock'], 'var' => $var);
			}
		} else {
			$passArray[] = array('pid' => $order_product['product_id'], 'qty_left' => $product_query->row['quantity'], 'var' => '');
		}
	}
}

return $passArray;


ublic function validate(){
if($this->config->get('amazon_status') != 0 &&
	$this->config->get('openbay_amazon_token') != '' &&
	$this->config->get('openbay_amazon_enc_string1') != '' &&
	$this->config->get('openbay_amazon_enc_string2') != ''){
	return true;
}else{
	return false;
}


ublic function deleteProduct($product_id){
$this->db->query("DELETE FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . $this->db->escape($product_id) . "'");


ublic function deleteOrder($order_id){
/**
 * @todo
 */


ublic function getOrder($orderId) {
$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_order` WHERE `order_id` = '".(int)$orderId."' LIMIT 1");

if($qry->num_rows > 0){
	return $qry->row;
}else{
	return false;
}


ublic function getCarriers() {
return array(
	"Blue Package",
	"Canada Post",
	"City Link",
	"DHL",
	"DHL Global Mail",
	"Fastway",
	"FedEx",
	"FedEx SmartPost",
	"GLS",
	"GO!",
	"Hermes Logistik Gruppe",
	"Newgistics",
	"NipponExpress",
	"OSM",
	"OnTrac",
	"Parcelforce",
	"Royal Mail",
	"SagawaExpress",
	"Streamlite",
	"TNT",
	"Target",
	"UPS",
	"UPS Mail Innovations",
	"USPS",
	"YamatoTransport",
);


ublic function parseCategoryTemplate($xml) {
$simplexml = null;

libxml_use_internal_errors(true);
if(($simplexml = simplexml_load_string($xml)) == false) {
	return false;
}

$category = (string)$simplexml->filename;

$tabs = array();
foreach($simplexml->tabs->tab as $tab) {
	$attributes = $tab->attributes();
	$tabs[] = array(
		'id' => (string)$attributes['id'],
		'name' => (string)$tab->name,
	);
}

$fields = array();
$fieldTypes = array('required', 'desired', 'optional');
foreach ($fieldTypes as $type) {
	foreach ($simplexml->fields->$type->field as $field) {
		$attributes = $field->attributes();
		$fields[] = array(
			'name' => (string)$attributes['name'],
			'title' => (string)$field->title,
			'definition' => (string)$field->definition,
			'accepted' => (array)$field->accepted,
			'type' => (string)$type,
			'child' => false,
			'order' => isset($attributes['order']) ? (string)$attributes['order'] : '',
			'tab' => (string)$attributes['tab'],
		);
	}
	foreach ($simplexml->fields->$type->childfield as $field) {
		$attributes = $field->attributes();
		$fields[] = array(
			'name' => (string)$attributes['name'],
			'title' => (string)$field->title,
			'definition' => (string)$field->definition,
			'accepted' => (array)$field->accepted,
			'type' => (string)$type,
			'child' => true,
			'parent' => (array)$field->parent,
			'order' => isset($attributes['order']) ? (string)$attributes['order'] : '',
			'tab' => (string)$attributes['tab'],
		);
	}
}

foreach($fields as $index => $field) {
	$fields[$index]['unordered_index'] = $index;
}

usort($fields, array('Amazon','compareFields'));

return array(
	'category' => $category,
	'fields' => $fields,
	'tabs' => $tabs,
);


rivate static function compareFields($field1, $field2) {
if($field1['order'] == $field2['order']) {
	return ($field1['unordered_index'] < $field2['unordered_index']) ? -1 : 1;
} else if(!empty($field1['order']) && empty($field2['order'])) {
	return -1;
} else if(!empty($field2['order']) && empty($field1['order'])) {
	return 1;
} else {
	return ($field1['order'] < $field2['order']) ? -1 : 1;
}

public mixed getLinkedSkus ( $productId, $var='' )
Исходный код метода:
return $this->db->query("SELECT `amazon_sku` FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . (int)$productId . "' AND `var` = '" . $this->db->escape($var) . "'")->rows;
public mixed getOrderdProducts ( $orderId )
Исходный код метода:
return $this->db->query("SELECT `op`.`product_id`, `p`.`quantity` as `quantity_left`
	FROM `" . DB_PREFIX . "order_product` as `op`
	LEFT JOIN `" . DB_PREFIX . "product` as `p`
	ON `p`.`product_id` = `op`.`product_id`
	WHERE `op`.`order_id` = '" . (int)$orderId . "'
	")->rows;
public mixed osProducts ( $order_id )
Исходный код метода:
en;
rivate $encPass;
rivate $encSalt;
rivate $server = 'http://uk-amazon.openbaypro.com/';
rivate $registry;

ublic function __construct($registry) {
$this->registry = $registry;

$this->token   = $registry->get('config')->get('openbay_amazon_token');
$this->encPass = $registry->get('config')->get('openbay_amazon_enc_string1');
$this->encSalt = $registry->get('config')->get('openbay_amazon_enc_string2');



ublic function __get($name) {
return $this->registry->get($name);


ublic function orderNew($orderId) {
if ($this->config->get('amazon_status') != 1) {
	return;
}

/* Is called from front-end? */
if (!defined('HTTPS_CATALOG')) {
	$this->load->model('openbay/amazon_order');
	$amazonOrderId = $this->model_openbay_amazon_order->getAmazonOrderId($orderId);

	$this->load->library('log');
	$logger = new Log('amazon_stocks.log');
	$logger->write('orderNew() called with order id: ' . $orderId);

	//Stock levels update
	if ($this->openbay->addonLoad('openstock')) {
		$logger->write('openStock found installed.');

		$osProducts = $this->osProducts($orderId);
		$logger->write(print_r($osProducts, true));
		$quantityData = array();
		foreach ($osProducts as $osProduct) {
			$amazonSkuRows = $this->getLinkedSkus($osProduct['pid'], $osProduct['var']);
			foreach($amazonSkuRows as $amazonSkuRow) {
				$quantityData[$amazonSkuRow['amazon_sku']] = $osProduct['qty_left'];
			}
		}
		if(!empty($quantityData)) {
			$logger->write('Updating quantities with data: ' . print_r($quantityData, true));
			$this->updateQuantities($quantityData);
		} else {
			$logger->write('No quantity data need to be posted.');
		}
	} else {
		$orderedProducts = $this->getOrderdProducts($orderId);
		$orderedProductIds = array();
		foreach($orderedProducts as $orderedProduct) {
			$orderedProductIds[] = $orderedProduct['product_id'];
		}
		$this->putStockUpdateBulk($orderedProductIds);
	}
	$logger->write('orderNew() exiting');
}


ublic function productUpdateListen($productId, $data) {
$logger = new Log('amazon_stocks.log');
$logger->write('productUpdateListen called for product id: ' . $productId);

if ($this->openbay->addonLoad('openstock') && (isset($data['has_option']) && $data['has_option'] == 1)) {
	$logger->write('openStock found installed and product has options.');
	$quantityData = array();
	foreach($data['product_option_stock'] as $optStock) {
		$amazonSkuRows = $this->getLinkedSkus($productId, $optStock['var']);
		foreach($amazonSkuRows as $amazonSkuRow) {
			$quantityData[$amazonSkuRow['amazon_sku']] = $optStock['stock'];
		}
	}
	if(!empty($quantityData)) {
		$logger->write('Updating quantities with data: ' . print_r($quantityData, true));
		$this->updateQuantities($quantityData);
	} else {
		$logger->write('No quantity data need to be posted.');
	}

} else {
	$this->putStockUpdateBulk(array($productId));
}
$logger->write('productUpdateListen() exiting');


ublic function updateOrder($orderId, $orderStatusString, $courier_id = '', $courierFromList = true, $tracking_no = '') {

if ($this->config->get('amazon_status') != 1) {
	return;
}

/* Is called from admin? */
if (!defined('HTTPS_CATALOG')) {
	return;
}

$amazonOrder = $this->getOrder($orderId);

if(!$amazonOrder) {
	return;
}

$amazonOrderId = $amazonOrder['amazon_order_id'];


$log = new Log('amazon.log');
$log->write("Order's $amazonOrderId status changed to $orderStatusString");


$this->load->model('openbay/amazon');
$amazonOrderProducts = $this->model_openbay_amazon->getAmazonOrderedProducts($orderId);


$requestNode = new SimpleXMLElement('<Request/>');

$requestNode->addChild('AmazonOrderId', $amazonOrderId);
$requestNode->addChild('Status', $orderStatusString);

if(!empty($courier_id)) {
	if($courierFromList) {
		$requestNode->addChild('CourierId', $courier_id);
	} else {
		$requestNode->addChild('CourierOther', $courier_id);
	}
	$requestNode->addChild('TrackingNo', $tracking_no);
}

$orderItemsNode = $requestNode->addChild('OrderItems');

foreach ($amazonOrderProducts as $product) {
	$newOrderItem = $orderItemsNode->addChild('OrderItem');
	$newOrderItem->addChild('ItemId', htmlspecialchars($product['amazon_order_item_id']));
	$newOrderItem->addChild('Quantity', (int)$product['quantity']);
}

$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->loadXML($requestNode->asXML());
$doc->formatOutput = true;

$this->model_openbay_amazon->updateAmazonOrderTracking($orderId, $courier_id, $courierFromList, !empty($courier_id) ? $tracking_no : '');
$log->write('Request: ' . $doc->saveXML());
$response = $this->callWithResponse('order/update2', $doc->saveXML(), false);
$log->write("Response for Order's status update: $response");


ublic function getCategoryTemplates() {
$result = $this->callWithResponse("productv2/RequestTemplateList");
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function registerInsertion($data) {
$result = $this->callWithResponse("productv2/RegisterInsertionRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function insertProduct($data) {
$result = $this->callWithResponse("productv2/InsertProductRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function updateQuantities($data) {
$result = $this->callWithResponse("product/UpdateQuantityRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function getStockUpdatesStatus($data) {
$result = $this->callWithResponse("status/StockUpdates", $data);
if(isset($result)) {
	return $result;
} else {
	return false;
}


ublic function callNoResponse($method, $data = array(), $isJson = true) {
if  ($isJson) {
	$argString = json_encode($data);
} else {
	$argString = $data;
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$crypt = $this->encrypt($argString, $token, true);

$defaults = array(
	CURLOPT_POST => 1,
	CURLOPT_HEADER => 0,
	CURLOPT_URL => $this->server . $method,
	CURLOPT_USERAGENT => 'OpenBay Pro for Amazon/Opencart',
	CURLOPT_FRESH_CONNECT => 1,
	CURLOPT_RETURNTRANSFER => 1,
	CURLOPT_FORBID_REUSE => 1,
	CURLOPT_TIMEOUT => 2,
	CURLOPT_SSL_VERIFYPEER => 0,
	CURLOPT_SSL_VERIFYHOST => 0,
	CURLOPT_POSTFIELDS => 'token=' . $this->token . '&data=' . rawurlencode($crypt),
);
$ch = curl_init();

curl_setopt_array($ch, $defaults);

curl_exec($ch);

curl_close($ch);


ublic function callWithResponse($method, $data = array(), $isJson = true) {
if  ($isJson) {
	$argString = json_encode($data);
} else {
	$argString = $data;
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$crypt = $this->encrypt($argString, $token, true);

$defaults = array(
	CURLOPT_POST            => 1,
	CURLOPT_HEADER          => 0,
	CURLOPT_URL             => $this->server . $method,
	CURLOPT_USERAGENT       => 'OpenBay Pro for Amazon/Opencart',
	CURLOPT_FRESH_CONNECT   => 1,
	CURLOPT_RETURNTRANSFER  => 1,
	CURLOPT_FORBID_REUSE    => 1,
	CURLOPT_TIMEOUT         => 30,
	CURLOPT_SSL_VERIFYPEER  => 0,
	CURLOPT_SSL_VERIFYHOST  => 0,
	CURLOPT_POSTFIELDS      => 'token=' . $this->token . '&data=' . rawurlencode($crypt),
);
$ch = curl_init();

curl_setopt_array($ch, $defaults);

$response = curl_exec($ch);

curl_close($ch);

return $response;


ublic function decryptArgs($crypt, $isBase64 = true) {
if ($isBase64) {
	$crypt = base64_decode($crypt, true);
	if (!$crypt) {
		return false;
	}
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$data = $this->decrypt($crypt, $token);

return $data;


rivate function encrypt($msg, $k, $base64 = false) {
if (!$td = mcrypt_module_open('rijndael-256', '', 'ctr', ''))
	return false;

$iv = mcrypt_create_iv(32, MCRYPT_RAND);

if (mcrypt_generic_init($td, $k, $iv) !== 0)
	return false;

$msg = mcrypt_generic($td, $msg);
$msg = $iv . $msg;
$mac = $this->pbkdf2($msg, $k, 1000, 32);
$msg .= $mac;

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

if ($base64) {
	$msg = base64_encode($msg);
}

return $msg;


rivate function decrypt($msg, $k, $base64 = false) {
if ($base64) {
	$msg = base64_decode($msg);
}

if (!$td = mcrypt_module_open('rijndael-256', '', 'ctr', '')) {
	return false;
}

$iv = substr($msg, 0, 32);
$mo = strlen($msg) - 32;
$em = substr($msg, $mo);
$msg = substr($msg, 32, strlen($msg) - 64);
$mac = $this->pbkdf2($iv . $msg, $k, 1000, 32);

if ($em !== $mac) {
	return false;
}

if (mcrypt_generic_init($td, $k, $iv) !== 0) {
	return false;
}

$msg = mdecrypt_generic($td, $msg);
$msg = unserialize($msg);

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

return $msg;


rivate function pbkdf2($p, $s, $c, $kl, $a = 'sha256') {
$hl = strlen(hash($a, null, true));
$kb = ceil($kl / $hl);
$dk = '';

for ($block = 1; $block <= $kb; $block++) {

	$ib = $b = hash_hmac($a, $s . pack('N', $block), $p, true);

	for ($i = 1; $i < $c; $i++)
		$ib ^= ($b = hash_hmac($a, $b, $p, true));

	$dk .= $ib;
}

return substr($dk, 0, $kl);


ublic function getServer() {
return $this->server;


ublic function putStockUpdateBulk($productIdArray, $endInactive = false){
$this->load->library('log');
$logger = new Log('amazon_stocks.log');
$logger->write('Updating stock using putStockUpdateBulk()');
$quantityData = array();
foreach($productIdArray as $productId) {
	$amazonRows = $this->getLinkedSkus($productId);
	foreach($amazonRows as $amazonRow) {
		$productRow = $this->db->query("SELECT quantity, status FROM `" . DB_PREFIX . "product`
			WHERE `product_id` = '" . (int)$productId . "'")->row;

		if(!empty($productRow)) {
			if($endInactive && $productRow['status'] == '0') {
				$quantityData[$amazonRow['amazon_sku']] = 0;
			} else {
				$quantityData[$amazonRow['amazon_sku']] = $productRow['quantity'];
			}
		}
	}
}
if(!empty($quantityData)) {
	$logger->write('Quantity data to be sent:' . print_r($quantityData, true));
	$response = $this->updateQuantities($quantityData);
	$logger->write('Submit to API. Response: ' . print_r($response, true));
} else {
	$logger->write('No quantity data need to be posted.');
}


ublic function getLinkedSkus($productId, $var='') {
return $this->db->query("SELECT `amazon_sku` FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . (int)$productId . "' AND `var` = '" . $this->db->escape($var) . "'")->rows;


ublic function getOrderdProducts($orderId) {
return $this->db->query("SELECT `op`.`product_id`, `p`.`quantity` as `quantity_left`
	FROM `" . DB_PREFIX . "order_product` as `op`
	LEFT JOIN `" . DB_PREFIX . "product` as `p`
	ON `p`.`product_id` = `op`.`product_id`
	WHERE `op`.`order_id` = '" . (int)$orderId . "'
	")->rows;


ublic function osProducts($order_id){
$order_product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'");

$passArray = array();
foreach ($order_product_query->rows as $order_product) {
	$product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product` WHERE `product_id` = '".(int)$order_product['product_id']."' LIMIT 1");

	if (!empty($product_query->row)) {
		if (isset($product_query->row['has_option']) && ($product_query->row['has_option'] == 1)) {
			$pOption_query = $this->db->query("
				SELECT `oo`.`product_option_value_id`
				FROM `" . DB_PREFIX . "order_option` `oo`
					LEFT JOIN `" . DB_PREFIX . "product_option_value` `pov` ON (`pov`.`product_option_value_id` = `oo`.`product_option_value_id`)
					LEFT JOIN `" . DB_PREFIX . "option` `o` ON (`o`.`option_id` = `pov`.`option_id`)
				WHERE `oo`.`order_product_id` = '" . (int)$order_product['order_product_id'] . "'
				AND `oo`.`order_id` = '" . (int)$order_id . "'
				AND ((`o`.`type` = 'radio') OR (`o`.`type` = 'select') OR (`o`.`type` = 'image'))
				ORDER BY `oo`.`order_option_id`
				ASC");

			if ($pOption_query->num_rows != 0) {
				$pOptions = array();
				foreach ($pOption_query->rows as $pOptionRow) {
					$pOptions[] = $pOptionRow['product_option_value_id'];
				}

				$var = implode(':', $pOptions);
				$qtyLeftRow = $this->db->query("SELECT `stock` FROM `" . DB_PREFIX . "product_option_relation` WHERE `product_id` = '" . (int)$order_product['product_id'] . "' AND `var` = '" . $this->db->escape($var) . "'")->row;

				if(empty($qtyLeftRow)) {
					$qtyLeftRow['stock'] = 0;
				}

				$passArray[] = array('pid' => $order_product['product_id'], 'qty_left' => $qtyLeftRow['stock'], 'var' => $var);
			}
		} else {
			$passArray[] = array('pid' => $order_product['product_id'], 'qty_left' => $product_query->row['quantity'], 'var' => '');
		}
	}
}

return $passArray;


ublic function validate(){
if($this->config->get('amazon_status') != 0 &&
	$this->config->get('openbay_amazon_token') != '' &&
	$this->config->get('openbay_amazon_enc_string1') != '' &&
	$this->config->get('openbay_amazon_enc_string2') != ''){
	return true;
}else{
	return false;
}


ublic function deleteProduct($product_id){
$this->db->query("DELETE FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . $this->db->escape($product_id) . "'");


ublic function deleteOrder($order_id){
/**
 * @todo
 */


ublic function getOrder($orderId) {
$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_order` WHERE `order_id` = '".(int)$orderId."' LIMIT 1");

if($qry->num_rows > 0){
	return $qry->row;
}else{
	return false;
}


ublic function getCarriers() {
return array(
	"Blue Package",
	"Canada Post",
	"City Link",
	"DHL",
	"DHL Global Mail",
	"Fastway",
	"FedEx",
	"FedEx SmartPost",
	"GLS",
	"GO!",
	"Hermes Logistik Gruppe",
	"Newgistics",
	"NipponExpress",
	"OSM",
	"OnTrac",
	"Parcelforce",
	"Royal Mail",
	"SagawaExpress",
	"Streamlite",
	"TNT",
	"Target",
	"UPS",
	"UPS Mail Innovations",
	"USPS",
	"YamatoTransport",
);


ublic function parseCategoryTemplate($xml) {
$simplexml = null;

libxml_use_internal_errors(true);
if(($simplexml = simplexml_load_string($xml)) == false) {
	return false;
}

$category = (string)$simplexml->filename;

$tabs = array();
foreach($simplexml->tabs->tab as $tab) {
	$attributes = $tab->attributes();
	$tabs[] = array(
		'id' => (string)$attributes['id'],
		'name' => (string)$tab->name,
	);
}

$fields = array();
$fieldTypes = array('required', 'desired', 'optional');
foreach ($fieldTypes as $type) {
	foreach ($simplexml->fields->$type->field as $field) {
		$attributes = $field->attributes();
		$fields[] = array(
			'name' => (string)$attributes['name'],
			'title' => (string)$field->title,
			'definition' => (string)$field->definition,
			'accepted' => (array)$field->accepted,
			'type' => (string)$type,
			'child' => false,
			'order' => isset($attributes['order']) ? (string)$attributes['order'] : '',
			'tab' => (string)$attributes['tab'],
		);
	}
	foreach ($simplexml->fields->$type->childfield as $field) {
		$attributes = $field->attributes();
		$fields[] = array(
			'name' => (string)$attributes['name'],
			'title' => (string)$field->title,
			'definition' => (string)$field->definition,
			'accepted' => (array)$field->accepted,
			'type' => (string)$type,
			'child' => true,
			'parent' => (array)$field->parent,
			'order' => isset($attributes['order']) ? (string)$attributes['order'] : '',
			'tab' => (string)$attributes['tab'],
		);
	}
}

foreach($fields as $index => $field) {
	$fields[$index]['unordered_index'] = $index;
}

usort($fields, array('Amazon','compareFields'));

return array(
	'category' => $category,
	'fields' => $fields,
	'tabs' => $tabs,
);


rivate static function compareFields($field1, $field2) {
if($field1['order'] == $field2['order']) {
	return ($field1['unordered_index'] < $field2['unordered_index']) ? -1 : 1;
} else if(!empty($field1['order']) && empty($field2['order'])) {
	return -1;
} else if(!empty($field2['order']) && empty($field1['order'])) {
	return 1;
} else {
	return ($field1['order'] < $field2['order']) ? -1 : 1;
}

public mixed validate ( )
Исходный код метода:
rivate $token;
rivate $encPass;
rivate $encSalt;
rivate $server = 'http://uk-amazon.openbaypro.com/';
rivate $registry;

ublic function __construct($registry) {
$this->registry = $registry;

$this->token   = $registry->get('config')->get('openbay_amazon_token');
$this->encPass = $registry->get('config')->get('openbay_amazon_enc_string1');
$this->encSalt = $registry->get('config')->get('openbay_amazon_enc_string2');



ublic function __get($name) {
return $this->registry->get($name);


ublic function orderNew($orderId) {
if ($this->config->get('amazon_status') != 1) {
	return;
}

/* Is called from front-end? */
if (!defined('HTTPS_CATALOG')) {
	$this->load->model('openbay/amazon_order');
	$amazonOrderId = $this->model_openbay_amazon_order->getAmazonOrderId($orderId);

	$this->load->library('log');
	$logger = new Log('amazon_stocks.log');
	$logger->write('orderNew() called with order id: ' . $orderId);

	//Stock levels update
	if ($this->openbay->addonLoad('openstock')) {
		$logger->write('openStock found installed.');

		$osProducts = $this->osProducts($orderId);
		$logger->write(print_r($osProducts, true));
		$quantityData = array();
		foreach ($osProducts as $osProduct) {
			$amazonSkuRows = $this->getLinkedSkus($osProduct['pid'], $osProduct['var']);
			foreach($amazonSkuRows as $amazonSkuRow) {
				$quantityData[$amazonSkuRow['amazon_sku']] = $osProduct['qty_left'];
			}
		}
		if(!empty($quantityData)) {
			$logger->write('Updating quantities with data: ' . print_r($quantityData, true));
			$this->updateQuantities($quantityData);
		} else {
			$logger->write('No quantity data need to be posted.');
		}
	} else {
		$orderedProducts = $this->getOrderdProducts($orderId);
		$orderedProductIds = array();
		foreach($orderedProducts as $orderedProduct) {
			$orderedProductIds[] = $orderedProduct['product_id'];
		}
		$this->putStockUpdateBulk($orderedProductIds);
	}
	$logger->write('orderNew() exiting');
}


ublic function productUpdateListen($productId, $data) {
$logger = new Log('amazon_stocks.log');
$logger->write('productUpdateListen called for product id: ' . $productId);

if ($this->openbay->addonLoad('openstock') && (isset($data['has_option']) && $data['has_option'] == 1)) {
	$logger->write('openStock found installed and product has options.');
	$quantityData = array();
	foreach($data['product_option_stock'] as $optStock) {
		$amazonSkuRows = $this->getLinkedSkus($productId, $optStock['var']);
		foreach($amazonSkuRows as $amazonSkuRow) {
			$quantityData[$amazonSkuRow['amazon_sku']] = $optStock['stock'];
		}
	}
	if(!empty($quantityData)) {
		$logger->write('Updating quantities with data: ' . print_r($quantityData, true));
		$this->updateQuantities($quantityData);
	} else {
		$logger->write('No quantity data need to be posted.');
	}

} else {
	$this->putStockUpdateBulk(array($productId));
}
$logger->write('productUpdateListen() exiting');


ublic function updateOrder($orderId, $orderStatusString, $courier_id = '', $courierFromList = true, $tracking_no = '') {

if ($this->config->get('amazon_status') != 1) {
	return;
}

/* Is called from admin? */
if (!defined('HTTPS_CATALOG')) {
	return;
}

$amazonOrder = $this->getOrder($orderId);

if(!$amazonOrder) {
	return;
}

$amazonOrderId = $amazonOrder['amazon_order_id'];


$log = new Log('amazon.log');
$log->write("Order's $amazonOrderId status changed to $orderStatusString");


$this->load->model('openbay/amazon');
$amazonOrderProducts = $this->model_openbay_amazon->getAmazonOrderedProducts($orderId);


$requestNode = new SimpleXMLElement('<Request/>');

$requestNode->addChild('AmazonOrderId', $amazonOrderId);
$requestNode->addChild('Status', $orderStatusString);

if(!empty($courier_id)) {
	if($courierFromList) {
		$requestNode->addChild('CourierId', $courier_id);
	} else {
		$requestNode->addChild('CourierOther', $courier_id);
	}
	$requestNode->addChild('TrackingNo', $tracking_no);
}

$orderItemsNode = $requestNode->addChild('OrderItems');

foreach ($amazonOrderProducts as $product) {
	$newOrderItem = $orderItemsNode->addChild('OrderItem');
	$newOrderItem->addChild('ItemId', htmlspecialchars($product['amazon_order_item_id']));
	$newOrderItem->addChild('Quantity', (int)$product['quantity']);
}

$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->loadXML($requestNode->asXML());
$doc->formatOutput = true;

$this->model_openbay_amazon->updateAmazonOrderTracking($orderId, $courier_id, $courierFromList, !empty($courier_id) ? $tracking_no : '');
$log->write('Request: ' . $doc->saveXML());
$response = $this->callWithResponse('order/update2', $doc->saveXML(), false);
$log->write("Response for Order's status update: $response");


ublic function getCategoryTemplates() {
$result = $this->callWithResponse("productv2/RequestTemplateList");
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function registerInsertion($data) {
$result = $this->callWithResponse("productv2/RegisterInsertionRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function insertProduct($data) {
$result = $this->callWithResponse("productv2/InsertProductRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function updateQuantities($data) {
$result = $this->callWithResponse("product/UpdateQuantityRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function getStockUpdatesStatus($data) {
$result = $this->callWithResponse("status/StockUpdates", $data);
if(isset($result)) {
	return $result;
} else {
	return false;
}


ublic function callNoResponse($method, $data = array(), $isJson = true) {
if  ($isJson) {
	$argString = json_encode($data);
} else {
	$argString = $data;
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$crypt = $this->encrypt($argString, $token, true);

$defaults = array(
	CURLOPT_POST => 1,
	CURLOPT_HEADER => 0,
	CURLOPT_URL => $this->server . $method,
	CURLOPT_USERAGENT => 'OpenBay Pro for Amazon/Opencart',
	CURLOPT_FRESH_CONNECT => 1,
	CURLOPT_RETURNTRANSFER => 1,
	CURLOPT_FORBID_REUSE => 1,
	CURLOPT_TIMEOUT => 2,
	CURLOPT_SSL_VERIFYPEER => 0,
	CURLOPT_SSL_VERIFYHOST => 0,
	CURLOPT_POSTFIELDS => 'token=' . $this->token . '&data=' . rawurlencode($crypt),
);
$ch = curl_init();

curl_setopt_array($ch, $defaults);

curl_exec($ch);

curl_close($ch);


ublic function callWithResponse($method, $data = array(), $isJson = true) {
if  ($isJson) {
	$argString = json_encode($data);
} else {
	$argString = $data;
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$crypt = $this->encrypt($argString, $token, true);

$defaults = array(
	CURLOPT_POST            => 1,
	CURLOPT_HEADER          => 0,
	CURLOPT_URL             => $this->server . $method,
	CURLOPT_USERAGENT       => 'OpenBay Pro for Amazon/Opencart',
	CURLOPT_FRESH_CONNECT   => 1,
	CURLOPT_RETURNTRANSFER  => 1,
	CURLOPT_FORBID_REUSE    => 1,
	CURLOPT_TIMEOUT         => 30,
	CURLOPT_SSL_VERIFYPEER  => 0,
	CURLOPT_SSL_VERIFYHOST  => 0,
	CURLOPT_POSTFIELDS      => 'token=' . $this->token . '&data=' . rawurlencode($crypt),
);
$ch = curl_init();

curl_setopt_array($ch, $defaults);

$response = curl_exec($ch);

curl_close($ch);

return $response;


ublic function decryptArgs($crypt, $isBase64 = true) {
if ($isBase64) {
	$crypt = base64_decode($crypt, true);
	if (!$crypt) {
		return false;
	}
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$data = $this->decrypt($crypt, $token);

return $data;


rivate function encrypt($msg, $k, $base64 = false) {
if (!$td = mcrypt_module_open('rijndael-256', '', 'ctr', ''))
	return false;

$iv = mcrypt_create_iv(32, MCRYPT_RAND);

if (mcrypt_generic_init($td, $k, $iv) !== 0)
	return false;

$msg = mcrypt_generic($td, $msg);
$msg = $iv . $msg;
$mac = $this->pbkdf2($msg, $k, 1000, 32);
$msg .= $mac;

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

if ($base64) {
	$msg = base64_encode($msg);
}

return $msg;


rivate function decrypt($msg, $k, $base64 = false) {
if ($base64) {
	$msg = base64_decode($msg);
}

if (!$td = mcrypt_module_open('rijndael-256', '', 'ctr', '')) {
	return false;
}

$iv = substr($msg, 0, 32);
$mo = strlen($msg) - 32;
$em = substr($msg, $mo);
$msg = substr($msg, 32, strlen($msg) - 64);
$mac = $this->pbkdf2($iv . $msg, $k, 1000, 32);

if ($em !== $mac) {
	return false;
}

if (mcrypt_generic_init($td, $k, $iv) !== 0) {
	return false;
}

$msg = mdecrypt_generic($td, $msg);
$msg = unserialize($msg);

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

return $msg;


rivate function pbkdf2($p, $s, $c, $kl, $a = 'sha256') {
$hl = strlen(hash($a, null, true));
$kb = ceil($kl / $hl);
$dk = '';

for ($block = 1; $block <= $kb; $block++) {

	$ib = $b = hash_hmac($a, $s . pack('N', $block), $p, true);

	for ($i = 1; $i < $c; $i++)
		$ib ^= ($b = hash_hmac($a, $b, $p, true));

	$dk .= $ib;
}

return substr($dk, 0, $kl);


ublic function getServer() {
return $this->server;


ublic function putStockUpdateBulk($productIdArray, $endInactive = false){
$this->load->library('log');
$logger = new Log('amazon_stocks.log');
$logger->write('Updating stock using putStockUpdateBulk()');
$quantityData = array();
foreach($productIdArray as $productId) {
	$amazonRows = $this->getLinkedSkus($productId);
	foreach($amazonRows as $amazonRow) {
		$productRow = $this->db->query("SELECT quantity, status FROM `" . DB_PREFIX . "product`
			WHERE `product_id` = '" . (int)$productId . "'")->row;

		if(!empty($productRow)) {
			if($endInactive && $productRow['status'] == '0') {
				$quantityData[$amazonRow['amazon_sku']] = 0;
			} else {
				$quantityData[$amazonRow['amazon_sku']] = $productRow['quantity'];
			}
		}
	}
}
if(!empty($quantityData)) {
	$logger->write('Quantity data to be sent:' . print_r($quantityData, true));
	$response = $this->updateQuantities($quantityData);
	$logger->write('Submit to API. Response: ' . print_r($response, true));
} else {
	$logger->write('No quantity data need to be posted.');
}


ublic function getLinkedSkus($productId, $var='') {
return $this->db->query("SELECT `amazon_sku` FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . (int)$productId . "' AND `var` = '" . $this->db->escape($var) . "'")->rows;


ublic function getOrderdProducts($orderId) {
return $this->db->query("SELECT `op`.`product_id`, `p`.`quantity` as `quantity_left`
	FROM `" . DB_PREFIX . "order_product` as `op`
	LEFT JOIN `" . DB_PREFIX . "product` as `p`
	ON `p`.`product_id` = `op`.`product_id`
	WHERE `op`.`order_id` = '" . (int)$orderId . "'
	")->rows;


ublic function osProducts($order_id){
$order_product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'");

$passArray = array();
foreach ($order_product_query->rows as $order_product) {
	$product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product` WHERE `product_id` = '".(int)$order_product['product_id']."' LIMIT 1");

	if (!empty($product_query->row)) {
		if (isset($product_query->row['has_option']) && ($product_query->row['has_option'] == 1)) {
			$pOption_query = $this->db->query("
				SELECT `oo`.`product_option_value_id`
				FROM `" . DB_PREFIX . "order_option` `oo`
					LEFT JOIN `" . DB_PREFIX . "product_option_value` `pov` ON (`pov`.`product_option_value_id` = `oo`.`product_option_value_id`)
					LEFT JOIN `" . DB_PREFIX . "option` `o` ON (`o`.`option_id` = `pov`.`option_id`)
				WHERE `oo`.`order_product_id` = '" . (int)$order_product['order_product_id'] . "'
				AND `oo`.`order_id` = '" . (int)$order_id . "'
				AND ((`o`.`type` = 'radio') OR (`o`.`type` = 'select') OR (`o`.`type` = 'image'))
				ORDER BY `oo`.`order_option_id`
				ASC");

			if ($pOption_query->num_rows != 0) {
				$pOptions = array();
				foreach ($pOption_query->rows as $pOptionRow) {
					$pOptions[] = $pOptionRow['product_option_value_id'];
				}

				$var = implode(':', $pOptions);
				$qtyLeftRow = $this->db->query("SELECT `stock` FROM `" . DB_PREFIX . "product_option_relation` WHERE `product_id` = '" . (int)$order_product['product_id'] . "' AND `var` = '" . $this->db->escape($var) . "'")->row;

				if(empty($qtyLeftRow)) {
					$qtyLeftRow['stock'] = 0;
				}

				$passArray[] = array('pid' => $order_product['product_id'], 'qty_left' => $qtyLeftRow['stock'], 'var' => $var);
			}
		} else {
			$passArray[] = array('pid' => $order_product['product_id'], 'qty_left' => $product_query->row['quantity'], 'var' => '');
		}
	}
}

return $passArray;


ublic function validate(){
if($this->config->get('amazon_status') != 0 &&
	$this->config->get('openbay_amazon_token') != '' &&
	$this->config->get('openbay_amazon_enc_string1') != '' &&
	$this->config->get('openbay_amazon_enc_string2') != ''){
	return true;
}else{
	return false;
}


ublic function deleteProduct($product_id){
$this->db->query("DELETE FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . $this->db->escape($product_id) . "'");


ublic function deleteOrder($order_id){
/**
 * @todo
 */


ublic function getOrder($orderId) {
$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_order` WHERE `order_id` = '".(int)$orderId."' LIMIT 1");

if($qry->num_rows > 0){
	return $qry->row;
}else{
	return false;
}


ublic function getCarriers() {
return array(
	"Blue Package",
	"Canada Post",
	"City Link",
	"DHL",
	"DHL Global Mail",
	"Fastway",
	"FedEx",
	"FedEx SmartPost",
	"GLS",
	"GO!",
	"Hermes Logistik Gruppe",
	"Newgistics",
	"NipponExpress",
	"OSM",
	"OnTrac",
	"Parcelforce",
	"Royal Mail",
	"SagawaExpress",
	"Streamlite",
	"TNT",
	"Target",
	"UPS",
	"UPS Mail Innovations",
	"USPS",
	"YamatoTransport",
);


ublic function parseCategoryTemplate($xml) {
$simplexml = null;

libxml_use_internal_errors(true);
if(($simplexml = simplexml_load_string($xml)) == false) {
	return false;
}

$category = (string)$simplexml->filename;

$tabs = array();
foreach($simplexml->tabs->tab as $tab) {
	$attributes = $tab->attributes();
	$tabs[] = array(
		'id' => (string)$attributes['id'],
		'name' => (string)$tab->name,
	);
}

$fields = array();
$fieldTypes = array('required', 'desired', 'optional');
foreach ($fieldTypes as $type) {
	foreach ($simplexml->fields->$type->field as $field) {
		$attributes = $field->attributes();
		$fields[] = array(
			'name' => (string)$attributes['name'],
			'title' => (string)$field->title,
			'definition' => (string)$field->definition,
			'accepted' => (array)$field->accepted,
			'type' => (string)$type,
			'child' => false,
			'order' => isset($attributes['order']) ? (string)$attributes['order'] : '',
			'tab' => (string)$attributes['tab'],
		);
	}
	foreach ($simplexml->fields->$type->childfield as $field) {
		$attributes = $field->attributes();
		$fields[] = array(
			'name' => (string)$attributes['name'],
			'title' => (string)$field->title,
			'definition' => (string)$field->definition,
			'accepted' => (array)$field->accepted,
			'type' => (string)$type,
			'child' => true,
			'parent' => (array)$field->parent,
			'order' => isset($attributes['order']) ? (string)$attributes['order'] : '',
			'tab' => (string)$attributes['tab'],
		);
	}
}

foreach($fields as $index => $field) {
	$fields[$index]['unordered_index'] = $index;
}

usort($fields, array('Amazon','compareFields'));

return array(
	'category' => $category,
	'fields' => $fields,
	'tabs' => $tabs,
);


rivate static function compareFields($field1, $field2) {
if($field1['order'] == $field2['order']) {
	return ($field1['unordered_index'] < $field2['unordered_index']) ? -1 : 1;
} else if(!empty($field1['order']) && empty($field2['order'])) {
	return -1;
} else if(!empty($field2['order']) && empty($field1['order'])) {
	return 1;
} else {
	return ($field1['order'] < $field2['order']) ? -1 : 1;
}

public mixed deleteProduct ( $product_id )
Исходный код метода:
rivate $encPass;
rivate $encSalt;
rivate $server = 'http://uk-amazon.openbaypro.com/';
rivate $registry;

ublic function __construct($registry) {
$this->registry = $registry;

$this->token   = $registry->get('config')->get('openbay_amazon_token');
$this->encPass = $registry->get('config')->get('openbay_amazon_enc_string1');
$this->encSalt = $registry->get('config')->get('openbay_amazon_enc_string2');



ublic function __get($name) {
return $this->registry->get($name);


ublic function orderNew($orderId) {
if ($this->config->get('amazon_status') != 1) {
	return;
}

/* Is called from front-end? */
if (!defined('HTTPS_CATALOG')) {
	$this->load->model('openbay/amazon_order');
	$amazonOrderId = $this->model_openbay_amazon_order->getAmazonOrderId($orderId);

	$this->load->library('log');
	$logger = new Log('amazon_stocks.log');
	$logger->write('orderNew() called with order id: ' . $orderId);

	//Stock levels update
	if ($this->openbay->addonLoad('openstock')) {
		$logger->write('openStock found installed.');

		$osProducts = $this->osProducts($orderId);
		$logger->write(print_r($osProducts, true));
		$quantityData = array();
		foreach ($osProducts as $osProduct) {
			$amazonSkuRows = $this->getLinkedSkus($osProduct['pid'], $osProduct['var']);
			foreach($amazonSkuRows as $amazonSkuRow) {
				$quantityData[$amazonSkuRow['amazon_sku']] = $osProduct['qty_left'];
			}
		}
		if(!empty($quantityData)) {
			$logger->write('Updating quantities with data: ' . print_r($quantityData, true));
			$this->updateQuantities($quantityData);
		} else {
			$logger->write('No quantity data need to be posted.');
		}
	} else {
		$orderedProducts = $this->getOrderdProducts($orderId);
		$orderedProductIds = array();
		foreach($orderedProducts as $orderedProduct) {
			$orderedProductIds[] = $orderedProduct['product_id'];
		}
		$this->putStockUpdateBulk($orderedProductIds);
	}
	$logger->write('orderNew() exiting');
}


ublic function productUpdateListen($productId, $data) {
$logger = new Log('amazon_stocks.log');
$logger->write('productUpdateListen called for product id: ' . $productId);

if ($this->openbay->addonLoad('openstock') && (isset($data['has_option']) && $data['has_option'] == 1)) {
	$logger->write('openStock found installed and product has options.');
	$quantityData = array();
	foreach($data['product_option_stock'] as $optStock) {
		$amazonSkuRows = $this->getLinkedSkus($productId, $optStock['var']);
		foreach($amazonSkuRows as $amazonSkuRow) {
			$quantityData[$amazonSkuRow['amazon_sku']] = $optStock['stock'];
		}
	}
	if(!empty($quantityData)) {
		$logger->write('Updating quantities with data: ' . print_r($quantityData, true));
		$this->updateQuantities($quantityData);
	} else {
		$logger->write('No quantity data need to be posted.');
	}

} else {
	$this->putStockUpdateBulk(array($productId));
}
$logger->write('productUpdateListen() exiting');


ublic function updateOrder($orderId, $orderStatusString, $courier_id = '', $courierFromList = true, $tracking_no = '') {

if ($this->config->get('amazon_status') != 1) {
	return;
}

/* Is called from admin? */
if (!defined('HTTPS_CATALOG')) {
	return;
}

$amazonOrder = $this->getOrder($orderId);

if(!$amazonOrder) {
	return;
}

$amazonOrderId = $amazonOrder['amazon_order_id'];


$log = new Log('amazon.log');
$log->write("Order's $amazonOrderId status changed to $orderStatusString");


$this->load->model('openbay/amazon');
$amazonOrderProducts = $this->model_openbay_amazon->getAmazonOrderedProducts($orderId);


$requestNode = new SimpleXMLElement('<Request/>');

$requestNode->addChild('AmazonOrderId', $amazonOrderId);
$requestNode->addChild('Status', $orderStatusString);

if(!empty($courier_id)) {
	if($courierFromList) {
		$requestNode->addChild('CourierId', $courier_id);
	} else {
		$requestNode->addChild('CourierOther', $courier_id);
	}
	$requestNode->addChild('TrackingNo', $tracking_no);
}

$orderItemsNode = $requestNode->addChild('OrderItems');

foreach ($amazonOrderProducts as $product) {
	$newOrderItem = $orderItemsNode->addChild('OrderItem');
	$newOrderItem->addChild('ItemId', htmlspecialchars($product['amazon_order_item_id']));
	$newOrderItem->addChild('Quantity', (int)$product['quantity']);
}

$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->loadXML($requestNode->asXML());
$doc->formatOutput = true;

$this->model_openbay_amazon->updateAmazonOrderTracking($orderId, $courier_id, $courierFromList, !empty($courier_id) ? $tracking_no : '');
$log->write('Request: ' . $doc->saveXML());
$response = $this->callWithResponse('order/update2', $doc->saveXML(), false);
$log->write("Response for Order's status update: $response");


ublic function getCategoryTemplates() {
$result = $this->callWithResponse("productv2/RequestTemplateList");
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function registerInsertion($data) {
$result = $this->callWithResponse("productv2/RegisterInsertionRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function insertProduct($data) {
$result = $this->callWithResponse("productv2/InsertProductRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function updateQuantities($data) {
$result = $this->callWithResponse("product/UpdateQuantityRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function getStockUpdatesStatus($data) {
$result = $this->callWithResponse("status/StockUpdates", $data);
if(isset($result)) {
	return $result;
} else {
	return false;
}


ublic function callNoResponse($method, $data = array(), $isJson = true) {
if  ($isJson) {
	$argString = json_encode($data);
} else {
	$argString = $data;
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$crypt = $this->encrypt($argString, $token, true);

$defaults = array(
	CURLOPT_POST => 1,
	CURLOPT_HEADER => 0,
	CURLOPT_URL => $this->server . $method,
	CURLOPT_USERAGENT => 'OpenBay Pro for Amazon/Opencart',
	CURLOPT_FRESH_CONNECT => 1,
	CURLOPT_RETURNTRANSFER => 1,
	CURLOPT_FORBID_REUSE => 1,
	CURLOPT_TIMEOUT => 2,
	CURLOPT_SSL_VERIFYPEER => 0,
	CURLOPT_SSL_VERIFYHOST => 0,
	CURLOPT_POSTFIELDS => 'token=' . $this->token . '&data=' . rawurlencode($crypt),
);
$ch = curl_init();

curl_setopt_array($ch, $defaults);

curl_exec($ch);

curl_close($ch);


ublic function callWithResponse($method, $data = array(), $isJson = true) {
if  ($isJson) {
	$argString = json_encode($data);
} else {
	$argString = $data;
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$crypt = $this->encrypt($argString, $token, true);

$defaults = array(
	CURLOPT_POST            => 1,
	CURLOPT_HEADER          => 0,
	CURLOPT_URL             => $this->server . $method,
	CURLOPT_USERAGENT       => 'OpenBay Pro for Amazon/Opencart',
	CURLOPT_FRESH_CONNECT   => 1,
	CURLOPT_RETURNTRANSFER  => 1,
	CURLOPT_FORBID_REUSE    => 1,
	CURLOPT_TIMEOUT         => 30,
	CURLOPT_SSL_VERIFYPEER  => 0,
	CURLOPT_SSL_VERIFYHOST  => 0,
	CURLOPT_POSTFIELDS      => 'token=' . $this->token . '&data=' . rawurlencode($crypt),
);
$ch = curl_init();

curl_setopt_array($ch, $defaults);

$response = curl_exec($ch);

curl_close($ch);

return $response;


ublic function decryptArgs($crypt, $isBase64 = true) {
if ($isBase64) {
	$crypt = base64_decode($crypt, true);
	if (!$crypt) {
		return false;
	}
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$data = $this->decrypt($crypt, $token);

return $data;


rivate function encrypt($msg, $k, $base64 = false) {
if (!$td = mcrypt_module_open('rijndael-256', '', 'ctr', ''))
	return false;

$iv = mcrypt_create_iv(32, MCRYPT_RAND);

if (mcrypt_generic_init($td, $k, $iv) !== 0)
	return false;

$msg = mcrypt_generic($td, $msg);
$msg = $iv . $msg;
$mac = $this->pbkdf2($msg, $k, 1000, 32);
$msg .= $mac;

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

if ($base64) {
	$msg = base64_encode($msg);
}

return $msg;


rivate function decrypt($msg, $k, $base64 = false) {
if ($base64) {
	$msg = base64_decode($msg);
}

if (!$td = mcrypt_module_open('rijndael-256', '', 'ctr', '')) {
	return false;
}

$iv = substr($msg, 0, 32);
$mo = strlen($msg) - 32;
$em = substr($msg, $mo);
$msg = substr($msg, 32, strlen($msg) - 64);
$mac = $this->pbkdf2($iv . $msg, $k, 1000, 32);

if ($em !== $mac) {
	return false;
}

if (mcrypt_generic_init($td, $k, $iv) !== 0) {
	return false;
}

$msg = mdecrypt_generic($td, $msg);
$msg = unserialize($msg);

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

return $msg;


rivate function pbkdf2($p, $s, $c, $kl, $a = 'sha256') {
$hl = strlen(hash($a, null, true));
$kb = ceil($kl / $hl);
$dk = '';

for ($block = 1; $block <= $kb; $block++) {

	$ib = $b = hash_hmac($a, $s . pack('N', $block), $p, true);

	for ($i = 1; $i < $c; $i++)
		$ib ^= ($b = hash_hmac($a, $b, $p, true));

	$dk .= $ib;
}

return substr($dk, 0, $kl);


ublic function getServer() {
return $this->server;


ublic function putStockUpdateBulk($productIdArray, $endInactive = false){
$this->load->library('log');
$logger = new Log('amazon_stocks.log');
$logger->write('Updating stock using putStockUpdateBulk()');
$quantityData = array();
foreach($productIdArray as $productId) {
	$amazonRows = $this->getLinkedSkus($productId);
	foreach($amazonRows as $amazonRow) {
		$productRow = $this->db->query("SELECT quantity, status FROM `" . DB_PREFIX . "product`
			WHERE `product_id` = '" . (int)$productId . "'")->row;

		if(!empty($productRow)) {
			if($endInactive && $productRow['status'] == '0') {
				$quantityData[$amazonRow['amazon_sku']] = 0;
			} else {
				$quantityData[$amazonRow['amazon_sku']] = $productRow['quantity'];
			}
		}
	}
}
if(!empty($quantityData)) {
	$logger->write('Quantity data to be sent:' . print_r($quantityData, true));
	$response = $this->updateQuantities($quantityData);
	$logger->write('Submit to API. Response: ' . print_r($response, true));
} else {
	$logger->write('No quantity data need to be posted.');
}


ublic function getLinkedSkus($productId, $var='') {
return $this->db->query("SELECT `amazon_sku` FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . (int)$productId . "' AND `var` = '" . $this->db->escape($var) . "'")->rows;


ublic function getOrderdProducts($orderId) {
return $this->db->query("SELECT `op`.`product_id`, `p`.`quantity` as `quantity_left`
	FROM `" . DB_PREFIX . "order_product` as `op`
	LEFT JOIN `" . DB_PREFIX . "product` as `p`
	ON `p`.`product_id` = `op`.`product_id`
	WHERE `op`.`order_id` = '" . (int)$orderId . "'
	")->rows;


ublic function osProducts($order_id){
$order_product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'");

$passArray = array();
foreach ($order_product_query->rows as $order_product) {
	$product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product` WHERE `product_id` = '".(int)$order_product['product_id']."' LIMIT 1");

	if (!empty($product_query->row)) {
		if (isset($product_query->row['has_option']) && ($product_query->row['has_option'] == 1)) {
			$pOption_query = $this->db->query("
				SELECT `oo`.`product_option_value_id`
				FROM `" . DB_PREFIX . "order_option` `oo`
					LEFT JOIN `" . DB_PREFIX . "product_option_value` `pov` ON (`pov`.`product_option_value_id` = `oo`.`product_option_value_id`)
					LEFT JOIN `" . DB_PREFIX . "option` `o` ON (`o`.`option_id` = `pov`.`option_id`)
				WHERE `oo`.`order_product_id` = '" . (int)$order_product['order_product_id'] . "'
				AND `oo`.`order_id` = '" . (int)$order_id . "'
				AND ((`o`.`type` = 'radio') OR (`o`.`type` = 'select') OR (`o`.`type` = 'image'))
				ORDER BY `oo`.`order_option_id`
				ASC");

			if ($pOption_query->num_rows != 0) {
				$pOptions = array();
				foreach ($pOption_query->rows as $pOptionRow) {
					$pOptions[] = $pOptionRow['product_option_value_id'];
				}

				$var = implode(':', $pOptions);
				$qtyLeftRow = $this->db->query("SELECT `stock` FROM `" . DB_PREFIX . "product_option_relation` WHERE `product_id` = '" . (int)$order_product['product_id'] . "' AND `var` = '" . $this->db->escape($var) . "'")->row;

				if(empty($qtyLeftRow)) {
					$qtyLeftRow['stock'] = 0;
				}

				$passArray[] = array('pid' => $order_product['product_id'], 'qty_left' => $qtyLeftRow['stock'], 'var' => $var);
			}
		} else {
			$passArray[] = array('pid' => $order_product['product_id'], 'qty_left' => $product_query->row['quantity'], 'var' => '');
		}
	}
}

return $passArray;


ublic function validate(){
if($this->config->get('amazon_status') != 0 &&
	$this->config->get('openbay_amazon_token') != '' &&
	$this->config->get('openbay_amazon_enc_string1') != '' &&
	$this->config->get('openbay_amazon_enc_string2') != ''){
	return true;
}else{
	return false;
}


ublic function deleteProduct($product_id){
$this->db->query("DELETE FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . $this->db->escape($product_id) . "'");


ublic function deleteOrder($order_id){
/**
 * @todo
 */


ublic function getOrder($orderId) {
$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_order` WHERE `order_id` = '".(int)$orderId."' LIMIT 1");

if($qry->num_rows > 0){
	return $qry->row;
}else{
	return false;
}


ublic function getCarriers() {
return array(
	"Blue Package",
	"Canada Post",
	"City Link",
	"DHL",
	"DHL Global Mail",
	"Fastway",
	"FedEx",
	"FedEx SmartPost",
	"GLS",
	"GO!",
	"Hermes Logistik Gruppe",
	"Newgistics",
	"NipponExpress",
	"OSM",
	"OnTrac",
	"Parcelforce",
	"Royal Mail",
	"SagawaExpress",
	"Streamlite",
	"TNT",
	"Target",
	"UPS",
	"UPS Mail Innovations",
	"USPS",
	"YamatoTransport",
);


ublic function parseCategoryTemplate($xml) {
$simplexml = null;

libxml_use_internal_errors(true);
if(($simplexml = simplexml_load_string($xml)) == false) {
	return false;
}

$category = (string)$simplexml->filename;

$tabs = array();
foreach($simplexml->tabs->tab as $tab) {
	$attributes = $tab->attributes();
	$tabs[] = array(
		'id' => (string)$attributes['id'],
		'name' => (string)$tab->name,
	);
}

$fields = array();
$fieldTypes = array('required', 'desired', 'optional');
foreach ($fieldTypes as $type) {
	foreach ($simplexml->fields->$type->field as $field) {
		$attributes = $field->attributes();
		$fields[] = array(
			'name' => (string)$attributes['name'],
			'title' => (string)$field->title,
			'definition' => (string)$field->definition,
			'accepted' => (array)$field->accepted,
			'type' => (string)$type,
			'child' => false,
			'order' => isset($attributes['order']) ? (string)$attributes['order'] : '',
			'tab' => (string)$attributes['tab'],
		);
	}
	foreach ($simplexml->fields->$type->childfield as $field) {
		$attributes = $field->attributes();
		$fields[] = array(
			'name' => (string)$attributes['name'],
			'title' => (string)$field->title,
			'definition' => (string)$field->definition,
			'accepted' => (array)$field->accepted,
			'type' => (string)$type,
			'child' => true,
			'parent' => (array)$field->parent,
			'order' => isset($attributes['order']) ? (string)$attributes['order'] : '',
			'tab' => (string)$attributes['tab'],
		);
	}
}

foreach($fields as $index => $field) {
	$fields[$index]['unordered_index'] = $index;
}

usort($fields, array('Amazon','compareFields'));

return array(
	'category' => $category,
	'fields' => $fields,
	'tabs' => $tabs,
);


rivate static function compareFields($field1, $field2) {
if($field1['order'] == $field2['order']) {
	return ($field1['unordered_index'] < $field2['unordered_index']) ? -1 : 1;
} else if(!empty($field1['order']) && empty($field2['order'])) {
	return -1;
} else if(!empty($field2['order']) && empty($field1['order'])) {
	return 1;
} else {
	return ($field1['order'] < $field2['order']) ? -1 : 1;
}

public mixed deleteOrder ( $order_id )
Исходный код метода:
n;
rivate $encPass;
rivate $encSalt;
rivate $server = 'http://uk-amazon.openbaypro.com/';
rivate $registry;

ublic function __construct($registry) {
$this->registry = $registry;

$this->token   = $registry->get('config')->get('openbay_amazon_token');
$this->encPass = $registry->get('config')->get('openbay_amazon_enc_string1');
$this->encSalt = $registry->get('config')->get('openbay_amazon_enc_string2');



ublic function __get($name) {
return $this->registry->get($name);


ublic function orderNew($orderId) {
if ($this->config->get('amazon_status') != 1) {
	return;
}

/* Is called from front-end? */
if (!defined('HTTPS_CATALOG')) {
	$this->load->model('openbay/amazon_order');
	$amazonOrderId = $this->model_openbay_amazon_order->getAmazonOrderId($orderId);

	$this->load->library('log');
	$logger = new Log('amazon_stocks.log');
	$logger->write('orderNew() called with order id: ' . $orderId);

	//Stock levels update
	if ($this->openbay->addonLoad('openstock')) {
		$logger->write('openStock found installed.');

		$osProducts = $this->osProducts($orderId);
		$logger->write(print_r($osProducts, true));
		$quantityData = array();
		foreach ($osProducts as $osProduct) {
			$amazonSkuRows = $this->getLinkedSkus($osProduct['pid'], $osProduct['var']);
			foreach($amazonSkuRows as $amazonSkuRow) {
				$quantityData[$amazonSkuRow['amazon_sku']] = $osProduct['qty_left'];
			}
		}
		if(!empty($quantityData)) {
			$logger->write('Updating quantities with data: ' . print_r($quantityData, true));
			$this->updateQuantities($quantityData);
		} else {
			$logger->write('No quantity data need to be posted.');
		}
	} else {
		$orderedProducts = $this->getOrderdProducts($orderId);
		$orderedProductIds = array();
		foreach($orderedProducts as $orderedProduct) {
			$orderedProductIds[] = $orderedProduct['product_id'];
		}
		$this->putStockUpdateBulk($orderedProductIds);
	}
	$logger->write('orderNew() exiting');
}


ublic function productUpdateListen($productId, $data) {
$logger = new Log('amazon_stocks.log');
$logger->write('productUpdateListen called for product id: ' . $productId);

if ($this->openbay->addonLoad('openstock') && (isset($data['has_option']) && $data['has_option'] == 1)) {
	$logger->write('openStock found installed and product has options.');
	$quantityData = array();
	foreach($data['product_option_stock'] as $optStock) {
		$amazonSkuRows = $this->getLinkedSkus($productId, $optStock['var']);
		foreach($amazonSkuRows as $amazonSkuRow) {
			$quantityData[$amazonSkuRow['amazon_sku']] = $optStock['stock'];
		}
	}
	if(!empty($quantityData)) {
		$logger->write('Updating quantities with data: ' . print_r($quantityData, true));
		$this->updateQuantities($quantityData);
	} else {
		$logger->write('No quantity data need to be posted.');
	}

} else {
	$this->putStockUpdateBulk(array($productId));
}
$logger->write('productUpdateListen() exiting');


ublic function updateOrder($orderId, $orderStatusString, $courier_id = '', $courierFromList = true, $tracking_no = '') {

if ($this->config->get('amazon_status') != 1) {
	return;
}

/* Is called from admin? */
if (!defined('HTTPS_CATALOG')) {
	return;
}

$amazonOrder = $this->getOrder($orderId);

if(!$amazonOrder) {
	return;
}

$amazonOrderId = $amazonOrder['amazon_order_id'];


$log = new Log('amazon.log');
$log->write("Order's $amazonOrderId status changed to $orderStatusString");


$this->load->model('openbay/amazon');
$amazonOrderProducts = $this->model_openbay_amazon->getAmazonOrderedProducts($orderId);


$requestNode = new SimpleXMLElement('<Request/>');

$requestNode->addChild('AmazonOrderId', $amazonOrderId);
$requestNode->addChild('Status', $orderStatusString);

if(!empty($courier_id)) {
	if($courierFromList) {
		$requestNode->addChild('CourierId', $courier_id);
	} else {
		$requestNode->addChild('CourierOther', $courier_id);
	}
	$requestNode->addChild('TrackingNo', $tracking_no);
}

$orderItemsNode = $requestNode->addChild('OrderItems');

foreach ($amazonOrderProducts as $product) {
	$newOrderItem = $orderItemsNode->addChild('OrderItem');
	$newOrderItem->addChild('ItemId', htmlspecialchars($product['amazon_order_item_id']));
	$newOrderItem->addChild('Quantity', (int)$product['quantity']);
}

$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->loadXML($requestNode->asXML());
$doc->formatOutput = true;

$this->model_openbay_amazon->updateAmazonOrderTracking($orderId, $courier_id, $courierFromList, !empty($courier_id) ? $tracking_no : '');
$log->write('Request: ' . $doc->saveXML());
$response = $this->callWithResponse('order/update2', $doc->saveXML(), false);
$log->write("Response for Order's status update: $response");


ublic function getCategoryTemplates() {
$result = $this->callWithResponse("productv2/RequestTemplateList");
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function registerInsertion($data) {
$result = $this->callWithResponse("productv2/RegisterInsertionRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function insertProduct($data) {
$result = $this->callWithResponse("productv2/InsertProductRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function updateQuantities($data) {
$result = $this->callWithResponse("product/UpdateQuantityRequest", $data);
if(isset($result)) {
	return (array)json_decode($result);
} else {
	return array();
}


ublic function getStockUpdatesStatus($data) {
$result = $this->callWithResponse("status/StockUpdates", $data);
if(isset($result)) {
	return $result;
} else {
	return false;
}


ublic function callNoResponse($method, $data = array(), $isJson = true) {
if  ($isJson) {
	$argString = json_encode($data);
} else {
	$argString = $data;
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$crypt = $this->encrypt($argString, $token, true);

$defaults = array(
	CURLOPT_POST => 1,
	CURLOPT_HEADER => 0,
	CURLOPT_URL => $this->server . $method,
	CURLOPT_USERAGENT => 'OpenBay Pro for Amazon/Opencart',
	CURLOPT_FRESH_CONNECT => 1,
	CURLOPT_RETURNTRANSFER => 1,
	CURLOPT_FORBID_REUSE => 1,
	CURLOPT_TIMEOUT => 2,
	CURLOPT_SSL_VERIFYPEER => 0,
	CURLOPT_SSL_VERIFYHOST => 0,
	CURLOPT_POSTFIELDS => 'token=' . $this->token . '&data=' . rawurlencode($crypt),
);
$ch = curl_init();

curl_setopt_array($ch, $defaults);

curl_exec($ch);

curl_close($ch);


ublic function callWithResponse($method, $data = array(), $isJson = true) {
if  ($isJson) {
	$argString = json_encode($data);
} else {
	$argString = $data;
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$crypt = $this->encrypt($argString, $token, true);

$defaults = array(
	CURLOPT_POST            => 1,
	CURLOPT_HEADER          => 0,
	CURLOPT_URL             => $this->server . $method,
	CURLOPT_USERAGENT       => 'OpenBay Pro for Amazon/Opencart',
	CURLOPT_FRESH_CONNECT   => 1,
	CURLOPT_RETURNTRANSFER  => 1,
	CURLOPT_FORBID_REUSE    => 1,
	CURLOPT_TIMEOUT         => 30,
	CURLOPT_SSL_VERIFYPEER  => 0,
	CURLOPT_SSL_VERIFYHOST  => 0,
	CURLOPT_POSTFIELDS      => 'token=' . $this->token . '&data=' . rawurlencode($crypt),
);
$ch = curl_init();

curl_setopt_array($ch, $defaults);

$response = curl_exec($ch);

curl_close($ch);

return $response;


ublic function decryptArgs($crypt, $isBase64 = true) {
if ($isBase64) {
	$crypt = base64_decode($crypt, true);
	if (!$crypt) {
		return false;
	}
}

$token = $this->pbkdf2($this->encPass, $this->encSalt, 1000, 32);
$data = $this->decrypt($crypt, $token);

return $data;


rivate function encrypt($msg, $k, $base64 = false) {
if (!$td = mcrypt_module_open('rijndael-256', '', 'ctr', ''))
	return false;

$iv = mcrypt_create_iv(32, MCRYPT_RAND);

if (mcrypt_generic_init($td, $k, $iv) !== 0)
	return false;

$msg = mcrypt_generic($td, $msg);
$msg = $iv . $msg;
$mac = $this->pbkdf2($msg, $k, 1000, 32);
$msg .= $mac;

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

if ($base64) {
	$msg = base64_encode($msg);
}

return $msg;


rivate function decrypt($msg, $k, $base64 = false) {
if ($base64) {
	$msg = base64_decode($msg);
}

if (!$td = mcrypt_module_open('rijndael-256', '', 'ctr', '')) {
	return false;
}

$iv = substr($msg, 0, 32);
$mo = strlen($msg) - 32;
$em = substr($msg, $mo);
$msg = substr($msg, 32, strlen($msg) - 64);
$mac = $this->pbkdf2($iv . $msg, $k, 1000, 32);

if ($em !== $mac) {
	return false;
}

if (mcrypt_generic_init($td, $k, $iv) !== 0) {
	return false;
}

$msg = mdecrypt_generic($td, $msg);
$msg = unserialize($msg);

mcrypt_generic_deinit($td);
mcrypt_module_close($td);

return $msg;


rivate function pbkdf2($p, $s, $c, $kl, $a = 'sha256') {
$hl = strlen(hash($a, null, true));
$kb = ceil($kl / $hl);
$dk = '';

for ($block = 1; $block <= $kb; $block++) {

	$ib = $b = hash_hmac($a, $s . pack('N', $block), $p, true);

	for ($i = 1; $i < $c; $i++)
		$ib ^= ($b = hash_hmac($a, $b, $p, true));

	$dk .= $ib;
}

return substr($dk, 0, $kl);


ublic function getServer() {
return $this->server;


ublic function putStockUpdateBulk($productIdArray, $endInactive = false){
$this->load->library('log');
$logger = new Log('amazon_stocks.log');
$logger->write('Updating stock using putStockUpdateBulk()');
$quantityData = array();
foreach($productIdArray as $productId) {
	$amazonRows = $this->getLinkedSkus($productId);
	foreach($amazonRows as $amazonRow) {
		$productRow = $this->db->query("SELECT quantity, status FROM `" . DB_PREFIX . "product`
			WHERE `product_id` = '" . (int)$productId . "'")->row;

		if(!empty($productRow)) {
			if($endInactive && $productRow['status'] == '0') {
				$quantityData[$amazonRow['amazon_sku']] = 0;
			} else {
				$quantityData[$amazonRow['amazon_sku']] = $productRow['quantity'];
			}
		}
	}
}
if(!empty($quantityData)) {
	$logger->write('Quantity data to be sent:' . print_r($quantityData, true));
	$response = $this->updateQuantities($quantityData);
	$logger->write('Submit to API. Response: ' . print_r($response, true));
} else {
	$logger->write('No quantity data need to be posted.');
}


ublic function getLinkedSkus($productId, $var='') {
return $this->db->query("SELECT `amazon_sku` FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . (int)$productId . "' AND `var` = '" . $this->db->escape($var) . "'")->rows;


ublic function getOrderdProducts($orderId) {
return $this->db->query("SELECT `op`.`product_id`, `p`.`quantity` as `quantity_left`
	FROM `" . DB_PREFIX . "order_product` as `op`
	LEFT JOIN `" . DB_PREFIX . "product` as `p`
	ON `p`.`product_id` = `op`.`product_id`
	WHERE `op`.`order_id` = '" . (int)$orderId . "'
	")->rows;


ublic function osProducts($order_id){
$order_product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "order_product` WHERE `order_id` = '" . (int)$order_id . "'");

$passArray = array();
foreach ($order_product_query->rows as $order_product) {
	$product_query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "product` WHERE `product_id` = '".(int)$order_product['product_id']."' LIMIT 1");

	if (!empty($product_query->row)) {
		if (isset($product_query->row['has_option']) && ($product_query->row['has_option'] == 1)) {
			$pOption_query = $this->db->query("
				SELECT `oo`.`product_option_value_id`
				FROM `" . DB_PREFIX . "order_option` `oo`
					LEFT JOIN `" . DB_PREFIX . "product_option_value` `pov` ON (`pov`.`product_option_value_id` = `oo`.`product_option_value_id`)
					LEFT JOIN `" . DB_PREFIX . "option` `o` ON (`o`.`option_id` = `pov`.`option_id`)
				WHERE `oo`.`order_product_id` = '" . (int)$order_product['order_product_id'] . "'
				AND `oo`.`order_id` = '" . (int)$order_id . "'
				AND ((`o`.`type` = 'radio') OR (`o`.`type` = 'select') OR (`o`.`type` = 'image'))
				ORDER BY `oo`.`order_option_id`
				ASC");

			if ($pOption_query->num_rows != 0) {
				$pOptions = array();
				foreach ($pOption_query->rows as $pOptionRow) {
					$pOptions[] = $pOptionRow['product_option_value_id'];
				}

				$var = implode(':', $pOptions);
				$qtyLeftRow = $this->db->query("SELECT `stock` FROM `" . DB_PREFIX . "product_option_relation` WHERE `product_id` = '" . (int)$order_product['product_id'] . "' AND `var` = '" . $this->db->escape($var) . "'")->row;

				if(empty($qtyLeftRow)) {
					$qtyLeftRow['stock'] = 0;
				}

				$passArray[] = array('pid' => $order_product['product_id'], 'qty_left' => $qtyLeftRow['stock'], 'var' => $var);
			}
		} else {
			$passArray[] = array('pid' => $order_product['product_id'], 'qty_left' => $product_query->row['quantity'], 'var' => '');
		}
	}
}

return $passArray;


ublic function validate(){
if($this->config->get('amazon_status') != 0 &&
	$this->config->get('openbay_amazon_token') != '' &&
	$this->config->get('openbay_amazon_enc_string1') != '' &&
	$this->config->get('openbay_amazon_enc_string2') != ''){
	return true;
}else{
	return false;
}


ublic function deleteProduct($product_id){
$this->db->query("DELETE FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . $this->db->escape($product_id) . "'");


ublic function deleteOrder($order_id){
/**
 * @todo
 */


ublic function getOrder($orderId) {
$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_order` WHERE `order_id` = '".(int)$orderId."' LIMIT 1");

if($qry->num_rows > 0){
	return $qry->row;
}else{
	return false;
}


ublic function getCarriers() {
return array(
	"Blue Package",
	"Canada Post",
	"City Link",
	"DHL",
	"DHL Global Mail",
	"Fastway",
	"FedEx",
	"FedEx SmartPost",
	"GLS",
	"GO!",
	"Hermes Logistik Gruppe",
	"Newgistics",
	"NipponExpress",
	"OSM",
	"OnTrac",
	"Parcelforce",
	"Royal Mail",
	"SagawaExpress",
	"Streamlite",
	"TNT",
	"Target",
	"UPS",
	"UPS Mail Innovations",
	"USPS",
	"YamatoTransport",
);


ublic function parseCategoryTemplate($xml) {
$simplexml = null;

libxml_use_internal_errors(true);
if(($simplexml = simplexml_load_string($xml)) == false) {
	return false;
}

$category = (string)$simplexml->filename;

$tabs = array();
foreach($simplexml->tabs->tab as $tab) {
	$attributes = $tab->attributes();
	$tabs[] = array(
		'id' => (string)$attributes['id'],
		'name' => (string)$tab->name,
	);
}

$fields = array();
$fieldTypes = array('required', 'desired', 'optional');
foreach ($fieldTypes as $type) {
	foreach ($simplexml->fields->$type->field as $field) {
		$attributes = $field->attributes();
		$fields[] = array(
			'name' => (string)$attributes['name'],
			'title' => (string)$field->title,
			'definition' => (string)$field->definition,
			'accepted' => (array)$field->accepted,
			'type' => (string)$type,
			'child' => false,
			'order' => isset($attributes['order']) ? (string)$attributes['order'] : '',
			'tab' => (string)$attributes['tab'],
		);
	}
	foreach ($simplexml->fields->$type->childfield as $field) {
		$attributes = $field->attributes();
		$fields[] = array(
			'name' => (string)$attributes['name'],
			'title' => (string)$field->title,
			'definition' => (string)$field->definition,
			'accepted' => (array)$field->accepted,
			'type' => (string)$type,
			'child' => true,
			'parent' => (array)$field->parent,
			'order' => isset($attributes['order']) ? (string)$attributes['order'] : '',
			'tab' => (string)$attributes['tab'],
		);
	}
}

foreach($fields as $index => $field) {
	$fields[$index]['unordered_index'] = $index;
}

usort($fields, array('Amazon','compareFields'));

return array(
	'category' => $category,
	'fields' => $fields,
	'tabs' => $tabs,
);


rivate static function compareFields($field1, $field2) {
if($field1['order'] == $field2['order']) {
	return ($field1['unordered_index'] < $field2['unordered_index']) ? -1 : 1;
} else if(!empty($field1['order']) && empty($field2['order'])) {
	return -1;
} else if(!empty($field2['order']) && empty($field1['order'])) {
	return 1;
} else {
	return ($field1['order'] < $field2['order']) ? -1 : 1;
}

public mixed getOrder ( $orderId )
Исходный код метода:
$qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_order` WHERE `order_id` = '".(int)$orderId."' LIMIT 1");

if($qry->num_rows > 0){
	return $qry->row;
}else{
	return false;
}
public mixed getCarriers ( )
Исходный код метода:
return array(
	"Blue Package",
	"Canada Post",
	"City Link",
	"DHL",
	"DHL Global Mail",
	"Fastway",
	"FedEx",
	"FedEx SmartPost",
	"GLS",
	"GO!",
	"Hermes Logistik Gruppe",
	"Newgistics",
	"NipponExpress",
	"OSM",
	"OnTrac",
	"Parcelforce",
	"Royal Mail",
	"SagawaExpress",
	"Streamlite",
	"TNT",
	"Target",
	"UPS",
	"UPS Mail Innovations",
	"USPS",
	"YamatoTransport",
);
public mixed parseCategoryTemplate ( $xml )
Исходный код метода:
$simplexml = null;

libxml_use_internal_errors(true);
if(($simplexml = simplexml_load_string($xml)) == false) {
	return false;
}

$category = (string)$simplexml->filename;

$tabs = array();
foreach($simplexml->tabs->tab as $tab) {
	$attributes = $tab->attributes();
	$tabs[] = array(
		'id' => (string)$attributes['id'],
		'name' => (string)$tab->name,
	);
}

$fields = array();
$fieldTypes = array('required', 'desired', 'optional');
foreach ($fieldTypes as $type) {
	foreach ($simplexml->fields->$type->field as $field) {
		$attributes = $field->attributes();
		$fields[] = array(
			'name' => (string)$attributes['name'],
			'title' => (string)$field->title,
			'definition' => (string)$field->definition,
			'accepted' => (array)$field->accepted,
			'type' => (string)$type,
			'child' => false,
			'order' => isset($attributes['order']) ? (string)$attributes['order'] : '',
			'tab' => (string)$attributes['tab'],
		);
	}
	foreach ($simplexml->fields->$type->childfield as $field) {
		$attributes = $field->attributes();
		$fields[] = array(
			'name' => (string)$attributes['name'],
			'title' => (string)$field->title,
			'definition' => (string)$field->definition,
			'accepted' => (array)$field->accepted,
			'type' => (string)$type,
			'child' => true,
			'parent' => (array)$field->parent,
			'order' => isset($attributes['order']) ? (string)$attributes['order'] : '',
			'tab' => (string)$attributes['tab'],
		);
	}
}

foreach($fields as $index => $field) {
	$fields[$index]['unordered_index'] = $index;
}

usort($fields, array('Amazon','compareFields'));

return array(
	'category' => $category,
	'fields' => $fields,
	'tabs' => $tabs,
);

Связанные классы:

Название класса Роль
class Config { ... } Компонент Config используется в данном классе
class DB { ... } Компонент Db используется в данном классе

Комментарии