-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Paul Vidal
committed
Sep 7, 2016
0 parents
commit f91ec8b
Showing
7 changed files
with
344 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "tecactus/sunat-php", | ||
"description": "SUNAT package for PHP", | ||
"keywords": [ | ||
"sunat", | ||
"php", | ||
"exchange rate", | ||
"tipo de cambio", | ||
"ruc", | ||
"validar", | ||
"validate", | ||
"consultar", | ||
"consulta ruc", | ||
"consulta ruc sin codigo captcha", | ||
"captcha" | ||
], | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Paul Vidal", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"minimum-stability": "dev", | ||
"require": { | ||
"php": ">=5.5.0", | ||
"guzzlehttp/guzzle": "^6.2", | ||
"nesbot/carbon": "~1.10" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Tecactus\\Sunat\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# SUNAT-PHP | ||
|
||
## Instalación | ||
|
||
Instalar usando composer: | ||
|
||
```bash | ||
composer require tecactus/sunat-php | ||
``` | ||
|
||
O agregar la siguiente línea a tu archivo composer.json: | ||
|
||
```json | ||
"require": { | ||
... | ||
"tecactus/sunat-php": "1.*" | ||
... | ||
} | ||
``` | ||
|
||
## Uso | ||
|
||
### Para consultar datos RUC | ||
|
||
```php | ||
// incluir el autoloader de vendor | ||
require 'vendor/autoload.php'; | ||
|
||
// crear un nuevo objeto de la clase RUC | ||
$sunatRuc = new Tecactus\Sunat\RUC('tu-token-de-acceso-personal'); | ||
|
||
// para consultar los datos usando el número de RUC | ||
print_r( $sunatRuc->getByRuc('12345678901') ); | ||
|
||
// para consultar los datos usando el númer de DNI | ||
print_r( $sunatRuc->getByDni('12345678') ); | ||
|
||
// para devolver el resultado como un array pasar 'true' como segundo argumento. | ||
print_r( $sunatRuc->getByRuc('12345678901', true) ); | ||
``` | ||
|
||
### Para consultar Tipo de Cambio | ||
|
||
```php | ||
// incluir el autoloader de vendor | ||
require 'vendor/autoload.php'; | ||
|
||
// crear un nuevo objeto de la clase ExchangeRate | ||
$sunatTipoCambio = new Tecactus\Sunat\ExchangeRate('tu-token-de-acceso-personal'); | ||
|
||
// para consultar los tipos de cambio de un mes por ejemplo: | ||
// Enero del 2016 | ||
print_r( $sunatTipoCambio->get(2016, 1) ); | ||
|
||
// para consultar los tipos de cambio de un día en específico por ejemplo: | ||
// Enero 13 del 2016 | ||
print_r( $sunatTipoCambio->get(2016, 1, 13) ); | ||
|
||
// Hay días en donde no se establece un tipo de cambio en particular | ||
// en ese caso la SUNAT especifica el uso del tipo de cambio del | ||
// día anterior, por ejemplo: | ||
|
||
// El día Enero 10 de 2016 nos devuelve que no hay resultados: | ||
print_r( $sunatTipoCambio->get(2016, 1, 10) ); // retorna un mensaje que no se encontraron datos para ese día. | ||
|
||
// Pero podemos obtener el resultado el día anterior más cercado pasando 'true' | ||
// como cuarto argumento | ||
print_r( $sunatTipoCambio->get(2016, 1, 10, true) ); // esto nos devuelve el tipo de cambio del día 9 ya que el 10 no existe. | ||
|
||
|
||
// para devolver el resultado como un array pasar 'true' como quinto argumento. | ||
print_r( $sunatTipoCambio->get(2016, 1, null, false, true) ); | ||
|
||
``` | ||
|
||
## Token de Acceso Personal | ||
|
||
Para crear tokens de acceso personal debes de iniciar sesión en Tecactus: | ||
|
||
[https://tecactus.com/auth/login](https://tecactus.com/auth/login) | ||
|
||
Si no estas registrado aún, puedes hacerlo en: | ||
|
||
[https://tecactus.com/auth/register](https://tecactus.com/auth/register) | ||
|
||
Debes de activar tu cuenta si aún no lo has hecho. | ||
Luego ver el panel de gestión de Tokens de acceso en: | ||
|
||
[https://tecactus.com/developers/configuracion/tokens](https://tecactus.com/developers/configuracion/tokens) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Tecactus\Sunat\Exception; | ||
|
||
class InvalidDateException extends \Exception | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Tecactus\Sunat\Exception; | ||
|
||
class InvalidDniException extends \Exception | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace Tecactus\Sunat\Exception; | ||
|
||
class InvalidRucException extends \Exception | ||
{ | ||
// | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
namespace Tecactus\Sunat; | ||
|
||
|
||
use GuzzleHttp\Client; | ||
use Tecactus\Sunat\Exception\InvalidDateException; | ||
use Carbon\Carbon; | ||
|
||
class ExchangeRate | ||
{ | ||
protected $client; | ||
protected $baseUri; | ||
protected $apiToken; | ||
|
||
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, '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); | ||
} | ||
|
||
protected function getQuery($year, $month, $day, $forcePrevious) { | ||
$query = []; | ||
$query['year'] = $year; | ||
$query['month'] = $month; | ||
if ($day) { | ||
$query['day'] = $day; | ||
} | ||
if ($forcePrevious) { | ||
$query['force_previous'] = $forcePrevious; | ||
} | ||
return $query; | ||
} | ||
|
||
protected function validate($year, $month, $day) | ||
{ | ||
$currentDay = $this->getCurrentDate('day'); | ||
$currentMonth = $this->getCurrentDate('month'); | ||
$currentYear = $this->getCurrentDate('year'); | ||
$lastDayOfMonth = $this->getCurrentDate('last_day_of_month'); | ||
|
||
$day = is_null($day) ? $currentDay : $day; | ||
|
||
$date = Carbon::create($year, $month, $day)->startOfDay(); | ||
|
||
if ($year > $currentYear) { | ||
throw new InvalidDateException("Year should be equal or lower to $currentYear."); | ||
} elseif (1 < $month && $month > 12) { | ||
throw new InvalidDateException("Month should be a value between 01 and 12."); | ||
} elseif (!(1 <= $day && $day <= $lastDayOfMonth)) { | ||
throw new InvalidDateException("Day should be a value between 01 and $lastDayOfMonth."); | ||
} elseif ($date->isFuture()) { | ||
throw new InvalidDateException("You cannot set a future date."); | ||
} | ||
} | ||
|
||
protected function getCurrentDate($value = null) | ||
{ | ||
$currentDate = new Carbon(); | ||
switch ($value) { | ||
case null: | ||
return $currentDate; | ||
break; | ||
case 'day': | ||
return $currentDate->day; | ||
break; | ||
case 'month': | ||
return $currentDate->month; | ||
break; | ||
case 'year': | ||
return $currentDate->year; | ||
break; | ||
case 'last_day_of_month': | ||
return $currentDate->endOfMonth()->day; | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
<?php | ||
|
||
namespace Tecactus\Sunat; | ||
|
||
|
||
use GuzzleHttp\Client; | ||
use Tecactus\Sunat\Exception\InvalidRucException; | ||
use Tecactus\Sunat\Exception\InvalidDniException; | ||
|
||
class RUC | ||
{ | ||
protected $client; | ||
protected $baseUri; | ||
protected $apiToken; | ||
|
||
public function __construct($apiToken) | ||
{ | ||
$this->baseUri = "https://tecactus.com/"; | ||
$this->apiToken = $apiToken; | ||
$this->client = new Client(['base_uri' => $this->baseUri, '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.'); | ||
} | ||
$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.'); | ||
} | ||
$response = $this->client->request('POST', 'api/sunat/query/dni', ['query' => 'dni=' . $dni]); | ||
return json_decode($response->getBody()->getContents(), $asArray); | ||
} | ||
|
||
public function validate($value) | ||
{ | ||
$value = trim((string)$value); | ||
|
||
if (is_numeric($value)) { | ||
if (($valuelength = strlen($value)) == 8){ | ||
$sum = 0; | ||
for ($i = 0; $i < $valuelength - 1; $i++){ | ||
$digit = $this->charAt($value, $i) - '0'; | ||
if ( $i==0 ) { | ||
$sum += ($digit*2); | ||
} else { | ||
$sum += ($digit*($valuelength - $i)); | ||
} | ||
} | ||
$diff = $sum % 11; | ||
if ($diff == 1) $diff = 11; | ||
if ($diff + ($this->charAt($value, $valuelength - 1) - '0') == 11) { | ||
return true; | ||
} | ||
return false; | ||
} elseif (($valuelength = strlen($value)) == 11){ | ||
$sum = 0; | ||
$x = 6; | ||
for ($i = 0; $i < $valuelength - 1; $i++){ | ||
if ( $i == 4 ) { | ||
$x = 8; | ||
} | ||
$digit = $this->charAt($value, $i) - '0'; | ||
$x--; | ||
if ( $i==0 ) { | ||
$sum += ($digit*$x); | ||
} else { | ||
$sum += ($digit*$x); | ||
} | ||
} | ||
$diff = $sum % 11; | ||
$diff = 11 - $diff; | ||
if ($diff >= 10) { | ||
$diff = $diff - 10; | ||
} | ||
if ($diff == $this->charAt($value, $valuelength - 1 ) - '0') { | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
protected function validateDni($value) | ||
{ | ||
if (is_numeric($value)) { | ||
return strlen($value) == 8; | ||
} | ||
return false; | ||
} | ||
|
||
protected function charAt($string, $index){ | ||
if($index < mb_strlen($string)){ | ||
return mb_substr($string, $index, 1); | ||
} | ||
else{ | ||
return -1; | ||
} | ||
} | ||
} |