PHP code example of szymon-s292 / ksef-client

1. Go to this page and download the library: Download szymon-s292/ksef-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

szymon-s292 / ksef-client example snippets


use KSeFClient\KsefMode;
use KSeFClient\Auth;

$auth = new Auth(
    "1091041978",   // nip podmiotu
    KsefMode::TEST, // środowisko KSeF
    null,           // token KSeF - puste jeżeli uwierzytelniasz się przez certyfikat
    $ksef_cert,     // certyfikat KSeF 
    $ksef_pkey,     // klucz prywatny KSeF 
    $pkey_pass,     // hasło do klucza prywatnego KSeF 
    false,          // czy używać automatycznego przechowywania tokenów w bazie
);

use KSeFClient\KsefMode;
use KSeFClient\InteractiveSession;

$interactive_session = new InteractiveSession(
    $auth,         // klasa 'Auth' utworzona wcześniej do uwierzytelnienia w KSeF
    KsefMode::TEST // środowisko KSeF
);

use KSeFClient\KsefApi;
use KSeFClient\KsefMode;

$ksef_api = new KsefApi(
    KsefMode::TEST, // środowisko KSeF
);



use KSeFClient\KsefMode;
use KSeFClient\Auth;

// wczytanie certyfikatu i klucza prywatnego
$ksef_cert = file_get_contents(__DIR__ . "/1091041978.crt");
$ksef_pkey = file_get_contents(__DIR__ . "/1091041978.key");
$pkey_pass = "HASLO_KLUCZA_PRYWATNEGO";

// utworzenie klasy z autoryzacją certyfikatem
$auth = new Auth("1091041978", KsefMode::TEST, null, $ksef_cert, $ksef_pkey, $pkey_pass, false);

// wygenerowanie podpisu XAdES, utworzenie sesji i pobranie tokenów autoryzujących
$tokens = $auth->auth_with_xades();

// zwracana wartość
/* 
[
    "access_token" => "",
    "access_token_expires_at" => "",
    "refresh_token" => "",
    "refresh_token_expires_at" => ""
]
*/



use KSeFClient\KsefMode;
use KSeFClient\Auth;

// utworzenie klasy z autoryzacją tokenem
$auth = new Auth("1091041978", KsefMode::TEST, $ksef_token, null, null, null, false);

// autoryzacja tokenem KSeF
$tokens = $auth->auth_with_token();

// zwracana wartość
/* 
[
    "access_token" => "",
    "access_token_expires_at" => "",
    "refresh_token" => "",
    "refresh_token_expires_at" => ""
]
*/

define('KSEF_DB_HOST', 'localhost');
define('KSEF_DB_USER', 'ksef-client');
define('KSEF_DB_PASS', '');
define('KSEF_DB_NAME', 'ksef-client');



use KSeFClient\KsefMode;
use KSeFClient\Auth;

// utworzenie klasy z autoryzacją certyfikatem i automatycznym zapisem/odświeżaniem (ostatni parametr true)
$auth = new Auth("1091041978", KsefMode::TEST, null, $ksef_cert, $ksef_pkey, $pkey_pass, true);

// pobranie tokenów dostępowych z bazy, odświeżenie lub wygenerowanie nowych
$tokens = $auth->get_access_token();

// zwracana wartość
/* 
[
    "access_token" => "",
    "access_token_expires_at" => "",
]
*/



use KSeFClient\KsefMode;
use KSeFClient\Auth;
use KSeFClient\InteractiveSession;

$interactive_session = new InteractiveSession(
    $auth, // wcześniej utworzona klasa 'Auth' do uwierzytelnienia w KSeF
    KsefMode::TEST
);

// zwracany jest numer referencyjny sesji interaktywnej
$session_reference_number = $interactive_session->start_session("FA (3)", "1-0E", "FA"); 

$invoice_reference_number = $interactive_session->send($xml); // wysyłka faktury w formacie XML FA (3) do KSeF w sesji interaktywnej, zwracany jest numer referencyjny faktury w sesji interaktywnej

$interactive_session->close_session();

$ksef_api = new KsefApi(KsefMode::TEST);
$auth = new Auth("1091041978", KsefMode::TEST, null, $ksef_cert, $ksef_pkey, $pkey_pass, false);
$status = $ksef_api->get_invoice_from_session($auth->get_access_token()['access_token'], $session_reference_number, $invoice_reference_number);

// zwracana wartość taka sama jak w [dokumentacji KSeF](https://api.ksef.mf.gov.pl/docs/v2/index.html#tag/Status-wysylki-i-UPO/paths/~1sessions~1%7BreferenceNumber%7D~1invoices~1%7BinvoiceReferenceNumber%7D/get)

/*
[
    "ordinalNumber" => 1,
    "referenceNumber" => "",
    "invoicingDate" => "",
    "upoDownloadUrl" => "",
    "status" => [
        "code" => 200,
        "description" => "",
        "details" => [],
        "extensions" => {}
    ],
]
*/ 

$upo_xml = $ksef_api->download_upo($auth->get_access_token()['access_token'], $status['upoDownloadUrl']);