/ksef-java-rest-client

Krajowy System eFaktur Java Rest API

Primary LanguageJavaApache License 2.0Apache-2.0

Sonarcloud Status Renovate enabled Maven Central

KSeF java REST client

KSeF

  • API version: 1.0.0
    • Build date: 2022-01-15

Krajowy Systemu e-Faktur

Projekt na bardzo wczesnym etapie rozwoju. Status: RC1.

Celem projektu jest stworzenie elastycznego klienta API KSeF na platformę Java, z wykorzystaniem popularnych bibliotek wywołań http i serializacji JSON. Obecnie zaimplementowany jest tylko klient OkHttp OkHttpApiClient i serializer Gson GsonJsonSerializer.

Pomoc w rozwoju projektu jest bardzo mile widziana.

Przykładowe żądania

Zależności projektowe

<dependencies>
      <dependency>
        <groupId>io.alapierre.ksef</groupId>
        <artifactId>ksef-client-okhttp</artifactId>
        <version>${project.version}</version>
      </dependency>
    
      <dependency>
        <groupId>io.alapierre.ksef</groupId>
        <artifactId>ksef-gson-serializer</artifactId>
        <version>${project.version}</version>
      </dependency>
    
      <dependency>
        <groupId>io.alapierre.ksef</groupId>
        <artifactId>ksef-api</artifactId>
        <version>${project.version}</version>
      </dependency>
    
      <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
      </dependency>
    
      <dependency>
        <groupId>io.alapierre.crypto</groupId>
        <artifactId>digital-signature</artifactId>
        <version>1.0</version>
      </dependency>
    
      <dependency>
        <groupId>io.alapierre.ksef</groupId>
        <artifactId>ksef-xml-model</artifactId>
        <version>${project.version}</version>
      </dependency>
    
</dependencies>

Pobranie wyzwania autoryzacyjnego, autoryzacja podpisem i wysłanie faktury

package io.alapierre.ksef.sample;

import eu.europa.esig.dss.model.DSSDocument;
import io.alapierre.crypto.dss.signer.P12Signer;
import io.alapierre.ksef.client.ApiClient;
import io.alapierre.ksef.client.ApiException;
import io.alapierre.ksef.client.JsonSerializer;
import io.alapierre.ksef.client.api.InterfejsyInteraktywneFakturaApi;
import io.alapierre.ksef.client.api.InterfejsyInteraktywneSesjaApi;
import io.alapierre.ksef.client.model.rest.auth.AuthorisationChallengeRequest;
import io.alapierre.ksef.client.okhttp.OkHttpApiClient;
import io.alapierre.ksef.client.serializer.gson.GsonJsonSerializer;
import io.alapierre.ksef.xml.model.AuthRequestUtil;
import lombok.val;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.security.KeyStore;

public class Main {

  public static final String NIP_FIRMY = "NIP firmy";
  private static final  File tokenFile = new File("token.p12");
  private static final  KeyStore.PasswordProtection pas = new KeyStore.PasswordProtection("_____token_password_____".toCharArray());

  public static void main(String[] args)  {

    try {

      JsonSerializer serializer = new GsonJsonSerializer();
      ApiClient client = new OkHttpApiClient(serializer);

      InterfejsyInteraktywneSesjaApi sesjaApi = new InterfejsyInteraktywneSesjaApi(client);

      val challenge = sesjaApi.authorisationChallengeCall(NIP_FIRMY, AuthorisationChallengeRequest.IdentifierType.onip);

      System.out.println(challenge);

      val auth = AuthRequestUtil.prepareAuthRequest(challenge.getChallenge(), NIP_FIRMY);
      val toSigned = AuthRequestUtil.requestToBytes(auth);

      // podpis elektroniczny XML
      ByteArrayOutputStream signed = signRequest(toSigned);

      val signedResponse = sesjaApi.initSessionSignedCall(signed.toByteArray());

      // signedResponse.getSessionToken() zawiera token sesyjny

      val invoiceApi = new InterfejsyInteraktywneFakturaApi(client);
      invoiceApi.invoiceSend(new File("FA1.xml"), signedResponse.getSessionToken().getToken());

    } catch (ApiException ex) {
      System.out.printf("Błąd wywołania API %d (%s) opis błędu %s", ex.getCode(), ex.getMessage(),  ex.getResponseBody());
    } catch (IOException e) {
      System.out.println(e.getMessage());
    }
  }

  public static ByteArrayOutputStream signRequest(byte[] toSigned) throws IOException {

    val signer = new P12Signer(pas, tokenFile);

    ByteArrayInputStream is = new ByteArrayInputStream(toSigned);
    DSSDocument signedDocument = signer.sign(is);

    ByteArrayOutputStream signed = new ByteArrayOutputStream();
    signedDocument.writeTo(signed);

    return signed;
  }
}

Biblioteka ułatwiająca wykonanie podpisu elektronicznego XADES na karcie kryptograficznej lub za pomocą tokena w formacie PKCS#12:

<dependency>
  <groupId>io.alapierre.crypto</groupId>
  <artifactId>digital-signature</artifactId>
  <version>1.0</version>
</dependency>

Build Requirements

Building the API client library requires:

  1. Java 1.8+ and Java 11 (to build ksef-client-jdk11-http-client)
  2. Maven
  3. Maven configured to build multi JDK projects

Configuring maven to build multi JDK projects

put toolchain.xml file in your ${user.home}/.m2:

<?xml version="1.0" encoding="UTF-8"?>
<toolchains>
    <!-- JDK toolchains -->
    <toolchain>
        <type>jdk</type>
        <provides>
            <version>1.8</version>
            <vendor>openjdk</vendor>
        </provides>
        <configuration>
            <jdkHome>path to jdk 1.8</jdkHome>
        </configuration>
    </toolchain>
    <toolchain>
        <type>jdk</type>
        <provides>
            <version>11</version>
            <vendor>openjdk</vendor>
        </provides>
        <configuration>
            <jdkHome>path to jdk 11</jdkHome>
        </configuration>
    </toolchain>
</toolchains>