greenter / gre-api
API用于新平台电子发票指南 - SUNAT。
v1.0.2
2023-01-13 21:00 UTC
Requires
- php: >=7.4
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- guzzlehttp/guzzle: ^7.3
- guzzlehttp/psr7: ^1.7 || ^2.0
Requires (Dev)
- phpstan/phpstan: ^0.12.51
- phpunit/phpunit: ^8.0 || ^9.0
README
新GRE平台。
要求
- PHP 7.4或更高版本。
curl
扩展
安装
通过 Composer 安装
composer require greenter/gre-api
使用
首先需要从SUNAT门户网站获取client_id和client_secret,您可以参考官方指南。
<?php $apiInstance = new Greenter\Sunat\GRE\Api\AuthApi( new GuzzleHttp\Client() ); $client_id = 'client_id_example'; // El client_id generado en menú sol $grant_type = 'password'; $scope = 'https://api-cpe.sunat.gob.pe'; $client_secret = 'client_secret_example'; // client_secret generado en menú sol $username = 'username_example'; // <Numero de RUC> + <Usuario SOL> $password = 'password_example'; // Contraseña SOL try { $result = $apiInstance->getToken($grant_type, $scope, $client_id, $client_secret, $username, $password); print_r($result); } catch (Exception $e) { echo 'Excepcion cuando invocaba AuthApi->getToken: ', $e->getMessage(), PHP_EOL; }
发送凭证
$config = Greenter\Sunat\GRE\Configuration::getDefaultConfiguration() ->setAccessToken($token); $cpeApi = new Greenter\Sunat\GRE\Api\CpeApi( new GuzzleHttp\Client(), $config->setHost('https://api.sunat.gob.pe/v1') ); $greZip = file_get_contents('20161515648-09-T001-124.zip'); $doc = (new Greenter\Sunat\GRE\Model\CpeDocument()) ->setArchivo((new Greenter\Sunat\GRE\Model\CpeDocumentArchivo()) ->setNomArchivo('20161515648-09-T001-124.zip') ->setArcGreZip(base64_encode($greZip)) ->setHashZip(hash('sha256', $greZip)) ); $result = $cpeApi->enviarCpe('20161515648-09-T001-124', $doc); $ticket = $result->getNumTicket();