PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/system/library/amazon.php

https://gitlab.com/hazelnuts23/unitedfoodstuff
PHP | 502 lines | 421 code | 75 blank | 6 comment | 61 complexity | 1d18fd82e22cf7d315ceab5caa45e3a2 MD5 | raw file
  1. <?php
  2. class Amazon {
  3. private $token;
  4. private $enc1;
  5. private $enc2;
  6. private $url = 'http://uk-amazon.openbaypro.com/';
  7. private $registry;
  8. public function __construct($registry) {
  9. $this->registry = $registry;
  10. $this->token = $this->config->get('openbay_amazon_token');
  11. $this->enc1 = $this->config->get('openbay_amazon_enc_string1');
  12. $this->enc2 = $this->config->get('openbay_amazon_enc_string2');
  13. }
  14. public function __get($name) {
  15. return $this->registry->get($name);
  16. }
  17. public function call($method, $data = array(), $is_json = true) {
  18. if ($is_json) {
  19. $arg_string = json_encode($data);
  20. } else {
  21. $arg_string = $data;
  22. }
  23. $crypt = $this->encryptArgs($arg_string);
  24. $defaults = array(
  25. CURLOPT_POST => 1,
  26. CURLOPT_HEADER => 0,
  27. CURLOPT_URL => $this->url . $method,
  28. CURLOPT_USERAGENT => 'OpenBay Pro for Amazon/Opencart',
  29. CURLOPT_FRESH_CONNECT => 1,
  30. CURLOPT_RETURNTRANSFER => 1,
  31. CURLOPT_FORBID_REUSE => 1,
  32. CURLOPT_TIMEOUT => 30,
  33. CURLOPT_SSL_VERIFYPEER => 0,
  34. CURLOPT_SSL_VERIFYHOST => 0,
  35. CURLOPT_POSTFIELDS => 'token=' . $this->token . '&data=' . rawurlencode($crypt) . '&opencart_version=' . VERSION,
  36. );
  37. $ch = curl_init();
  38. curl_setopt_array($ch, $defaults);
  39. $response = curl_exec($ch);
  40. curl_close($ch);
  41. return $response;
  42. }
  43. public function callNoResponse($method, $data = array(), $is_json = true) {
  44. if ($is_json) {
  45. $arg_string = json_encode($data);
  46. } else {
  47. $arg_string = $data;
  48. }
  49. $crypt = $this->encryptArgs($arg_string);
  50. $defaults = array(
  51. CURLOPT_POST => 1,
  52. CURLOPT_HEADER => 0,
  53. CURLOPT_URL => $this->url . $method,
  54. CURLOPT_USERAGENT => 'OpenBay Pro for Amazon/Opencart',
  55. CURLOPT_FRESH_CONNECT => 1,
  56. CURLOPT_RETURNTRANSFER => 1,
  57. CURLOPT_FORBID_REUSE => 1,
  58. CURLOPT_TIMEOUT => 2,
  59. CURLOPT_SSL_VERIFYPEER => 0,
  60. CURLOPT_SSL_VERIFYHOST => 0,
  61. CURLOPT_POSTFIELDS => 'token=' . $this->token . '&data=' . rawurlencode($crypt) . '&opencart_version=' . VERSION,
  62. );
  63. $ch = curl_init();
  64. curl_setopt_array($ch, $defaults);
  65. curl_exec($ch);
  66. curl_close($ch);
  67. }
  68. public function encryptArgs($data) {
  69. $token = $this->openbay->pbkdf2($this->enc1, $this->enc2, 1000, 32);
  70. $crypt = $this->openbay->encrypt($data, $token, true);
  71. return $crypt;
  72. }
  73. public function decryptArgs($crypt, $is_base_64 = true) {
  74. if ($is_base_64) {
  75. $crypt = base64_decode($crypt, true);
  76. if (!$crypt) {
  77. return false;
  78. }
  79. }
  80. $token = $this->openbay->pbkdf2($this->enc1, $this->enc2, 1000, 32);
  81. $data = $this->openbay->decrypt($crypt, $token);
  82. return $data;
  83. }
  84. public function getServer() {
  85. return $this->url;
  86. }
  87. public function productUpdateListen($product_id, $data = array()) {
  88. $logger = new Log('amazon_stocks.log');
  89. $logger->write('productUpdateListen (' . $product_id . ')');
  90. $product = $this->db->query("SELECT DISTINCT * FROM `" . DB_PREFIX . "product` WHERE `product_id` = '" . (int)$product_id . "' LIMIT 1")->row;
  91. if ($this->openbay->addonLoad('openstock') && (isset($product['has_option']) && $product['has_option'] == 1)) {
  92. $this->load->model('module/openstock');
  93. $logger->write('Variant item');
  94. $quantity_data = array();
  95. // check if post data['variant'], if not then call db to get variants
  96. if (!isset($data['variant'])) {
  97. $variants = $this->model_module_openstock->getVariants($product_id);
  98. } else {
  99. $variants = $data['variant'];
  100. }
  101. foreach ($variants as $variant) {
  102. $amazon_sku_rows = $this->getLinkedSkus($product_id, $variant['sku']);
  103. foreach($amazon_sku_rows as $amazon_sku_row) {
  104. $quantity_data[$amazon_sku_row['amazon_sku']] = $variant['stock'];
  105. }
  106. }
  107. if(!empty($quantity_data)) {
  108. $logger->write('Updating with: ' . print_r($quantity_data, true));
  109. $this->updateQuantities($quantity_data);
  110. } else {
  111. $logger->write('Not required.');
  112. }
  113. } else {
  114. $this->putStockUpdateBulk(array($product_id));
  115. }
  116. $logger->write('productUpdateListen() exit');
  117. }
  118. public function bulkUpdateOrders($orders) {
  119. // Is the module enabled and called from admin?
  120. if ($this->config->get('openbay_amazon_status') != 1 || !defined('HTTPS_CATALOG')) {
  121. return;
  122. }
  123. $this->load->model('openbay/amazon');
  124. $log = new Log('amazon.log');
  125. $log->write('Called bulkUpdateOrders method');
  126. $request = array(
  127. 'orders' => array(),
  128. );
  129. foreach ($orders as $order) {
  130. $amazon_order = $this->getOrder($order['order_id']);
  131. $amazon_order_products = $this->model_openbay_amazon->getAmazonOrderedProducts($order['order_id']);
  132. $products = array();
  133. foreach ($amazon_order_products as $amazon_order_product) {
  134. $products[] = array(
  135. 'amazon_order_item_id' => $amazon_order_product['amazon_order_item_id'],
  136. 'quantity' => $amazon_order_product['quantity'],
  137. );
  138. }
  139. $order_info = array(
  140. 'amazon_order_id' => $amazon_order['amazon_order_id'],
  141. 'status' => $order['status'],
  142. 'products' => $products,
  143. );
  144. if ($order['status'] == 'shipped' && !empty($order['carrier'])) {
  145. if ($order['carrier_from_list']) {
  146. $order_info['carrier_id'] = $order['carrier'];
  147. } else {
  148. $order_info['carrier_name'] = $order['carrier'];
  149. }
  150. $order_info['tracking'] = $order['tracking'];
  151. }
  152. $request['orders'][] = $order_info;
  153. }
  154. $log->write('order/bulkUpdate call: ' . print_r($request, 1));
  155. $response = $this->call('order/bulkUpdate', $request);
  156. $log->write('order/bulkUpdate response: ' . $response);
  157. }
  158. public function updateOrder($order_id, $order_status_string, $courier_id = '', $courier_from_list = true, $tracking_no = '') {
  159. if ($this->config->get('openbay_amazon_status') != 1) {
  160. return;
  161. }
  162. /* Is called from admin? */
  163. if (!defined('HTTPS_CATALOG')) {
  164. return;
  165. }
  166. $amazon_order = $this->getOrder($order_id);
  167. if(!$amazon_order) {
  168. return;
  169. }
  170. $amazon_order_id = $amazon_order['amazon_order_id'];
  171. $log = new Log('amazon.log');
  172. $log->write("Order's $amazon_order_id status changed to $order_status_string");
  173. $this->load->model('openbay/amazon');
  174. $amazon_order_products = $this->model_openbay_amazon->getAmazonOrderedProducts($order_id);
  175. $request_node = new SimpleXMLElement('<Request/>');
  176. $request_node->addChild('AmazonOrderId', $amazon_order_id);
  177. $request_node->addChild('Status', $order_status_string);
  178. if(!empty($courier_id)) {
  179. if($courier_from_list) {
  180. $request_node->addChild('CourierId', $courier_id);
  181. } else {
  182. $request_node->addChild('CourierOther', $courier_id);
  183. }
  184. $request_node->addChild('TrackingNo', $tracking_no);
  185. }
  186. $order_items_node = $request_node->addChild('OrderItems');
  187. foreach ($amazon_order_products as $product) {
  188. $new_order_item = $order_items_node->addChild('OrderItem');
  189. $new_order_item->addChild('ItemId', htmlspecialchars($product['amazon_order_item_id']));
  190. $new_order_item->addChild('Quantity', (int)$product['quantity']);
  191. }
  192. $doc = new DOMDocument('1.0');
  193. $doc->preserveWhiteSpace = false;
  194. $doc->loadXML($request_node->asXML());
  195. $doc->formatOutput = true;
  196. $this->model_openbay_amazon->updateAmazonOrderTracking($order_id, $courier_id, $courier_from_list, !empty($courier_id) ? $tracking_no : '');
  197. $log->write('Request: ' . $doc->saveXML());
  198. $response = $this->call('order/update2', $doc->saveXML(), false);
  199. $log->write("Response for Order's status update: $response");
  200. }
  201. public function getCategoryTemplates() {
  202. $result = $this->call("productv2/RequestTemplateList");
  203. if(isset($result)) {
  204. return (array)json_decode($result);
  205. } else {
  206. return array();
  207. }
  208. }
  209. public function registerInsertion($data) {
  210. $result = $this->call("productv2/RegisterInsertionRequest", $data);
  211. if(isset($result)) {
  212. return (array)json_decode($result);
  213. } else {
  214. return array();
  215. }
  216. }
  217. public function insertProduct($data) {
  218. $result = $this->call("productv2/InsertProductRequest", $data);
  219. if(isset($result)) {
  220. return (array)json_decode($result);
  221. } else {
  222. return array();
  223. }
  224. }
  225. public function updateQuantities($data) {
  226. $result = $this->call("product/UpdateQuantityRequest", $data);
  227. if(isset($result)) {
  228. return (array)json_decode($result);
  229. } else {
  230. return array();
  231. }
  232. }
  233. public function getStockUpdatesStatus($data) {
  234. $result = $this->call("status/StockUpdates", $data);
  235. if(isset($result)) {
  236. return $result;
  237. } else {
  238. return false;
  239. }
  240. }
  241. public function putStockUpdateBulk($product_id_array, $end_inactive = false){
  242. $this->load->library('log');
  243. $logger = new Log('amazon_stocks.log');
  244. $logger->write('Updating stock using putStockUpdateBulk()');
  245. $quantity_data = array();
  246. foreach($product_id_array as $product_id) {
  247. $amazon_rows = $this->getLinkedSkus($product_id);
  248. foreach($amazon_rows as $amazon_row) {
  249. $product_row = $this->db->query("SELECT `quantity`, `status` FROM `" . DB_PREFIX . "product` WHERE `product_id` = '" . (int)$product_id . "'")->row;
  250. if(!empty($product_row)) {
  251. if($end_inactive && $product_row['status'] == '0') {
  252. $quantity_data[$amazon_row['amazon_sku']] = 0;
  253. } else {
  254. $quantity_data[$amazon_row['amazon_sku']] = $product_row['quantity'];
  255. }
  256. }
  257. }
  258. }
  259. if(!empty($quantity_data)) {
  260. $logger->write('Quantity data to be sent:' . print_r($quantity_data, true));
  261. $response = $this->updateQuantities($quantity_data);
  262. $logger->write('Submit to API. Response: ' . print_r($response, true));
  263. } else {
  264. $logger->write('No quantity data need to be posted.');
  265. }
  266. }
  267. public function getLinkedSkus($product_id, $var='') {
  268. return $this->db->query("SELECT `amazon_sku` FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . (int)$product_id . "' AND `var` = '" . $this->db->escape($var) . "'")->rows;
  269. }
  270. public function getOrderdProducts($order_id) {
  271. 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)$order_id . "'")->rows;
  272. }
  273. public function validate(){
  274. if($this->config->get('openbay_amazon_status') != 0 &&
  275. $this->config->get('openbay_amazon_token') != '' &&
  276. $this->config->get('openbay_amazon_enc_string1') != '' &&
  277. $this->config->get('openbay_amazon_enc_string2') != ''){
  278. return true;
  279. }else{
  280. return false;
  281. }
  282. }
  283. public function deleteProduct($product_id){
  284. $this->db->query("DELETE FROM `" . DB_PREFIX . "amazon_product_link` WHERE `product_id` = '" . (int)$product_id . "'");
  285. }
  286. public function orderDelete($order_id){
  287. /**
  288. * @todo
  289. */
  290. }
  291. public function getOrder($order_id) {
  292. $qry = $this->db->query("SELECT * FROM `" . DB_PREFIX . "amazon_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1");
  293. if($qry->num_rows > 0){
  294. return $qry->row;
  295. }else{
  296. return false;
  297. }
  298. }
  299. public function getCarriers() {
  300. return array(
  301. "USPS",
  302. "UPS",
  303. "UPSMI",
  304. "FedEx",
  305. "DHL",
  306. "Fastway",
  307. "GLS",
  308. "GO!",
  309. "Hermes Logistik Gruppe",
  310. "Royal Mail",
  311. "Parcelforce",
  312. "City Link",
  313. "TNT",
  314. "Target",
  315. "SagawaExpress",
  316. "NipponExpress",
  317. "YamatoTransport",
  318. "DHL Global Mail",
  319. "UPS Mail Innovations",
  320. "FedEx SmartPost",
  321. "OSM",
  322. "OnTrac",
  323. "Streamlite",
  324. "Newgistics",
  325. "Canada Post",
  326. "Blue Package",
  327. "Chronopost",
  328. "Deutsche Post",
  329. "DPD",
  330. "La Poste",
  331. "Parcelnet",
  332. "Poste Italiane",
  333. "SDA",
  334. "Smartmail",
  335. "FEDEX_JP",
  336. "JP_EXPRESS",
  337. "NITTSU",
  338. "SAGAWA",
  339. "YAMATO",
  340. "BlueDart",
  341. "AFL/Fedex",
  342. "Aramex",
  343. "India Post",
  344. "Professional",
  345. "DTDC",
  346. "Overnite Express",
  347. "First Flight",
  348. "Delhivery",
  349. "Lasership",
  350. );
  351. }
  352. public function parseCategoryTemplate($xml) {
  353. $simplexml = null;
  354. libxml_use_internal_errors(true);
  355. if(($simplexml = simplexml_load_string($xml)) == false) {
  356. return false;
  357. }
  358. $category = (string)$simplexml->filename;
  359. $tabs = array();
  360. foreach($simplexml->tabs->tab as $tab) {
  361. $attributes = $tab->attributes();
  362. $tabs[] = array(
  363. 'id' => (string)$attributes['id'],
  364. 'name' => (string)$tab->name,
  365. );
  366. }
  367. $fields = array();
  368. $field_types = array('required', 'desired', 'optional');
  369. foreach ($field_types as $type) {
  370. foreach ($simplexml->fields->$type->field as $field) {
  371. $attributes = $field->attributes();
  372. $fields[] = array(
  373. 'name' => (string)$attributes['name'],
  374. 'title' => (string)$field->title,
  375. 'definition' => (string)$field->definition,
  376. 'accepted' => (array)$field->accepted,
  377. 'type' => (string)$type,
  378. 'child' => false,
  379. 'order' => (isset($attributes['order'])) ? (string)$attributes['order'] : '',
  380. 'tab' => (string)$attributes['tab'],
  381. );
  382. }
  383. foreach ($simplexml->fields->$type->childfield as $field) {
  384. $attributes = $field->attributes();
  385. $fields[] = array(
  386. 'name' => (string)$attributes['name'],
  387. 'title' => (string)$field->title,
  388. 'definition' => (string)$field->definition,
  389. 'accepted' => (array)$field->accepted,
  390. 'type' => (string)$type,
  391. 'child' => true,
  392. 'parent' => (array)$field->parent,
  393. 'order' => (isset($attributes['order'])) ? (string)$attributes['order'] : '',
  394. 'tab' => (string)$attributes['tab'],
  395. );
  396. }
  397. }
  398. foreach($fields as $index => $field) {
  399. $fields[$index]['unordered_index'] = $index;
  400. }
  401. usort($fields, array('Amazon','compareFields'));
  402. return array(
  403. 'category' => $category,
  404. 'fields' => $fields,
  405. 'tabs' => $tabs,
  406. );
  407. }
  408. private static function compareFields($field1, $field2) {
  409. if($field1['order'] == $field2['order']) {
  410. return ($field1['unordered_index'] < $field2['unordered_index']) ? -1 : 1;
  411. } else if(!empty($field1['order']) && empty($field2['order'])) {
  412. return -1;
  413. } else if(!empty($field2['order']) && empty($field1['order'])) {
  414. return 1;
  415. } else {
  416. return ($field1['order'] < $field2['order']) ? -1 : 1;
  417. }
  418. }
  419. }