Skip to content

Commit

Permalink
Merge pull request #4 from tecnospeed/feature/cancel
Browse files Browse the repository at this point in the history
Feature/cancel
  • Loading branch information
ferfabricio authored Mar 12, 2019
2 parents 0f0e0ef + 843276d commit 1a4898c
Show file tree
Hide file tree
Showing 25 changed files with 951 additions and 49 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
# Changelog
Alterações na biblioteca

## [1.3.0] - 12/03/2019
### Adicionado

Cancelamento de NFSe utilizando o id da nota, consulta do status do cancelamento.

## [1.2.0] - 11/03/2019
### Adicionado

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ Os arquivos serão salvos na pasta configurada (a qual precisa ter permissão de

[Exemplo de download utilizando o ID pode ser encontrado aqui](https://github.com/tecnospeed/plugnotas-php/blob/master/examples/nfse.download.php) e [exemplo de download utilizando o CNPJ e ID Integração aqui](https://github.com/tecnospeed/plugnotas-php/blob/master/examples/nfse.download.cnpjId.php).

### Cancelamento

O cancelamento de uma NFSe pode ser realizado da mesma forma com que a busca e o download, informando o ID da Nfse ou o Cnpj do prestador e o ID Integração.

Da mesma forma que a consulta e o download do PDF da Nfse é necessário de um objeto do tipo `TecnoSpeed\Plugnotas\Configuration`, o qual deve ser setado num novo objeto `TecnoSpeed\Plugnotas\Nfse` utilizando o método `setConfiguration`.

Os respectivos métodos para realizar este procedimento são: `cancel` e `cancelByCnpjAndIdIntegracao`.

Ao criar um cancelamento será retornado um protocolo, tal protocolo pode ser utilizado para consultar o status do cancelamento utilizando a rota `cancelStatus`.

### Exemplos

Você pode conferir alguns exemplos na pasta `/examples`.
Expand Down
22 changes: 22 additions & 0 deletions examples/nfse.cancel.cnpjId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

require '../vendor/autoload.php';

use TecnoSpeed\Plugnotas\Configuration;
use TecnoSpeed\Plugnotas\Nfse;

try {
// Criando configuração (este objeto é onde você irá colocar seu api-key)
$configuration = new Configuration(
Configuration::TYPE_ENVIRONMENT_SANDBOX, // Ambiente a ser enviada a requisição
'2da392a6-79d2-4304-a8b7-959572c7e44d' // API-Key
);

$nfse = new Nfse();
$nfse->setConfiguration($configuration);
$cancelation = $nfse->cancelByCnpjAndIdIntegracao('00000000000191', '000000000001914');
var_dump($cancelation);
} catch (\Exception $e) {
var_dump($e);
}

22 changes: 22 additions & 0 deletions examples/nfse.cancel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

require '../vendor/autoload.php';

use TecnoSpeed\Plugnotas\Configuration;
use TecnoSpeed\Plugnotas\Nfse;

try {
// Criando configuração (este objeto é onde você irá colocar seu api-key)
$configuration = new Configuration(
Configuration::TYPE_ENVIRONMENT_SANDBOX, // Ambiente a ser enviada a requisição
'2da392a6-79d2-4304-a8b7-959572c7e44d' // API-Key
);

$nfse = new Nfse();
$nfse->setConfiguration($configuration);
$cancelation = $nfse->cancel('5c3118127ab98414de5e2bd6');
var_dump($cancelation);
} catch (\Exception $e) {
var_dump($e);
}

Empty file added examples/tmp/.gitkeep
Empty file.
11 changes: 8 additions & 3 deletions src/Communication/CallApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public function __construct(Configuration $configuration)
$this->url = $configuration->getUrl();
}

public function setClient($client)
{
$this->client = $client;
}

public function send($method, $destination, $data)
{
try {
Expand All @@ -42,7 +47,7 @@ public function send($method, $destination, $data)

return ResponseObject::parse($response);
}

$response = $this->client->request(
$method,
$destination,
Expand Down Expand Up @@ -74,7 +79,7 @@ public function download($method, $destination, $data, $fileName)

return ResponseObject::parse($response);
}

$response = $this->client->request(
$method,
$destination,
Expand All @@ -85,7 +90,7 @@ public function download($method, $destination, $data, $fileName)
]
);

return true;
return ResponseObject::parse($response);
} catch (ClientException $ce) {
$response = $ce->getResponse();
return ResponseObject::parse($response);
Expand Down
6 changes: 2 additions & 4 deletions src/Interfaces/IDfe.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@

interface IDfe
{
public function downloadPdf($id);
public function downloadPdfByCnpjAndIdIntegracao($cnpj, $idIntegracao);
public function cancel($id);
public function download($id);
public function find($id);
public function findByCnpjAndIdIntegracao($cnpj, $idIntegracao);
public function findByIdOrProtocol($idOrProtocol);
public function send(Configuration $configuration);
public function toArray();
public function validate();
Expand Down
66 changes: 39 additions & 27 deletions src/Nfse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@
use FerFabricio\Hydratator\Hydratate;
use Respect\Validation\Validator as v;
use TecnoSpeed\Plugnotas\Abstracts\BuilderAbstract;
use TecnoSpeed\Plugnotas\Configuration;
use TecnoSpeed\Plugnotas\Communication\CallApi;
use TecnoSpeed\Plugnotas\Error\ConfigurationRequiredError;
use TecnoSpeed\Plugnotas\Configuration;
use TecnoSpeed\Plugnotas\Error\RequiredError;
use TecnoSpeed\Plugnotas\Error\ValidationError;
use TecnoSpeed\Plugnotas\Interfaces\IDfe;
Expand All @@ -16,9 +15,12 @@
use TecnoSpeed\Plugnotas\Nfse\Rps;
use TecnoSpeed\Plugnotas\Nfse\Servico;
use TecnoSpeed\Plugnotas\Nfse\Tomador;
use TecnoSpeed\Plugnotas\Traits\Communication;

class Nfse extends BuilderAbstract implements IDfe
{
use Communication;

private $cidadePrestacao;
private $configuration;
private $enviarEmail;
Expand Down Expand Up @@ -155,7 +157,7 @@ public function validate()
v::keyNested('servico.discriminacao'),
v::keyNested('servico.cnae'),
v::keyNested('servico.iss.aliquota'),
v::keyNested('servico.valor.servico')
v::keyNested('servico.valor.servico')
)->validate($data) ||
!v::allOf(
v::keyNested('prestador.cpfCnpj'),
Expand Down Expand Up @@ -210,45 +212,40 @@ public static function fromArray($data)

public function find($id)
{
if (!$this->configuration) {
throw new ConfigurationRequiredError('É necessário setar a configuração utilizando o método setConfiguration.');
}

$communication = new CallApi($this->configuration);
$communication = $this->getCallApiInstance($this->configuration);
return $communication->send('GET', "/nfse/${id}", null);
}

public function findByCnpjAndIdIntegracao($cnpj, $idIntegracao)
{
if (!$this->configuration) {
throw new ConfigurationRequiredError('É necessário setar a configuração utilizando o método setConfiguration.');
}

$communication = new CallApi($this->configuration);
$communication = $this->getCallApiInstance($this->configuration);
return $communication->send('GET', "/nfse/consultar/${idIntegracao}/${cnpj}", null);
}

public function findByIdOrProtocol($idOrProtocol)
{
if (!$this->configuration) {
throw new ConfigurationRequiredError('É necessário setar a configuração utilizando o método setConfiguration.');
}

$communication = new CallApi($this->configuration);
$communication = $this->getCallApiInstance($this->configuration);
return $communication->send('GET', "/nfse/consultar/${idOrProtocol}", null);
}

public function downloadPdf($id)
public function findCancel($id)
{
if (!$this->configuration) {
throw new ConfigurationRequiredError('É necessário setar a configuração utilizando o método setConfiguration.');
}
$communication = $this->getCallApiInstance($this->configuration);
return $communication->send('GET', "/nfse/cancelar/status/${id}", null);
}

public function download($id)
{
return $this->downloadPdf($id);
}

public function downloadPdf($id)
{
$communication = $this->getCallApiInstance($this->configuration);
if (!$this->configuration->getNfseDownloadDirectory()) {
throw new RequiredError('É necessário setar o diretório para download do PDF.');
}

$communication = new CallApi($this->configuration);
return $communication->download(
'GET',
"/nfse/pdf/${id}",
Expand All @@ -259,20 +256,35 @@ public function downloadPdf($id)

public function downloadPdfByCnpjAndIdIntegracao($cnpj, $idIntegracao)
{
if (!$this->configuration) {
throw new ConfigurationRequiredError('É necessário setar a configuração utilizando o método setConfiguration.');
}
$communication = $this->getCallApiInstance($this->configuration);

if (!$this->configuration->getNfseDownloadDirectory()) {
throw new RequiredError('É necessário setar o diretório para download do PDF.');
}

$communication = new CallApi($this->configuration);
return $communication->download(
'GET',
"/nfse/pdf/${idIntegracao}/${cnpj}",
null,
$this->configuration->getNfseDownloadDirectory() . '/' . $cnpj . '-' . $idIntegracao . '.pdf'
);
}

public function cancel($id)
{
$communication = $this->getCallApiInstance($this->configuration);
return $communication->send('POST', "/nfse/cancelar/${id}", null);
}

public function cancelByCnpjAndIdIntegracao($cnpj, $idIntegracao)
{
$communication = $this->getCallApiInstance($this->configuration);
return $communication->send('POST', "/nfse/pdf/${idIntegracao}/${cnpj}", null);
}

public function cancelStatus($id)
{
$communication = $this->getCallApiInstance($this->configuration);
return $communication->send('POST', "/nfse/cancelar/status/${id}", null);
}
}
12 changes: 8 additions & 4 deletions src/Nfse/Prestador.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@

use FerFabricio\Hydratator\Hydratate;
use Respect\Validation\Validator as v;
use TecnoSpeed\Plugnotas\Configuration;
use TecnoSpeed\Plugnotas\Communication\CallApi;
use TecnoSpeed\Plugnotas\Abstracts\BuilderAbstract;
use TecnoSpeed\Plugnotas\Common\Endereco;
use TecnoSpeed\Plugnotas\Common\Telefone;
use TecnoSpeed\Plugnotas\Abstracts\BuilderAbstract;
use TecnoSpeed\Plugnotas\Communication\CallApi;
use TecnoSpeed\Plugnotas\Configuration;
use TecnoSpeed\Plugnotas\Error\InvalidTypeError;
use TecnoSpeed\Plugnotas\Error\RequiredError;
use TecnoSpeed\Plugnotas\Error\ValidationError;
use TecnoSpeed\Plugnotas\Traits\Communication;

class Prestador extends BuilderAbstract
{
use Communication;

private $certificado;
private $cpfCnpj;
private $email;
Expand Down Expand Up @@ -255,7 +259,7 @@ public function send(Configuration $configuration)
{
$this->validate();

$communication = new CallApi($configuration);
$communication = $this->getCallApiInstance($configuration);
return $communication->send('POST', '/nfse/prestador', $this->toArray(true));
}
}
7 changes: 0 additions & 7 deletions src/Nfse/Rps.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace TecnoSpeed\Plugnotas\Nfse;

use Respect\Validation\Validator as v;
use TecnoSpeed\Plugnotas\Abstracts\BuilderAbstract;
use TecnoSpeed\Plugnotas\Error\ValidationError;

Expand All @@ -13,9 +12,6 @@ class Rps extends BuilderAbstract

public function setDataEmissao(\DateTimeInterface $dataEmissao)
{
if (!v::date()->validate($dataEmissao)) {
throw new ValidationError('dataEmissao deve ser uma data válida.');
}
$this->dataEmissao = $dataEmissao->format('Y-m-d\TH:i:s');
}

Expand All @@ -26,9 +22,6 @@ public function getDataEmissao()

public function setCompetencia(\DateTimeInterface $competencia)
{
if (!v::date()->validate($competencia)) {
throw new ValidationError('competencia deve ser uma data válida.');
}
$this->competencia = $competencia->format('Y-m-d');
}

Expand Down
6 changes: 5 additions & 1 deletion src/Nfse/Servico.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
use TecnoSpeed\Plugnotas\Abstracts\BuilderAbstract;
use TecnoSpeed\Plugnotas\Configuration;
use TecnoSpeed\Plugnotas\Communication\CallApi;
use TecnoSpeed\Plugnotas\Error\RequiredError;
use TecnoSpeed\Plugnotas\Error\ValidationError;
use TecnoSpeed\Plugnotas\Nfse\Servico\Deducao;
use TecnoSpeed\Plugnotas\Nfse\Servico\Evento;
use TecnoSpeed\Plugnotas\Nfse\Servico\Iss;
use TecnoSpeed\Plugnotas\Nfse\Servico\Obra;
use TecnoSpeed\Plugnotas\Nfse\Servico\Retencao;
use TecnoSpeed\Plugnotas\Nfse\Servico\Valor;
use TecnoSpeed\Plugnotas\Traits\Communication;

class Servico extends BuilderAbstract
{
use Communication;

private $cnae;
private $codigo;
private $codigoCidadeIncidencia;
Expand Down Expand Up @@ -212,7 +216,7 @@ public function send(Configuration $configuration)
{
$this->validate();

$communication = new CallApi($configuration);
$communication = $this->getCallApiInstance($configuration);
return $communication->send('POST', '/nfse/servico', $this->toArray(true));
}

Expand Down
9 changes: 6 additions & 3 deletions src/Nfse/Tomador.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@
use TecnoSpeed\Plugnotas\Common\Endereco;
use TecnoSpeed\Plugnotas\Common\Telefone;
use TecnoSpeed\Plugnotas\Error\InvalidTypeError;
use TecnoSpeed\Plugnotas\Error\RequiredError;
use TecnoSpeed\Plugnotas\Error\ValidationError;
use TecnoSpeed\Plugnotas\Abstracts\BuilderAbstract;

use TecnoSpeed\Plugnotas\Traits\Communication;

class Tomador extends BuilderAbstract
{
use Communication;

private $cpfCnpj;
private $email;
private $endereco;
Expand Down Expand Up @@ -128,7 +131,7 @@ public function getTelefone()

public function validate()
{
$data = $this->toArray();
$data = $this->toArray(true);
if(
!v::allOf(
v::keyNested('cpfCnpj'),
Expand All @@ -147,7 +150,7 @@ public function send(Configuration $configuration)
{
$this->validate();

$communication = new CallApi($configuration);
$communication = $this->getCallApiInstance($configuration);
return $communication->send('POST', '/nfse/tomador', $this->toArray(true));
}

Expand Down
Loading

0 comments on commit 1a4898c

Please sign in to comment.