Skip to content

Instantly share code, notes, and snippets.

@dlopez525
Created December 30, 2022 21:05
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlopez525/1cfdfcd4d9438eae7f72328cfe1886ee to your computer and use it in GitHub Desktop.
Save dlopez525/1cfdfcd4d9438eae7f72328cfe1886ee to your computer and use it in GitHub Desktop.
GRE-Sunat
<?php
use Carbon\Carbon;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
class SunatGenerateToken
{
protected $sunatClientId;
protected $sunatClientSecret;
protected $username;
protected $password;
public function __construct($args)
{
$this->sunatClientId = $args['sunat_client_id'];
$this->sunatClientSecret = $args['sunat_client_secret'];
$this->username = $args['ruc'].$args['usuario_sol'];
$this->password = $args['clave_sol'];
}
private function generateSunatToken()
{
$sunatUri = "https://api-seguridad.sunat.gob.pe/v1/clientessol/{$this->sunatClientId}/oauth2/token/";
$params = [
'grant_type' => 'password',
'scope' => 'https://api-cpe.sunat.gob.pe',
'client_id' => $this->sunatClientId,
'client_secret' => $this->sunatClientSecret,
'username' => $this->username,
'password' => $this->password
];
/**
* PARA PRUEBAS USANDO NUBEFACT
*/
// $sunatUri = "https://gre-test.nubefact.com/v1/clientessol/test-85e5b0ae-255c-4891-a595-0b98c65c9854/oauth2/token";
// $params = [
// 'grant_type' => 'password',
// 'scope' => 'https://api-cpe.sunat.gob.pe',
// 'client_id' => "test-85e5b0ae-255c-4891-a595-0b98c65c9854",
// 'client_secret' => "test-Hty/M6QshYvPgItX2P0+Kw==",
// 'username' => "{$ruc}MODDATOS",
// 'password' => "MODDATOS"
// ];
try {
$client = new GuzzleClient();
$res = $client->request('POST', $sunatUri, [
'form_params' => $params
]);
$response = json_decode($res->getBody()->getContents(), true);
return ['success' => true, 'response' => $response];
} catch (ClientException $e) {
$response = json_decode($e->getResponse()->getBody()->getContents(), true);
return ['success' => false, 'response' => $response, 'code' => $e->getCode()];
}
}
}
<?php
use Carbon\Carbon;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Exception\ClientException;
class SunatSendDocument
{
const SUNAT_SEND_API_ENDPOINT = 'https://api-cpe.sunat.gob.pe/v1/contribuyente/gem/comprobantes/';
const SUNAT_SEND_API_ENDPOINT_TEST = "https://gre-test.nubefact.com/v1/contribuyente/gem/comprobantes/";
const SUNAT_CONSULT_API_ENDPOINT = 'https://api-cpe.sunat.gob.pe/v1/contribuyente/gem/comprobantes/envios/';
const SUNAT_CONSULT_API_ENDPOINT_TEST = "https://gre-test.nubefact.com/v1/contribuyente/gem/comprobantes/envios/";
private $fileName;
private $ruc;
protected $token;
public function send($fileName) {
$args['sunat_client_id'] = '';
$args['sunat_client_secret'] = '';
$args['ruc'] = '';
$args['usuario_sol'] = '';
$args['clave_sol'] = '';
$this->fileName = $fileName;
$this->token = (new SunatGenerateToken($args))->getToken();
return $this->request();
}
private function request()
{
$client = new GuzzleClient();
try {
$data = [
'archivo' => [
'nomArchivo' => "$this->fileName.zip",
'arcGreZip' => self::getFileBinary(),
'hashZip' => self::getFileHash(),
],
];
$data = json_encode($data);
$urlSend = self::SUNAT_SEND_API_ENDPOINT;
/**
* PARA PRUEBAS
*/
// $urlSend = self::SUNAT_SEND_API_ENDPOINT_TEST;
$res = $client->request('POST', $urlSend."{$this->fileName}", [
'headers' => [
'User-Agent' => 'GyOManager/1.0',
'Content-Type' => 'application/json',
'Authorization' => "Bearer {$this->token['token']}",
],
'body' => $data,
]);
$response = json_decode($res->getBody(), true);
return ['success' => true, 'data' => $response];
} catch (ClientException $e) {
$response = json_decode($e->getResponse()->getBody()->getContents(), true);
return ['success' => false, 'data' => $response];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment