@extends('layouts.app') @section('title', 'KSeF – Auth & Faktury') @php // dane z uwierzytelniania (kroki 1-3) $result = $result ?? session('ksef_result') ?? null; $error = $error ?? session('ksef_error') ?? null; // dane z wysyłki faktury / UPO (kroki 4-5) $invoiceResult = $invoiceResult ?? session('ksef_invoice_result') ?? null; $invoiceError = $invoiceError ?? session('ksef_invoice_error') ?? null; // lista plików faktur (może być ["FA1.xml","FA2.xml"] albo [["name"=>"FA1.xml"], ...]) $invoiceFiles = $invoiceFiles ?? []; // batch summary (z sendAllInvoices) $batch = (is_array($invoiceResult) && isset($invoiceResult['batch'])) ? $invoiceResult['batch'] : null; // helper do nazwy pliku niezależnie od formatu $fileNameOf = function($file) { if (is_array($file)) return $file['name'] ?? ($file['filename'] ?? ''); return is_string($file) ? basename($file) : ''; }; @endphp @section('content')

KSeF – kroki 1–5

1) challenge → 2) unsigned AuthTokenRequest.xml → 3) podpis XAdES + redeem tokenów → 4) wysyłka faktury XML (szyfrowanie AES/RSA, sesja online) → 5) pobranie UPO dla wysłanej faktury.

{{-- Błąd auth --}} @if (!empty($error))
Błąd (auth): {{ $error }}
@endif {{-- Błąd faktury / UPO --}} @if (!empty($invoiceError))
Błąd (faktura): {{ $invoiceError }}
@endif {{-- ====================== TRYB WSADOWY: Wyślij WSZYSTKIE ====================== --}}

Tryb wsadowy – Wyślij WSZYSTKIE faktury

Uruchamia pełny proces dla każdej faktury z storage/faktury_xml: challenge → unsigned → podpis XAdES → auth+redeem → wysyłka → UPO → zapis → przeniesienie do wyslane/.

@csrf
{{-- Podsumowanie batcha --}} @if ($batch)
Przetworzono: {{ $batch['processed'] ?? 0 }} Sukcesy: {{ isset($batch['ok']) ? count($batch['ok']) : 0 }} Błędy: {{ isset($batch['errors']) ? count($batch['errors']) : 0 }}
@if (!empty($invoiceResult['message']))

{{ $invoiceResult['message'] }}

@endif @if (!empty($batch['ok']))
Wysłane poprawnie
@foreach ($batch['ok'] as $ok) @endforeach
Plik Session ref Invoice ref Ścieżka UPO Przeniesiono do
{{ $ok['file'] ?? '' }} {{ $ok['sessionReference'] ?? '' }} {{ $ok['invoiceReference'] ?? '' }} {{ $ok['upoPath'] ?? '' }} {{ $ok['movedTo'] ?? '' }}
@endif @if (!empty($batch['errors']))
Błędy
@foreach ($batch['errors'] as $err) @endforeach
Plik Komunikat Trace (dev)
{{ $err['file'] ?? '' }} {{ $err['error'] ?? '' }} @if (!empty($err['trace']))
pokaż
{{ $err['trace'] }}
@else @endif
@endif @endif
{{-- ====================== KROK 1: challenge + unsigned XML ====================== --}}

Krok 1 – Challenge i utworzenie AuthTokenRequest (unsigned)

Ten krok:
- pobiera challenge z KSeF,
- buduje AuthTokenRequest XML na podstawie konfiguracji zapisanej w systemie,
- zapisuje plik unsigned XML do storage/app.

