vlsv/sber-api-registry-oauth-client

Sber API Registry 的客户端,用于处理令牌

v1.1.9 2022-10-10 11:35 UTC

This package is auto-updated.

Last update: 2024-09-10 16:18:41 UTC


README

phpunit tests

Sber API Registry 的客户端,用于处理令牌

获取 access_token 以便后续操作可用的 API。

基本信息

需要在 API 调用端进行的工作改进说明 - API 调用端设置。API 调用根据 OAuth 2.0 规范进行,首先获取一个令牌(access_token),允许直接请求 API。

重要!每次向 API 发起新请求时都需要获取新的令牌。令牌的有效期为 60 秒。

有两种类型的令牌实现

  1. OAUTH-令牌 - 如果不需要访问客户端数据,也不需要客户端同意获取其数据。
  2. OIDC-令牌 - 如果 API 产品需要在客户同意的范围内处理客户数据。

环境变量要求

获取令牌需要

  1. ClientIdClientSecret,可以通过 注册应用程序 来获取。
  2. 为了双向 TLS 连接,需要 颁发证书

安装库

composer require vlsv/sber-api-registry-oauth-client

获取证书

官方文档

准备证书

openssl pkcs12 -in {path_to_certificate}/cert.p12 -out {path_to_certificate}/cert.pem -nodes

获取 access_token

提供的令牌请求方法参数是最基本的,实际上还有更多(参见源代码)。

require_once(__DIR__ . '/vendor/autoload.php');

$config = new \Vlsv\SberApiRegistryOauthClient\ClientConfig(
    clientId: 'client_id',
    clientSecret: 'client_secret',
    certPath: 'cert_path',
    certPassword: 'cert_password',
);

$oAuthClient = new \Vlsv\SberApiRegistryOauthClient\OAuthClient($config);

// OAUTH-токен
try {
    $accessToken = $oAuthClient
        ->getOauthToken(scope: 'https://api.sberbank.ru/qr/order.create')
        ->getAccessToken();
} catch (\Vlsv\SberApiRegistryOauthClient\Exception\ApiException $exception) {
    echo $exception->getMessage();

    if ($exception->getResponseObject()) {
        echo $exception->getResponseObject()->getMoreInformation();
    }
} catch (\Throwable $exception) {
    echo $exception->getMessage();
}

// OIDC-токен
try {
    $accessToken = $oAuthClient->getOidcToken(
       scope: 'https://api.sberbank.ru/qr/order.create',
       code: 'authorization_code',
       redirectUri: 'redirect_uri',
   )->getAccessToken();
} catch (\Vlsv\SberApiRegistryOauthClient\Exception\ApiException $exception) {
    echo $exception->getMessage();

    if ($exception->getResponseObject()) {
        echo $exception->getResponseObject()->getMoreInformation();
    }
} catch (\Throwable $exception) {
    echo $exception->getMessage();
}

测试

所有组。

composer tests

功能和单元测试。

composer tests-feature && composer tests-unit

对于集成测试(库)

cp phpunit.xml.dist phpunit.xml

根据 官方文档,使用注册时获得的值填写变量 CLIENT_IDCLIENT_SECRET 等。在调用获取令牌的方法时,指定 SberPay QR/Плати QR 的 scope

composer tests-integration