Example #1
0
 public static function getInstance()
 {
     if (self::$__instance == NULL) {
         self::$__instance = new Config();
     }
     return self::$__instance;
 }
 /**
  * Call Snap Fulfil API
  *
  * @param string $action
  * @param array $parameters
  * @param bool $put
  * @param int $instance
  * @param bool $log
  * @param bool $delete
  * @param bool $gui (specifies if a gui is requesting it
  * @return array|stdClass
  * @throws SnapFulfilAPIError
  */
 public static function call($action, $parameters = null, $put = FALSE, $instance = 1, $log = FALSE, $delete = FALSE, $gui = FALSE)
 {
     if (defined('DISABLE_SNAP') && DISABLE_SNAP === true) {
         return new stdClass();
     }
     $config = SnapConfig::config($instance);
     $ch = curl_init($config['url'] . $action);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
     curl_setopt($ch, CURLOPT_TIMEOUT, 120);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
     curl_setopt($ch, CURLOPT_USERPWD, $config['username'] . ":" . $config['password']);
     $header_params = array('Content-type: application/json');
     $post_fields = SafeJson::encode($parameters);
     if (!empty($parameters)) {
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
     }
     if ($put) {
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
         if ($parameters === NULL) {
             $header_params[] = 'Content-length: 0';
         }
     }
     if ($delete) {
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
         if ($parameters === NULL) {
             $header_params[] = 'Content-length: 0';
         }
     }
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header_params);
     $result = curl_exec($ch);
     $header = curl_getinfo($ch);
     curl_close($ch);
     // temp code to track api calls to snap
     if ($log) {
         //$trace     = debug_backtrace();
         $trace = debug_backtrace();
         $api_audit = ClassRegistry::init('ApiAudit');
         $api_data = ['reference_name' => "Snap API", 'reference_id' => 0, 'source' => "", 'destination' => $action, 'sent' => json_encode(['parameters' => $parameters, 'trace' => $trace]), 'received' => $header['http_code'] . " - " . $result, 'notes' => 'Snap API Call'];
         $api_audit->clear();
         $api_audit->create($api_data);
         $api_audit->save();
     }
     switch ($header['http_code']) {
         case 200:
             //returned data
             return json_decode($result);
             break;
         case 201:
             //entity created
             return json_decode($result);
             break;
         case 204:
             //No Content
             return FALSE;
             break;
         case 304:
             //              error_log("Already exists");
             //if being sent to the GUI display the duplicate error message
             if ($result == '' && $delete == FALSE && $gui == TRUE) {
                 throw new SnapFulfilAPIError("shipment already exists in Snap.", $header['http_code']);
             } elseif ($result == '' && $delete == FALSE) {
                 //don't error out when being processed by the SQS snap send
                 return TRUE;
             } else {
                 //catch all other situations
                 throw new SnapFulfilAPIError("Snap send error.", $header['http_code']);
             }
             break;
         case 400:
             //invalid entity//
             // not in Snap
             if (strpos($result, 'SKUId":"SKU"') !== FALSE) {
                 throw new SnapFulfilAPIError("One of the submitted SKUs is not in the Snap, " . "the shipment didn't send correctly, contact Support to resolve.", $header['http_code']);
             } else {
                 throw new SnapFulfilAPIError("Invalid entity.", $header['http_code']);
             }
             break;
         case 401:
             //not authorized
             //              error_log("Not authorized");
             return FALSE;
             break;
         case 404:
             //entity not found
             //              $error = !empty($result) ? $result : "No message returned";
             //              error_log("Not found error: " . $error);
             throw new SnapFulfilAPIError("Entity not found", $header['http_code']);
             break;
         case 409:
             //Conflict
             //              $error = !empty($result) ? $result : "No message returned";
             //              error_log("Conflict: " . $error);
             return FALSE;
             break;
         case 500:
         case 503:
             sleep(5);
             break;
         default:
             //              error_log(curl_error($ch));
             throw new InternalErrorException();
             break;
     }
     return false;
 }