diff --git a/CHANGELOG.md b/CHANGELOG.md index 371ad47..0a0e5f5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v4.7.1 + +### Bugfix + +- [#139](https://github.com/pedro-teixeira/correios/pull/139) Atualização do WebService de Rastreamento dos Correios + # v4.7.0 ### Bugfix diff --git a/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php b/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php index 9d0cdce..1cd0dca 100644 --- a/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php +++ b/app/code/community/PedroTeixeira/Correios/Model/Carrier/CorreiosMethod.php @@ -583,94 +583,118 @@ public function getTracking($trackings) } /** - * Protected Get Tracking, opens the request to Correios - * + * Loads the parameters and calls the webservice using SOAP + * * @param string $code Code - * - * @return bool + * + * @return bool|array + * + * @throws Exception */ - protected function _getTracking($code) + protected function _getTrackingRequest($code) { - $error = Mage::getModel('shipping/tracking_result_error'); - $error->setTracking($code); - $error->setCarrier($this->_code); - $error->setCarrierTitle($this->getConfigData('title')); - $error->setErrorMessage($this->getConfigData('urlerror')); + $response = false; + $params = array( + 'usuario' => $this->getConfigData('sro_username'), + 'senha' => $this->getConfigData('sro_password'), + 'tipo' => $this->getConfigData('sro_type'), + 'resultado' => 'T', + 'lingua' => $this->getConfigData('sro_language'), + 'objetos' => $code, + ); - $url = 'http://websro.correios.com.br/sro_bin/txect01$.QueryList'; - $url .= '?P_LINGUA=001&P_TIPO=001&P_COD_UNI=' . $code; try { - $client = new Zend_Http_Client(); - $client->setUri($url); - $content = $client->request(); - $body = $content->getBody(); + $client = new SoapClient($this->getConfigData('url_sro_correios')); + $response = $client->buscaEventos($params); + if (empty($response)) { + throw new Exception("Empty response"); + } } catch (Exception $e) { - $this->_result->append($error); - return false; + Mage::log("Soap Error: {$e->getMessage()}"); } + return $response; + } - if (!preg_match('#]+)>(.*?)
#is', $body, $matches)) { - $this->_result->append($error); - return false; - } - $table = $matches[2]; - - if (!preg_match_all('/(.*)<\/tr>/i', $table, $columns, PREG_SET_ORDER)) { - $this->_result->append($error); - return false; + /** + * Loads tracking progress details + * + * @param SimpleXMLElement $evento XML Element Node + * @param bool $isDelivered Delivery Flag + * + * @return array + */ + protected function _getTrackingProgressDetails(SimpleXMLElement $evento, $isDelivered=false) + { + $date = new Zend_Date($evento->data, 'dd/MM/YYYY', new Zend_Locale('pt_BR')); + $track = array( + 'deliverydate' => $date->toString('YYYY-MM-dd'), + 'deliverytime' => $evento->hora . ':00', + 'status' => $evento->descricao, + ); + if (!$isDelivered) { + $msg = array($evento->descricao); + if (isset($evento->destino) && isset($evento->destino->local)) { + $msg = array("{$evento->descricao} para {$evento->destino->local}"); + } + $track['activity'] = implode(' | ', $msg); + $track['deliverylocation'] = "{$evento->local} - {$evento->cidade}/{$evento->uf}"; } + return $track; + } + /** + * Loads progress data using the WSDL response + * + * @param string $request Request response + * + * @return array + */ + protected function _getTrackingProgress($request) + { + $track = array(); $progress = array(); - for ($i = 0; $i < count($columns); $i++) { - $column = $columns[$i][1]; - - $description = ''; - $found = false; - if (preg_match('/(.*)<\/td>(.*)<\/td>(.*)<\/font><\/td>/i', - $column, - $matches - ) - ) { - if (preg_match('/(.*)<\/td>/i', $columns[$i + 1][1], $matchesDescription)) { - $description = str_replace(' ', '', $matchesDescription[1]); - } + $eventTypes = explode(',', $this->getConfigData("sro_event_type_last")); - $found = true; - } elseif (preg_match( - '/(.*)<\/td>(.*)<\/td>(.*)<\/font><\/td>/i', - $column, - $matches - ) - ) { - $found = true; + if (count($request->return->objeto->evento) == 1) { + $progress[] = $this->_getTrackingProgressDetails($request->return->objeto->evento); + } else { + foreach ($request->return->objeto->evento as $evento) { + $progress[] = $this->_getTrackingProgressDetails($evento); + $isDelivered = ((int) $evento->status < 2 && in_array($evento->tipo, $eventTypes)); + if ($isDelivered) { + $track = $this->_getTrackingProgressDetails($evento, $isDelivered); + } } + } - if ($found) { - $datetime = explode(' ', $matches[1]); - $locale = new Zend_Locale('pt_BR'); - $date = ''; - $date = new Zend_Date($datetime[0], 'dd/MM/YYYY', $locale); - - $track = array( - 'deliverydate' => $date->toString('YYYY-MM-dd'), - 'deliverytime' => $datetime[1] . ':00', - 'deliverylocation' => htmlentities($matches[2], ENT_IGNORE, 'ISO-8859-1'), - 'status' => htmlentities($matches[3], ENT_IGNORE, 'ISO-8859-1'), - 'activity' => htmlentities($matches[3], ENT_IGNORE, 'ISO-8859-1') - ); - - if ($description !== '') { - $track['activity'] = $matches[3] . ' - ' . htmlentities($description, ENT_IGNORE, 'ISO-8859-1'); - } + $progress[] = $track; + return $progress; + } - $progress[] = $track; - } + /** + * Protected Get Tracking, opens the request to Correios + * + * @param string $code Code + * + * @return bool + */ + protected function _getTracking($code) + { + $error = Mage::getModel('shipping/tracking_result_error'); + $error->setTracking($code); + $error->setCarrier($this->_code); + $error->setCarrierTitle($this->getConfigData('title')); + $error->setErrorMessage($this->getConfigData('urlerror')); + + $request = $this->_getTrackingRequest($code); + if (!isset($request->return)) { + $this->_result->append($error); + return false; } + $progress = $this->_getTrackingProgress($request); if (!empty($progress)) { - $track = $progress[0]; + $track = array_pop($progress); $track['progressdetail'] = $progress; $tracking = Mage::getModel('shipping/tracking_result_status'); diff --git a/app/code/community/PedroTeixeira/Correios/Model/Sro.php b/app/code/community/PedroTeixeira/Correios/Model/Sro.php index 336b418..cd68a81 100644 --- a/app/code/community/PedroTeixeira/Correios/Model/Sro.php +++ b/app/code/community/PedroTeixeira/Correios/Model/Sro.php @@ -48,38 +48,37 @@ public function getShippedTracks() /** * Load XML response from Correios * - * @param string $number Tracking Code + * @param string $trackingCode Tracking Code * - * @throws Zend_Http_Client_Adapter_Exception + * @throws Exception * * @link http://www.correios.com.br/para-voce/correios-de-a-a-z/pdf/rastreamento-de-objetos/ * Manual_SROXML_28fev14.pdf + * @link http://www.corporativo.correios.com.br/encomendas/sigepweb/doc/ + * Manual_de_Implementacao_do_Web_Service_SIGEPWEB_Logistica_Reversa.pdf * - * @return SimpleXMLElement + * @return boolean|PedroTeixeira_Correios_Model_Sro */ - public function request($number) + public function request($trackingCode) { - $client = new Zend_Http_Client($this->getConfigData("url_sro_correios")); - $client->setParameterPost('Usuario', $this->getConfigData('sro_username')); - $client->setParameterPost('Senha', $this->getConfigData('sro_password')); - $client->setParameterPost('Tipo', $this->getConfigData('sro_type')); - $client->setParameterPost('Resultado', $this->getConfigData('sro_result')); - $client->setParameterPost('Objetos', $number); + $params = array( + 'usuario' => $this->getConfigData('sro_username'), + 'senha' => $this->getConfigData('sro_password'), + 'tipo' => $this->getConfigData('sro_type'), + 'resultado' => $this->getConfigData('sro_result'), + 'lingua' => $this->getConfigData('sro_language'), + 'objetos' => $trackingCode, + ); + try { - $response = $client->request(Zend_Http_Client::POST)->getBody(); + $client = new SoapClient($this->getConfigData('url_sro_correios')); + $response = $client->buscaEventos($params); if (empty($response)) { - throw new Zend_Http_Client_Adapter_Exception("Empty response"); - } - libxml_use_internal_errors(true); - $this->_xml = simplexml_load_string($response); - if (!$this->_xml || !isset($this->_xml->objeto)) { - throw new Zend_Http_Client_Adapter_Exception("Invalid XML"); + throw new Exception("Empty response"); } - } catch (Zend_Http_Exception $e) { - Mage::log("{$e->getMessage()}"); - Mage::log("TRACKING: {$number}"); - Mage::log("RESPONSE: {$response}"); - return false; + $this->_xml = $response->return; + } catch (Exception $e) { + Mage::log("Soap Error: {$e->getMessage()}"); } return $this; } diff --git a/app/code/community/PedroTeixeira/Correios/etc/config.xml b/app/code/community/PedroTeixeira/Correios/etc/config.xml index c977cbc..52e86f7 100644 --- a/app/code/community/PedroTeixeira/Correios/etc/config.xml +++ b/app/code/community/PedroTeixeira/Correios/etc/config.xml @@ -421,7 +421,8 @@ SRO L U - + 101 + BDE,BDI,BDR,FC,LDI,OEC 0,1,2,7,20,24,25,34,45,54,55 0,1,2,7,20,24,25,34,45,54,55 @@ -443,8 +444,8 @@ 0,1 0,1 0,1 - - http://websro.correios.com.br/sro_bin/sroii_xml.eventos + + http://webservice.correios.com.br/service/rastro/Rastro.wsdl http://websro.correios.com.br/sro_bin/txect01$.QueryList