diff --git a/src/Ws/Services/ExtService.php b/src/Ws/Services/ExtService.php index bc17e9b..832fad6 100644 --- a/src/Ws/Services/ExtService.php +++ b/src/Ws/Services/ExtService.php @@ -8,6 +8,7 @@ namespace Greenter\Ws\Services; +use Greenter\Model\Response\Error; use Greenter\Model\Response\StatusResult; /** @@ -31,15 +32,20 @@ public function getStatus($ticket) ]; $response = $client->call('getStatus', ['parameters' => $params]); $status = $response->status; - $cdrZip = $status->content; $code = $status->statusCode; - $result - ->setCode($code) - ->setSuccess(true); + $result->setCode($code); - if ('0' == $code || '99' == $code) { + if ($this->isPending($code)) { + $result->setError($this->getCustomError($code)); + + return $result; + } + + if ($this->isProcessed($code)) { + $cdrZip = $status->content; $result + ->setSuccess(true) ->setCdrResponse($this->extractResponse($cdrZip)) ->setCdrZip($cdrZip); @@ -55,4 +61,27 @@ public function getStatus($ticket) return $result; } + + /** + * @param string $code + * @return Error + */ + private function getCustomError($code) + { + $error = new Error(); + $error->setCode($code) + ->setMessage('El procesamiento del comprobante aún no ha terminado'); + + return $error; + } + + private function isProcessed($code) + { + return '0' == $code || '99' == $code; + } + + private function isPending($code) + { + return '98' == $code; + } } diff --git a/tests/Ws/Services/FeSunatTest.php b/tests/Ws/Services/FeSunatTest.php index 9d8fc52..016ab44 100644 --- a/tests/Ws/Services/FeSunatTest.php +++ b/tests/Ws/Services/FeSunatTest.php @@ -133,6 +133,18 @@ public function testGetStatus() $this->assertContains('aceptada', $result->getCdrResponse()->getDescription()); } + public function testGetStatusPending() + { + $service = $this->getExtServicePendingProcess(); + + $result = $service->getStatus('223123123213'); + + $this->assertFalse($result->isSuccess()); + $this->assertNull($result->getCdrResponse()); + $this->assertNotNull($result->getError()); + $this->assertNotEmpty($result->getError()->getMessage()); + } + /** * @dataProvider codeProvider * @param $code diff --git a/tests/Ws/Services/FeSunatTestBase.php b/tests/Ws/Services/FeSunatTestBase.php index f3e92fe..4f16621 100644 --- a/tests/Ws/Services/FeSunatTestBase.php +++ b/tests/Ws/Services/FeSunatTestBase.php @@ -34,19 +34,12 @@ protected function getExtSender() ->getMock(); $stub->method('call') - ->will($this->returnCallback(function ($func, $params) { + ->will($this->returnCallback(function () { $zipContent = file_get_contents(__DIR__.'/../../Resources/cdrBaja.zip'); $obj = new \stdClass(); - if ($func == 'getStatus') { - $obj->status = new \stdClass(); - $obj->status->statusCode = '0'; - $obj->status->content = $zipContent; - } elseif ($func == 'getStatusCdr') { - $obj->statusCdr = new \stdClass(); - $obj->statusCdr->statusCode = '0'; - $obj->statusCdr->statusMessage = 'ACEPTADA'; - $obj->statusCdr->content = $zipContent; - } + $obj->status = new \stdClass(); + $obj->status->statusCode = '0'; + $obj->status->content = $zipContent; return $obj; })); @@ -212,4 +205,25 @@ private function getErrorCodeProvider() /**@var $stub ErrorCodeProviderInterface */ return $stub; } + + /** + * @return ExtService + */ + protected function getExtServicePendingProcess() + { + $stub = $this->getMockBuilder(WsClientInterface::class) + ->getMock(); + $obj = new \stdClass(); + $obj->status = new \stdClass(); + $obj->status->statusCode = '98'; + + $stub->method('call') + ->willReturn($obj); + + /**@var $stub WsClientInterface */ + $sunat = new ExtService(); + $sunat->setClient($stub); + + return $sunat; + } } \ No newline at end of file