diff --git a/README.md b/README.md index 20a7824..654192b 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,7 @@ Lib to communicate with SOAP web services and generate NFS-e (Nota Fiscal de Ser - Nota Carioca - GerarNfse - ConsultarNfsePorRps + - ConsultarNfse # Install @@ -20,6 +21,8 @@ Check examples/ folder You must have a valid certificate to use Nota Carioca staging environment. +### GerarNfse + ```php use NFSePHP\NotaCarioca\GerarNfseNotaCariocaFactory; use NFSePHP\NotaCarioca\SoapHandler; @@ -145,9 +148,51 @@ array (size=1) ``` +### ConsultaNfse + +```php +use NFSePHP\NotaCarioca\ConsultarNfseFactory; +use NFSePHP\NotaCarioca\SoapHandler; + +$rps = [ + 'Prestador' => [ + 'Cnpj' => '10738989000199', + 'InscricaoMunicipal' => '04409477', + ], + 'PeriodoEmissao' => [ + 'DataInicial' => '2020-01-30', + 'DataFinal' => '2020-01-30', + ], + 'Tomador' => [ + 'CpfCnpj' => [ + 'Cpf' => '11911464728', + ], + ], +]; + +$env = 'dev'; // dev or prod +$notaCariocaConsulta = new ConsultarNfseFactory($rps, $env); + +$soapHandler = new SoapHandler(['cert_path' => '/path/to/valid/cert.pfx', 'cert_pass' => 'certpassword']); + +// Send SOAP xml +$response = $soapHandler->send($notaCariocaConsulta); + +if ($soapHandler->isSuccess($response)) { + $nfs = $notaCariocaConsulta->formatSuccessResponse($response); + + var_dump($nfs); +} else { + $errors = $soapHandler->getErrors($response); + + var_dump($errors); +} + +``` + # TODO's -- Add missing operations (CancelarNfse, ConsultaNfse, ConsultarLoteRps, ConsultarSituacaoLoteRps, EnviarLoteRps) +- Add missing operations (CancelarNfse, ConsultarLoteRps, ConsultarSituacaoLoteRps, EnviarLoteRps) - Add tests Inspired by https://github.com/nfephp-org diff --git a/examples/NotaCarioca/ConsultarNfse.php b/examples/NotaCarioca/ConsultarNfse.php new file mode 100644 index 0000000..856e009 --- /dev/null +++ b/examples/NotaCarioca/ConsultarNfse.php @@ -0,0 +1,119 @@ + [ + 'Cnpj' => '11111111111111', + 'InscricaoMunicipal' => '11111111', + ], + 'PeriodoEmissao' => [ + 'DataInicial' => '2020-01-30', + 'DataFinal' => '2020-01-30', + ], + 'Tomador' => [ + 'CpfCnpj' => [ + 'Cpf' => '11111111111', + ], + ], +]; + +$env = 'dev'; // dev or prod +$notaCariocaConsulta = new ConsultarNfseFactory($rps, $env); + +$soapHandler = new SoapHandler(['cert_path' => '/path/to/valid/cert.pfx', 'cert_pass' => 'certpassword']); + +// Send SOAP xml +$response = $soapHandler->send($notaCariocaConsulta); + +if ($soapHandler->isSuccess($response)) { + $nfs = $notaCariocaConsulta->formatSuccessResponse($response); + + var_dump($nfs); +} else { + $errors = $soapHandler->getErrors($response); + + var_dump($errors); +} + +/* Response + +array (size=7) + 0 => + array (size=14) + 'Numero' => string '6' (length=1) + 'CodigoVerificacao' => string 'RE4C-ZXKX' (length=9) + 'DataEmissao' => string '2020-01-30T09:15:03' (length=19) + 'IdentificacaoRps' => + array (size=3) + 'Numero' => string '7' (length=1) + 'Serie' => string '1' (length=1) + 'Tipo' => string '1' (length=1) + 'DataEmissaoRps' => string '2020-01-30' (length=10) + 'NaturezaOperacao' => string '1' (length=1) + 'OptanteSimplesNacional' => string '2' (length=1) + 'IncentivadorCultural' => string '2' (length=1) + 'Competencia' => string '2020-01-30T00:00:00' (length=19) + 'Servico' => + array (size=5) + 'Valores' => + array (size=6) + 'ValorServicos' => string '100' (length=3) + 'IssRetido' => string '2' (length=1) + 'ValorIss' => string '5' (length=1) + 'BaseCalculo' => string '100' (length=3) + 'Aliquota' => string '0.05' (length=4) + 'ValorLiquidoNfse' => string '100' (length=3) + 'ItemListaServico' => string '1002' (length=4) + 'CodigoTributacaoMunicipio' => string '100203' (length=6) + 'Discriminacao' => string 'Teste de RPS' (length=12) + 'CodigoMunicipio' => string '111111' (length=7) + 'ValorCredito' => string '0.5' (length=3) + 'PrestadorServico' => + array (size=4) + 'IdentificacaoPrestador' => + array (size=2) + 'Cnpj' => string '111111' (length=14) + 'InscricaoMunicipal' => string '4409477' (length=7) + 'RazaoSocial' => string '111111' (length=48) + 'Endereco' => + array (size=7) + 'Endereco' => string '1111111' (length=39) + 'Numero' => string '111' (length=3) + 'Complemento' => string '11111' (length=15) + 'Bairro' => string '11111' (length=7) + 'CodigoMunicipio' => string '111111' (length=7) + 'Uf' => string 'RJ' (length=2) + 'Cep' => string '11111111' (length=8) + 'Contato' => string '' (length=0) + 'TomadorServico' => + array (size=3) + 'IdentificacaoTomador' => + array (size=1) + 'CpfCnpj' => + array (size=1) + 'Cpf' => string '11111111111' (length=11) + 'RazaoSocial' => string 'Fulano de Tal' (length=13) + 'Endereco' => + array (size=7) + 'Endereco' => string '1111111' (length=29) + 'Numero' => string '111' (length=3) + 'Complemento' => string 'Sobre Loja' (length=10) + 'Bairro' => string 'Centro' (length=6) + 'CodigoMunicipio' => string '3304557' (length=7) + 'Uf' => string 'RJ' (length=2) + 'Cep' => string '11111111' (length=8) + 'OrgaoGerador' => + array (size=2) + 'CodigoMunicipio' => string '11111' (length=7) + 'Uf' => string 'RJ' (length=2) + 1 => + array (size=14) + 'Numero' => string '7' (length=1) + 'CodigoVerificacao' => string 'QKSW-PINP' (length=9) + 'DataEmissao' => string '2020-01-30T09:20:56' (length=19) + ...... +*/ diff --git a/examples/NotaCarioca/ConsultaNfsePorRps.php b/examples/NotaCarioca/ConsultarNfsePorRps.php similarity index 100% rename from examples/NotaCarioca/ConsultaNfsePorRps.php rename to examples/NotaCarioca/ConsultarNfsePorRps.php diff --git a/src/NotaCarioca/ConsultarNfseFactory.php b/src/NotaCarioca/ConsultarNfseFactory.php new file mode 100644 index 0000000..60a732a --- /dev/null +++ b/src/NotaCarioca/ConsultarNfseFactory.php @@ -0,0 +1,103 @@ +getEncoder()->decode($responseXml, ''); + + $responseArr = []; + if (isset($resultArr['ListaNfse']) and isset($resultArr['ListaNfse']['CompNfse'])) { + foreach ($resultArr['ListaNfse']['CompNfse'] as $nfse) { + $responseArr[] = $nfse['Nfse']['InfNfse']; + } + } + + return $responseArr; + } + + /** + * {@inheritdoc} + */ + public function getSchemaStructure(): array + { + return [ + 'ConsultarNfseEnvio' => [ + 'Prestador' => ['Cnpj', 'InscricaoMunicipal'], + 'PeriodoEmissao' => ['DataInicial', 'DataFinal'], + 'Tomador' => [ + 'CpfCnpj' => [ + 'Cpf?', + 'Cnpj?', + ], + ], + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function getEnvelopeXml(): string + { + $structure = $this->getSchemaStructure(); + + $rps = [ + 'ConsultarNfseEnvio' => [ + '@xmlns' => 'http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd', + 'Prestador' => $this->rps['Prestador'], + 'PeriodoEmissao' => $this->rps['PeriodoEmissao'], + 'Tomador' => $this->rps['Tomador'], + ], + ]; + + // Validate array based on structure + try { + $schema = Schema::parse($structure); + $valid = $schema->validate($rps); + } catch (ValidationException $ex) { + throw new \Exception(__FILE__.':'.__LINE__.' - '.$ex->getMessage()); + } + + $xml = $this->getEncoder()->encode($rps, 'xml', ['xml_root_node_name' => 'rootnode', 'remove_empty_tags' => true]); + + // clean up encode tag added by encoder + $xml = str_replace('', '', $xml); + $xml = str_replace('', '', $xml); + $xml = str_replace('', '', $xml); + + // Envelope request + $this->addEnvelope($xml); + + // header('Content-type: text/xml'); + // print_r($xml); + // exit(); + + return $xml; + } +}