Hej,
Integruje sie z KSEF w php.
Problem w tym, ze jak sprawdzam status sesji, to dostaje 415 Błąd odszyfrowania dostarczonego klucza. Wiem, ze sa dwa certyfikaty do tokena i symmetricKey i uzywam tego od symmetricKey, ale nie wiem juz kompletnie co robie nie tak. Zawiesiłem się i może ktoś pomoże.
Poniżej fragment funkcji, ktorych uzywam.
public function getSymmetricKey(): array{
if(!empty($this->symmetricKey) && !empty($this->encryptedSymmetricKeyBase64)){
return [
'symmetricKey' => $this->symmetricKey,
'encryptedSymmetricKeyBase64' => $this->encryptedSymmetricKeyBase64,
];
}
$symmetricKey = $this->symmetricKey = random_bytes(32); // 256 bitów
$certPem = file_get_contents(Storage::path($this->publicCertSymmetricPath));
$cert = openssl_x509_read($certPem);
$publicKeyPem = openssl_pkey_get_public($cert);
$encrypted = '';
$ok = openssl_public_encrypt(
$symmetricKey,
$encrypted,
$publicKeyPem,
OPENSSL_PKCS1_OAEP_PADDING
);
if (!$ok) {
throw new \Exception('Nie udało się zaszyfrować symmetric key: ' . openssl_error_string());
}
$this->encryptedSymmetricKeyBase64 = base64_encode($encrypted);
return [
'symmetricKey' => $this->symmetricKey,
'encryptedSymmetricKeyBase64' => $this->encryptedSymmetricKeyBase64,
];
}
public function getInitVector(): array{
if(!empty($this->initVector) && !empty($this->initVectorBase64)){
return [
'initVector'=>$this->initVector,
'initVectorBase64'=>$this->initVectorBase64,
];
}
$iv = $this->initVector = random_bytes(16);
$this->initVectorBase64 = base64_encode($iv);
return [
'initVector'=>$this->initVector,
'initVectorBase64'=>$this->initVectorBase64,
];
}
public function startSession(){
$symetricData = $this->getSymmetricKey();
$vectorData = $this->getInitVector();
$inputData = [
'formCode'=>[
'systemCode'=>"FA (3)",
'schemaVersion'=> "1-0E",
'value'=>"FA"
],
'encryption'=>[
'encryptedSymmetricKey'=>$symetricData['encryptedSymmetricKeyBase64'],
'initializationVector'=>$vectorData['initVectorBase64']
]
];
$response = $this->http->post('sessions/online', [
'headers' => [
'Authorization' => 'Bearer ' . $this->accessToken,
'Accept' => 'application/json',
'Content-Type' => 'application/json',
],
'json' => $inputData,
]);
$data = json_decode($response->getBody()->getContents(), true);
$this->sessionReferenceNumber = $data['referenceNumber'] ?? null;
$this->sessionValidUntil = $data['validUntil'] ?? null;
sleep(1);
$body = $this->getSessionStatus();
if(!isset($body['status']) || !is_array($body['status'])) {
throw new RuntimeException('Blad pobierania statusu sesji');
}
if($body['status']['code']==100){
sleep(2);
$body = $this->getSessionStatus();
}
if($body['status']['code']!=200) {
dd($body);
throw new RuntimeException('Bledny status sesji: '. $body['status']['code'].', '.$body['status']['description']);
}
}
public function getSessionStatus(): array
{
try {
$response = $this->http->get(
'sessions/' . $this->sessionReferenceNumber,
[
'headers' => [
'Authorization' => 'Bearer ' . $this->accessToken,
'Accept' => 'application/json',
],
]
);
return json_decode(
$response->getBody()->getContents(),
true,
512,
JSON_THROW_ON_ERROR
);
} catch (RequestException $e) {
if ($e->hasResponse()) {
throw new RuntimeException(
'KSeF session status error ' .
$e->getResponse()->getStatusCode() .
': ' .
$e->getResponse()->getBody()->getContents(),
$e->getResponse()->getStatusCode(),
$e
);
}
throw new RuntimeException(
'Błąd komunikacji z KSeF: ' . $e->getMessage(),
0,
$e
);
}
}