USPS.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ vMhZP2SvEKS+kPQl1LoieLC5zRWyPYXhuYCLLiez8hw=
  5. *
  6. * @link http://www.phpprobid.com
  7. * @copyright Copyright (c) 2017 Online Ventures Software & CodeCube SRL
  8. * @license http://www.phpprobid.com/license Commercial License
  9. *
  10. * @version 7.10 [rev.7.10.01]
  11. */
  12. /**
  13. * USPS shipping carrier model class
  14. */
  15. namespace Ppb\Model\Shipping\Carrier;
  16. use Cube\Config\Xml,
  17. Cube\Locale\Format,
  18. Ppb\Service\Table\Relational\Locations as LocationsService;
  19. class USPS extends AbstractCarrier
  20. {
  21. /**
  22. * shipping carrier name
  23. */
  24. const NAME = 'USPS';
  25. /**
  26. * form elements (carrier module settings)
  27. */
  28. const USERNAME = 'username';
  29. const PASSWORD = 'password';
  30. /**
  31. * shipping carrier specific constants
  32. */
  33. const SERVER = 'http://Production.ShippingAPIs.com/ShippingAPI.dll';
  34. const CONTAINER = "Variable";
  35. const SIZE = "Regular";
  36. const MACHINABLE = "True";
  37. const WEIGHT_UOM = 'lbs';
  38. const DIMENSIONS_UOM = 'in';
  39. /**
  40. * currency
  41. */
  42. const CURRENCY = 'USD';
  43. /**
  44. *
  45. * shipping carrier description
  46. *
  47. * @var string
  48. */
  49. protected $_description = 'USPS Description';
  50. /**
  51. *
  52. * service type
  53. *
  54. * @var string
  55. */
  56. private $_service = 'ALL';
  57. public function __construct()
  58. {
  59. parent::__construct(self::NAME, self::CURRENCY);
  60. }
  61. /**
  62. *
  63. * get USPS setup form elements
  64. *
  65. * @return array
  66. */
  67. public function getElements()
  68. {
  69. return array(
  70. array(
  71. 'form_id' => self::NAME,
  72. 'id' => self::USERNAME,
  73. 'element' => 'text',
  74. 'label' => $this->_('Username'),
  75. 'description' => $this->_('Enter your USPS account username.'),
  76. 'attributes' => array(
  77. 'class' => 'form-control input-medium',
  78. ),
  79. ),
  80. array(
  81. 'form_id' => self::NAME,
  82. 'id' => self::PASSWORD,
  83. 'element' => 'text',
  84. 'label' => $this->_('Password'),
  85. 'description' => $this->_('Enter your USPS account password (optional).'),
  86. 'attributes' => array(
  87. 'class' => 'form-control input-medium',
  88. ),
  89. )
  90. );
  91. }
  92. public function getService()
  93. {
  94. return $this->_service;
  95. }
  96. public function setService($service)
  97. {
  98. if ($service == "USPSBPM") {
  99. $service = "BPM";
  100. }
  101. else if ($service == "USPSFCM") {
  102. $service = "First Class";
  103. }
  104. else if ($service == "USPSMM") {
  105. $service = "Media";
  106. }
  107. else {
  108. $service = 'ALL';
  109. }
  110. $this->_service = $service;
  111. return $this;
  112. }
  113. public function getMethods()
  114. {
  115. return array();
  116. }
  117. /**
  118. *
  119. * get price method - gets the price of a selected method,
  120. * or outputs a list of available methods for the selected input data
  121. *
  122. * @param string $methodName (optional) method name
  123. *
  124. * @return bool|float|array returns an array of methods, the price for the specified method
  125. * or false if the price cannot be calculated
  126. * if there is an error, the $_error variable will be set
  127. */
  128. public function getPrice($methodName = null)
  129. {
  130. $sourceCountry = strtoupper($this->getSourceCountry());
  131. $destCountry = strtoupper($this->getDestCountry());
  132. if ($sourceCountry != 'US') {
  133. $this->setError('The service can only be used when shipping from the US.');
  134. return false; // will only send from the USA
  135. }
  136. $weight = $this->getWeight();
  137. $pounds = strtok($weight, '.');
  138. $ounces = intval(intval(substr($weight, strrpos($weight, '.') + 1)) * 16 / 10);
  139. $dimensions = $this->getDimensions();
  140. $width = Format::getInstance()->localizedToNumeric($dimensions[self::W]);
  141. $length = Format::getInstance()->localizedToNumeric($dimensions[self::L]);
  142. $height = Format::getInstance()->localizedToNumeric($dimensions[self::H]);
  143. if ($destCountry == 'US') {
  144. // may need to urlencode xml portion
  145. $str = self::SERVER . "?API=RateV4&XML="
  146. . "<RateV4Request%20USERID=\"" . urlencode($this->_data[self::USERNAME]) . "\"%20PASSWORD=\"" . urlencode($this->_data[self::PASSWORD]) . "\">"
  147. . "<Revision/>"
  148. . "<Package%20ID=\"0\">"
  149. . "<Service>" . urlencode($this->getService()) . "</Service>"
  150. . "<ZipOrigination>" . urlencode($this->getSourceZip()) . "</ZipOrigination>"
  151. . "<ZipDestination>" . urlencode($this->getDestZip()) . "</ZipDestination>"
  152. . "<Pounds>" . urlencode($pounds) . "</Pounds><Ounces>" . urlencode($ounces) . "</Ounces>"
  153. . "<Container>" . self::CONTAINER . "</Container>"
  154. . "<Size>" . self::SIZE . "</Size>"
  155. . "<Width>" . urlencode($width) . "</Width>"
  156. . "<Length>" . urlencode($length) . "</Length>"
  157. . "<Height>" . urlencode($height) . "</Height>"
  158. . "<Machinable>" . self::MACHINABLE . "</Machinable>"
  159. . "</Package></RateV4Request>";
  160. }
  161. else {
  162. $locationsService = new LocationsService();
  163. $country = $locationsService->findBy('iso_code', $destCountry);
  164. $destCountryName = (!empty($country['name'])) ? $country['name'] : $destCountry;
  165. $str = self::SERVER . "?API=IntlRateV2&XML="
  166. . "<IntlRateV2Request%20USERID=\"" . urlencode($this->_data[self::USERNAME]) . "\"%20PASSWORD=\"" . urlencode($this->_data[self::PASSWORD]) . "\">"
  167. . "<Package%20ID=\"0\">"
  168. . "<Pounds>" . $this->getWeight() . "</Pounds><Ounces>0</Ounces>"
  169. . "<Machinable>" . self::MACHINABLE . "</Machinable>"
  170. . "<MailType>Package</MailType>"
  171. . "<GXG>"
  172. . "<POBoxFlag>Y</POBoxFlag>"
  173. . "<GiftFlag>Y</GiftFlag>"
  174. . "</GXG>"
  175. . "<ValueOfContents>200</ValueOfContents>"
  176. . "<Country>" . urlencode($destCountryName) . "</Country>"
  177. . "<Container>" . self::CONTAINER . "</Container>"
  178. . "<Size>" . self::SIZE . "</Size>"
  179. . "<Width>" . urlencode($width) . "</Width>"
  180. . "<Length>" . urlencode($length) . "</Length>"
  181. . "<Height>" . urlencode($height) . "</Height>"
  182. . "<Girth>" . urlencode($height) . "</Girth>"
  183. . "</Package></IntlRateV2Request>";
  184. }
  185. $ch = curl_init();
  186. curl_setopt($ch, CURLOPT_URL, $str);
  187. curl_setopt($ch, CURLOPT_HEADER, 0);
  188. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  189. $res = curl_exec($ch);
  190. curl_close($ch);
  191. $xmlObject = new Xml();
  192. $xmlObject->setData($res);
  193. $fatalError = $xmlObject->getData();
  194. $error = $xmlObject->getData('Error');
  195. $package = $xmlObject->getData('Package');
  196. if (isset($fatalError['Description'])) {
  197. $this->setError($fatalError['Description']);
  198. return false;
  199. }
  200. else if (isset($error['Description'])) {
  201. $this->setError($error['Description']);
  202. return false;
  203. }
  204. else if (isset($package['Error']['Description'])) {
  205. $this->setError($package['Error']['Description']);
  206. return false;
  207. }
  208. $result = array();
  209. if ($destCountry == 'US') {
  210. $methods = $xmlObject->getData('Postage');
  211. $nameKey = 'MailService';
  212. $rateKey = 'Rate';
  213. }
  214. else {
  215. $methods = $xmlObject->getData('Service');
  216. $nameKey = 'SvcDescription';
  217. $rateKey = 'Postage';
  218. }
  219. foreach ($methods as $method) {
  220. $price = $method[$rateKey];
  221. if ($price > 0) {
  222. $result[] = array(
  223. 'code' => $method[$nameKey],
  224. 'name' => $this->_formatServiceName($method[$nameKey]),
  225. 'price' => $method[$rateKey],
  226. 'currency' => self::CURRENCY,
  227. );
  228. }
  229. }
  230. if ($methodName !== null) {
  231. if (!array_key_exists($methodName, $result)) {
  232. $translate = $this->getTranslate();
  233. $this->setError(
  234. sprintf($translate->_('The "%s" shipping method does not exist.'), $methodName));
  235. return false;
  236. }
  237. else {
  238. return doubleval($result[$methodName]);
  239. }
  240. }
  241. return $result;
  242. }
  243. /**
  244. *
  245. * format the name of the service to remove any html tags
  246. *
  247. * @param string $name
  248. *
  249. * @return string
  250. */
  251. protected function _formatServiceName($name)
  252. {
  253. return strip_tags(
  254. str_ireplace(array('&lt;', '&gt;'), array('<', '>'), $name));
  255. }
  256. }