Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #42 from giansalex/hotfix/ruc-random-url
Browse files Browse the repository at this point in the history
Fix random URL
  • Loading branch information
giansalex authored May 16, 2021
2 parents a8b5bf9 + 452801d commit 0dce25c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
4 changes: 0 additions & 4 deletions docs/ruc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
8 changes: 6 additions & 2 deletions src/Peru/Sunat/Async/Ruc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/Peru/Sunat/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -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=';
}
17 changes: 17 additions & 0 deletions src/Peru/Sunat/RandomTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Peru\Sunat;

trait RandomTrait
{
private static $Pattern = '/<input type="hidden" name="numRnd" value="(.*)">/';

private function getRandom(string $html): string
{
preg_match_all(self::$Pattern, $html, $matches, PREG_SET_ORDER);

return count($matches) > 0 ? $matches[0][1] : '';
}
}
6 changes: 5 additions & 1 deletion src/Peru/Sunat/Ruc.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
class Ruc implements RucInterface
{
use RandomTrait;

/**
* @var ClientInterface
*/
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 0dce25c

Please sign in to comment.