@csrf
{{-- PODGLĄD unsigned XML --}} @if (!empty($result) && (isset($result['xml']) || isset($result['storagePath'])))
Wynik kroku 1 (unsigned XML)
@if(isset($result['challenge']))
Challenge
{{ $result['challenge'] }}
@endif @if(isset($result['timestamp']))
Timestamp (ms)
{{ $result['timestamp'] }} (challenge ważny ~10 min)
@endif
@if(isset($result['xml']))
AuthTokenRequest (unsigned)
{{ $result['xml'] }}
@endif
@if(isset($result['storagePath'])) {{-- Pobierz unsigned plik --}}
@csrf
{{-- KROK 2: Podpisz XAdES --}}
@csrf
@endif
@endif {{-- ====================== KROK 2: podpisany XML ====================== --}} @if (!empty($result) && isset($result['signedPath']))
Krok 2 – dokument podpisany XAdES
Ścieżka (storage)
{{ $result['signedPath'] }}
@if(isset($result['signedXml']))
AuthTokenRequest (XAdES signed)
{{ $result['signedXml'] }}
@endif
{{-- KROK 3: Wyślij podpisany XML do KSeF i zrób redeem --}}
@csrf
{{-- pobierz podpisany plik --}}
@csrf
@endif {{-- ====================== KROK 3: tokeny z redeem ====================== --}} @if (!empty($result) && (isset($result['authenticationToken']) || isset($result['referenceNumber'])))
Krok 3 – Wynik uwierzytelnienia (tokeny)
@if(isset($result['referenceNumber']))
referenceNumber
{{ $result['referenceNumber'] }}
@endif @if(isset($result['authenticationToken']))
authenticationToken
{{ $result['authenticationToken'] }}
@endif @if(isset($result['authenticationTokenValidUntil']))
auth token validUntil
{{ $result['authenticationTokenValidUntil'] }}
@endif @if(isset($result['accessToken']))
accessToken
{{ $result['accessToken'] }}
@endif @if(isset($result['accessTokenValidUntil']))
accessToken validUntil
{{ $result['accessTokenValidUntil'] }}
@endif @if(isset($result['refreshToken']))
refreshToken
{{ $result['refreshToken'] }}
@endif @if(isset($result['refreshTokenValidUntil']))
refreshToken validUntil
{{ $result['refreshTokenValidUntil'] }}
@endif
@if(isset($result['raw']))
Pełna odpowiedź KSeF (JSON)
{{ json_encode($result['raw'], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}
@endif
@endif {{-- ====================== KROK 4: wysyłka faktury do KSeF ====================== --}}

Krok 4 – Wyślij fakturę XML do KSeF

Używamy accessToken z kroku 3. Tworzymy sesję online w KSeF, szyfrujemy fakturę AES-256, klucz AES szyfrujemy RSA/OAEP kluczem MF, wrzucamy fakturę i zamykamy sesję.

{{-- Formularz wyboru faktury --}}
@csrf
@if (empty($invoiceFiles))
Brak plików w katalogu z fakturami.
@endif
{{-- Wynik wysyłki pojedynczej faktury --}} @if (!empty($invoiceResult) && isset($invoiceResult['result'])) @php $r = $invoiceResult['result']; $sessionRefForUpo = $r['sessionReferenceNumber'] ?? null; $invoiceRefForUpo = $r['detailsSend']['json']['invoiceReferenceNumber'] ?? $r['detailsSend']['json']['referenceNumber'] ?? $r['detailsSend']['json']['elementReferenceNumber'] ?? null; @endphp
Odpowiedź z KSeF dla faktury {{ $invoiceResult['file'] ?? '' }}
@if(isset($r['sessionReferenceNumber']))
sessionReferenceNumber
{{ $r['sessionReferenceNumber'] }}
@endif @if(isset($r['statusOpen']))
statusOpen (open session)
{{ $r['statusOpen'] }}
@endif @if(isset($r['statusSend']))
statusSend (send invoice)
{{ $r['statusSend'] }}
@endif @if(isset($r['statusClose']))
statusClose (close session)
{{ $r['statusClose'] }}
@endif
Szczegóły wysyłki (JSON)
{{ json_encode($r, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}
{{-- ====================== KROK 5: Pobranie UPO ====================== --}}
Krok 5 – Pobierz UPO dla tej faktury

UPO jest dostępne pod /api/v2/sessions/{referenceNumber}/invoices/{invoiceReferenceNumber}/upo.

@if($sessionRefForUpo && $invoiceRefForUpo)
@csrf
@else
Nie udało się automatycznie ustalić numeru sesji lub numeru faktury. Wprowadź je ręcznie (z odpowiedzi wysyłki) żeby pobrać UPO.
@csrf
@endif @endif
{{-- ====================== PODGLĄD UPO XML ====================== --}} @if (!empty($invoiceResult) && isset($invoiceResult['upoXml']))
UPO faktury (XML z KSeF)
@if(isset($invoiceResult['upoSessionReferenceNumber']))
Session ref
{{ $invoiceResult['upoSessionReferenceNumber'] }}
@endif @if(isset($invoiceResult['upoInvoiceReferenceNumber']))
Invoice ref
{{ $invoiceResult['upoInvoiceReferenceNumber'] }}
@endif
{{ $invoiceResult['upoXml'] }}
@endif
@endsection