fba.php (9340B)
1 <?php 2 namespace openbay; 3 4 final class fba { 5 private $api_key; 6 private $api_account_id; 7 private $encryption_key; 8 private $encryption_iv; 9 private $url = 'https://api.openbaypro.io/'; 10 private $registry; 11 12 private $logging = 1; 13 private $logging_verbose = 1; 14 private $max_log_size = 50; 15 16 /** 17 * Status IDs = 18 * 0 = new 19 * 1 = error 20 * 2 = held 21 * 3 = shipped 22 * 4 = cancelled 23 */ 24 25 /** 26 * Type IDs = 27 * 0 = new 28 * 1 = shipping 29 * 2 = cancel 30 */ 31 32 public function __construct($registry) { 33 $this->registry = $registry; 34 35 $this->api_key = $this->config->get('openbay_fba_api_key'); 36 $this->api_account_id = $this->config->get('openbay_fba_api_account_id'); 37 $this->logging = $this->config->get('openbay_fba_debug_log'); 38 39 $this->setEncryptionKey($this->config->get('openbay_fba_encryption_key')); 40 $this->setEncryptionIv($this->config->get('openbay_fba_encryption_iv')); 41 42 if ($this->logging == 1) { 43 $this->setLogger(); 44 } 45 } 46 47 public function __get($name) { 48 return $this->registry->get($name); 49 } 50 51 public function getEncryptionKey() { 52 return $this->encryption_key; 53 } 54 55 public function setEncryptionKey($key) { 56 $this->encryption_key = $key; 57 } 58 59 public function getEncryptionIv() { 60 return $this->encryption_iv; 61 } 62 63 public function setEncryptionIv($encryption_iv) { 64 $this->encryption_iv = $encryption_iv; 65 } 66 67 public function setApiKey($api_key) { 68 $this->api_key = $api_key; 69 } 70 71 public function setAccountId($api_account_id) { 72 $this->api_account_id = $api_account_id; 73 } 74 75 public function call($uri, $data = array(), $request_type = 'GET') { 76 $this->log("Request: " . $request_type . " : " . $this->url . $uri); 77 78 $headers = array(); 79 $headers[] = 'X-Auth-Token: ' . $this->api_key; 80 $headers[] = 'X-Account-ID: ' . $this->api_account_id; 81 $headers[] = 'X-Endpoint-Version: 2'; 82 $headers[] = 'Content-Type: application/json'; 83 84 $defaults = array( 85 CURLOPT_HEADER => 0, 86 CURLOPT_HTTPHEADER => $headers, 87 CURLOPT_URL => $this->url . $uri, 88 CURLOPT_USERAGENT => 'OpenBay Pro for Fulfillment by Amazon', 89 CURLOPT_FRESH_CONNECT => 1, 90 CURLOPT_RETURNTRANSFER => 1, 91 CURLOPT_FORBID_REUSE => 1, 92 CURLOPT_TIMEOUT => 30, 93 CURLOPT_SSL_VERIFYPEER => 0, 94 CURLOPT_SSL_VERIFYHOST => 0, 95 ); 96 97 if ($this->logging_verbose == 1) { 98 $defaults[CURLOPT_VERBOSE] = 1; 99 $defaults[CURLOPT_STDERR] = fopen(DIR_LOGS . 'fba_verbose.log', "a+"); 100 } 101 102 if ($request_type == "POST") { 103 $this->log('Request body:'); 104 $this->log(print_r($data, true)); 105 $defaults[CURLOPT_POST] = json_encode($data); 106 $defaults[CURLOPT_POSTFIELDS] = json_encode($data); 107 } else { 108 $defaults[CURLOPT_CUSTOMREQUEST] = "GET"; 109 } 110 111 $curl = curl_init(); 112 113 curl_setopt_array($curl, $defaults); 114 115 $result = curl_exec($curl); 116 117 if (!$result) { 118 $this->log('call() - Curl Failed ' . curl_error($curl) . ' ' . curl_errno($curl)); 119 120 $response = array('error' => true, 'error_messages' => array(curl_error($curl) . ' ' . curl_errno($curl)), 'body' => null, 'response_http' => 0); 121 } else { 122 $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE); 123 124 $this->log("Response: " . $http_code . " : " . strlen($result) . " bytes"); 125 126 $encoding = mb_detect_encoding($result); 127 128 if ($encoding == 'UTF-8') { 129 $result = preg_replace('/[^(\x20-\x7F)]*/', '', $result); 130 } 131 132 $result_parsed = json_decode($result, 1); 133 134 $this->log('Raw json response:'); 135 $this->log($result); 136 137 $this->log('Parsed response:'); 138 $this->log(print_r($result_parsed, true)); 139 140 $response = array( 141 'error' => false, 142 'error_messages' => array(), 143 'body' => (isset($result_parsed['result']) ? $result_parsed['result'] : ''), 144 'response_http' => $http_code 145 ); 146 147 if (isset($result_parsed['errors']) && !empty($result_parsed['errors'])) { 148 $response['error'] = true; 149 $response['error_messages'] = $result_parsed['errors']; 150 } 151 } 152 153 curl_close($curl); 154 155 return $response; 156 } 157 158 public function getServerUrl() { 159 return $this->url; 160 } 161 162 public function validate() { 163 if ($this->config->get('openbay_fba_api_account_id') && $this->config->get('openbay_fba_api_key') && $this->config->get('openbay_fba_encryption_key') && $this->config->get('openbay_fba_encryption_iv')) { 164 return true; 165 } else { 166 return false; 167 } 168 } 169 170 private function setLogger() { 171 if(file_exists(DIR_LOGS . 'fulfillment_by_amazon.log')) { 172 if(filesize(DIR_LOGS . 'fulfillment_by_amazon.log') > ($this->max_log_size * 1000000)) { 173 rename(DIR_LOGS . 'fulfillment_by_amazon.log', DIR_LOGS . '_fulfillment_by_amazon_' . date('Y-m-d_H-i-s') . '.log'); 174 } 175 } 176 177 $this->logger = new \Log('fulfillment_by_amazon.log'); 178 } 179 180 public function log($data) { 181 if ($this->logging == 1) { 182 if (function_exists('getmypid')) { 183 $process_id = getmypid(); 184 $data = $process_id . ' - ' . $data; 185 } 186 187 $this->logger->write($data); 188 } 189 } 190 191 public function createFBAOrderID($order_id) { 192 $this->db->query("INSERT INTO `" . DB_PREFIX . "fba_order` SET `order_id` = '" . (int)$order_id . "', `status` = 0, `created` = now()"); 193 194 return $this->db->getLastId(); 195 } 196 197 public function updateFBAOrderStatus($order_id, $status_id) { 198 $this->db->query("UPDATE `" . DB_PREFIX . "fba_order` SET `status` = '" . (int)$status_id . "' WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 199 } 200 201 public function updateFBAOrderRef($order_id, $ref) { 202 $this->db->query("UPDATE `" . DB_PREFIX . "fba_order` SET `fba_order_fulfillment_ref` = '" . $this->db->escape($ref) . "' WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 203 } 204 205 public function updateFBAOrderFulfillmentID($order_id, $fba_order_fulfillment_id) { 206 $this->db->query("UPDATE `" . DB_PREFIX . "fba_order` SET `fba_order_fulfillment_id` = '" . (int)$fba_order_fulfillment_id . "' WHERE `order_id` = '" . (int)$order_id . "'"); 207 } 208 209 public function createFBAFulfillmentID($order_id, $type) { 210 $this->db->query("INSERT INTO `" . DB_PREFIX . "fba_order_fulfillment` SET `created` = now(), `order_id` = '" . (int)$order_id . "', `type` = '" . (int)$type . "'"); 211 212 $id = $this->db->getLastId(); 213 214 $this->db->query("UPDATE `" . DB_PREFIX . "fba_order` SET `fba_order_fulfillment_id` = '" . (int)$id . "' WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 215 216 return $id; 217 } 218 219 public function populateFBAFulfillment($request_body, $response_body, $header_code, $fba_order_fulfillment_id) { 220 $this->db->query(" 221 UPDATE `" . DB_PREFIX . "fba_order_fulfillment` 222 SET 223 `request_body` = '" . $this->db->escape($request_body) . "', 224 `response_body` = '" . $this->db->escape($response_body) . "', 225 `response_header_code` = '" . (int)$header_code . "' 226 WHERE 227 `fba_order_fulfillment_id` = '" . (int)$fba_order_fulfillment_id . "' 228 "); 229 230 $insert_id = $this->db->getLastId(); 231 232 return $insert_id; 233 } 234 235 public function getFBAOrders($filter) { 236 $sql = ""; 237 238 // start date filter 239 if (isset($filter['filter_start'])) { 240 $sql .= " AND `created` >= '".$filter['filter_start']."'"; 241 } 242 // end date filter 243 if (isset($filter['filter_end'])) { 244 $sql .= " AND `created` <= '".$filter['filter_end']."'"; 245 } 246 // status filter 247 if (isset($filter['filter_status'])) { 248 $sql .= " AND `status` = '".$filter['filter_status']."'"; 249 } 250 251 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "fba_order` WHERE 1 ".$sql." ORDER BY `created` DESC"); 252 253 if ($query->num_rows == 0) { 254 return false; 255 } else { 256 return $query->rows; 257 } 258 } 259 260 public function getFBAOrder($order_id) { 261 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "fba_order` WHERE `order_id` = '" . (int)$order_id . "' LIMIT 1"); 262 263 if ($query->num_rows == 0) { 264 return false; 265 } else { 266 $fba_order = $query->row; 267 $fba_order['fulfillments'] = $this->getFBAOrderFulfillments($order_id); 268 269 return $fba_order; 270 } 271 } 272 273 public function getFBAOrderByRef($ref) { 274 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "fba_order` WHERE `fba_order_fulfillment_ref` = '" . $this->db->escape($ref) . "' LIMIT 1"); 275 276 if ($query->num_rows == 0) { 277 return false; 278 } else { 279 $fba_order = $query->row; 280 $fba_order['fulfillments'] = $fba_order['order_id']; 281 282 return $fba_order; 283 } 284 } 285 286 public function getFBAOrderFulfillments($order_id) { 287 $query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "fba_order_fulfillment` WHERE `order_id` = '" . (int)$order_id . "' ORDER BY `created` DESC"); 288 289 if ($query->num_rows == 0) { 290 return false; 291 } else { 292 return $query->rows; 293 } 294 } 295 296 public function hasOrderFBAItems($order_id) { 297 $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "order_product` `op` LEFT JOIN `" . DB_PREFIX . "product` `p` ON `op`.`product_id` = `p`.`product_id` WHERE `p`.`location` = 'FBA' AND `op`.`order_id` = '".(int)$order_id."'"); 298 299 if ($query->num_rows == 0) { 300 return false; 301 } else { 302 return $query->row['total']; 303 } 304 } 305 }