UPS.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ CxzqYeA6NWTxKzHBWMnQKW818Dsa4MBkZZN6EjlC9BY=
  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. * UPS shipping carrier model class
  14. */
  15. namespace Ppb\Model\Shipping\Carrier;
  16. class UPS extends AbstractCarrier
  17. {
  18. /**
  19. * shipping carrier name
  20. */
  21. const NAME = 'UPS';
  22. /**
  23. * shipping carrier specific constants
  24. */
  25. const UPS_RATE = 'Regular+Daily+Pickup';
  26. const CONTAINER_CODE = 'Customer+Counter'; // customer packaging
  27. const ADDRESS_TYPE = '1';
  28. /**
  29. * currency
  30. */
  31. const CURRENCY = 'USD';
  32. /**
  33. *
  34. * UPS carrier description
  35. *
  36. * @var string
  37. */
  38. protected $_description = 'UPS Description';
  39. /**
  40. *
  41. * carrier methods array - defined by each carrier class
  42. *
  43. * @var array
  44. */
  45. protected $_methods = array(
  46. self::DOM => array(
  47. '1DM' => 'Next Day Air Early AM',
  48. '1DA' => 'Next Day Air',
  49. '1DP' => 'Next Day Air Saver',
  50. '2DM' => '2nd Day Air Early AM',
  51. '2DA' => '2nd Day Air',
  52. '3DS' => '3 Day Select',
  53. 'GND' => 'Ground',
  54. ),
  55. self::INTL => array(
  56. 'XPR' => 'Worldwide Express',
  57. 'XDM' => 'Worldwide Express Plus',
  58. 'XPD' => 'Worldwide Expedited',
  59. 'WXS' => 'Worldwide Saver',
  60. ),
  61. );
  62. public function __construct()
  63. {
  64. parent::__construct(self::NAME, self::CURRENCY);
  65. }
  66. /**
  67. *
  68. * get UPS setup form elements
  69. *
  70. * @return array
  71. */
  72. public function getElements()
  73. {
  74. return array();
  75. }
  76. /**
  77. *
  78. * get price method - gets the price of a selected method,
  79. * or outputs a list of available methods for the selected input data
  80. *
  81. * @param string $methodName (optional) method name
  82. *
  83. * @return bool|float|array returns an array of methods, the price for the specified method or false if the price cannot be calculated
  84. * or false if the price cannot be calculated
  85. * if there is an error, the $_error variable will be set
  86. */
  87. public function getPrice($methodName = null)
  88. {
  89. $result = array();
  90. $methodsKey = ($this->_sourceCountry == $this->_destCountry) ? self::DOM : self::INTL;
  91. $methods = $this->_methods[$methodsKey];
  92. foreach ($methods as $key => $method) {
  93. $price = $this->_callService($key);
  94. if ($price > 0) {
  95. $result[$key] = array(
  96. 'code' => $key,
  97. 'name' => $method,
  98. 'price' => $price,
  99. 'currency' => self::CURRENCY,
  100. );
  101. }
  102. }
  103. if ($methodName !== null) {
  104. if (!array_key_exists($methodName, $result)) {
  105. $translate = $this->getTranslate();
  106. $this->setError(
  107. sprintf($translate->_('The "%s" shipping method does not exist.'), $methodName));
  108. return false;
  109. }
  110. else {
  111. return doubleval($result[$methodName]);
  112. }
  113. }
  114. if (count($result) > 0) {
  115. return $result;
  116. }
  117. $this->setError('UPS rate calculator error');
  118. return false;
  119. }
  120. protected function _callService($method)
  121. {
  122. $output = null;
  123. $url = join('&', array(
  124. 'http://www.ups.com/using/services/rave/qcostcgi.cgi?accept_UPS_license_agreement=yes',
  125. '10_action=4',
  126. '13_product=' . urlencode($method),
  127. '14_origCountry=' . strtoupper($this->getSourceCountry()),
  128. '15_origPostal=' . $this->getSourceZip(),
  129. '19_destPostal=' . $this->getDestZip(),
  130. '22_destCountry=' . strtoupper($this->getDestCountry()),
  131. '23_weight=' . $this->getWeight(),
  132. '47_rateChart=' . self::UPS_RATE,
  133. '48_container=' . self::CONTAINER_CODE,
  134. '49_residential=' . self::ADDRESS_TYPE
  135. ));
  136. $fp = fopen($url, 'r');
  137. if (is_resource($fp)) {
  138. while (!feof($fp)) {
  139. $result = explode("%", fgets($fp, 500));
  140. $mtd = (!empty($result[1])) ? $result[1] : null;
  141. if ($mtd == $method) {
  142. return doubleval($result[8]);
  143. }
  144. }
  145. fclose($fp);
  146. }
  147. return null;
  148. }
  149. }