Skip to content

Commit

Permalink
Merge pull request #12 from giansalex/feature/load-error-cdr
Browse files Browse the repository at this point in the history
Load error from cdr
  • Loading branch information
giansalex authored Oct 26, 2018
2 parents d986f95 + acab29c commit 4cafb9c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 80 deletions.
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ before_script:
- composer install --prefer-source --no-interaction

install:
- composer require satooshi/php-coveralls
- |
if [ $(phpenv version-name) = "7.1" ]; then
composer require php-coveralls/php-coveralls;
composer require --dev phpstan/phpstan;
fi
script:
- mkdir -p build/logs
- |
Expand All @@ -23,4 +24,7 @@ script:
- vendor/bin/phpunit --configuration phpunit.xml --coverage-clover build/logs/clover.xml

after_success:
- travis_retry php vendor/bin/coveralls -v
- |
if [ $(phpenv version-name) = "7.1" ]; then
travis_retry php vendor/bin/php-coveralls -v;
fi
22 changes: 22 additions & 0 deletions src/Ws/Services/BaseSunat.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Greenter\Ws\Services;

use Greenter\Model\Response\BillResult;
use Greenter\Model\Response\Error;
use Greenter\Validator\ErrorCodeProviderInterface;
use Greenter\Ws\Reader\CdrReaderInterface;
Expand Down Expand Up @@ -167,6 +168,26 @@ protected function getMessageError($code)
return $this->codeProvider->getValue($code);
}

protected function isExceptionCode($code)
{
$value = intval($code);

return $value >= 100 && $value <= 1999;
}

protected function loadErrorByCode(BillResult $result, $code)
{
$error = $this->getErrorByCode($code);

if (empty($error->getMessage()) && $result->getCdrResponse()) {
$error->setMessage($result->getCdrResponse()->getDescription());
}

$result
->setSuccess(false)
->setError($error);
}

private function getXmlResponse($content)
{
$filter = function ($filename) {
Expand All @@ -186,4 +207,5 @@ private function getFileExtension($filename)

return substr($filename, $lastDotPos + 1);
}

}
20 changes: 0 additions & 20 deletions src/Ws/Services/ConsultCdrService.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,4 @@ private function getStatusResult($method, $resultName, $ruc, $tipo, $serie, $num

return $result;
}

private function isExceptionCode($code)
{
$value = intval($code);

return $value >= 100 && $value <= 1999;
}

private function loadErrorByCode(StatusCdrResult $result, $code)
{
$error = $this->getErrorByCode($code);

if (empty($error->getMessage()) && $result->getCdrResponse()) {
$error->setMessage($result->getCdrResponse()->getDescription());
}

$result
->setSuccess(false)
->setError($error);
}
}
43 changes: 5 additions & 38 deletions src/Ws/Services/ExtService.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,47 +43,14 @@ public function getStatus($ticket)
$result
->setCdrResponse($this->extractResponse($cdrZip))
->setCdrZip($cdrZip);
}
} catch (\SoapFault $e) {
$result->setError($this->getErrorFromFault($e));
}

return $result;
}

/**
* @deprecated Use instead \Greenter\Ws\Services\ConsultCdrService
*
* @param string $ruc
* @param string $tipo
* @param string $serie
* @param int $numero
*
* @return StatusCdrResult
*/
public function getCdrStatus($ruc, $tipo, $serie, $numero)
{
$client = $this->getClient();
$result = new StatusCdrResult();

try {
$params = [
'rucComprobante' => $ruc,
'tipoComprobante' => $tipo,
'serieComprobante' => $serie,
'numeroComprobante' => $numero,
];
$response = $client->call('getStatusCdr', ['parameters' => $params]);
$statusCdr = $response->statusCdr;

$result->setCode($statusCdr->statusCode)
->setMessage($statusCdr->statusMessage)
->setCdrZip($statusCdr->content)
->setSuccess(true);
$code = $result->getCdrResponse()->getCode();
}

if ($statusCdr->content) {
$result->setCdrResponse($this->extractResponse($statusCdr->content));
if ($this->isExceptionCode($code)) {
$this->loadErrorByCode($result, $code);
}

} catch (\SoapFault $e) {
$result->setError($this->getErrorFromFault($e));
}
Expand Down
20 changes: 0 additions & 20 deletions tests/Ws/Services/FeSunatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,26 +133,6 @@ public function testGetStatus()
$this->assertContains('aceptada', $result->getCdrResponse()->getDescription());
}

public function testGetCdrStatus()
{
$wss = $this->getExtSender();
$result = $wss->getCdrStatus('20000000001', '01', 'F001', '1');

$this->assertTrue($result->isSuccess());
$this->assertEquals('0', $result->getCode());
$this->assertNotEmpty($result->getMessage());
$this->assertNotNull($result->getCdrResponse());
$this->assertContains('aceptada', $result->getCdrResponse()->getDescription());
}

public function testInvalidCdrStatus()
{
$wss = $this->getExtSunat();
$result = $wss->getCdrStatus('20000000001', '01', 'F001', '1');

$this->assertFalse($result->isSuccess());
}

/**
* @dataProvider codeProvider
* @param $code
Expand Down

0 comments on commit 4cafb9c

Please sign in to comment.