Skip to content

Commit

Permalink
Merge pull request #148 from zucchetti-pos/handle-24hrs-rejection
Browse files Browse the repository at this point in the history
fix: (Complements.php): suporta rejeição cStat 220
  • Loading branch information
robmachado authored Dec 5, 2024
2 parents 9212649 + 7ecffbb commit fcee7a7
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 32 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Pipeline
on:
push:
branches:
- master
- develop

pull_request:
branches:
- master

jobs:
testsuite:
name: Testes Automatizados
runs-on: ubuntu-20.04
strategy:
fail-fast: false
matrix:
php-version: ["7.4"]

steps:
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
extensions: mbstring, intl, mysql, zlib, dom, openssl, soap, json, simplexml, libxml

- name: checkout
uses: actions/checkout@v4

- name: Composer Install
run: |
composer install --no-progress -o --no-ansi --no-interaction
- name: Rodando PHPUnit
run: composer test
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
limpaTAB.php
*~
*.pfx
!tests/fixtures/certs/*.pfx
*.pem
config.json
local
Expand All @@ -11,4 +12,4 @@ docs
nbproject
build
composer.lock
config.json
config.json
8 changes: 4 additions & 4 deletions examples/testaCancelamento.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
error_reporting(E_ALL);
ini_set('display_errors', 'On');

include_once '../bootstrap.php';
include __DIR__ . '/../tests/bootstrap.php';

use NFePHP\Common\Certificate;
use NFePHP\MDFe\Common\Standardize;
Expand All @@ -12,16 +12,16 @@
"atualizacao" => date('Y-m-d H:i:s'),
"tpAmb" => 2,
"razaosocial" => 'FÁBRICA DE SOFTWARE MATRIZ',
"cnpj" => '',
"cnpj" => '06157250000116',
"ie" => '',
"siglaUF" => 'PR',
"versao" => '3.00'
];

try {
$certificate = Certificate::readPfx(
'',
''
file_get_contents(TESTS_FIXTURES . '/certs/cert_cnpj_06157250000116_senha_minhasenha.pfx'),
'minhasenha',
);

$tools = new Tools(json_encode($config), $certificate);
Expand Down
45 changes: 23 additions & 22 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/bootstrap.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
beStrictAboutTestsThatDoNotTestAnything="false"
failOnRisky="true"
failOnWarning="true">

<php>
<ini name="display_errors" value="1" />
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
</php>

<testsuites>
<testsuite name="sped-mdfe Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="tap" target="build/report.tap"/>
<log type="junit" target="build/report.junit.xml"/>
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
<log type="coverage-text" target="build/coverage.txt"/>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
</phpunit>
14 changes: 9 additions & 5 deletions src/Complements.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ protected static function addEventoMDFeProtocol($request, $response)
$retEv = $ret->getElementsByTagName('retEventoMDFe')->item(0);
$cStat = $retEv->getElementsByTagName('cStat')->item(0)->nodeValue;
$xMotivo = $retEv->getElementsByTagName('xMotivo')->item(0)->nodeValue;

if ($cStat != '135') {
throw DocumentsException::wrongDocument(4, "[$cStat] $xMotivo");
}

$tpEvento = $retEv->getElementsByTagName('tpEvento')->item(0)->nodeValue;
if ($tpEvento == '110111') {
$node = 'procCancMDFe';
Expand All @@ -118,9 +123,6 @@ protected static function addEventoMDFeProtocol($request, $response)
} else {
throw DocumentsException::wrongDocument(4, "Evento não disponivel.");
}
if ($cStat != '135') {
throw DocumentsException::wrongDocument(4, "[$cStat] $xMotivo");
}
return self::join(
$ev->saveXML($event),
$ret->saveXML($retEv),
Expand Down Expand Up @@ -195,14 +197,16 @@ public static function cancelRegister($mdfe, $cancelamento)
$proMDFe->getElementsByTagName('nProt')
->item(0)
->nodeValue = $nProt;
if (in_array($cStat, ['135', '136', '155'])
if (
in_array($cStat, ['135', '136', '155'])
&& $tpEvento == '110111'
&& $chaveEvento == $chaveMdfe
) {
$node = $dommdfe->importNode($evento, true);
$dommdfe->documentElement->appendChild($node);
break;
} elseif (in_array($cStat, ['135', '136', '155'])
} elseif (
in_array($cStat, ['135', '136', '155'])
&& $tpEvento == '110112'
&& $chaveEvento == $chaveMdfe
) {
Expand Down
85 changes: 85 additions & 0 deletions tests/ComplementsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

use NFePHP\MDFe\Complements;
use NFePHP\MDFe\Exception\DocumentsException;
use PHPUnit\Framework\TestCase;

class ComplementsTest extends TestCase
{

public function test_shouldThrowDocumentsExceptionWhenCancelTimePass24Hours()
{
$request = <<<XML
<eventoMDFe xmlns="http://www.portalfiscal.inf.br/mdfe" versao="3.00">
<infEvento Id="ID1101114324110615725000011658001000000075100000000101">
<cOrgao>43</cOrgao>
<tpAmb>1</tpAmb>
<CNPJ>06157250000116</CNPJ>
<chMDFe>43241106157250000116580010000000751000000001</chMDFe>
<dhEvento>2024-11-29T20:48:53-03:00</dhEvento>
<tpEvento>110111</tpEvento>
<nSeqEvento>1</nSeqEvento>
<detEvento versaoEvento="3.00">
<evCancMDFe>
<descEvento>Cancelamento</descEvento>
<nProt>943240033753981</nProt>
<xJust>nao ocorreu a entrega</xJust>
</evCancMDFe>
</detEvento>
</infEvento>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
<Reference URI="#ID1101114324110615725000011658001000000075100000000101">
<Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
<Transform Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>Rw2TlnWyJcdKqQClJ3puvkC0s/o=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue></SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate></X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
</eventoMDFe>
XML;

$response = <<<XML
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<mdfeCabecMsg xmlns="http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeRecepcaoEvento">
<cUF>43</cUF>
<versaoDados>3.00</versaoDados>
</mdfeCabecMsg>
</soap:Header>
<soap:Body>
<mdfeRecepcaoEventoResult
xmlns="http://www.portalfiscal.inf.br/mdfe/wsdl/MDFeRecepcaoEvento">
<retEventoMDFe xmlns="http://www.portalfiscal.inf.br/mdfe" versao="3.00">
<infEvento Id="ID999999999999999">
<tpAmb>1</tpAmb>
<verAplic>RS20240710093829</verAplic>
<cOrgao>43</cOrgao>
<cStat>220</cStat>
<xMotivo>Rejeicao: MDF-e autorizada ha mais de 24 horas</xMotivo>
</infEvento>
</retEventoMDFe>
</mdfeRecepcaoEventoResult>
</soap:Body>
</soap:Envelope>
XML;

$this->expectException(DocumentsException::class);
$this->expectExceptionMessage('O documento de resposta relata um erro [220] Rejeicao: MDF-e autorizada ha mais de 24 horas.');

Complements::toAuthorize($request, $response);
}
}
5 changes: 5 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

include dirname(__DIR__) . '/vendor/autoload.php';

define('TESTS_FIXTURES', __DIR__ . '/fixtures');
Binary file not shown.

0 comments on commit fcee7a7

Please sign in to comment.