WC_Correios_Webservice

Correios Webservice integration class.

Defined (1)

The class is defined in the following location(s).

/includes/class-wc-correios-webservice.php  
  1. class WC_Correios_Webservice { 
  2.  
  3. /** 
  4. * Webservice URL. 
  5. * @var string 
  6. */ 
  7. private $_webservice = 'http://ws.correios.com.br/calculador/CalcPrecoPrazo.aspx?'; 
  8.  
  9. /** 
  10. * Shipping method ID. 
  11. * @var string 
  12. */ 
  13. protected $id = ''; 
  14.  
  15. /** 
  16. * Shipping zone instance ID. 
  17. * @var int 
  18. */ 
  19. protected $instance_id = 0; 
  20.  
  21. /** 
  22. * ID from Correios service. 
  23. * @var string|array 
  24. */ 
  25. protected $service = ''; 
  26.  
  27. /** 
  28. * WooCommerce package containing the products. 
  29. * @var array 
  30. */ 
  31. protected $package = null; 
  32.  
  33. /** 
  34. * Origin postcode. 
  35. * @var string 
  36. */ 
  37. protected $origin_postcode = ''; 
  38.  
  39. /** 
  40. * Destination postcode. 
  41. * @var string 
  42. */ 
  43. protected $destination_postcode = ''; 
  44.  
  45. /** 
  46. * Login. 
  47. * @var string 
  48. */ 
  49. protected $login = ''; 
  50.  
  51. /** 
  52. * Password. 
  53. * @var string 
  54. */ 
  55. protected $password = ''; 
  56.  
  57. /** 
  58. * Package height. 
  59. * @var float 
  60. */ 
  61. protected $height = 0; 
  62.  
  63. /** 
  64. * Package width. 
  65. * @var float 
  66. */ 
  67. protected $width = 0; 
  68.  
  69. /** 
  70. * Package diameter. 
  71. * @var float 
  72. */ 
  73. protected $diameter = 0; 
  74.  
  75. /** 
  76. * Package length. 
  77. * @var float 
  78. */ 
  79. protected $length = 0; 
  80.  
  81. /** 
  82. * Package weight. 
  83. * @var float 
  84. */ 
  85. protected $weight = 0; 
  86.  
  87. /** 
  88. * Minimum height. 
  89. * @var float 
  90. */ 
  91. protected $minimum_height = 2; 
  92.  
  93. /** 
  94. * Minimum width. 
  95. * @var float 
  96. */ 
  97. protected $minimum_width = 11; 
  98.  
  99. /** 
  100. * Minimum length. 
  101. * @var float 
  102. */ 
  103. protected $minimum_length = 16; 
  104.  
  105. /** 
  106. * Declared value. 
  107. * @var string 
  108. */ 
  109. protected $declared_value = '0'; 
  110.  
  111. /** 
  112. * Own hands. 
  113. * @var string 
  114. */ 
  115. protected $own_hands = 'N'; 
  116.  
  117. /** 
  118. * Receipt notice. 
  119. * @var string 
  120. */ 
  121. protected $receipt_notice = 'N'; 
  122.  
  123. /** 
  124. * Package format. 
  125. * 1 * box/package 
  126. * 2 * roll/prism 
  127. * 3 - envelope 
  128. * @var string 
  129. */ 
  130. protected $format = '1'; 
  131.  
  132. /** 
  133. * Debug mode. 
  134. * @var string 
  135. */ 
  136. protected $debug = 'no'; 
  137.  
  138. /** 
  139. * Logger. 
  140. * @var WC_Logger 
  141. */ 
  142. protected $log = null; 
  143.  
  144. /** 
  145. * Initialize webservice. 
  146. * @param string $id Method ID. 
  147. * @param int $instance_id Instance ID. 
  148. */ 
  149. public function __construct( $id = 'correios', $instance_id = 0 ) { 
  150. $this->id = $id; 
  151. $this->instance_id = $instance_id; 
  152. $this->log = new WC_Logger(); 
  153.  
  154. /** 
  155. * Set the service 
  156. * @param string|array $service Service. 
  157. */ 
  158. public function set_service( $service = '' ) { 
  159. if ( is_array( $service ) ) { 
  160. $this->service = implode( ', ', $service ); 
  161. } else { 
  162. $this->service = $service; 
  163.  
  164. /** 
  165. * Set shipping package. 
  166. * @param array $package Shipping package. 
  167. */ 
  168. public function set_package( $package = array() ) { 
  169. $this->package = $package; 
  170. $correios_package = new WC_Correios_Package( $package ); 
  171.  
  172. if ( ! is_null( $correios_package ) ) { 
  173. $data = $correios_package->get_data(); 
  174.  
  175. $this->set_height( $data['height'] ); 
  176. $this->set_width( $data['width'] ); 
  177. $this->set_length( $data['length'] ); 
  178. $this->set_weight( $data['weight'] ); 
  179.  
  180. if ( 'yes' === $this->debug ) { 
  181. if ( ! empty( $data ) ) { 
  182. $data = array( 
  183. 'weight' => $this->get_weight(),  
  184. 'height' => $this->get_height(),  
  185. 'width' => $this->get_width(),  
  186. 'length' => $this->get_length(),  
  187. ); 
  188.  
  189. $this->log->add( $this->id, 'Weight and cubage of the order: ' . print_r( $data, true ) ); 
  190.  
  191. /** 
  192. * Set origin postcode. 
  193. * @param string $postcode Origin postcode. 
  194. */ 
  195. public function set_origin_postcode( $postcode = '' ) { 
  196. $this->origin_postcode = $postcode; 
  197.  
  198. /** 
  199. * Set destination postcode. 
  200. * @param string $postcode Destination postcode. 
  201. */ 
  202. public function set_destination_postcode( $postcode = '' ) { 
  203. $this->destination_postcode = $postcode; 
  204.  
  205. /** 
  206. * Set login. 
  207. * @param string $login User login. 
  208. */ 
  209. public function set_login( $login = '' ) { 
  210. $this->login = $login; 
  211.  
  212. /** 
  213. * Set password. 
  214. * @param string $password User login. 
  215. */ 
  216. public function set_password( $password = '' ) { 
  217. $this->password = $password; 
  218.  
  219. /** 
  220. * Set shipping package height. 
  221. * @param float $height Package height. 
  222. */ 
  223. public function set_height( $height = 0 ) { 
  224. $this->height = (float) $height; 
  225.  
  226. /** 
  227. * Set shipping package width. 
  228. * @param float $width Package width. 
  229. */ 
  230. public function set_width( $width = 0 ) { 
  231. $this->width = (float) $width; 
  232.  
  233. /** 
  234. * Set shipping package diameter. 
  235. * @param float $diameter Package diameter. 
  236. */ 
  237. public function set_diameter( $diameter = 0 ) { 
  238. $this->diameter = (float) $diameter; 
  239.  
  240. /** 
  241. * Set shipping package length. 
  242. * @param float $length Package length. 
  243. */ 
  244. public function set_length( $length = 0 ) { 
  245. $this->length = (float) $length; 
  246.  
  247. /** 
  248. * Set shipping package weight. 
  249. * @param float $weight Package weight. 
  250. */ 
  251. public function set_weight( $weight = 0 ) { 
  252. $this->weight = (float) $weight; 
  253.  
  254. /** 
  255. * Set minimum height. 
  256. * @param float $minimum_height Package minimum height. 
  257. */ 
  258. public function set_minimum_height( $minimum_height = 2 ) { 
  259. $this->minimum_height = 2 <= $minimum_height ? $minimum_height : 2; 
  260.  
  261. /** 
  262. * Set minimum width. 
  263. * @param float $minimum_width Package minimum width. 
  264. */ 
  265. public function set_minimum_width( $minimum_width = 11 ) { 
  266. $this->minimum_width = 11 <= $minimum_width ? $minimum_width : 11; 
  267.  
  268. /** 
  269. * Set minimum length. 
  270. * @param float $minimum_length Package minimum length. 
  271. */ 
  272. public function set_minimum_length( $minimum_length = 16 ) { 
  273. $this->minimum_length = 16 <= $minimum_length ? $minimum_length : 16; 
  274.  
  275. /** 
  276. * Set declared value. 
  277. * @param string $declared_value Declared value. 
  278. */ 
  279. public function set_declared_value( $declared_value = '0' ) { 
  280. $this->declared_value = $declared_value; 
  281.  
  282. /** 
  283. * Set own hands. 
  284. * @param string $own_hands Use 'N' for no and 'S' for yes. 
  285. */ 
  286. public function set_own_hands( $own_hands = 'N' ) { 
  287. $this->own_hands = $own_hands; 
  288.  
  289. /** 
  290. * Set receipt notice. 
  291. * @param string $receipt_notice Use 'N' for no and 'S' for yes. 
  292. */ 
  293. public function set_receipt_notice( $receipt_notice = 'N' ) { 
  294. $this->receipt_notice = $receipt_notice; 
  295.  
  296. /** 
  297. * Set shipping package format. 
  298. * @param string $format Package format. 
  299. */ 
  300. public function set_format( $format = '1' ) { 
  301. $this->format = $format; 
  302.  
  303. /** 
  304. * Set the debug mode. 
  305. * @param string $debug Yes or no. 
  306. */ 
  307. public function set_debug( $debug = 'no' ) { 
  308. $this->debug = $debug; 
  309.  
  310. /** 
  311. * Get webservice URL. 
  312. * @return string 
  313. */ 
  314. public function get_webservice_url() { 
  315. return apply_filters( 'woocommerce_correios_webservice_url', $this->_webservice, $this->id, $this->instance_id, $this->package ); 
  316.  
  317. /** 
  318. * Get origin postcode. 
  319. * @return string 
  320. */ 
  321. public function get_origin_postcode() { 
  322. return apply_filters( 'woocommerce_correios_origin_postcode', $this->origin_postcode, $this->id, $this->instance_id, $this->package ); 
  323.  
  324. /** 
  325. * Get login. 
  326. * @return string 
  327. */ 
  328. public function get_login() { 
  329. return apply_filters( 'woocommerce_correios_login', $this->login, $this->id, $this->instance_id, $this->package ); 
  330. /** 
  331. * Get password. 
  332. * @return string 
  333. */ 
  334. public function get_password() { 
  335. return apply_filters( 'woocommerce_correios_password', $this->password, $this->id, $this->instance_id, $this->package ); 
  336.  
  337. /** 
  338. * Get height. 
  339. * @return float 
  340. */ 
  341. public function get_height() { 
  342. return $this->float_to_string( $this->minimum_height <= $this->height ? $this->height : $this->minimum_height ); 
  343.  
  344. /** 
  345. * Get width. 
  346. * @return float 
  347. */ 
  348. public function get_width() { 
  349. return $this->float_to_string( $this->minimum_width <= $this->width ? $this->width : $this->minimum_width ); 
  350.  
  351. /** 
  352. * Get diameter. 
  353. * @return float 
  354. */ 
  355. public function get_diameter() { 
  356. return $this->float_to_string( $this->diameter ); 
  357.  
  358. /** 
  359. * Get length. 
  360. * @return float 
  361. */ 
  362. public function get_length() { 
  363. return $this->float_to_string( $this->minimum_length <= $this->length ? $this->length : $this->minimum_length ); 
  364.  
  365. /** 
  366. * Get weight. 
  367. * @return float 
  368. */ 
  369. public function get_weight() { 
  370. return $this->float_to_string( $this->weight ); 
  371.  
  372. /** 
  373. * Fix number format for XML. 
  374. * @param float $value Value with dot. 
  375. * @return string Value with comma. 
  376. */ 
  377. protected function float_to_string( $value ) { 
  378. $value = str_replace( '.', ', ', $value ); 
  379.  
  380. return $value; 
  381.  
  382. /** 
  383. * Check if is available. 
  384. * @return bool 
  385. */ 
  386. protected function is_available() { 
  387. $origin_postcode = $this->get_origin_postcode(); 
  388.  
  389. return ! empty( $this->service ) || ! empty( $this->destination_postcode ) || ! empty( $origin_postcode ) || 0 === $this->get_height(); 
  390.  
  391. /** 
  392. * Get shipping prices. 
  393. * @return SimpleXMLElement|array 
  394. */ 
  395. public function get_shipping() { 
  396. $shipping = null; 
  397.  
  398. // Checks if service and postcode are empty. 
  399. if ( ! $this->is_available() ) { 
  400. return $shipping; 
  401.  
  402. $args = apply_filters( 'woocommerce_correios_shipping_args', array( 
  403. 'nCdServico' => $this->service,  
  404. 'nCdEmpresa' => $this->get_login(),  
  405. 'sDsSenha' => $this->get_password(),  
  406. 'sCepDestino' => wc_correios_sanitize_postcode( $this->destination_postcode ),  
  407. 'sCepOrigem' => wc_correios_sanitize_postcode( $this->get_origin_postcode() ),  
  408. 'nVlAltura' => $this->get_height(),  
  409. 'nVlLargura' => $this->get_width(),  
  410. 'nVlDiametro' => $this->get_diameter(),  
  411. 'nVlComprimento' => $this->get_length(),  
  412. 'nVlPeso' => $this->get_weight(),  
  413. 'nCdFormato' => $this->format,  
  414. 'sCdMaoPropria' => $this->own_hands,  
  415. 'nVlValorDeclarado' => round( number_format( $this->declared_value, 2, '.', '' ) ),  
  416. 'sCdAvisoRecebimento' => $this->receipt_notice,  
  417. 'StrRetorno' => 'xml',  
  418. ), $this->id, $this->instance_id, $this->package ); 
  419.  
  420. $url = add_query_arg( $args, $this->get_webservice_url() ); 
  421.  
  422. if ( 'yes' === $this->debug ) { 
  423. $this->log->add( $this->id, 'Requesting Correios WebServices: ' . $url ); 
  424.  
  425. // Gets the WebServices response. 
  426. $response = wp_safe_remote_get( esc_url_raw( $url ), array( 'timeout' => 30 ) ); 
  427.  
  428. if ( is_wp_error( $response ) ) { 
  429. if ( 'yes' === $this->debug ) { 
  430. $this->log->add( $this->id, 'WP_Error: ' . $response->get_error_message() ); 
  431. } elseif ( $response['response']['code'] >= 200 && $response['response']['code'] < 300 ) { 
  432. try { 
  433. $result = wc_correios_safe_load_xml( $response['body'], LIBXML_NOCDATA ); 
  434. } catch ( Exception $e ) { 
  435. if ( 'yes' === $this->debug ) { 
  436. $this->log->add( $this->id, 'Correios WebServices invalid XML: ' . $e->getMessage() ); 
  437.  
  438. if ( isset( $result->cServico ) ) { 
  439. if ( 'yes' === $this->debug ) { 
  440. $this->log->add( $this->id, 'Correios WebServices response: ' . print_r( $result, true ) ); 
  441.  
  442. $shipping = $result->cServico; 
  443. } else { 
  444. if ( 'yes' === $this->debug ) { 
  445. $this->log->add( $this->id, 'Error accessing the Correios WebServices: ' . print_r( $response, true ) ); 
  446.  
  447. return $shipping;