承接 mihai3332001/oauth2-anaf 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

mihai3332001/oauth2-anaf

Composer 安装命令:

composer require mihai3332001/oauth2-anaf

包简介

Anaf E-Factura Symfony Plugin

README 文档

README

About Anaf E-Factura Symfony Plugin

Basic setup:

 // Use:
 $provider = new \League\OAuth2\Client\Provider\GenericProvider([
    'clientId'                => 'XXXXXX',    // The client ID assigned to you by the provider
    'clientSecret'            => 'XXXXXX',    // The client password assigned to you by the provider
    'redirectUri'             => 'https://my.example.com/your-redirect-url/',
    'urlAuthorize'            => 'https://logincert.anaf.ro/anaf-oauth2/v1/authorize',
    'urlAccessToken'          => 'https://logincert.anaf.ro/anaf-oauth2/v1/token',
     'urlResourceOwnerDetails' => 'https://logincert.anaf.ro/anaf-oauth2/v1/revoke'
]);

 // If we don't have an authorization code then get one
if (!isset($_GET['code'])) {

// Fetch the authorization URL from the provider; this returns the
// urlAuthorize option and generates and applies any necessary parameters
// (e.g. state).
$authorizationUrl = $provider->getAuthorizationUrl();

// Get the state generated for you and store it to the session.
$_SESSION['oauth2state'] = $provider->getState();

// Optional, only required when PKCE is enabled.
// Get the PKCE code generated for you and store it to the session.
$_SESSION['oauth2pkceCode'] = $provider->getPkceCode();

// Redirect the user to the authorization URL.
header('Location: ' . $authorizationUrl);
exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || empty($_SESSION['oauth2state']) || $_GET['state'] !== $_SESSION['oauth2state']) {

if (isset($_SESSION['oauth2state'])) {
    unset($_SESSION['oauth2state']);
}

exit('Invalid state');

} else {

try {

    // Optional, only required when PKCE is enabled.
    // Restore the PKCE code stored in the session.
    $provider->setPkceCode($_SESSION['oauth2pkceCode']);

    // Try to get an access token using the authorization code grant.
    $accessToken = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);
    $refreshToken = $accessToken->getToken();
    // We have an access token, which we may use in authenticated
    // requests against the service provider's API.
    echo 'Access Token: ' . $accessToken->getToken() . "<br>";
    echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "<br>";
    echo 'Expired in: ' . $accessToken->getExpires() . "<br>";
    echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "<br>";

    // Using the access token, we may look up details about the
    // resource owner.
    $resourceOwner = $provider->getResourceOwner($accessToken);

    var_export($resourceOwner->toArray());

    // The provider provides a way to get an authenticated API request for
    // the service, using the access token; it returns an object conforming
    // to Psr\Http\Message\RequestInterface.
    $request = $provider->getAuthenticatedRequest(
        'GET',
        'https://service.example.com/resource',
        $accessToken
    );

} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {

    // Failed to get the access token or user details.
    exit($e->getMessage());

}

}

Upload XML E-factura - POST method

     $provider =  new Anaf([
                'clientId'                => 'XXXXXX',    // The client ID assigned to you by the provider
                'clientSecret'            => 'XXXXXX',    // The client password assigned to you by the provider
                'redirectUri'             => 'https://my.example.com/your-redirect-url/',
                'urlAuthorize'            => 'https://logincert.anaf.ro/anaf-oauth2/v1/authorize',
                'urlAccessToken'          => 'https://logincert.anaf.ro/anaf-oauth2/v1/token',
                'urlResourceOwnerDetails' => 'https://logincert.anaf.ro/anaf-oauth2/v1/revoke'
            ]);

        //document that you want to send to ANAF;
        $options['body'] = $document;
        //cif code of your company
        // $getAnafRefreshToken your anaf token
        $requestAnafPOST = $provider->getAuthenticatedRequest(
                    'POST',
                    'https://api.anaf.ro/test/FCTEL/rest/upload?standard=UBL&cif=XXX',
                    $getAnafRefreshToken(),
                    $options
                );
        //your parse response, you can also use getResponse for status code
        $response = $provider->getParsedResponse($requestAnafPOST);

        $crawler = new Crawler($response);
        $indexIncarcare = $crawler->filterXPath('header')->attr('index_incarcare');

Stare Mesaj E-factura - GET method

       $provider =  new Anaf([
                'clientId'                => 'XXXXXX',    // The client ID assigned to you by the provider
                'clientSecret'            => 'XXXXXX',    // The client password assigned to you by the provider
                'redirectUri'             => 'https://my.example.com/your-redirect-url/',
                'urlAuthorize'            => 'https://logincert.anaf.ro/anaf-oauth2/v1/authorize',
                'urlAccessToken'          => 'https://logincert.anaf.ro/anaf-oauth2/v1/token',
                'urlResourceOwnerDetails' => 'https://logincert.anaf.ro/anaf-oauth2/v1/revoke'
            ]);

            $idincarcareSPV = 'XXXXXX';

            $requestAnafGET = $provider->getAuthenticatedRequest(
                'GET',
                'https://api.anaf.ro/test/FCTEL/rest/stareMesaj?id_incarcare=' . $idIncarcareSPV,
                $getAnafRefreshToken(),
            );

            $responseGET = $provider->getParsedResponse($requestAnafGET);
            $crawler2 = new Crawler($responseGET);
            $stareIncarcare = $crawler2->filterXPath('header')->attr('stare');
            $idDescarcare = $crawler2->filterXPath('header')->attr('id_descarcare');

Descarcare Raspuns E-factura - GET method

          $requestAnafDescarcare = $provider->getAuthenticatedRequest(
                'GET',
                'https://api.anaf.ro/test/FCTEL/rest/descarcare?id=' .$idDescarcare,
                $getAnafRefreshToken(),
           );
           $requestAnafDescarcareDOC = $provider->getParsedResponse($requestAnafDescarcare);

mihai3332001/oauth2-anaf 适用场景与选型建议

mihai3332001/oauth2-anaf 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 11 月 25 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 mihai3332001/oauth2-anaf 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 mihai3332001/oauth2-anaf 我们能提供哪些服务?
定制开发 / 二次开发

基于 mihai3332001/oauth2-anaf 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 21
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-25