A modest collection of PHP libraries used at SparkFun.
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.

226 lines
6.4 KiB

  1. <?php
  2. namespace SparkLib\UPS;
  3. use SparkLib\UPS\Rate\AddressType,
  4. SparkLib\UPS\Rate\CodeDescriptionType,
  5. SparkLib\UPS\Rate\DimensionsType,
  6. SparkLib\UPS\Rate\InvoiceLineTotalType,
  7. SparkLib\UPS\Rate\UsernameToken,
  8. SparkLib\UPS\Rate\PackageType,
  9. SparkLib\UPS\Rate\PackageWeightType,
  10. SparkLib\UPS\Rate\ServiceAccessToken,
  11. SparkLib\UPS\Rate\RateException,
  12. SparkLib\UPS\Rate\RateRequest,
  13. SparkLib\UPS\Rate\RequestType,
  14. SparkLib\UPS\Rate\ShipperType,
  15. SparkLib\UPS\Rate\ShipFromType,
  16. SparkLib\UPS\Rate\ShipmentRatingOptionsType,
  17. SparkLib\UPS\Rate\ShipToType,
  18. SparkLib\UPS\Rate\ShipmentType,
  19. SparkLib\UPS\Rate\UPSSecurity;
  20. use SoapClient,
  21. SoapHeader,
  22. SoapFault,
  23. DOMDocument;
  24. use SparkLib\Fail;
  25. class Rate {
  26. private $_wsdl = UPS_RATE_WSDL;
  27. private $_schema = UPS_SCHEMA;
  28. private $_client;
  29. private $_options;
  30. private $_request;
  31. private $_response;
  32. private $_shipper;
  33. private $_shipFrom;
  34. private $_shipTo;
  35. private $_shipment;
  36. private $_international = false;
  37. private $_PAK = false;
  38. private $_packages = [];
  39. private $_requestedServices = [];
  40. private $_rates = [];
  41. public $upsCodes = [
  42. 1 => 'Next Day Air',
  43. 2 => '2nd Day Air',
  44. 3 => 'Ground',
  45. 7 => 'Worldwide Express Saver',
  46. 8 => 'Worldwide Expedited',
  47. 11 => 'Standard',
  48. 12 => '3 Day Select',
  49. 13 => 'Next Day Air Saver',
  50. 14 => 'Next Day Air Early AM',
  51. 59 => '2nd Day Air AM',
  52. 54 => 'Worldwide Express Plus',
  53. 65 => 'UPS Saver'
  54. ];
  55. public function __construct() {
  56. $shipmentOptions = new ShipmentRatingOptionsType("Yes", null, null);
  57. $this->_shipment = new ShipmentType();
  58. $this->_shipment->setShipmentRatingOptions($shipmentOptions);
  59. }
  60. public function addServices() {
  61. }
  62. public function allowPakRates() {
  63. $this->_PAK = true;
  64. }
  65. public function addPackage($l, $w, $h, $weight, $value = null, $units_length = 'IN',
  66. $units_weight = 'LBS') {
  67. $package = new PackageType();
  68. $package->setPackageWeight(
  69. new PackageWeightType(new CodeDescriptionType($units_weight), $weight)
  70. );
  71. if ($this->_PAK && $weight <= constant('\PAK_RATE_THRESHOLD') && $this->_international) {
  72. $package->setPackagingType(new CodeDescriptionType('04'));
  73. $InvoiceLineTotal = new InvoiceLineTotalType('USD', $value);
  74. $this->_shipment->setInvoiceLineTotal($InvoiceLineTotal);
  75. } else {
  76. $package->setPackagingType(new CodeDescriptionType('02'));
  77. $package->setDimensions(
  78. new DimensionsType(new CodeDescriptionType($units_length), $l, $w, $h)
  79. );
  80. }
  81. array_push($this->_packages, $package);
  82. }
  83. public function setShipper($name, $street, $city, $state, $postal,
  84. $country, $account = null) {
  85. $shipperAddress = new AddressType($street, $city, $state, $postal, $country);
  86. $this->_shipper = new ShipperType($name, $account, $shipperAddress);
  87. }
  88. public function setShipFrom($name, $street, $city, $state, $postal,
  89. $country, $account = null) {
  90. $shipFromAddress = new AddressType($street, $city, $state, $postal, $country);
  91. $this->_shipFrom = new ShipFromType($name, $shipFromAddress);
  92. }
  93. public function setShipTo($name, $street, $city, $state, $postal,
  94. $country, $account = null) {
  95. if ($country == 'US') {
  96. $postal = substr(preg_replace('/[^0-9]+/', '', $postal), 0, 5);
  97. } else if ($country == 'CA') {
  98. $postal = substr(preg_replace('/[^0-9A-Za-z]+/', '', $postal), 0, 6);
  99. $this->_international = true;
  100. } else {
  101. $postal = substr(preg_replace('/[^0-9A-Za-z]+/', '', $postal), 0, 9);
  102. $this->_international = true;
  103. }
  104. $shipToAddress = new AddressType($street, $city, $state, $postal, $country);
  105. $this->_shipTo = new ShipToType($name, $shipToAddress);
  106. }
  107. public function sendRequest() {
  108. $this->_shipment->setShipper($this->_shipper);
  109. $this->_shipment->setShipFrom($this->_shipFrom);
  110. $this->_shipment->setShipTo($this->_shipTo);
  111. $this->_shipment->setPackage($this->_packages);
  112. $RequestType = new RequestType('Shop');
  113. $this->_request = new RateRequest($RequestType, new CodeDescriptionType('01'),
  114. null, $this->_shipment);
  115. $UsernameToken = new UsernameToken();
  116. $ServiceAccessToken = new ServiceAccessToken();
  117. $UPSSecurity = new UPSSecurity($UsernameToken, $ServiceAccessToken);
  118. $UsernameToken->setUsername(UPS_USERID);
  119. $UsernameToken->setPassword(UPS_USERPASS);
  120. $ServiceAccessToken->setAccessLicenseNumber(UPS_APIKEY);
  121. $header = new SoapHeader($this->_schema, 'UPSSecurity', $UPSSecurity);
  122. $this->_options = [
  123. 'soap_version' => 'SOAP_1_1',
  124. 'exceptions' => true,
  125. 'location' => UPS_RATE_SERVER,
  126. 'trace' => true
  127. ];
  128. $wsdl = $this->_wsdl;
  129. $this->_client = new SoapClient($wsdl, $this->_options);
  130. $this->_client->__setSoapHeaders($header);
  131. try {
  132. $this->_response = $this->_client->ProcessRate($this->_request, $this->_options);
  133. } catch (SoapFault $s) {
  134. if (isset($s->detail)) {
  135. $err = $s->detail->Errors->ErrorDetail->PrimaryErrorCode->Description;
  136. throw new RateException($err);
  137. }
  138. }
  139. }
  140. public function getLastRequest() {
  141. $request = $this->_client->__getLastRequest();
  142. if ($request) {
  143. $dom = new DOMDocument;
  144. $dom->preserveWhiteSpace = FALSE;
  145. $dom->formatOutput = TRUE;
  146. $dom->loadXML($request);
  147. return $dom->saveXml();
  148. }
  149. }
  150. public function getLastResponse() {
  151. $request = $this->_client->__getLastResponse();
  152. if ($request) {
  153. $dom = new DOMDocument;
  154. $dom->preserveWhiteSpace = FALSE;
  155. $dom->formatOutput = TRUE;
  156. $dom->loadXML($request);
  157. return $dom->saveXml();
  158. }
  159. }
  160. public function getRate($rate_code) {
  161. }
  162. public function getRates() {
  163. if ($this->_response) {
  164. $rates = $this->_response->RatedShipment;
  165. foreach ($rates as $rate) {
  166. $rateArr = [];
  167. $code = intval($rate->Service->Code);
  168. $rateArr['code'] = $code;
  169. $rateArr['service'] = $this->upsCodes[$code];
  170. $rateArr['charge'] = floatval($rate->TotalCharges->MonetaryValue);
  171. $rateArr['negotiated_charge'] = floatval($rate->NegotiatedRateCharges->TotalCharge->MonetaryValue);
  172. array_push($this->_rates, $rateArr);
  173. }
  174. }
  175. return $this->_rates;
  176. }
  177. }