FedExWebServices.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ ZVZHzvosLp+ee0BnANhYC5L7o8odOFagrOW/P3U53A0=
  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.9 [rev.7.9.01]
  11. */
  12. /**
  13. * FedEx Web Services integration
  14. */
  15. namespace Ppb\Model\Shipping\Carrier;
  16. class FedExWebServices extends AbstractCarrier
  17. {
  18. /**
  19. * shipping carrier name
  20. */
  21. const NAME = 'FedExWebServices';
  22. const WSDL_PATH = '/../../../../External/FedEx/wsdl/RateService_v16.wsdl';
  23. /**
  24. * form elements (carrier module settings)
  25. */
  26. const ACCOUNT_NUMBER = 'account_number';
  27. const METER_NUMBER = 'meter_number';
  28. const API_KEY = 'fedexws_key';
  29. const PASSWORD = 'fedexws_pwd';
  30. /**
  31. * shipping carrier specific constants
  32. */
  33. const WEIGHT_UOM = 'LB';
  34. const DIMENSIONS_UOM = 'IN';
  35. const DROPOFF_TYPE = 'REGULAR_PICKUP'; // valid values REGULAR_PICKUP, REQUEST_COURIER, ...
  36. const PACKAGING_TYPE = 'YOUR_PACKAGING'; // valid values FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, ...
  37. /**
  38. * currency
  39. */
  40. const CURRENCY = 'USD';
  41. /**
  42. *
  43. * FedEx WS carrier description
  44. *
  45. * @var string
  46. */
  47. protected $_description = 'FedExWebServices Description';
  48. /**
  49. *
  50. * package dimensions (in inches)
  51. *
  52. * @var array
  53. */
  54. protected $_dimensions = array(
  55. self::L => 25,
  56. self::W => 10,
  57. self::H => 7,
  58. );
  59. public function __construct()
  60. {
  61. parent::__construct(self::NAME, self::CURRENCY);
  62. }
  63. /**
  64. *
  65. * get FedEx Web Services setup form elements
  66. *
  67. * @return array
  68. */
  69. public function getElements()
  70. {
  71. return array(
  72. array(
  73. 'form_id' => self::NAME,
  74. 'id' => self::ACCOUNT_NUMBER,
  75. 'element' => 'text',
  76. 'label' => $this->_('Account Number'),
  77. 'attributes' => array(
  78. 'class' => 'form-control input-medium',
  79. ),
  80. ),
  81. array(
  82. 'form_id' => self::NAME,
  83. 'id' => self::METER_NUMBER,
  84. 'element' => 'text',
  85. 'label' => $this->_('Meter Number'),
  86. 'attributes' => array(
  87. 'class' => 'form-control input-medium',
  88. ),
  89. ),
  90. array(
  91. 'form_id' => self::NAME,
  92. 'id' => self::API_KEY,
  93. 'element' => 'text',
  94. 'label' => $this->_('API Key'),
  95. 'attributes' => array(
  96. 'class' => 'form-control input-medium',
  97. ),
  98. ),
  99. array(
  100. 'form_id' => self::NAME,
  101. 'id' => self::PASSWORD,
  102. 'element' => 'text',
  103. 'label' => $this->_('Password'),
  104. 'attributes' => array(
  105. 'class' => 'form-control input-medium',
  106. ),
  107. ),
  108. );
  109. }
  110. /**
  111. *
  112. * get price method - gets the price of a selected method,
  113. * or outputs a list of available methods for the selected input data
  114. *
  115. * @param string $methodName (optional) method name
  116. *
  117. * @return bool|float|array returns an array of methods, the price for the specified method or false if the price cannot be calculated
  118. * or false if the price cannot be calculated
  119. * if there is an error, the $_error variable will be set
  120. */
  121. public function getPrice($methodName = null)
  122. {
  123. $result = array();
  124. ini_set("soap.wsdl_cache_enabled", "0");
  125. $client = new \SoapClient(__DIR__ . self::WSDL_PATH, array('trace' => 1));
  126. $request = array(
  127. 'WebAuthenticationDetail' => array(
  128. 'UserCredential' => array(
  129. 'Key' => $this->_data[self::API_KEY],
  130. 'Password' => $this->_data[self::PASSWORD],
  131. ),
  132. ),
  133. 'ClientDetail' => array(
  134. 'AccountNumber' => $this->_data[self::ACCOUNT_NUMBER],
  135. 'MeterNumber' => $this->_data[self::METER_NUMBER],
  136. ),
  137. 'TransactionDetail' => array(
  138. 'CustomerTransactionId' => ' *** Rate Request using PHP ***'
  139. ),
  140. 'Version' => array(
  141. 'ServiceId' => 'crs',
  142. 'Major' => '16',
  143. 'Intermediate' => '0',
  144. 'Minor' => '0',
  145. ),
  146. 'ReturnTransitAndCommit' => true,
  147. 'RequestedShipment' => array(
  148. 'DropoffType' => self::DROPOFF_TYPE,
  149. 'ShipTimestamp' => date('c'),
  150. 'ServiceType' => $methodName, // valid values STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, ...
  151. 'PackagingType' => self::PACKAGING_TYPE,
  152. // 'RateRequestTypes' => 'ACCOUNT',
  153. // 'RateRequestTypes' => 'LIST',
  154. 'Shipper' => array(
  155. 'Address' => array(
  156. 'PostalCode' => $this->getSourceZip(),
  157. 'CountryCode' => strtoupper($this->getSourceCountry())
  158. ),
  159. ),
  160. 'Recipient' => array(
  161. 'Address' => array(
  162. 'PostalCode' => $this->getDestZip(),
  163. 'CountryCode' => strtoupper($this->getDestCountry()),
  164. ),
  165. ),
  166. 'PackageCount' => '1',
  167. 'RequestedPackageLineItems' => array(
  168. 'SequenceNumber' => 1,
  169. 'GroupPackageCount' => 1,
  170. 'Weight' => array(
  171. 'Value' => $this->getWeight(),
  172. 'Units' => self::WEIGHT_UOM
  173. ),
  174. 'Dimensions' => array(
  175. 'Length' => $this->_dimensions[self::L],
  176. 'Width' => $this->_dimensions[self::W],
  177. 'Height' => $this->_dimensions[self::H],
  178. 'Units' => self::DIMENSIONS_UOM
  179. ),
  180. ),
  181. ),
  182. );
  183. try {
  184. $response = $client->getRates($request);
  185. if (!in_array($response->HighestSeverity, array('FAILURE', 'ERROR', 'WARNING'))) {
  186. $rateReply = $response->RateReplyDetails;
  187. if ($methodName !== null) {
  188. $this->setCurrency(
  189. $rateReply->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Currency);
  190. return doubleval($rateReply->RatedShipmentDetails[0]->ShipmentRateDetail->TotalNetCharge->Amount);
  191. }
  192. foreach ($rateReply as $rateOption) {
  193. $ratedShipmentDetails = (is_array($rateOption->RatedShipmentDetails)) ?
  194. $rateOption->RatedShipmentDetails[0] : $rateOption->RatedShipmentDetails;
  195. $price = $ratedShipmentDetails->ShipmentRateDetail->TotalNetCharge->Amount;
  196. $result[] = array(
  197. 'code' => $rateOption->ServiceType,
  198. 'name' => ucwords(strtolower(str_replace('_', ' ', $rateOption->ServiceType))),
  199. 'price' => $price,
  200. 'currency' => $ratedShipmentDetails->ShipmentRateDetail->TotalNetCharge->Currency,
  201. );
  202. }
  203. }
  204. else {
  205. $notifications = $response->Notifications;
  206. if (isset($notifications->Message)) {
  207. $this->setError($notifications->Message);
  208. }
  209. else if (isset($notifications[0]->Message)) {
  210. $this->setError($notifications[0]->Message);
  211. }
  212. else {
  213. $this->setError('An unknown error has occurred.');
  214. }
  215. return false;
  216. }
  217. } catch (\SoapFault $exception) {
  218. $this->setError($exception->getMessage());
  219. return false;
  220. }
  221. return $result;
  222. }
  223. }