Skip to content

Commit

Permalink
Merge pull request #1089 from JulianaApolo/feature/improve-nfce-example
Browse files Browse the repository at this point in the history
Melhora o exemplo da NFCe
  • Loading branch information
robmachado authored Dec 13, 2024
2 parents cbf448b + 6177017 commit 168f3fe
Showing 1 changed file with 29 additions and 11 deletions.
40 changes: 29 additions & 11 deletions examples/5.0testNFce.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
use NFePHP\NFe\Make;
use NFePHP\Common\Certificate;
use NFePHP\Common\Soap\SoapFake;
use NFePHP\NFe\Common\Standardize;
use NFePHP\NFe\Complements;

$arr = [
"atualizacao" => "2017-02-20 09:11:21",
"tpAmb" => 2,
"tpAmb" => 2, //Homologação
"razaosocial" => "SUA RAZAO SOCIAL LTDA",
"cnpj" => "99999999999999",
"siglaUF" => "SP",
Expand All @@ -37,7 +39,6 @@
$tools->model('65');

try {

$make = new Make();


Expand All @@ -63,7 +64,7 @@
$std->tpImp = 1;
$std->tpEmis = 1;
$std->cDV = 2;
$std->tpAmb = 2;
$std->tpAmb = 2; //Homologação
$std->finNFe = 1;
$std->indFinal = 1;
$std->indPres = 1;
Expand Down Expand Up @@ -163,7 +164,7 @@
$tag->infAdProd = 'DE POLIESTER 100%';
$make->taginfAdProd($tag);

//Imposto
//Imposto
$std = new stdClass();
$std->item = 1; //item da NFe
$std->vTotTrib = 25.00;
Expand Down Expand Up @@ -286,16 +287,33 @@

$make->monta();
$xml = $make->getXML();


//Assina
$xml = $tools->signNFe($xml);

//Envia para a Sefaz
$idLote = str_pad(1, 15, '0', STR_PAD_LEFT); // Identificador do lote
$response = $tools->sefazEnviaLote([$xml], $idLote, 1); //1 = envio síncrono

$stdCl = new Standardize($response);
$respObj = $stdCl->toStd();

header('Content-Type: application/xml; charset=utf-8');
echo $xml;

} catch (\Exception $e) {
echo $e->getMessage();
}
if ($respObj->cStat != 104) {
throw new \Exception(sprintf('Lote não enviado (%s - %s)', $respObj->cStat, $respObj->xMotivo));
}

if ($respObj->protNFe->infProt->cStat != 100) {
throw new \Exception(sprintf('Nfce não autorizada (%s - %s)', $respObj->protNFe->infProt->cStat, $respObj->protNFe->infProt->xMotivo));
}

//Salva o protocolo de autorização no xml
$authorizedXml = Complements::toAuthorize($xml, $response);

//Gera o arquivo xml e salva
file_put_contents(__DIR__. '/nfce_protocolado.xml', $authorizedXml);

header('Content-Type: application/xml; charset=utf-8');
echo $authorizedXml;
} catch (\Exception $e) {
echo $e->getMessage();
}

0 comments on commit 168f3fe

Please sign in to comment.