diff --git a/README.md b/README.md index a674821..4c7297f 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ composer require giansalex/peru-consult ### Servicios Disponibles - Consulta de DNI - Consulta de RUC -- Validez de Usuario SOL (extras) +- Validez de Usuario SOL (extra) ### API :cloud: -Visitar [peru-consult-api](https://github.com/giansalex/peru-consult-api). +Ir a [peru-consult-api](https://github.com/giansalex/peru-consult-api). diff --git a/docs/ruc.md b/docs/ruc.md index f0ec140..30495d8 100644 --- a/docs/ruc.md +++ b/docs/ruc.md @@ -29,10 +29,6 @@ echo json_encode($company); ``` -!!! tip "Cambiar URL utilizada" - Si necesita cambiar la url utilizada internamente para obtener la informaciĆ³n del RUC, este es un ejemplo: - `$cs->urlConsult='http://e-consultaruc.sunat.gob.pe/cl-ti-itmrconsruc/jcrS03Alias';` - ## Resultado Resultado en formato json. diff --git a/src/Peru/Sunat/Async/Ruc.php b/src/Peru/Sunat/Async/Ruc.php index fb0b054..497ce0b 100644 --- a/src/Peru/Sunat/Async/Ruc.php +++ b/src/Peru/Sunat/Async/Ruc.php @@ -4,11 +4,14 @@ use Peru\Http\Async\ClientInterface; use Peru\Sunat\Endpoints; +use Peru\Sunat\RandomTrait; use Peru\Sunat\RucParser; use React\Promise\PromiseInterface; class Ruc { + use RandomTrait; + /** * @var ClientInterface */ @@ -34,8 +37,9 @@ public function __construct(ClientInterface $client, RucParser $parser) public function get(string $ruc): PromiseInterface { return $this->client - ->getAsync(Endpoints::RANDOM) - ->then(function ($random) use ($ruc) { + ->getAsync(Endpoints::RANDOM_PAGE) + ->then(function ($htmlRandom) use ($ruc) { + $random = $this->getRandom($htmlRandom); $url = Endpoints::CONSULT."?accion=consPorRuc&nroRuc=$ruc&numRnd=$random&actReturn=1&modo=1"; return $this->client->getAsync($url); diff --git a/src/Peru/Sunat/Endpoints.php b/src/Peru/Sunat/Endpoints.php index 3f949a0..185c53b 100644 --- a/src/Peru/Sunat/Endpoints.php +++ b/src/Peru/Sunat/Endpoints.php @@ -9,4 +9,5 @@ final class Endpoints public const CONSULT = 'https://e-consultaruc.sunat.gob.pe/cl-ti-itmrconsruc/jcrS00Alias'; public const RANDOM = 'https://e-consultaruc.sunat.gob.pe/cl-ti-itmrconsruc/captcha?accion=random'; public const USER_VALIDEZ = 'https://ww3.sunat.gob.pe/cl-ti-itestadousr/usrS00Alias'; + public const RANDOM_PAGE = 'https://e-consultaruc.sunat.gob.pe/cl-ti-itmrconsruc/jcrS00Alias?accion=consPorRazonSoc&razSoc='; } diff --git a/src/Peru/Sunat/RandomTrait.php b/src/Peru/Sunat/RandomTrait.php new file mode 100644 index 0000000..6a424a3 --- /dev/null +++ b/src/Peru/Sunat/RandomTrait.php @@ -0,0 +1,17 @@ +/'; + + private function getRandom(string $html): string + { + preg_match_all(self::$Pattern, $html, $matches, PREG_SET_ORDER); + + return count($matches) > 0 ? $matches[0][1] : ''; + } +} diff --git a/src/Peru/Sunat/Ruc.php b/src/Peru/Sunat/Ruc.php index 0f87e69..e1ebb38 100644 --- a/src/Peru/Sunat/Ruc.php +++ b/src/Peru/Sunat/Ruc.php @@ -16,6 +16,8 @@ */ class Ruc implements RucInterface { + use RandomTrait; + /** * @var ClientInterface */ @@ -46,7 +48,9 @@ public function __construct(ClientInterface $client, RucParser $parser) */ public function get(string $ruc): ?Company { - $random = $this->client->get(Endpoints::RANDOM); + $htmlRandom = $this->client->get(Endpoints::RANDOM_PAGE); + $random = $this->getRandom($htmlRandom); + $html = $this->client->get(Endpoints::CONSULT."?accion=consPorRuc&nroRuc=$ruc&numRnd=$random&actReturn=1&modo=1"); return $html === false ? null : $this->parser->parse($html);