public function sefazConsultaChave($chave = '', $tpAmb = '2', &$aRetorno = array(), $salvaMensagens = true)
{
$chNFe = preg_replace('/[^0-9]/', '', $chave);
if (strlen($chNFe) != 44) {
$msg = "Uma chave de 44 dígitos da NFe deve ser passada.";
throw new Exception\InvalidArgumentException($msg);
}
if ($tpAmb == '') {
$tpAmb = $this->aConfig['tpAmb'];
}
$cUF = substr($chNFe, 0, 2);
$siglaUF = $this->zGetSigla($cUF);
//carrega serviço
$servico = 'NfeConsultaProtocolo';
$this->zLoadServico('nfe', $servico, $siglaUF, $tpAmb);
if ($this->urlService == '') {
$msg = "A consulta de NFe não está disponível na SEFAZ {$siglaUF}!!!";
throw new Exception\RuntimeException($msg);
}
$cons = "<consSitNFe xmlns=\"{$this->urlPortal}\" versao=\"{$this->urlVersion}\">" . "<tpAmb>{$tpAmb}</tpAmb>" . "<xServ>CONSULTAR</xServ>" . "<chNFe>{$chNFe}</chNFe>" . "</consSitNFe>";
//validar mensagem com xsd
//if (! $this->validarXml($cons)) {
// $msg = 'Falha na validação. '.$this->error;
// throw new Exception\RuntimeException($msg);
//}
//montagem dos dados da mensagem SOAP
$body = "<nfeDadosMsg xmlns=\"{$this->urlNamespace}\">{$cons}</nfeDadosMsg>";
//envia a solicitação via SOAP
$retorno = $this->oSoap->send($this->urlService, $this->urlNamespace, $this->urlHeader, $body, $this->urlMethod);
$lastMsg = $this->oSoap->lastMsg;
$this->soapDebug = $this->oSoap->soapDebug;
//salva mensagens
if ($salvaMensagens) {
$filename = "{$chNFe}-consSitNFe.xml";
$this->zGravaFile('nfe', $tpAmb, $filename, $lastMsg);
$filename = "{$chNFe}-retConsSitNFe.xml";
$this->zGravaFile('nfe', $tpAmb, $filename, $retorno);
}
//tratar dados de retorno
$aRetorno = ReturnNFe::readReturnSefaz($servico, $retorno);
return (string) $retorno;
}