123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace MarketplaceRepository;
- class Repository
- {
-
- const TAX_COUNTRY = 'Romania';
- const MARKETPLACE_ID = 6;
- const PREFIX = 'SHOPIFY_';
- const SHOW_EMPTY_TAX = false;
- public $token;
- public $shop;
- public function __construct($token, $shop)
- {
- $this->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 . '". ';
- }
- }
|