定制 intermedia/ksef-api-v2 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

intermedia/ksef-api-v2

Composer 安装命令:

composer require intermedia/ksef-api-v2

包简介

README 文档

README

Developer-friendly & type-safe Php SDK specifically catered to leverage intermedia/ksef-api-v2 API.

Summary

KSeF API TR: Wersja API: 2.0.0 (build 2.0.0-tr-20251229.1+4e064b0f8497598eeb393ab74ab9a5ada61f64a5)
Klucze publiczne Ministerstwa Finansów (dla danego środowiska): Pobierz klucze
Historia zmian: Changelog
Rozszerzona dokumentacja API: ksef-docs

Adres serwera API:

  • Środowisko DEMO: https://api-demo.ksef.mf.gov.pl/v2
  • [deprecated] Środowisko DEMO: https://ksef-demo.mf.gov.pl/api/v2

Table of Contents

SDK Installation

The SDK relies on Composer to manage its dependencies.

To install the SDK and add it as a dependency to an existing composer.json file:

composer require "intermedia/ksef-api-v2"

SDK Example Usage

Example

declare(strict_types=1);

require 'vendor/autoload.php';

use Intermedia\Ksef\Apiv2;

$sdk = Apiv2\Client::builder()
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();



$response = $sdk->auth->getCurrentSessions(
    pageSize: 10
);

if ($response->authenticationListResponse !== null) {
    // handle response
}

AuthTokenRequest XML generation with XAdES signature example

declare(strict_types=1);

require 'vendor/autoload.php';

use Intermedia\Ksef\Apiv2\AuthTokenRequest;
use Intermedia\Ksef\Apiv2\Models\Components\{TContextIdentifier, TNip, SubjectIdentifierTypeEnum};

$req = new AuthTokenRequest(
    '20250625-CR-20F5EE4000-DA48AE4124-46',
    TContextIdentifier::fromNip(new TNip('5265877635')),
    SubjectIdentifierTypeEnum::CERTIFICATE_SUBJECT
);

// PEM Signature (private key and public certificate in separate files)
$signedXml = $req->signWithXadesToString('/path/to/private.pem', '/path/to/cert.pem');

// or PKCS#12 (private key and public certificate in one .p12 file)
$signedXml = $req->signWithXadesToString('/path/to/cert.p12', null, 'password_to_p12');

Authentication

Per-Client Security Schemes

This SDK supports the following security scheme globally:

Name Type Scheme
bearer http HTTP Bearer

To authenticate with the API the bearer parameter must be set when initializing the SDK. For example:

declare(strict_types=1);

require 'vendor/autoload.php';

use Intermedia\Ksef\Apiv2;

$sdk = Apiv2\Client::builder()
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();



$response = $sdk->auth->getCurrentSessions(
    pageSize: 10
);

if ($response->authenticationListResponse !== null) {
    // handle response
}

Available Resources and Operations

Available methods

Auth

Certificates

Invoices

Limits

  • getContext - Pobranie limitów dla bieżącego kontekstu
  • getSubject - Pobranie limitów dla bieżącego podmiotu
  • getApiRate - Pobranie aktualnie obowiązujących limitów API

Peppol

Permissions

Security

Sessions

Sessions.Invoices

  • getList - Pobranie faktur sesji
  • getStatus - Pobranie statusu faktury z sesji
  • getFailed - Pobranie niepoprawnie przetworzonych faktur sesji
  • getInvoiceUpoByKsefNumber - Pobranie UPO faktury z sesji na podstawie numeru KSeF
  • getUpo - Pobranie UPO faktury z sesji na podstawie numeru referencyjnego faktury
  • sendOnline - Wysłanie faktury

Tokens

  • generate - Wygenerowanie nowego tokena
  • getList - Pobranie listy wygenerowanych tokenów
  • getStatus - Pobranie statusu tokena
  • revoke - Unieważnienie tokena

Error Handling

Handling errors in this SDK should largely match your expectations. All operations return a response object or throw an exception.

By default an API error will raise a Errors\APIException exception, which has the following properties:

Property Type Description
$message string The error message
$statusCode int The HTTP status code
$rawResponse ?\Psr\Http\Message\ResponseInterface The raw HTTP response
$body string The response content

When custom error responses are specified for an operation, the SDK may also throw their associated exception. You can refer to respective Errors tables in SDK docs for more details on possible exception types for each operation. For example, the getCurrentSessions method throws the following exceptions:

Error Type Status Code Content Type
Errors\ExceptionResponse 400 application/json
Errors\TooManyRequestsResponseException 429 application/json
Errors\APIException 4XX, 5XX */*

Example

declare(strict_types=1);

require 'vendor/autoload.php';

use Intermedia\Ksef\Apiv2;
use Intermedia\Ksef\Apiv2\Models\Errors;

$sdk = Apiv2\Client::builder()
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();

try {
    $response = $sdk->auth->getCurrentSessions(
        pageSize: 10
    );

    if ($response->authenticationListResponse !== null) {
        // handle response
    }
} catch (Errors\ExceptionResponseThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\TooManyRequestsResponseExceptionThrowable $e) {
    // handle $e->$container data
    throw $e;
} catch (Errors\APIException $e) {
    // handle default exception
    throw $e;
}

Server Selection

Select Server by Index

You can override the default server globally using the setServerIndex(int $serverIdx) builder method when initializing the SDK client instance. The selected server will then be used as the default on the operations that use it. This table lists the indexes associated with the available servers:

# Server Description
0 https://api-demo.ksef.mf.gov.pl/v2 Środowisko DEMO
1 https://ksef-demo.mf.gov.pl/api/v2 [deprecated] Środowisko DEMO

Example

declare(strict_types=1);

require 'vendor/autoload.php';

use Intermedia\Ksef\Apiv2;

$sdk = Apiv2\Client::builder()
    ->setServerIndex(0)
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();



$response = $sdk->auth->getCurrentSessions(
    pageSize: 10
);

if ($response->authenticationListResponse !== null) {
    // handle response
}

Override Server URL Per-Client

The default server can also be overridden globally using the setServerUrl(string $serverUrl) builder method when initializing the SDK client instance. For example:

declare(strict_types=1);

require 'vendor/autoload.php';

use Intermedia\Ksef\Apiv2;

$sdk = Apiv2\Client::builder()
    ->setServerURL('https://ksef-demo.mf.gov.pl/api/v2')
    ->setSecurity(
        '<YOUR_BEARER_TOKEN_HERE>'
    )
    ->build();



$response = $sdk->auth->getCurrentSessions(
    pageSize: 10
);

if ($response->authenticationListResponse !== null) {
    // handle response
}

Development

Maturity

This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.

Contributions

While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.

SDK Created by Speakeasy

intermedia/ksef-api-v2 适用场景与选型建议

intermedia/ksef-api-v2 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.83k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2025 年 09 月 08 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 intermedia/ksef-api-v2 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 1.83k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 11
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 11
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-09-08