diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/EInvoiceAdapter.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/EInvoiceAdapter.java index 038a921..97ef610 100644 --- a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/EInvoiceAdapter.java +++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/EInvoiceAdapter.java @@ -120,7 +120,7 @@ public class EInvoiceAdapter implements SignalAdapter { throw new PluginException(EInvoiceAdapter.class.getSimpleName(), CONFIG_ERROR, "missing e-invoice configuration in model event - please check model configuration"); } - ItemCollection createDefinition = XMLParser.parseItemStructure(eInvoiceConfig.getItemValueString("create")); + ItemCollection createDefinition = XMLParser.parseItemStructure(eInvoiceConfig.getItemValueString("CREATE")); try { // Load the e-invoice template.... FileData xmlFileData = loadXMLTemplate(workitem, createDefinition); diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/KSeFExportAdapter.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/KSeFAPIAdapter.java similarity index 52% rename from office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/KSeFExportAdapter.java rename to office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/KSeFAPIAdapter.java index 3ae74df..c4e1ccf 100644 --- a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/KSeFExportAdapter.java +++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/einvoice/KSeFAPIAdapter.java @@ -15,26 +15,33 @@ import com.alexanderlogistics.ksef.api.KSeFAPIService; import jakarta.inject.Inject; /** - * This adapter exports the invoice data to KSeF + * This adapter exports the invoice data to KSeF and can poll the current status + * of an invoice * * To activate debug add the following event definition * *
* {@code
-
- true
-
+
+ true
+
+
+
+
+ true
+
+
* }
*
*
* @version 1.0
* @author rsoika
*/
-public class KSeFExportAdapter implements SignalAdapter {
+public class KSeFAPIAdapter implements SignalAdapter {
public static final String CONFIG_ERROR = "CONFIG_ERROR";
- private static Logger logger = Logger.getLogger(KSeFExportAdapter.class.getName());
+ private static Logger logger = Logger.getLogger(KSeFAPIAdapter.class.getName());
private boolean debug = false;
@Inject
@@ -56,25 +63,33 @@ public class KSeFExportAdapter implements SignalAdapter {
throws AdapterException {
// read debug flag
try {
- List
* {@code
-
+
textblock-ref
filename
true
-
+
+
}
*
*
@@ -92,18 +93,18 @@ public class KSeFAdapter implements SignalAdapter {
logger.info("├── 🔜 Convert Invoice to KSeF...");
// read configuration....
- ItemCollection eInvoiceConfig = workflowService.evalWorkflowResult(event, "e-invoice",
+ ItemCollection eInvoiceConfig = workflowService.evalWorkflowResult(event, "ksef",
workitem,
false);
- if (eInvoiceConfig == null || !eInvoiceConfig.hasItem("ksef")) {
+ if (eInvoiceConfig == null || !eInvoiceConfig.hasItem("CREATE")) {
throw new PluginException(EInvoiceAdapter.class.getSimpleName(), CONFIG_ERROR,
"missing e-invoice/ksef configuration in model event - please check model configuration");
}
- ItemCollection ksefDefinition = XMLParser.parseItemStructure(eInvoiceConfig.getItemValueString("ksef"));
+ ItemCollection ksefCreateDefinition = XMLParser.parseItemStructure(eInvoiceConfig.getItemValueString("CREATE"));
try {
// Load the e-invoice template....
- FileData xmlFileData = loadXMLTemplate(workitem, ksefDefinition);
+ FileData xmlFileData = loadXMLTemplate(workitem, ksefCreateDefinition);
updateEInvoice(xmlFileData, workitem);
diff --git a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/ksef/api/KSeFAPIService.java b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/ksef/api/KSeFAPIService.java
index 27ebd8b..46e20ca 100644
--- a/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/ksef/api/KSeFAPIService.java
+++ b/office-alexander-logistics-app/src/main/java/com/alexanderlogistics/ksef/api/KSeFAPIService.java
@@ -67,7 +67,7 @@ public class KSeFAPIService {
* @return referenceNumber from KSeF
* @throws PluginException
*/
- public String uploadInvoice(ItemCollection workitem, String fileName) throws PluginException {
+ public void uploadInvoice(ItemCollection workitem, String fileName) throws PluginException {
// First open an interactive session (reuses existing if valid)
kseFAuthManager.openSession();
@@ -182,9 +182,27 @@ public class KSeFAPIService {
textlist.add(referenceNumber);
fileData.setAttribute("ksef.referenceNumber", textlist);
+ workitem.setItemValue("ksef.referenceNumber", referenceNumber);
logger.info("├── ✅ Upload successful - Reference: " + referenceNumber);
- return referenceNumber;
+ // Close Session
+ kseFAuthManager.closeInteractiveSession();
+
+ // Status abfragen
+ // Now we need to wait until auth/{AuthRef} will return 200
+ long start = System.currentTimeMillis();
+ long timeout = 20_000; // 20 Sekunden
+ while (System.currentTimeMillis() - start < timeout) {
+ int code = kseFAuthManager.checkSessionStatus();
+
+ if (code >= 200) {
+ break; // ok or borken
+ }
+
+ // kleine Pause, um CPU zu schonen
+ kseFAuthManager.waitSomeTime(500);
+
+ }
} catch (Exception e) {
logger.severe("├── ⚠️ Invoice upload failed: " + e.getMessage());
@@ -194,11 +212,101 @@ public class KSeFAPIService {
} finally {
// finally we close the session
- kseFAuthManager.closeInteractiveSession();
+
kseFAuthManager.deleteCurrentAuthSession();
}
}
+ /**
+ * Poll Invoice Status
+ *
+ * https://ksef-test.mf.gov.pl/docs/v2/index.html#tag/Status-wysylki-i-UPO/paths/~1api~1v2~1sessions~1%7BreferenceNumber%7D/get
+ *
+ * @param workitem
+ * @return
+ * @throws PluginException
+ */
+ private int xxxgetInvoiceStatus(ItemCollection workitem) throws PluginException {
+
+ logger.info("├── 🔎 Get Invoice Status...");
+
+ // Validate session
+ if (kseFAuthManager.getAccessToken() == null) {
+ throw new PluginException(KSeFAuthManager.class.getSimpleName(), ERROR_API,
+ "API Error - missing AccessToken!");
+ }
+ if (kseFAuthManager.getSessionRefNumber() == null) {
+ throw new PluginException(KSeFAuthManager.class.getSimpleName(), ERROR_API,
+ "API Error - missing SessionRefNumber!");
+ }
+ if (kseFAuthManager.getSessionEncryption() == null) {
+ throw new PluginException(KSeFAuthManager.class.getSimpleName(), ERROR_API,
+ "API Error - missing Session Encryption!");
+ }
+
+ try {
+
+ String referenceNumber = workitem.getItemValueString("ksef.referenceNumber");
+ if (referenceNumber.isBlank()) {
+ throw new PluginException(KSeFAuthManager.class.getSimpleName(), ERROR_API,
+ "API Error - missing invoice referenceNumber!");
+ }
+
+ // Calculate hashes
+
+ // Send upload request
+ String uri = kseFAuthManager.getBaseURI() + "/sessions/" + kseFAuthManager.getSessionRefNumber();
+
+ logger.info("│ ├── GET: " + uri);
+
+ HttpRequest request = HttpRequest.newBuilder()
+ .uri(URI.create(uri))
+ .header("Content-Type", "application/json")
+ .header("Authorization", "Bearer " + kseFAuthManager.getAccessToken())
+ .GET()
+ .build();
+
+ HttpResponseThank you for your cooperation.
++
Kind regards
Alexander Global Logistics GmbH