PedroTeixeira_Correios - Version 4.7.1

Version Notes

Bugfix:

- Atualização do WebService de Rastreamento dos Correios

Download this release

Release Info

Developer Pedro Teixeira
Extension PedroTeixeira_Correios
Version 4.7.1
Comparing to
See all releases


Code changes from version 4.7.0 to 4.7.1

app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php CHANGED
@@ -583,94 +583,118 @@ class PedroTeixeira_Correios_Model_Carrier_CorreiosMethod
583
  }
584
 
585
  /**
586
- * Protected Get Tracking, opens the request to Correios
587
- *
588
  * @param string $code Code
589
- *
590
- * @return bool
 
 
591
  */
592
- protected function _getTracking($code)
593
  {
594
- $error = Mage::getModel('shipping/tracking_result_error');
595
- $error->setTracking($code);
596
- $error->setCarrier($this->_code);
597
- $error->setCarrierTitle($this->getConfigData('title'));
598
- $error->setErrorMessage($this->getConfigData('urlerror'));
 
 
 
 
599
 
600
- $url = 'http://websro.correios.com.br/sro_bin/txect01$.QueryList';
601
- $url .= '?P_LINGUA=001&P_TIPO=001&P_COD_UNI=' . $code;
602
  try {
603
- $client = new Zend_Http_Client();
604
- $client->setUri($url);
605
- $content = $client->request();
606
- $body = $content->getBody();
 
607
  } catch (Exception $e) {
608
- $this->_result->append($error);
609
- return false;
610
  }
 
 
611
 
612
- if (!preg_match('#<table ([^>]+)>(.*?)</table>#is', $body, $matches)) {
613
- $this->_result->append($error);
614
- return false;
615
- }
616
- $table = $matches[2];
617
-
618
- if (!preg_match_all('/<tr>(.*)<\/tr>/i', $table, $columns, PREG_SET_ORDER)) {
619
- $this->_result->append($error);
620
- return false;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
621
  }
 
 
622
 
 
 
 
 
 
 
 
 
 
 
623
  $progress = array();
624
- for ($i = 0; $i < count($columns); $i++) {
625
- $column = $columns[$i][1];
626
-
627
- $description = '';
628
- $found = false;
629
- if (preg_match('/<td rowspan="?2"?/i', $column)
630
- && preg_match(
631
- '/<td rowspan="?2"?>(.*)<\/td><td>(.*)<\/td><td><font color="[A-Z0-9]{6}">(.*)<\/font><\/td>/i',
632
- $column,
633
- $matches
634
- )
635
- ) {
636
- if (preg_match('/<td colspan="?2"?>(.*)<\/td>/i', $columns[$i + 1][1], $matchesDescription)) {
637
- $description = str_replace(' ', '', $matchesDescription[1]);
638
- }
639
 
640
- $found = true;
641
- } elseif (preg_match(
642
- '/<td rowspan="?1"?>(.*)<\/td><td>(.*)<\/td><td><font color="[A-Z0-9]{6}">(.*)<\/font><\/td>/i',
643
- $column,
644
- $matches
645
- )
646
- ) {
647
- $found = true;
 
648
  }
 
649
 
650
- if ($found) {
651
- $datetime = explode(' ', $matches[1]);
652
- $locale = new Zend_Locale('pt_BR');
653
- $date = '';
654
- $date = new Zend_Date($datetime[0], 'dd/MM/YYYY', $locale);
655
-
656
- $track = array(
657
- 'deliverydate' => $date->toString('YYYY-MM-dd'),
658
- 'deliverytime' => $datetime[1] . ':00',
659
- 'deliverylocation' => htmlentities($matches[2], ENT_IGNORE, 'ISO-8859-1'),
660
- 'status' => htmlentities($matches[3], ENT_IGNORE, 'ISO-8859-1'),
661
- 'activity' => htmlentities($matches[3], ENT_IGNORE, 'ISO-8859-1')
662
- );
663
-
664
- if ($description !== '') {
665
- $track['activity'] = $matches[3] . ' - ' . htmlentities($description, ENT_IGNORE, 'ISO-8859-1');
666
- }
667
 
668
- $progress[] = $track;
669
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
670
  }
671
 
 
672
  if (!empty($progress)) {
673
- $track = $progress[0];
674
  $track['progressdetail'] = $progress;
675
 
676
  $tracking = Mage::getModel('shipping/tracking_result_status');
583
  }
584
 
585
  /**
586
+ * Loads the parameters and calls the webservice using SOAP
587
+ *
588
  * @param string $code Code
589
+ *
590
+ * @return bool|array
591
+ *
592
+ * @throws Exception
593
  */
594
+ protected function _getTrackingRequest($code)
595
  {
596
+ $response = false;
597
+ $params = array(
598
+ 'usuario' => $this->getConfigData('sro_username'),
599
+ 'senha' => $this->getConfigData('sro_password'),
600
+ 'tipo' => $this->getConfigData('sro_type'),
601
+ 'resultado' => 'T',
602
+ 'lingua' => $this->getConfigData('sro_language'),
603
+ 'objetos' => $code,
604
+ );
605
 
 
 
606
  try {
607
+ $client = new SoapClient($this->getConfigData('url_sro_correios'));
608
+ $response = $client->buscaEventos($params);
609
+ if (empty($response)) {
610
+ throw new Exception("Empty response");
611
+ }
612
  } catch (Exception $e) {
613
+ Mage::log("Soap Error: {$e->getMessage()}");
 
614
  }
615
+ return $response;
616
+ }
617
 
618
+ /**
619
+ * Loads tracking progress details
620
+ *
621
+ * @param SimpleXMLElement $evento XML Element Node
622
+ * @param bool $isDelivered Delivery Flag
623
+ *
624
+ * @return array
625
+ */
626
+ protected function _getTrackingProgressDetails(SimpleXMLElement $evento, $isDelivered=false)
627
+ {
628
+ $date = new Zend_Date($evento->data, 'dd/MM/YYYY', new Zend_Locale('pt_BR'));
629
+ $track = array(
630
+ 'deliverydate' => $date->toString('YYYY-MM-dd'),
631
+ 'deliverytime' => $evento->hora . ':00',
632
+ 'status' => $evento->descricao,
633
+ );
634
+ if (!$isDelivered) {
635
+ $msg = array($evento->descricao);
636
+ if (isset($evento->destino) && isset($evento->destino->local)) {
637
+ $msg = array("{$evento->descricao} para {$evento->destino->local}");
638
+ }
639
+ $track['activity'] = implode(' | ', $msg);
640
+ $track['deliverylocation'] = "{$evento->local} - {$evento->cidade}/{$evento->uf}";
641
  }
642
+ return $track;
643
+ }
644
 
645
+ /**
646
+ * Loads progress data using the WSDL response
647
+ *
648
+ * @param string $request Request response
649
+ *
650
+ * @return array
651
+ */
652
+ protected function _getTrackingProgress($request)
653
+ {
654
+ $track = array();
655
  $progress = array();
656
+ $eventTypes = explode(',', $this->getConfigData("sro_event_type_last"));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
657
 
658
+ if (count($request->return->objeto->evento) == 1) {
659
+ $progress[] = $this->_getTrackingProgressDetails($request->return->objeto->evento);
660
+ } else {
661
+ foreach ($request->return->objeto->evento as $evento) {
662
+ $progress[] = $this->_getTrackingProgressDetails($evento);
663
+ $isDelivered = ((int) $evento->status < 2 && in_array($evento->tipo, $eventTypes));
664
+ if ($isDelivered) {
665
+ $track = $this->_getTrackingProgressDetails($evento, $isDelivered);
666
+ }
667
  }
668
+ }
669
 
670
+ $progress[] = $track;
671
+ return $progress;
672
+ }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
673
 
674
+ /**
675
+ * Protected Get Tracking, opens the request to Correios
676
+ *
677
+ * @param string $code Code
678
+ *
679
+ * @return bool
680
+ */
681
+ protected function _getTracking($code)
682
+ {
683
+ $error = Mage::getModel('shipping/tracking_result_error');
684
+ $error->setTracking($code);
685
+ $error->setCarrier($this->_code);
686
+ $error->setCarrierTitle($this->getConfigData('title'));
687
+ $error->setErrorMessage($this->getConfigData('urlerror'));
688
+
689
+ $request = $this->_getTrackingRequest($code);
690
+ if (!isset($request->return)) {
691
+ $this->_result->append($error);
692
+ return false;
693
  }
694
 
695
+ $progress = $this->_getTrackingProgress($request);
696
  if (!empty($progress)) {
697
+ $track = array_pop($progress);
698
  $track['progressdetail'] = $progress;
699
 
700
  $tracking = Mage::getModel('shipping/tracking_result_status');
app/code/community/PedroTeixeira/Correios/Model/Sro.php CHANGED
@@ -48,38 +48,37 @@ class PedroTeixeira_Correios_Model_Sro extends Varien_Object
48
  /**
49
  * Load XML response from Correios
50
  *
51
- * @param string $number Tracking Code
52
  *
53
- * @throws Zend_Http_Client_Adapter_Exception
54
  *
55
  * @link http://www.correios.com.br/para-voce/correios-de-a-a-z/pdf/rastreamento-de-objetos/
56
  * Manual_SROXML_28fev14.pdf
 
 
57
  *
58
- * @return SimpleXMLElement
59
  */
60
- public function request($number)
61
  {
62
- $client = new Zend_Http_Client($this->getConfigData("url_sro_correios"));
63
- $client->setParameterPost('Usuario', $this->getConfigData('sro_username'));
64
- $client->setParameterPost('Senha', $this->getConfigData('sro_password'));
65
- $client->setParameterPost('Tipo', $this->getConfigData('sro_type'));
66
- $client->setParameterPost('Resultado', $this->getConfigData('sro_result'));
67
- $client->setParameterPost('Objetos', $number);
 
 
 
68
  try {
69
- $response = $client->request(Zend_Http_Client::POST)->getBody();
 
70
  if (empty($response)) {
71
- throw new Zend_Http_Client_Adapter_Exception("Empty response");
72
- }
73
- libxml_use_internal_errors(true);
74
- $this->_xml = simplexml_load_string($response);
75
- if (!$this->_xml || !isset($this->_xml->objeto)) {
76
- throw new Zend_Http_Client_Adapter_Exception("Invalid XML");
77
  }
78
- } catch (Zend_Http_Exception $e) {
79
- Mage::log("{$e->getMessage()}");
80
- Mage::log("TRACKING: {$number}");
81
- Mage::log("RESPONSE: {$response}");
82
- return false;
83
  }
84
  return $this;
85
  }
48
  /**
49
  * Load XML response from Correios
50
  *
51
+ * @param string $trackingCode Tracking Code
52
  *
53
+ * @throws Exception
54
  *
55
  * @link http://www.correios.com.br/para-voce/correios-de-a-a-z/pdf/rastreamento-de-objetos/
56
  * Manual_SROXML_28fev14.pdf
57
+ * @link http://www.corporativo.correios.com.br/encomendas/sigepweb/doc/
58
+ * Manual_de_Implementacao_do_Web_Service_SIGEPWEB_Logistica_Reversa.pdf
59
  *
60
+ * @return boolean|PedroTeixeira_Correios_Model_Sro
61
  */
62
+ public function request($trackingCode)
63
  {
64
+ $params = array(
65
+ 'usuario' => $this->getConfigData('sro_username'),
66
+ 'senha' => $this->getConfigData('sro_password'),
67
+ 'tipo' => $this->getConfigData('sro_type'),
68
+ 'resultado' => $this->getConfigData('sro_result'),
69
+ 'lingua' => $this->getConfigData('sro_language'),
70
+ 'objetos' => $trackingCode,
71
+ );
72
+
73
  try {
74
+ $client = new SoapClient($this->getConfigData('url_sro_correios'));
75
+ $response = $client->buscaEventos($params);
76
  if (empty($response)) {
77
+ throw new Exception("Empty response");
 
 
 
 
 
78
  }
79
+ $this->_xml = $response->return;
80
+ } catch (Exception $e) {
81
+ Mage::log("Soap Error: {$e->getMessage()}");
 
 
82
  }
83
  return $this;
84
  }
app/code/community/PedroTeixeira/Correios/etc/config.xml CHANGED
@@ -421,7 +421,8 @@
421
  <sro_password>SRO</sro_password>
422
  <sro_type>L</sro_type>
423
  <sro_result>U</sro_result>
424
-
 
425
  <sro_event_type_notify>BDE,BDI,BDR,FC,LDI,OEC</sro_event_type_notify>
426
  <sro_event_status_notify_bde>0,1,2,7,20,24,25,34,45,54,55</sro_event_status_notify_bde>
427
  <sro_event_status_notify_bdi>0,1,2,7,20,24,25,34,45,54,55</sro_event_status_notify_bdi>
@@ -443,8 +444,8 @@
443
  <sro_event_status_last_bde>0,1</sro_event_status_last_bde>
444
  <sro_event_status_last_bdi>0,1</sro_event_status_last_bdi>
445
  <sro_event_status_last_bdr>0,1</sro_event_status_last_bdr>
446
-
447
- <url_sro_correios>http://websro.correios.com.br/sro_bin/sroii_xml.eventos</url_sro_correios>
448
  <url_tracking>http://websro.correios.com.br/sro_bin/txect01$.QueryList</url_tracking>
449
 
450
  <!-- CACHE -->
421
  <sro_password>SRO</sro_password>
422
  <sro_type>L</sro_type>
423
  <sro_result>U</sro_result>
424
+ <sro_language>101</sro_language>
425
+
426
  <sro_event_type_notify>BDE,BDI,BDR,FC,LDI,OEC</sro_event_type_notify>
427
  <sro_event_status_notify_bde>0,1,2,7,20,24,25,34,45,54,55</sro_event_status_notify_bde>
428
  <sro_event_status_notify_bdi>0,1,2,7,20,24,25,34,45,54,55</sro_event_status_notify_bdi>
444
  <sro_event_status_last_bde>0,1</sro_event_status_last_bde>
445
  <sro_event_status_last_bdi>0,1</sro_event_status_last_bdi>
446
  <sro_event_status_last_bdr>0,1</sro_event_status_last_bdr>
447
+
448
+ <url_sro_correios>http://webservice.correios.com.br/service/rastro/Rastro.wsdl</url_sro_correios>
449
  <url_tracking>http://websro.correios.com.br/sro_bin/txect01$.QueryList</url_tracking>
450
 
451
  <!-- CACHE -->
package.xml CHANGED
@@ -1,7 +1,7 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>PedroTeixeira_Correios</name>
4
- <version>4.7.0</version>
5
  <stability>stable</stability>
6
  <license uri="http://www.opensource.org/licenses/mit-license.php">MIT License</license>
7
  <channel>community</channel>
@@ -28,26 +28,11 @@ Conta com identifica&#xE7;&#xE3;o de erros dos Correios, m&#xE3;o pr&#xF3;pria,
28
  Mais informa&#xE7;&#xF5;es em https://pedroteixeira.io.</description>
29
  <notes>Bugfix:&#xD;
30
  &#xD;
31
- - Fix para frete com desconto/gr&#xE1;tis em &#xE1;reas com restri&#xE7;&#xE3;o de entrega&#xD;
32
- - Configura&#xE7;&#xE3;o de ajuste&#xD;
33
- - Added Validation to null postcode in cart update&#xD;
34
- - Removida a fun&#xE7;&#xE3;o trim em contexto de vari&#xE1;vel&#xD;
35
- - Corre&#xE7;&#xE3;o para Warning: Invalid argument supplied for foreach&#xD;
36
- - Fix noservices error&#xD;
37
- - Corre&#xE7;&#xE3;o para refer&#xEA;ncia de items para c&#xE1;lculo do request&#xD;
38
- &#xD;
39
- Feature:&#xD;
40
- &#xD;
41
- - Adicionada compatibilidade com pacote de produtos&#xD;
42
- - Monitoramento Autom&#xE1;tico da Encomenda, e Atualiza&#xE7;&#xE3;o do Status da Entrega&#xD;
43
- - Adicionado servi&#xE7;o 40436 com contrato&#xD;
44
- - Instalando com Modgit&#xD;
45
- - Evitando load de produto&#xD;
46
- - Aba dos Correios na edi&#xE7;&#xE3;o de produtos</notes>
47
  <authors><author><name>Pedro Teixeira</name><user>teixeirapedro</user><email>hello@pedroteixeira.io</email></author></authors>
48
- <date>2016-05-24</date>
49
- <time>10:14:12</time>
50
- <contents><target name="magecommunity"><dir name="PedroTeixeira"><dir name="Correios"><dir name="Helper"><file name="Data.php" hash="a030f07c60af40e9817054800096207f"/></dir><dir name="Model"><file name="Cache.php" hash="63903ef57e86e2be3cd79787063f3946"/><dir name="Carrier"><file name="CorreiosMethod.php" hash="7886063ac2184904947304d25af66454"/></dir><dir name="Http"><dir name="Client"><dir name="Adapter"><file name="Socket.php" hash="76da85f117c3f9950164a5d90500d82f"/></dir></dir></dir><file name="Observer.php" hash="10d46e3849f531cef13a6ee6dfc6dfa9"/><dir name="Source"><file name="CacheMode.php" hash="b34ae611b8bf913e4b646f4a156cc2a4"/><file name="PostMethods.php" hash="22f500a45f51515409fa9367d0a5e957"/><file name="WeightType.php" hash="4bf1608fb6b665dcc7d834ca35eee174"/></dir><file name="Sro.php" hash="25e2f1675755b57f2a11d47f874fcbc9"/></dir><dir name="etc"><file name="config.xml" hash="94380d19dcf930b359b48a9f128df413"/><file name="system.xml" hash="a176657e9f5565ef20d101059cca833c"/></dir><dir name="sql"><dir name="pedroteixeira_correios_setup"><file name="install-4.0.0.php" hash="eda109c002d9130fc1ce807784aa60a3"/><file name="install-4.4.0.php" hash="889c7376ee7bdd30bf07616b4f365e3b"/><file name="install-4.5.0.php" hash="e0daaac3649b166a33928da3844ad965"/><file name="install-4.6.0.php" hash="5168cdf9b1e60480d5bc90951c69cb20"/><file name="install-4.7.0.php" hash="ad7b3434d1c5fbb0b49212e8bc5923b9"/><file name="upgrade-4.0.0-4.1.0.php" hash="ca42fdabb0975f60d8cf5ef5ab122acd"/><file name="upgrade-4.1.0-4.2.0.php" hash="319cd1c4ddaa745da9af4a5111aa1534"/><file name="upgrade-4.2.0-4.3.0.php" hash="783e854d19b0e4915360cc1ec8b4d2d0"/><file name="upgrade-4.3.0-4.4.0.php" hash="982f41d3a113d21c4008de7e63918e45"/><file name="upgrade-4.4.0-4.5.0.php" hash="95dfd40fa6469a711b4b65a331f0b1b7"/><file name="upgrade-4.5.0-4.6.0.php" hash="24686aec7822436598e94680e3e6a948"/><file name="upgrade-4.6.0-4.7.0.php" hash="d12205fcbe16e21bdf233eae06500998"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="PedroTeixeira_Correios.xml" hash="e714579bed97c85f2bcc3988dec271da"/></dir></target></contents>
51
  <compatible/>
52
  <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php><package><name/><channel>connect.magentocommerce.com/core</channel><min/><max/></package></required></dependencies>
53
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>PedroTeixeira_Correios</name>
4
+ <version>4.7.1</version>
5
  <stability>stable</stability>
6
  <license uri="http://www.opensource.org/licenses/mit-license.php">MIT License</license>
7
  <channel>community</channel>
28
  Mais informa&#xE7;&#xF5;es em https://pedroteixeira.io.</description>
29
  <notes>Bugfix:&#xD;
30
  &#xD;
31
+ - Atualiza&#xE7;&#xE3;o do WebService de Rastreamento dos Correios</notes>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
  <authors><author><name>Pedro Teixeira</name><user>teixeirapedro</user><email>hello@pedroteixeira.io</email></author></authors>
33
+ <date>2016-06-17</date>
34
+ <time>13:04:20</time>
35
+ <contents><target name="magecommunity"><dir name="PedroTeixeira"><dir name="Correios"><dir name="Helper"><file name="Data.php" hash="a030f07c60af40e9817054800096207f"/></dir><dir name="Model"><file name="Cache.php" hash="63903ef57e86e2be3cd79787063f3946"/><dir name="Carrier"><file name="CorreiosMethod.php" hash="096daa6f10c197dc5a69d721d4e19d2f"/></dir><dir name="Http"><dir name="Client"><dir name="Adapter"><file name="Socket.php" hash="76da85f117c3f9950164a5d90500d82f"/></dir></dir></dir><file name="Observer.php" hash="10d46e3849f531cef13a6ee6dfc6dfa9"/><dir name="Source"><file name="CacheMode.php" hash="b34ae611b8bf913e4b646f4a156cc2a4"/><file name="PostMethods.php" hash="22f500a45f51515409fa9367d0a5e957"/><file name="WeightType.php" hash="4bf1608fb6b665dcc7d834ca35eee174"/></dir><file name="Sro.php" hash="fd8f738371ed437c6f8002d2d47debfa"/></dir><dir name="etc"><file name="config.xml" hash="eb6e65fbc48ae827eb68455e4a0d982a"/><file name="system.xml" hash="a176657e9f5565ef20d101059cca833c"/></dir><dir name="sql"><dir name="pedroteixeira_correios_setup"><file name="install-4.0.0.php" hash="eda109c002d9130fc1ce807784aa60a3"/><file name="install-4.4.0.php" hash="889c7376ee7bdd30bf07616b4f365e3b"/><file name="install-4.5.0.php" hash="e0daaac3649b166a33928da3844ad965"/><file name="install-4.6.0.php" hash="5168cdf9b1e60480d5bc90951c69cb20"/><file name="install-4.7.0.php" hash="ad7b3434d1c5fbb0b49212e8bc5923b9"/><file name="upgrade-4.0.0-4.1.0.php" hash="ca42fdabb0975f60d8cf5ef5ab122acd"/><file name="upgrade-4.1.0-4.2.0.php" hash="319cd1c4ddaa745da9af4a5111aa1534"/><file name="upgrade-4.2.0-4.3.0.php" hash="783e854d19b0e4915360cc1ec8b4d2d0"/><file name="upgrade-4.3.0-4.4.0.php" hash="982f41d3a113d21c4008de7e63918e45"/><file name="upgrade-4.4.0-4.5.0.php" hash="95dfd40fa6469a711b4b65a331f0b1b7"/><file name="upgrade-4.5.0-4.6.0.php" hash="24686aec7822436598e94680e3e6a948"/><file name="upgrade-4.6.0-4.7.0.php" hash="d12205fcbe16e21bdf233eae06500998"/></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="PedroTeixeira_Correios.xml" hash="e714579bed97c85f2bcc3988dec271da"/></dir></target></contents>
36
  <compatible/>
37
  <dependencies><required><php><min>5.2.0</min><max>6.0.0</max></php><package><name/><channel>connect.magentocommerce.com/core</channel><min/><max/></package></required></dependencies>
38
  </package>