/lib/EasyPost/Shipment.php

https://gitlab.com/raymund/addressTax · PHP · 292 lines · 161 code · 41 blank · 90 comment · 31 complexity · 4eaa740cd726ad1dccee41a0c6a5d77b MD5 · raw file

  1. <?php
  2. namespace EasyPost;
  3. class Shipment extends EasypostResource
  4. {
  5. /**
  6. * retrieve a shipment
  7. *
  8. * @param string $id
  9. * @param string $apiKey
  10. * @return mixed
  11. */
  12. public static function retrieve($id, $apiKey = null)
  13. {
  14. return self::_retrieve(get_class(), $id, $apiKey);
  15. }
  16. /**
  17. * retrieve all shipments
  18. *
  19. * @param mixed $params
  20. * @param string $apiKey
  21. * @return mixed
  22. */
  23. public static function all($params = null, $apiKey = null)
  24. {
  25. return self::_all(get_class(), $params, $apiKey);
  26. }
  27. /**
  28. * save a shipment
  29. *
  30. * @return $this
  31. */
  32. public function save()
  33. {
  34. return self::_save(get_class());
  35. }
  36. /**
  37. * create a shipment
  38. *
  39. * @param mixed $params
  40. * @param string $apiKey
  41. * @return mixed
  42. */
  43. public static function create($params = null, $apiKey = null)
  44. {
  45. if (!isset($params['shipment']) || !is_array($params['shipment'])) {
  46. $clone = $params;
  47. unset($params);
  48. $params['shipment'] = $clone;
  49. }
  50. return self::_create(get_class(), $params, $apiKey);
  51. }
  52. /**
  53. * create a shipment from tracking code
  54. *
  55. * @param mixed $params
  56. * @param string $apiKey
  57. * @return mixed
  58. */
  59. public static function create_from_tracking_code($params = null, $apiKey = null)
  60. {
  61. $class = get_class();
  62. if (!isset($params['tracking_code'])) {
  63. $clone = $params;
  64. unset($params);
  65. $params['tracking_code'] = $clone;
  66. }
  67. $requestor = new Requestor($apiKey);
  68. $url = self::classUrl($class) . '/create_from_tracking_code';
  69. list($response, $apiKey) = $requestor->request('post', $url, $params);
  70. return Util::convertToEasyPostObject($response, $apiKey);
  71. }
  72. /**
  73. * get rates for a shipment
  74. *
  75. * @param mixed $params
  76. * @return $this
  77. * @throws \EasyPost\Error
  78. */
  79. public function get_rates($params = null)
  80. {
  81. $requestor = new Requestor($this->_apiKey);
  82. $url = $this->instanceUrl() . '/rates';
  83. list($response, $apiKey) = $requestor->request('get', $url, $params);
  84. $this->refreshFrom($response, $apiKey, true);
  85. return $this;
  86. }
  87. /**
  88. * buy a shipment
  89. *
  90. * @param mixed $params
  91. * @return $this
  92. * @throws \EasyPost\Error
  93. */
  94. public function buy($params = null)
  95. {
  96. $requestor = new Requestor($this->_apiKey);
  97. $url = $this->instanceUrl() . '/buy';
  98. if (isset($params['id']) && (!isset($params['rate']) || !is_array($params['rate']))) {
  99. $clone = $params;
  100. unset($params);
  101. $params['rate'] = $clone;
  102. }
  103. list($response, $apiKey) = $requestor->request('post', $url, $params);
  104. $this->refreshFrom($response, $apiKey, true);
  105. return $this;
  106. }
  107. /**
  108. * refund a shipment
  109. *
  110. * @param mixed $params
  111. * @return $this
  112. * @throws \EasyPost\Error
  113. */
  114. public function refund($params = null)
  115. {
  116. $requestor = new Requestor($this->_apiKey);
  117. $url = $this->instanceUrl() . '/refund';
  118. list($response, $apiKey) = $requestor->request('get', $url, $params);
  119. $this->refreshFrom($response, $apiKey, true);
  120. return $this;
  121. }
  122. /**
  123. * get the shipment barcode
  124. *
  125. * @param mixed $params
  126. * @return mixed
  127. * @throws \EasyPost\Error
  128. */
  129. public function barcode($params = null)
  130. {
  131. $requestor = new Requestor($this->_apiKey);
  132. $url = $this->instanceUrl() . '/barcode';
  133. list($response, $apiKey) = $requestor->request('get', $url, $params);
  134. return $response['barcode_url'];
  135. }
  136. /**
  137. * get the shipment stamp
  138. *
  139. * @param mixed $params
  140. * @return mixed
  141. * @throws \EasyPost\Error
  142. */
  143. public function stamp($params = null)
  144. {
  145. $requestor = new Requestor($this->_apiKey);
  146. $url = $this->instanceUrl() . '/stamp';
  147. list($response, $apiKey) = $requestor->request('get', $url, $params);
  148. return $response['stamp_url'];
  149. }
  150. /**
  151. * get the shipment label
  152. *
  153. * @param mixed $params
  154. * @return $this
  155. * @throws \EasyPost\Error
  156. */
  157. public function label($params = null)
  158. {
  159. $requestor = new Requestor($this->_apiKey);
  160. $url = $this->instanceUrl() . '/label';
  161. if (!isset($params['file_format'])) {
  162. $clone = $params;
  163. unset($params);
  164. $params['file_format'] = $clone;
  165. }
  166. list($response, $apiKey) = $requestor->request('get', $url, $params);
  167. $this->refreshFrom($response, $apiKey);
  168. return $this;
  169. }
  170. /**
  171. * insure the shipment
  172. *
  173. * @param mixed $params
  174. * @return $this
  175. * @throws \EasyPost\Error
  176. */
  177. public function insure($params = null)
  178. {
  179. $requestor = new Requestor($this->_apiKey);
  180. $url = $this->instanceUrl() . '/insure';
  181. if (!isset($params['amount'])) {
  182. $clone = $params;
  183. unset($params);
  184. $params['amount'] = $clone;
  185. }
  186. list($response, $apiKey) = $requestor->request('post', $url, $params);
  187. $this->refreshFrom($response, $apiKey);
  188. return $this;
  189. }
  190. /**
  191. * get the lowest rate for the shipment
  192. *
  193. * @param array $carriers
  194. * @param array $services
  195. * @return bool
  196. * @throws \EasyPost\Error
  197. */
  198. public function lowest_rate($carriers=array(), $services=array())
  199. {
  200. $lowest_rate = false;
  201. $carriers_include = array();
  202. $carriers_exclude = array();
  203. $services_include = array();
  204. $services_exclude = array();
  205. if(!is_array($carriers)) {
  206. $carriers = explode(',', $carriers);
  207. }
  208. for ($a = 0, $b = count($carriers); $a < $b; $a++) {
  209. $carriers[$a] = trim(strtolower($carriers[$a]));
  210. if (substr($carriers[$a], 0, 1) == '!') {
  211. $carriers_exclude[] = substr($carriers[$a], 1);
  212. } else {
  213. $carriers_include[] = $carriers[$a];
  214. }
  215. }
  216. if(!is_array($services)) {
  217. $services = explode(',', $services);
  218. }
  219. for ($c = 0, $d = count($services); $c < $d; $c++) {
  220. $services[$c] = trim(strtolower($services[$c]));
  221. if (substr($services[$c], 0, 1) == '!') {
  222. $services_exclude[] = substr($services[$c], 1);
  223. } else {
  224. $services_include[] = $services[$c];
  225. }
  226. }
  227. for ($i = 0, $k = count($this->rates); $i < $k; $i++) {
  228. $rate_carrier = strtolower($this->rates[$i]->carrier);
  229. if (!empty($carriers_include[0]) && !in_array($rate_carrier, $carriers_include)) {
  230. continue;
  231. }
  232. if (!empty($carriers_exclude[0]) && in_array($rate_carrier, $carriers_exclude)) {
  233. continue;
  234. }
  235. $rate_service = strtolower($this->rates[$i]->service);
  236. if (!empty($services_include[0]) && !in_array($rate_service, $services_include)) {
  237. continue;
  238. }
  239. if (!empty($services_exclude[0]) && in_array($rate_service, $services_exclude)) {
  240. continue;
  241. }
  242. if (!$lowest_rate || floatval($this->rates[$i]->rate) < floatval($lowest_rate->rate)) {
  243. $lowest_rate = clone($this->rates[$i]);
  244. }
  245. }
  246. if ($lowest_rate == false) {
  247. throw new Error('No rates found.');
  248. }
  249. return $lowest_rate;
  250. }
  251. }