PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/system/library/openbay/amazon.php

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