AustraliaPost.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. *
  4. * PHP Pro Bid $Id$ I6nwNjNrN8mxHrM2YnldKBUNU64nRCB/RyfuYU59sVY=
  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. * Australia Post shipping carrier model class
  14. */
  15. namespace Ppb\Model\Shipping\Carrier;
  16. use Ppb\Model\Shipping as ShippingModel;
  17. class AustraliaPost extends AbstractCarrier
  18. {
  19. /**
  20. * shipping carrier name
  21. */
  22. const NAME = 'AustraliaPost';
  23. /**
  24. * form elements (carrier module settings)
  25. */
  26. const API_KEY = 'api_key';
  27. const PRODUCTION_MODE = 'production_mode';
  28. /**
  29. * shipping carrier specific constants
  30. */
  31. const PROD_SERVER = 'https://auspost.com.au/api/';
  32. const TEST_SERVER = 'https://test.npe.auspost.com.au/api/';
  33. const TEST_API_KEY = '28744ed5982391881611cca6cf5c2409';
  34. /**
  35. * api urls
  36. */
  37. const URL_DOM_PARCEL_SERVICE_LIST = 'postage/parcel/domestic/service.json';
  38. const URL_INTL_PARCEL_SERVICE_LIST = 'postage/parcel/international/service.json';
  39. const URL_DOM_PARCEL_CALCULATE = 'postage/parcel/domestic/calculate.json';
  40. const URL_INTL_PARCEL_CALCULATE = 'postage/parcel/international/calculate.json';
  41. /**
  42. * currency
  43. */
  44. const CURRENCY = 'AUD';
  45. /**
  46. *
  47. * shipping carrier description
  48. *
  49. * @var string
  50. */
  51. protected $_description = 'Australia Post Description';
  52. /**
  53. *
  54. * carrier weight uom (override)
  55. *
  56. * @var string
  57. */
  58. protected static $_carrierWeightUom = ShippingModel::UOM_KG;
  59. /**
  60. *
  61. * carrier dimensions uom (override)
  62. *
  63. * @var string
  64. */
  65. protected static $_carrierDimensionsUom = ShippingModel::UOM_CM;
  66. /**
  67. *
  68. * api key
  69. *
  70. * @var string
  71. */
  72. private $_apiKey = self::TEST_API_KEY;
  73. /**
  74. *
  75. * server url
  76. *
  77. * @var string
  78. */
  79. private $_server = self::TEST_SERVER;
  80. /**
  81. *
  82. * package dimensions (in cm)
  83. *
  84. * @var array
  85. */
  86. protected $_dimensions = array(
  87. self::L => 40,
  88. self::W => 40,
  89. self::H => 20,
  90. );
  91. public function __construct()
  92. {
  93. parent::__construct(self::NAME, self::CURRENCY);
  94. if ($this->_data[self::PRODUCTION_MODE]) {
  95. $this->_apiKey = $this->_data[self::API_KEY];
  96. $this->_server = self::PROD_SERVER;
  97. }
  98. }
  99. /**
  100. *
  101. * get Australia Post setup form elements
  102. *
  103. * @return array
  104. */
  105. public function getElements()
  106. {
  107. return array(
  108. array(
  109. 'form_id' => self::NAME,
  110. 'id' => self::API_KEY,
  111. 'element' => 'text',
  112. 'label' => $this->_('API Key'),
  113. 'description' => $this->_('Enter your Australia Post API Key.'),
  114. 'attributes' => array(
  115. 'class' => 'form-control input-medium',
  116. ),
  117. ),
  118. array(
  119. 'form_id' => self::NAME,
  120. 'id' => self::PRODUCTION_MODE,
  121. 'element' => 'checkbox',
  122. 'label' => $this->_('Production Mode'),
  123. 'multiOptions' => array(
  124. 1 => null,
  125. ),
  126. 'description' => $this->_('Check this checkbox to switch the module to production mode.'),
  127. ),
  128. );
  129. }
  130. /**
  131. *
  132. * get price method - gets the price of a selected method,
  133. * or outputs a list of available methods for the selected input data
  134. *
  135. * @param string $methodName (optional) method name
  136. *
  137. * @return bool|float|array returns an array of methods, the price for the specified method
  138. * or false if the price cannot be calculated
  139. * if there is an error, the $_error variable will be set
  140. */
  141. public function getPrice($methodName = null)
  142. {
  143. $result = array();
  144. $sourceCountry = strtoupper($this->getSourceCountry());
  145. $destCountry = strtoupper($this->getDestCountry());
  146. if ($sourceCountry != 'AU') {
  147. $this->setError('The service can only be used when shipping from Australia.');
  148. return false; // will only send from Australia
  149. }
  150. if ($destCountry == 'AU') {
  151. if ($methodName !== null) {
  152. $url = self::URL_DOM_PARCEL_CALCULATE . '?' .
  153. join('&', array(
  154. 'from_postcode=' . $this->getSourceZip(),
  155. 'to_postcode=' . $this->getDestZip(),
  156. 'length=' . $this->_dimensions[self::L],
  157. 'width=' . $this->_dimensions[self::W],
  158. 'height=' . $this->_dimensions[self::H],
  159. 'weight=' . $this->getWeight(),
  160. 'service_code=' . $methodName,
  161. ));
  162. }
  163. else {
  164. $url = self::URL_DOM_PARCEL_SERVICE_LIST . '?' .
  165. join('&', array(
  166. 'from_postcode=' . $this->getSourceZip(),
  167. 'to_postcode=' . $this->getDestZip(),
  168. 'length=' . $this->_dimensions[self::L],
  169. 'width=' . $this->_dimensions[self::W],
  170. 'height=' . $this->_dimensions[self::H],
  171. 'weight=' . $this->getWeight(),
  172. ));
  173. }
  174. }
  175. else {
  176. if ($methodName !== null) {
  177. $url = self::URL_INTL_PARCEL_CALCULATE . '?' .
  178. join('&', array(
  179. 'country_code=' . $destCountry,
  180. 'weight=' . $this->getWeight(),
  181. 'service_code=' . $methodName,
  182. ));
  183. }
  184. else {
  185. $url = self::URL_INTL_PARCEL_SERVICE_LIST . '?' .
  186. join('&', array(
  187. 'country_code=' . $destCountry,
  188. 'weight=' . $this->getWeight(),
  189. ));
  190. }
  191. }
  192. $request = $this->_makeRequest($url);
  193. if (isset($request['error']['errorMessage'])) {
  194. $this->setError($request['error']['errorMessage']);
  195. return false;
  196. }
  197. if (isset($request['services']['service'])) {
  198. foreach ($request['services']['service'] as $row) {
  199. $result[] = array(
  200. 'code' => $row['code'],
  201. 'name' => $row['name'],
  202. 'price' => $row['price'],
  203. 'currency' => self::CURRENCY,
  204. );
  205. }
  206. }
  207. else if (isset($request['postage_result']['total_cost'])) {
  208. $result = doubleval($request['postage_result']['total_cost']);
  209. }
  210. else {
  211. $this->setError('No rate(s) available for the requested input.');
  212. }
  213. return $result;
  214. }
  215. /**
  216. *
  217. * make request
  218. *
  219. * @param string $url
  220. *
  221. * @return mixed
  222. */
  223. private function _makeRequest($url)
  224. {
  225. $ch = curl_init();
  226. curl_setopt($ch, CURLOPT_URL, $this->_server . $url);
  227. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  228. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  229. 'Auth-Key: ' . $this->_apiKey,
  230. ));
  231. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  232. $contents = curl_exec($ch);
  233. curl_close($ch);
  234. return json_decode($contents, true);
  235. }
  236. }