NFePHP\NFe\ToolsNFe::sefazConsultaChave PHP Метод

sefazConsultaChave() публичный Метод

sefazConsultaChave Consulta o status da NFe pela chave de 44 digitos
public sefazConsultaChave ( string $chave = '', string $tpAmb = '2', array &$aRetorno = [], $salvaMensagens = true ) : string
$chave string
$tpAmb string
$aRetorno array
Результат string
    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;
    }

Usage Example

Пример #1
0
<?php

error_reporting(E_ALL);
ini_set('display_errors', 'On');
require_once '../../bootstrap.php';
use NFePHP\NFe\ToolsNFe;
$nfe = new ToolsNFe('../../config/config.json');
$nfe->setModelo('55');
$chave = '52160500067985000172550010000000101000000100';
$tpAmb = '2';
$aResposta = array();
$xml = $nfe->sefazConsultaChave($chave, $tpAmb, $aResposta);
echo '<br><br><pre>';
echo htmlspecialchars($nfe->soapDebug);
echo '</pre><br><pre>';
print_r($aResposta);
echo "<pre><br>";