-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b2c4351
commit 2a9cb74
Showing
3 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
require __DIR__.'/../../vendor/autoload.php'; | ||
|
||
use NFSePHP\NotaCarioca\CancelarNfseFactory; | ||
use NFSePHP\NotaCarioca\SoapHandler; | ||
|
||
$nfse = [ | ||
'IdentificacaoNfse' => [ | ||
'Numero' => '9', | ||
'Cnpj' => '1111111111111', | ||
'InscricaoMunicipal' => '11111111', | ||
'CodigoMunicipio' => '111111', | ||
], | ||
'CodigoCancelamento' => '1', | ||
// 1 Erro na emissão | ||
// 2 Serviço não prestado | ||
// 3 Duplicidade da nota | ||
// 9 Outros | ||
]; | ||
|
||
$env = 'dev'; // dev or prod | ||
$notaCariocaCancel = new CancelarNfseFactory($nfse, $env); | ||
|
||
$soapHandler = new SoapHandler(['cert_path' => '/path/to/valid/cert.pfx', 'cert_pass' => 'certpassword']); | ||
|
||
// Send SOAP xml | ||
$response = $soapHandler->send($notaCariocaCancel); | ||
|
||
if ($soapHandler->isSuccess($response)) { | ||
$nfs = $notaCariocaCancel->formatSuccessResponse($response); | ||
|
||
var_dump($nfs); | ||
} else { | ||
$errors = $soapHandler->getErrors($response); | ||
|
||
var_dump($errors); | ||
} | ||
|
||
/* Response | ||
array (size=2) | ||
'DataHoraCancelamento' => string '2020-02-20T12:13:22' (length=19) | ||
'Pedido' => int 54804 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace NFSePHP\NotaCarioca; | ||
|
||
use Garden\Schema\Schema; | ||
use Garden\Schema\ValidationException; | ||
use NFSePHP\XmlFactoryInterface; | ||
|
||
/** | ||
* Class to generate XML to the ConsultarNfse Web Service operation. | ||
*/ | ||
class CancelarNfseFactory extends NotaCariocaFactoryBase implements XmlFactoryInterface | ||
{ | ||
public function __construct(array $rps, string $env = 'dev') | ||
{ | ||
parent::__construct($rps, $env); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getOperation(): string | ||
{ | ||
return 'CancelarNfse'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function formatSuccessResponse(string $responseXml): array | ||
{ | ||
$resp = $this->getEncoder()->decode($responseXml, ''); | ||
|
||
if (isset($resp['Cancelamento']) and isset($resp['Cancelamento']['Confirmacao'])) { | ||
return [ | ||
'DataHoraCancelamento' => $resp['Cancelamento']['Confirmacao']['DataHoraCancelamento'], | ||
'Pedido' => $resp['Cancelamento']['Confirmacao']['@Id'], | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getSchemaStructure(): array | ||
{ | ||
return [ | ||
'CancelarNfseEnvio' => [ | ||
'Pedido' => [ | ||
'InfPedidoCancelamento' => [ | ||
'IdentificacaoNfse' => [ | ||
'Numero', | ||
'Cnpj', | ||
'InscricaoMunicipal', | ||
'CodigoMunicipio', | ||
], | ||
'CodigoCancelamento', | ||
], | ||
], | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getEnvelopeXml(): string | ||
{ | ||
$structure = $this->getSchemaStructure(); | ||
|
||
$rps = [ | ||
'CancelarNfseEnvio' => [ | ||
'@xmlns' => 'http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd', | ||
'Pedido' => [ | ||
'@xmlns' => 'http://www.abrasf.org.br/ABRASF/arquivos/nfse.xsd', | ||
'InfPedidoCancelamento' => [ | ||
'IdentificacaoNfse' => $this->rps['IdentificacaoNfse'], | ||
'CodigoCancelamento' => $this->rps['CodigoCancelamento'], | ||
], | ||
], | ||
], | ||
]; | ||
|
||
// 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 version="1.0"?>', '', $xml); | ||
$xml = str_replace('<rootnode>', '', $xml); | ||
$xml = str_replace('</rootnode>', '', $xml); | ||
|
||
// Envelope request | ||
$this->addEnvelope($xml); | ||
|
||
// header('Content-type: text/xml'); | ||
// print_r($xml); | ||
// exit(); | ||
|
||
return $xml; | ||
} | ||
} |