token = $token; $this->shop = $shop; } private function shopifyCall($token, $shop, $api_endpoint, $query = array(), $method = 'GET', $request_headers = array()) { $url = "https://" . $shop . ".myshopify.com" . $api_endpoint; if (!is_null($query) && in_array($method, array('GET', 'DELETE'))) $url = $url . "?" . http_build_query($query); $curl = curl_init($url); curl_setopt($curl, CURLOPT_HEADER, TRUE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE); curl_setopt($curl, CURLOPT_MAXREDIRS, 3); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_USERAGENT, 'My New Shopify App v.1'); curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 100); curl_setopt($curl, CURLOPT_TIMEOUT, 600); curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); $request_headers[] = ""; if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token; curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers); if ($method != 'GET' && in_array($method, array('POST', 'PUT'))) { if (is_array($query)) $query = http_build_query($query); curl_setopt ($curl, CURLOPT_POSTFIELDS, $query); } $response = curl_exec($curl); $error_number = curl_errno($curl); $error_message = curl_error($curl); curl_close($curl); sleep(3); if ($error_number) { if(is_array($error_message)) { throw new \Exception($this->printException(json_encode($error_message) . $api_endpoint)); } else { throw new \Exception($this->printException($error_message . $api_endpoint)); } } $response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2); if(isset($response[1])) { $response[1] = json_decode($response[1], true); if(isset($response[1]['errors'])) { if(is_array($response[1]['errors'])) { throw new \Exception($this->printException(json_encode($response[1]['errors']) . $api_endpoint)); } else { throw new \Exception($this->printException($response[1]['errors'] . $api_endpoint)); } } else { return $response[1]; } } else { throw new \Exception($this->printException('err CURL')); } } public function getCurrency() { $result = $this->shopifyCall($this->token, $this->shop, "/admin/shop.json", array(), 'GET'); if(!isset($result['shop']['currency'])) { throw new \Exception($this->printException('no currency information found')); } return $result['shop']['currency']; } public function getTax() { $result = $this->shopifyCall($this->token, $this->shop, "/admin/countries.json", array(), 'GET'); if(!isset($result['countries']) || empty($result['countries'])) { throw new \Exception($this->printException('no information found on VAT value (error code: 1)')); } foreach($result['countries'] as $country) { if(isset($country['name']) && $country['name'] == self::TAX_COUNTRY) { if(isset($country['tax'])) { return $country['tax']; } } } throw new \Exception($this->printException('no information found on VAT value (error code: 2)')); } public function areTaxesIncluded() { $result = $this->shopifyCall($this->token, $this->shop, "/admin/shop.json", array(), 'GET'); if(!isset($result['shop']['taxes_included'])) { throw new \Exception($this->printException('no information found on VAT value (error code: 3)')); } return $result['shop']['taxes_included']; } public function getProducts() { $result = $this->shopifyCall($this->token, $this->shop, "/admin/products.json", array(), 'GET'); if(!isset($result['products'])) { throw new \Exception($this->printException('no product information found')); } return $result['products']; } public function getOrders() { $result = $this->shopifyCall($this->token, $this->shop, "/admin/orders.json?status=any", array(), 'GET'); if(!isset($result['orders'])) { throw new \Exception($this->printException('no order information found')); } return array_reverse($result['orders']) ; } public function getLocations() { $result = $this->shopifyCall($this->token, $this->shop, "/admin/locations.json", array(), 'GET'); if(!isset($result['locations'])) { throw new \Exception($this->printException('no location information found')); } return $result['locations']; } public function getProductVariant($productId, $variantId) { $result = $this->shopifyCall($this->token, $this->shop, "/admin/products/" . $productId . "/variants/" . $variantId . ".json", array(), 'GET'); if(!isset($result['variant'])) { throw new \Exception($this->printException('no information was found about the product option with the id: ' . $variantId . ' which belongs to the product with the id: ' . $productId)); } return $result['variant']; } public function getInventoryLevels($inventory_item_id) { $result = $this->shopifyCall($this->token, $this->shop, "/admin/inventory_levels.json?inventory_item_ids=" . $inventory_item_id, array(), 'GET'); if(!isset($result['inventory_levels'])) { throw new \Exception($this->printException('no inventory level information found for inventory item id: ' . $inventory_item_id)); } return $result['inventory_levels']; } public function setInventoryLevel($location_id, $inventory_item_id, $qty, $disconnect_if_necessary) { $post = array( 'location_id' => $location_id, 'inventory_item_id' => $inventory_item_id, 'available' => (int) $qty, 'disconnect_if_necessary' => $disconnect_if_necessary ); $this->shopifyCall($this->token, $this->shop, "/admin/inventory_levels/set.json", $post, 'POST'); } public function adjustInventoryLevel($location_id, $inventory_item_id, $adjustQty, $oldQty) { if((int)$adjustQty != 0) { $post = array( 'location_id' => $location_id, 'inventory_item_id' => $inventory_item_id, 'available_adjustment' => (int) $adjustQty, ); $result = $this->shopifyCall($this->token, $this->shop, "/admin/inventory_levels/adjust.json", $post, 'POST'); $oldQty = (int) $oldQty; $adjustQty = (int) $adjustQty; if( !isset($result['inventory_level']['available']) || ($oldQty + $adjustQty) !== (int) $result['inventory_level']['available'] ) { throw new \Exception($this->printException('the inventory level with the inventory item id: ' . $inventory_item_id . ' and location id: ' . $location_id . ' was not adjusted correctly')); } } } protected function stripSpecialChars($string) { $string = str_replace(array("\n", "\r", " "), array('', '', ''), $string); $string = preg_replace('/[%&\'"]/', '', $string); $string = html_entity_decode($string, ENT_COMPAT, 'UTF-8'); $string = preg_replace( '/[^[:print:]]/', '',$string); return $string; } protected function printException($message) { return 'Err Shopify: "' . $message . '". '; } }