Skip to content

Commit

Permalink
windows bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Vidal committed May 5, 2017
1 parent c884ca0 commit bb2b9ef
Show file tree
Hide file tree
Showing 6 changed files with 4,139 additions and 15 deletions.
8 changes: 8 additions & 0 deletions src/Exception/ServerErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Tecactus\Sunat\Exception;

class ServerErrorException extends \Exception
{
//
}
8 changes: 8 additions & 0 deletions src/Exception/UnauthenticatedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Tecactus\Sunat\Exception;

class UnauthenticatedException extends \Exception
{
//
}
8 changes: 8 additions & 0 deletions src/Exception/UndefinedErrorException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Tecactus\Sunat\Exception;

class UndefinedErrorException extends \Exception
{
//
}
28 changes: 24 additions & 4 deletions src/ExchangeRate.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

use GuzzleHttp\Client;
use Tecactus\Sunat\Exception\InvalidDateException;
use Tecactus\Sunat\Exception\UnauthenticatedException;
use Tecactus\Sunat\Exception\ServerErrorException;
use Tecactus\Sunat\Exception\UndefinedErrorException;
use Carbon\Carbon;

class ExchangeRate
Expand All @@ -18,14 +21,31 @@ public function __construct($apiToken)
// $this->baseUri = "https://tecactus.com/";
$this->baseUri = "http://tecactus.app/";
$this->apiToken = $apiToken;
$this->client = new Client(['base_uri' => $this->baseUri, 'verify' => false, 'headers' => ['Accept' => 'application/json', 'Authorization' => 'Bearer ' . $this->apiToken]]);
$this->client = new Client(['base_uri' => $this->baseUri, 'verify' => __DIR__.'/cacert.pem', 'headers' => ['Accept' => 'application/json', 'Authorization' => 'Bearer ' . $this->apiToken]]);
}

public function get($year, $month, $day = null, $forcePrevious = false, $asArray = false)
{
$this->validate($year, $month, $day);
$response = $this->client->request('POST', 'api/sunat/exchange-rate', ['query' => $this->getQuery($year, $month, $day, $forcePrevious)]);
return json_decode($response->getBody()->getContents(), $asArray);
try {
$this->validate($year, $month, $day);
$response = $this->client->request('POST', 'api/sunat/exchange-rate', ['query' => $this->getQuery($year, $month, $day, $forcePrevious)]);
return json_decode($response->getBody()->getContents(), $asArray);
} catch (ClientException $e) {
$status = $e->getResponse()->getStatusCode();
switch ($status) {
case 401:
throw new UnauthenticatedException('Token seems not to be valid.');
break;

case 500:
throw new ServerErrorException('Server error.');
break;

default:
throw new UndefinedErrorException('An unexpected error has ben ocurred.');
break;
}
}
}

protected function getQuery($year, $month, $day, $forcePrevious) {
Expand Down
59 changes: 48 additions & 11 deletions src/RUC.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
use GuzzleHttp\Client;
use Tecactus\Sunat\Exception\InvalidRucException;
use Tecactus\Sunat\Exception\InvalidDniException;
use Tecactus\Sunat\Exception\UnauthenticatedException;
use Tecactus\Sunat\Exception\ServerErrorException;
use Tecactus\Sunat\Exception\UndefinedErrorException;

class RUC
{
Expand All @@ -17,27 +20,61 @@ public function __construct($apiToken)
{
$this->baseUri = "https://tecactus.com/";
$this->apiToken = $apiToken;
$this->client = new Client(['base_uri' => $this->baseUri, 'verify' => false, 'headers' => ['Accept' => 'application/json', 'Authorization' => 'Bearer ' . $this->apiToken]]);
$this->client = new Client(['base_uri' => $this->baseUri, 'verify' => __DIR__.'/cacert.pem', 'headers' => ['Accept' => 'application/json', 'Authorization' => 'Bearer ' . $this->apiToken]]);
}

public function getByRuc($ruc, $asArray = false)
{
if (!$this->validate($ruc)) {
throw new InvalidRucException('RUC number seems not to be valid.');
try {
if (!$this->validate($ruc)) {
throw new InvalidRucException('RUC number seems not to be valid.');
}
$response = $this->client->request('POST', 'api/sunat/query/ruc', ['query' => [
'ruc' => $ruc
]]);
return json_decode($response->getBody()->getContents(), $asArray);
} catch (ClientException $e) {
$status = $e->getResponse()->getStatusCode();
switch ($status) {
case 401:
throw new UnauthenticatedException('Token seems not to be valid.');
break;

case 500:
throw new ServerErrorException('Server error.');
break;

default:
throw new UndefinedErrorException('An unexpected error has ben ocurred.');
break;
}
}
$response = $this->client->request('POST', 'api/sunat/query/ruc', ['query' => [
'ruc' => $ruc
]]);
return json_decode($response->getBody()->getContents(), $asArray);
}

public function getByDni($dni, $asArray = false)
{
if (!$this->validateDni($dni)) {
throw new InvalidDniException('DNI number seems not to be valid.');
try {
if (!$this->validateDni($dni)) {
throw new InvalidDniException('DNI number seems not to be valid.');
}
$response = $this->client->request('POST', 'api/sunat/query/dni', ['query' => 'dni=' . $dni]);
return json_decode($response->getBody()->getContents(), $asArray);
} catch (ClientException $e) {
$status = $e->getResponse()->getStatusCode();
switch ($status) {
case 401:
throw new UnauthenticatedException('Token seems not to be valid.');
break;

case 500:
throw new ServerErrorException('Server error.');
break;

default:
throw new UndefinedErrorException('An unexpected error has ben ocurred.');
break;
}
}
$response = $this->client->request('POST', 'api/sunat/query/dni', ['query' => 'dni=' . $dni]);
return json_decode($response->getBody()->getContents(), $asArray);
}

public function validate($value)
Expand Down
Loading

0 comments on commit bb2b9ef

Please sign in to comment.