Repository.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace MarketplaceRepository;
  3. class Repository
  4. {
  5. const TAX_COUNTRY = 'Romania';
  6. const MARKETPLACE_ID = 6;
  7. const PREFIX = 'SHOPIFY_';
  8. const SHOW_EMPTY_TAX = false;
  9. public $token;
  10. public $shop;
  11. public function __construct($token, $shop)
  12. {
  13. $this->token = $token;
  14. $this->shop = $shop;
  15. }
  16. private function shopifyCall($token, $shop, $api_endpoint, $query = array(), $method = 'GET', $request_headers = array()) {
  17. $url = "https://" . $shop . ".myshopify.com" . $api_endpoint;
  18. if (!is_null($query) && in_array($method, array('GET', 'DELETE'))) $url = $url . "?" . http_build_query($query);
  19. $curl = curl_init($url);
  20. curl_setopt($curl, CURLOPT_HEADER, TRUE);
  21. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  22. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
  23. curl_setopt($curl, CURLOPT_MAXREDIRS, 3);
  24. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  25. curl_setopt($curl, CURLOPT_USERAGENT, 'My New Shopify App v.1');
  26. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 100);
  27. curl_setopt($curl, CURLOPT_TIMEOUT, 600);
  28. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  29. $request_headers[] = "";
  30. if (!is_null($token)) $request_headers[] = "X-Shopify-Access-Token: " . $token;
  31. curl_setopt($curl, CURLOPT_HTTPHEADER, $request_headers);
  32. if ($method != 'GET' && in_array($method, array('POST', 'PUT'))) {
  33. if (is_array($query)) $query = http_build_query($query);
  34. curl_setopt ($curl, CURLOPT_POSTFIELDS, $query);
  35. }
  36. $response = curl_exec($curl);
  37. $error_number = curl_errno($curl);
  38. $error_message = curl_error($curl);
  39. curl_close($curl);
  40. sleep(3);
  41. if ($error_number)
  42. {
  43. if(is_array($error_message))
  44. {
  45. throw new \Exception($this->printException(json_encode($error_message) . $api_endpoint));
  46. }
  47. else
  48. {
  49. throw new \Exception($this->printException($error_message . $api_endpoint));
  50. }
  51. }
  52. $response = preg_split("/\r\n\r\n|\n\n|\r\r/", $response, 2);
  53. if(isset($response[1]))
  54. {
  55. $response[1] = json_decode($response[1], true);
  56. if(isset($response[1]['errors']))
  57. {
  58. if(is_array($response[1]['errors']))
  59. {
  60. throw new \Exception($this->printException(json_encode($response[1]['errors']) . $api_endpoint));
  61. }
  62. else
  63. {
  64. throw new \Exception($this->printException($response[1]['errors'] . $api_endpoint));
  65. }
  66. }
  67. else
  68. {
  69. return $response[1];
  70. }
  71. }
  72. else
  73. {
  74. throw new \Exception($this->printException('err CURL'));
  75. }
  76. }
  77. public function getCurrency()
  78. {
  79. $result = $this->shopifyCall($this->token, $this->shop, "/admin/shop.json", array(), 'GET');
  80. if(!isset($result['shop']['currency']))
  81. {
  82. throw new \Exception($this->printException('no currency information found'));
  83. }
  84. return $result['shop']['currency'];
  85. }
  86. public function getTax()
  87. {
  88. $result = $this->shopifyCall($this->token, $this->shop, "/admin/countries.json", array(), 'GET');
  89. if(!isset($result['countries']) || empty($result['countries']))
  90. {
  91. throw new \Exception($this->printException('no information found on VAT value (error code: 1)'));
  92. }
  93. foreach($result['countries'] as $country)
  94. {
  95. if(isset($country['name']) && $country['name'] == self::TAX_COUNTRY)
  96. {
  97. if(isset($country['tax']))
  98. {
  99. return $country['tax'];
  100. }
  101. }
  102. }
  103. throw new \Exception($this->printException('no information found on VAT value (error code: 2)'));
  104. }
  105. public function areTaxesIncluded()
  106. {
  107. $result = $this->shopifyCall($this->token, $this->shop, "/admin/shop.json", array(), 'GET');
  108. if(!isset($result['shop']['taxes_included']))
  109. {
  110. throw new \Exception($this->printException('no information found on VAT value (error code: 3)'));
  111. }
  112. return $result['shop']['taxes_included'];
  113. }
  114. public function getProducts()
  115. {
  116. $result = $this->shopifyCall($this->token, $this->shop, "/admin/products.json", array(), 'GET');
  117. if(!isset($result['products']))
  118. {
  119. throw new \Exception($this->printException('no product information found'));
  120. }
  121. return $result['products'];
  122. }
  123. public function getOrders()
  124. {
  125. $result = $this->shopifyCall($this->token, $this->shop, "/admin/orders.json?status=any", array(), 'GET');
  126. if(!isset($result['orders']))
  127. {
  128. throw new \Exception($this->printException('no order information found'));
  129. }
  130. return array_reverse($result['orders']) ;
  131. }
  132. public function getLocations()
  133. {
  134. $result = $this->shopifyCall($this->token, $this->shop, "/admin/locations.json", array(), 'GET');
  135. if(!isset($result['locations']))
  136. {
  137. throw new \Exception($this->printException('no location information found'));
  138. }
  139. return $result['locations'];
  140. }
  141. public function getProductVariant($productId, $variantId)
  142. {
  143. $result = $this->shopifyCall($this->token, $this->shop, "/admin/products/" . $productId . "/variants/" . $variantId . ".json", array(), 'GET');
  144. if(!isset($result['variant']))
  145. {
  146. 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));
  147. }
  148. return $result['variant'];
  149. }
  150. public function getInventoryLevels($inventory_item_id)
  151. {
  152. $result = $this->shopifyCall($this->token, $this->shop, "/admin/inventory_levels.json?inventory_item_ids=" . $inventory_item_id, array(), 'GET');
  153. if(!isset($result['inventory_levels']))
  154. {
  155. throw new \Exception($this->printException('no inventory level information found for inventory item id: ' . $inventory_item_id));
  156. }
  157. return $result['inventory_levels'];
  158. }
  159. public function setInventoryLevel($location_id, $inventory_item_id, $qty, $disconnect_if_necessary)
  160. {
  161. $post = array(
  162. 'location_id' => $location_id,
  163. 'inventory_item_id' => $inventory_item_id,
  164. 'available' => (int) $qty,
  165. 'disconnect_if_necessary' => $disconnect_if_necessary
  166. );
  167. $this->shopifyCall($this->token, $this->shop, "/admin/inventory_levels/set.json", $post, 'POST');
  168. }
  169. public function adjustInventoryLevel($location_id, $inventory_item_id, $adjustQty, $oldQty)
  170. {
  171. if((int)$adjustQty != 0)
  172. {
  173. $post = array(
  174. 'location_id' => $location_id,
  175. 'inventory_item_id' => $inventory_item_id,
  176. 'available_adjustment' => (int) $adjustQty,
  177. );
  178. $result = $this->shopifyCall($this->token, $this->shop, "/admin/inventory_levels/adjust.json", $post, 'POST');
  179. $oldQty = (int) $oldQty;
  180. $adjustQty = (int) $adjustQty;
  181. if(
  182. !isset($result['inventory_level']['available']) ||
  183. ($oldQty + $adjustQty) !== (int) $result['inventory_level']['available']
  184. )
  185. {
  186. 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'));
  187. }
  188. }
  189. }
  190. protected function stripSpecialChars($string)
  191. {
  192. $string = str_replace(array("\n", "\r", "&nbsp;"), array('', '', ''), $string);
  193. $string = preg_replace('/[%&\'"]/', '', $string);
  194. $string = html_entity_decode($string, ENT_COMPAT, 'UTF-8');
  195. $string = preg_replace( '/[^[:print:]]/', '',$string);
  196. return $string;
  197. }
  198. protected function printException($message)
  199. {
  200. return 'Err Shopify: "' . $message . '". ';
  201. }
  202. }