vesia/esia-gateway-client

ESIA网关的纯客户端

0.5.2 2024-09-06 07:32 UTC

README

ESIA网关通信的客户端。当前版本仅支持通过ЦПГ HTTP API进行交互。

安装

最小PHP版本:8.1

  1. 执行 composer require vanta/esia-gateway-client
  2. 必须安装PSR兼容客户端

使用方法

创建客户端实例

use GuzzleHttp\Psr7\HttpFactory;
use Symfony\Component\HttpClient\CurlHttpClient;
use Symfony\Component\HttpClient\Psr18Client;
use Vanta\Integration\EsiaGateway\RestClientBuilder;
use Vanta\Integration\EsiaGateway\Infrastructure\HttpClient\ConfigurationClient;

$builder = RestClientBuilder::create(
    new Psr18Client(new CurlHttpClient(), new HttpFactory(), new HttpFactory()),
    new ConfigurationClient(
        clientId: 'YOUR_CLIENT_ID',
        clientSecret: 'YOUR_CLIENT_SECRET',
        url: 'YOUR_URL_GATEWAY',
        redirectUri: 'https://auth.vanta.ru'
    )
);

$client = $builder->createEsiaGatewayClient('https://demo.gate.esia.pro', 'https://pos-credit.ru');

生成用户授权URL

$authorizationUrl = $builder->createAuthorizationUrlBuilder()
    ->withPermission(ScopePermission::DRIVERS_LICENSE_DOC)
    ->withoutPermission(ScopePermission::MOBILE)
    ->build()
;

将授权码交换为访问令牌和刷新令牌

$accessToken = $client->getPairKeyByAuthorizationCode($code);

将刷新令牌交换为访问令牌和新的刷新令牌

$accessToken = $client->getPairKeyByRefreshToken($refreshToken);

获取用户信息

$userInfo = $client->getUserInfo($accessToken);