You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
7.3 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. ## PHP Shipping API
  2. ![Test Coverage](http://img.shields.io/badge/coverage-86%25-green.svg)
  3. [![Build Status](https://travis-ci.org/pdt256/shipping.svg?branch=master)](https://travis-ci.org/pdt256/shipping)
  4. [![Downloads](https://img.shields.io/packagist/dt/pdt256/shipping.svg)](https://packagist.org/packages/pdt256/shipping)
  5. [![License](https://img.shields.io/packagist/l/pdt256/shipping.svg)](https://github.com/pdt256/shipping/blob/master/LICENSE.txt)
  6. A shipping rate wrapper for USPS, UPS, and Fedex.
  7. ## Introduction
  8. This is a PHP shipping package that wraps API calls to UPS, FedEx, and USPS for shipping rates.
  9. Multiple packages can be added to get additional rates.
  10. All code (including tests) conform to the PSR-2 coding standards.
  11. The namespace and autoloader are using the PSR-4 standard.
  12. ## Example
  13. Create a shipment object:
  14. ```php
  15. $shipment = new Shipment;
  16. $shipment
  17. ->setFromIsResidential(false)
  18. ->setFromStateProvinceCode('IN')
  19. ->setFromPostalCode('46205')
  20. ->setFromCountryCode('US')
  21. ->setToIsResidential(true)
  22. ->setToPostalCode('20101')
  23. ->setToCountryCode('US');
  24. $package = new Package;
  25. $package
  26. ->setLength(12)
  27. ->setWidth(4)
  28. ->setHeight(3)
  29. ->setWeight(3);
  30. $shipment->addPackage($package);
  31. ```
  32. ## UPS (Stub) Example
  33. Below is an example request to get shipping rates from the UPS API.
  34. Notice: The below line uses a stub class to fake a response from the UPS API.
  35. You can immediately use this method in your code until you get an account with UPS.
  36. ```php
  37. 'requestAdapter' => new RateRequest\StubUPS(),
  38. ```
  39. ```php
  40. use pdt256\Shipping\UPS;
  41. use pdt256\Shipping\RateRequest;
  42. $ups = new UPS\Rate([
  43. 'prod' => false,
  44. 'accessKey' => 'XXXX',
  45. 'userId' => 'XXXX',
  46. 'password' => 'XXXX',
  47. 'shipperNumber' => 'XXXX',
  48. 'shipment' => $shipment,
  49. 'approvedCodes' => [
  50. '03', // 1-5 business days
  51. '02', // 2 business days
  52. '01', // next business day 10:30am
  53. '13', // next business day by 3pm
  54. '14', // next business day by 8am
  55. ],
  56. 'requestAdapter' => new RateRequest\StubUPS(),
  57. ]);
  58. $rates = $ups->getRates();
  59. ```
  60. Output array sorted by cost: (in cents)
  61. ```php
  62. array (
  63. 0 =>
  64. pdt256\Shipping\Quote::__set_state(array(
  65. 'code' => '03',
  66. 'name' => 'UPS Ground',
  67. 'cost' => 1910,
  68. 'transitTime' => NULL,
  69. 'deliveryEstimate' => NULL,
  70. 'carrier' => 'ups',
  71. )),
  72. 1 =>
  73. pdt256\Shipping\Quote::__set_state(array(
  74. 'code' => '02',
  75. 'name' => 'UPS 2nd Day Air',
  76. 'cost' => 4923,
  77. 'transitTime' => NULL,
  78. 'deliveryEstimate' => NULL,
  79. 'carrier' => 'ups',
  80. )),
  81. 2 =>
  82. pdt256\Shipping\Quote::__set_state(array(
  83. 'code' => '13',
  84. 'name' => 'UPS Next Day Air Saver',
  85. 'cost' => 8954,
  86. 'transitTime' => NULL,
  87. 'deliveryEstimate' => NULL,
  88. 'carrier' => 'ups',
  89. )),
  90. 3 =>
  91. pdt256\Shipping\Quote::__set_state(array(
  92. 'code' => '01',
  93. 'name' => 'UPS Next Day Air',
  94. 'cost' => 9328,
  95. 'transitTime' => NULL,
  96. 'deliveryEstimate' => NULL,
  97. 'carrier' => 'ups',
  98. )),
  99. )
  100. ```
  101. ## USPS (Stub) Example
  102. ```php
  103. use pdt256\Shipping\USPS;
  104. use pdt256\Shipping\RateRequest;
  105. $usps = new USPS\Rate([
  106. 'prod' => false,
  107. 'username' => 'XXXX',
  108. 'password' => 'XXXX',
  109. 'shipment' => $shipment,
  110. 'approvedCodes' => [
  111. '1', // 1-3 business days
  112. '4', // 2-8 business days
  113. ],
  114. 'requestAdapter' => new RateRequest\StubUSPS(),
  115. ]);
  116. $rates = $usps->getRates();
  117. ```
  118. Output array sorted by cost: (in cents)
  119. ```php
  120. array (
  121. 0 =>
  122. pdt256\Shipping\Quote::__set_state(array(
  123. 'code' => '4',
  124. 'name' => 'Parcel Post',
  125. 'cost' => 1001,
  126. 'transitTime' => NULL,
  127. 'deliveryEstimate' => NULL,
  128. 'carrier' => 'usps',
  129. )),
  130. 1 =>
  131. pdt256\Shipping\Quote::__set_state(array(
  132. 'code' => '1',
  133. 'name' => 'Priority Mail',
  134. 'cost' => 1220,
  135. 'transitTime' => NULL,
  136. 'deliveryEstimate' => NULL,
  137. 'carrier' => 'usps',
  138. )),
  139. )
  140. ```
  141. ## Fedex (Stub) Example
  142. ```php
  143. use pdt256\Shipping\Fedex;
  144. use pdt256\Shipping\RateRequest;
  145. $fedex = new Fedex\Rate([
  146. 'prod' => FALSE,
  147. 'key' => 'XXXX',
  148. 'password' => 'XXXX',
  149. 'accountNumber' => 'XXXX',
  150. 'meterNumber' => 'XXXX',
  151. 'dropOffType' => 'BUSINESS_SERVICE_CENTER',
  152. 'shipment' => $shipment,
  153. 'approvedCodes' => [
  154. 'FEDEX_EXPRESS_SAVER', // 1-3 business days
  155. 'FEDEX_GROUND', // 1-5 business days
  156. 'GROUND_HOME_DELIVERY', // 1-5 business days
  157. 'FEDEX_2_DAY', // 2 business days
  158. 'STANDARD_OVERNIGHT', // overnight
  159. ],
  160. 'requestAdapter' => new RateRequest\StubFedex(),
  161. ]);
  162. $rates = $fedex->getRates();
  163. ```
  164. Output array sorted by cost: (in cents)
  165. ```php
  166. array (
  167. 0 =>
  168. pdt256\Shipping\Quote::__set_state(array(
  169. 'code' => 'GROUND_HOME_DELIVERY',
  170. 'name' => 'Ground Home Delivery',
  171. 'cost' => 1655,
  172. 'transitTime' => 'THREE_DAYS',
  173. 'deliveryEstimate' => NULL,
  174. 'carrier' => 'fedex',
  175. )),
  176. 1 =>
  177. pdt256\Shipping\Quote::__set_state(array(
  178. 'code' => 'FEDEX_EXPRESS_SAVER',
  179. 'name' => 'Fedex Express Saver',
  180. 'cost' => 2989,
  181. 'transitTime' => NULL,
  182. 'deliveryEstimate' =>
  183. DateTime::__set_state(array(
  184. 'date' => '2014-09-30 20:00:00',
  185. 'timezone_type' => 3,
  186. 'timezone' => 'UTC',
  187. )),
  188. 'carrier' => 'fedex',
  189. )),
  190. 2 =>
  191. pdt256\Shipping\Quote::__set_state(array(
  192. 'code' => 'FEDEX_2_DAY',
  193. 'name' => 'Fedex 2 Day',
  194. 'cost' => 4072,
  195. 'transitTime' => NULL,
  196. 'deliveryEstimate' =>
  197. DateTime::__set_state(array(
  198. 'date' => '2014-09-29 20:00:00',
  199. 'timezone_type' => 3,
  200. 'timezone' => 'UTC',
  201. )),
  202. 'carrier' => 'fedex',
  203. )),
  204. 3 =>
  205. pdt256\Shipping\Quote::__set_state(array(
  206. 'code' => 'STANDARD_OVERNIGHT',
  207. 'name' => 'Standard Overnight',
  208. 'cost' => 7834,
  209. 'transitTime' => NULL,
  210. 'deliveryEstimate' =>
  211. DateTime::__set_state(array(
  212. 'date' => '2014-09-26 20:00:00',
  213. 'timezone_type' => 3,
  214. 'timezone' => 'UTC',
  215. )),
  216. 'carrier' => 'fedex',
  217. )),
  218. )
  219. ```
  220. ## Unit Tests:
  221. ```bash
  222. vendor/bin/phpunit
  223. ```
  224. ### With Code Coverage:
  225. ```bash
  226. vendor/bin/phpunit --coverage-text --coverage-html coverage_report
  227. ```
  228. ### With Live API Tests:
  229. ```bash
  230. ./live_phpunit.sh
  231. ```
  232. ## Run Coding Standards Test:
  233. ```bash
  234. vendor/bin/phpcs --standard=PSR2 src/ tests/
  235. ```
  236. ### License
  237. The MIT License (MIT)
  238. Copyright (c) 2014 Jamie Isaacs <pdt256@gmail.com>
  239. Permission is hereby granted, free of charge, to any person obtaining a copy
  240. of this software and associated documentation files (the "Software"), to deal
  241. in the Software without restriction, including without limitation the rights
  242. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  243. copies of the Software, and to permit persons to whom the Software is
  244. furnished to do so, subject to the following conditions:
  245. The above copyright notice and this permission notice shall be included in
  246. all copies or substantial portions of the Software.
  247. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  248. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  249. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  250. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  251. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  252. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  253. THE SOFTWARE.