Guest User

Untitled

a guest
Jul 17th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.63 KB | None | 0 0
  1. <?php
  2.  
  3. namespace ShippingStation;
  4.  
  5. class ShippingStation
  6. {
  7. /**
  8. * Shipping Station Curl
  9. * @var
  10. */
  11. private $curl;
  12.  
  13. /**
  14. * Construct
  15. * @param $config
  16. */
  17. public function __construct($config)
  18. {
  19. $this->curl = new ShippingStationCurl($config['api_key'],$config['api_token']);
  20. }
  21.  
  22. /**
  23. * Create a new ShipStation Account
  24. * @param $data
  25. */
  26. public function registerAccount($data)
  27. {
  28. $this->curl->post('accounts/registeraccount',$data);
  29. }
  30.  
  31. /**
  32. * List all tags defined for this account
  33. */
  34. public function listTags()
  35. {
  36. $this->curl->get('accounts/listtags');
  37. }
  38.  
  39. /**
  40. * Retrieves the shipping carrier account details for the specified carried code.
  41. * @param null $carrierCode
  42. */
  43. public function listCarriers($carrierCode = null)
  44. {
  45. $url = $carrierCode ? 'carriers/getcarrier?carrierCode='.$carrierCode : 'carriers/getcarrier';
  46. $this->curl->get($url);
  47. }
  48.  
  49. /**
  50. * Add funds to a carrier account using payment information on the file
  51. * @param $data
  52. */
  53. public function addFunds($data)
  54. {
  55. $this->curl->post('carrier/addfunds',$data);
  56. }
  57.  
  58.  
  59. /**
  60. * Retrieves a list of packages for the specified carrier
  61. * @param null $carrierCode
  62. */
  63. public function listPackages($carrierCode = null)
  64. {
  65. $url = $carrierCode ? 'carriers/listpackages?carrierCode='.$carrierCode : 'carriers/listpackages';
  66. $this->curl->get($url);
  67. }
  68.  
  69. /**
  70. * Retrieves a list of available shipping services for the specified carrier
  71. * @param null $carrierCode
  72. */
  73. public function listServices($carrierCode = null)
  74. {
  75. $url = $carrierCode ? 'carriers/listservices?carrierCode='.$carrierCode : 'carriers/listservices';
  76. $this->curl->get($url);
  77. }
  78.  
  79. /**
  80. * Get a specific customer
  81. * @param null $customer
  82. */
  83. public function customer($customer = null)
  84. {
  85. $url = $customer ? "customers/{$customer}" : 'customers';
  86. $this->curl->get($url);
  87. }
  88.  
  89. /**
  90. * params: stateCode, countryCode, tagId, marketplaceId, sortBy, sortDir, page, pageSize
  91. * @param array $params
  92. */
  93. public function customers($params = [])
  94. {
  95. $cust_params = '';
  96.  
  97. if(!empty($params)) {
  98. foreach($params as $param => $value) {
  99. if($param == 'stateCode') {
  100. $cust_params .= "stateCode={$value}";
  101. }
  102. if($param == 'countryCode') {
  103. $cust_params .= "countryCode={$value}";
  104. }
  105. if($param == 'tagId') {
  106. $cust_params .= "tagId={$value}";
  107. }
  108. if($param == 'marketplaceId') {
  109. $cust_params .= "marketplaceId={$value}";
  110. }
  111. if($param == 'sortBy') {
  112. $cust_params .= "sortBy={$value}";
  113. }
  114. if($param == 'sortDir') {
  115. $cust_params .= "sortDir={$value}";
  116. }
  117. if($param == 'page') {
  118. $cust_params .= "page={$value}";
  119. }
  120. if($param == 'pageSize') {
  121. $cust_params .= "pageSize={$value}";
  122. }
  123. }
  124. }
  125.  
  126. if($cust_params != '') {
  127. $this->curl->get("customers?{$cust_params}");
  128. }
  129.  
  130. $this->curl->get('customers');
  131. }
  132.  
  133. }
  134.  
  135. <?php
  136.  
  137. namespace ShippingStation;
  138.  
  139. use Exception;
  140. use IlluminateContractsConfigRepository;
  141.  
  142. class ShippingStationCurl
  143. {
  144. /**
  145. * Define method constants
  146. */
  147. const METHOD_POST = 'POST';
  148. const METHOD_PUT = 'PUT';
  149. const METHOD_GET = 'GET';
  150. const METHOD_DELETE = 'DELETE';
  151.  
  152. /**
  153. * Api key
  154. * @var
  155. */
  156. protected $api_key;
  157.  
  158. /**
  159. * Http code
  160. * @var
  161. */
  162. protected $http_code = 200;
  163.  
  164. /**
  165. * Api secret
  166. * @var
  167. */
  168. protected $api_secret;
  169.  
  170. /**
  171. * Error
  172. * @var string
  173. **/
  174. protected $errors = null;
  175.  
  176. /**
  177. * Auth Token
  178. * @var
  179. */
  180. protected $token;
  181.  
  182. /**
  183. * End point URL
  184. * @var string
  185. */
  186. private $endpoint = 'https://ss.shipstation.com/';
  187.  
  188. /**
  189. * Array containing headers from last performed request.
  190. * @var array
  191. */
  192. private $headers = [
  193. 'Accept' => 'Accept: application/json'
  194. ];
  195.  
  196. /**
  197. * @param string $key
  198. * @param string $value
  199. */
  200. public function setHeader($key, $value)
  201. {
  202. $this->headers[$key] = $value ? "{$key}: {$value}": $value;
  203. }
  204.  
  205. /**
  206. * Constructor
  207. * @param Repository $config
  208. * @throws Exception
  209. */
  210. public function __construct($api_key, $api_secret)
  211. {
  212. /*if(empty($api_key) or empty($api_secret)) {
  213. throw new Exception('API Key/Secret is empty.');
  214. }*/
  215.  
  216. $this->api_key = $api_key;
  217. $this->api_secret = $api_secret;
  218.  
  219. // create tokens
  220. $this->token = base64_encode(trim($this->api_key . ':' . $this->api_secret));
  221. $this->setHeader('Authorization','Basic '. $this->token);
  222. }
  223.  
  224. /**
  225. * Request method
  226. * @param $method
  227. * @param $url
  228. * @param array $parameters
  229. * @param array $headers
  230. * @return mixed
  231. * @throws Exception
  232. */
  233. private function request($method, $url, array $data = [], array $headers = [])
  234. {
  235. $curl = curl_init();
  236.  
  237. curl_setopt_array($curl, [
  238. CURLOPT_URL => $this->endpoint . $url,
  239. CURLOPT_RETURNTRANSFER => 1,
  240. CURLOPT_SSL_VERIFYPEER => 0,
  241. CURLOPT_SSL_VERIFYHOST => 0,
  242. CURLOPT_HEADER => 1,
  243. CURLINFO_HEADER_OUT => 1,
  244. CURLOPT_VERBOSE => 1,
  245. ]);
  246.  
  247. switch ($method) {
  248. case 'PUT':
  249. case 'PATCH':
  250. case 'POST':
  251. curl_setopt_array($curl, [
  252. CURLOPT_CUSTOMREQUEST => $method,
  253. CURLOPT_POSTFIELDS => $data,
  254. ]);
  255. break;
  256. case 'DELETE':
  257. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');
  258. break;
  259. default:
  260. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
  261. break;
  262. }
  263.  
  264. curl_setopt($curl, CURLOPT_HTTPHEADER, array_filter(array_values($this->headers)));
  265.  
  266. $response = curl_exec($curl);
  267. $this->http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  268.  
  269. if(curl_error($curl)){
  270. $this->errors = curl_error($curl);
  271. }
  272.  
  273. $header_size = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
  274. curl_close($curl);
  275.  
  276. if(isset($this->errors)){
  277. throw new Exception($this->errors,$this->http_code);
  278. }
  279.  
  280. $json = json_decode(substr($response, $header_size), false, 512, JSON_BIGINT_AS_STRING);
  281. return $json;
  282. }
  283.  
  284. /**
  285. * GET
  286. * @param $url
  287. * @return mixed
  288. */
  289. public function get($url)
  290. {
  291. return $this->request(self::METHOD_GET, $url);
  292. }
  293.  
  294. /**
  295. * POST
  296. * @param $url
  297. * @param array $data
  298. * @return mixed
  299. */
  300. public function post($url, $data = [])
  301. {
  302. return $this->request(self::METHOD_POST, $url, $data);
  303. }
  304.  
  305. /**
  306. * PUT
  307. * @param $url
  308. * @param array $data
  309. * @return mixed
  310. */
  311. public function put($url, $data = [])
  312. {
  313. return $this->request(self::METHOD_PUT, $url, $data);
  314. }
  315.  
  316. /**
  317. * DELETE
  318. * @param $url
  319. * @param array $data
  320. * @return mixed
  321. */
  322. public function delete($url, $data = [])
  323. {
  324. return $this->request(self::METHOD_DELETE, $url, $data);
  325. }
  326.  
  327. }
Add Comment
Please, Sign In to add comment