Skip to content
This repository was archived by the owner on Dec 7, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/jre/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ repositories {
}

dependencies {
implementation 'com.fiskaly.sdk:fiskaly-sdk:1.2.200-jre'
//implementation 'com.fiskaly.sdk:fiskaly-sdk:1.2.200-jre'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this intentionally still in there? If so, maybe clarify the reasoning behind this with another comment.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not certain whether this is the best way to make it use the local version of the SDK (I get the impression that it's possible to use a local Maven repository to do it instead, without changing the dependencies in the gradle file, but I wasn't able to get that working due to inexperience with Maven in general), so I left this line commented out in case it was better to go back to it. If you think that the way I've done it is okay, I will remove the commented-out line.

implementation files('../../build/libs/fiskaly-sdk-1.2.200.jar')
implementation "com.google.code.gson:gson:2.8.6"
implementation "net.iharder:base64:2.3.9"
implementation "net.java.dev.jna:jna:5.5.0"
}

application {
Expand Down
12 changes: 12 additions & 0 deletions examples/jre/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ $ ./gradlew build

## Run example

First, if you want to test the V1 API, set environment variables `FISKALY_API_KEY` and `FISKALY_API_SECRET` to your API key and secret:

```bash
export FISKALY_API_KEY=yourAPIkey
export FISKALY_API_SECRET=yourAPIsecret
```
If you want to test the V2 API, set environment variables `FISKALY_API_KEY_V2` and `FISKALY_API_SECRET_V2` to your API key and secret for V2, in the same way.

If you want to use an existing TSS for V2 instead of creating a new one (e.g. because you have reached the limit of active TSS), also set `FISKALY_TSS_UUID_V2` to the UUID of that TSS. If you do this, you will need to either set `FISKALY_TSS_ADMIN_PIN` to the admin PIN of that TSS, or set `FISKALY_TSS_PUK` to the admin PUK of that TSS. The PUK will only be used if the PIN is empty.

Next, run the sample:

```bash
$ ./gradlew run
```
285 changes: 283 additions & 2 deletions examples/jre/src/main/java/com/fiskaly/sdk/demo/jre/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,294 @@

import com.fiskaly.sdk.*;

import java.io.IOException;
import java.util.Collections;
import java.util.UUID;

import com.fiskaly.sdk.factories.GsonFactory;
import com.google.gson.Gson;

public class Main {
static UUID tssUUID = null;
static UUID clientUUID = null;
static String adminPUK = "";
static FiskalyHttpClient client = null;
static String adminPIN = "0123456789";
static String transactionUUID = "";
static int transactionRevision = 0;
private static final Gson GSON = GsonFactory.createGson();

public static void main(String[] args) throws Exception {
final String apiKey = System.getenv("FISKALY_API_KEY");
final String apiSecret = System.getenv("FISKALY_API_SECRET");
final FiskalyHttpClient client =
new FiskalyHttpClient(apiKey, apiSecret, "https://kassensichv.io/api/v1");
client =
new FiskalyHttpClient(apiKey, apiSecret, "https://kassensichv.io/api/v1");
listTSS();
createTSSV1();
createClient();
createTransactionV1();
finishTransactionV1();

//now do something similar on v2
final String apiKeyv2 = System.getenv("FISKALY_API_KEY_V2");
final String apiSecretv2 = System.getenv("FISKALY_API_SECRET_V2");
//set these to use an existing TSS instead of creating a new one
final String existingTSS = System.getenv("FISKALY_TSS_UUID_V2");
//set either a PIN (if the TSS already has one set) or a PUK (if the TSS doesn't have a PIN set)
final String existingTSSAdminPIN = System.getenv("FISKALY_TSS_ADMIN_PIN");
final String existingTSSPUK = System.getenv("FISKALY_TSS_PUK");
client =
new FiskalyHttpClient(apiKeyv2, apiSecretv2, "https://kassensichv.fiskaly.com/api/v2", "https://kassensichv-middleware.fiskaly.com");
listTSS();
final Boolean useExistingTSS = existingTSS != null && !existingTSS.isEmpty();
if (!useExistingTSS) {
createTSS();
} else {
tssUUID = UUID.fromString(existingTSS);
adminPUK = existingTSSPUK;
System.out.println("Using existing TSS '"+existingTSS+"' with PIN '"+existingTSSAdminPIN+"' and PUK '"+existingTSSPUK+"'");
}
if (!useExistingTSS || existingTSSAdminPIN == null || existingTSSAdminPIN.isEmpty()) {
personalizeTSS();
changeAdminPIN();
} else if (useExistingTSS) {
adminPIN = existingTSSAdminPIN;
}
authenticateAdmin();
initializeTSS();
createClient();
updateClient();
createTransaction();
updateTransaction();
finishTransaction();
//don't disable a TSS we didn't create
if (existingTSS == null) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be existingTss != null or am I reading this wrong?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

existingTSS is a TSS UUID you set with the FISKALY_TSS_UUID_V2 environment variable. If you set that (e.g. because there is some problem creating new TSS so you need to use an existing one; I added this code while it was temporarily not possible to create new TSS), then this code uses that existing TSS instead of creating its own, so it doesn't need to disable it. If you don't set FISKALY_TSS_UUID_V2 then the code creates a TSS at the beginning and disables it at the end. The idea is that after the whole thing runs, there should be the same number of active TSS as before it started.

disableTSS disables the TSS tssUUID, which will either be the UUID from existingTSS, or the UUID of the new TSS that the code created for you because you didn't give it an existing one to use. So it will succeed whether existingTSS is null or not.

disableTSS();
}
}

private static void listTSS() throws IOException, FiskalyHttpException, FiskalyClientException, FiskalyHttpTimeoutException {
final FiskalyHttpResponse response = client.request("GET", "/tss");
System.out.println("List TSS response:");
System.out.println(response);
}

public static void createTSS() throws Exception {
tssUUID = UUID.randomUUID();
final FiskalyHttpResponse response = client.request("PUT", "/tss/" + tssUUID, "{}".getBytes());
System.out.println("Create TSS response:");
System.out.println(response);
final String decodedBody = new String(response.body);
final CreateTSSResponse body = GSON.fromJson(decodedBody, CreateTSSResponse.class);
System.out.println("admin puk = "+body.admin_puk);
adminPUK = body.admin_puk;
}

public static void setTSSState(String state) throws Exception {
final String body = "{ \"state\" : \""+state+"\" }";
System.out.println("Setting TSS state to "+state);
System.out.println(body);
final FiskalyHttpResponse response = client.request("PATCH", "/tss/" + tssUUID, body.getBytes());
System.out.println("Set TSS state response:");
System.out.println(response);
}

public static void personalizeTSS() throws Exception {
setTSSState("UNINITIALIZED");
}

public static void changeAdminPIN() throws Exception {
final String body = "{ \"admin_puk\" : \""+ adminPUK +"\", \"new_admin_pin\" : \""+adminPIN+"\" }";
System.out.println("Setting admin PIN to "+adminPIN);
System.out.println(body);
final FiskalyHttpResponse response = client.request("PATCH", "/tss/" + tssUUID + "/admin", body.getBytes());
System.out.println("Set admin PIN response:");
System.out.println(response);
}

public static void authenticateAdmin() throws Exception {
final String body = "{ \"admin_pin\" : \""+adminPIN+"\" }";
System.out.println("Authenticating admin");
System.out.println(body);
final FiskalyHttpResponse response = client.request("POST", "/tss/" + tssUUID + "/admin/auth", body.getBytes());
System.out.println("Authenticate admin response:");
System.out.println(response);
}

public static void initializeTSS() throws Exception {
setTSSState("INITIALIZED");
}

public static void createClient() throws Exception {
clientUUID = UUID.randomUUID();
final String body = "{ \"serial_number\": \"JRE Test Client Serial\"}";
System.out.println("Creating client");
System.out.println(body);
final FiskalyHttpResponse response = client.request("PUT", "/tss/" + tssUUID + "/client/" + clientUUID, body.getBytes());
System.out.println("Create Client response:");
System.out.println(response);
}

public static void updateClient() throws Exception {
final String body = "{ \"state\": \"REGISTERED\", \"metadata\": {\"custom_field\": \"custom_value\"}}";
System.out.println("Updating client");
System.out.println(body);
final FiskalyHttpResponse response = client.request("PATCH", "/tss/" + tssUUID + "/client/" + clientUUID, body.getBytes());
System.out.println("Update Client response:");
System.out.println(response);
}

public static FiskalyHttpResponse transactionRequest(String body) throws Exception {
transactionRevision++; //transaction revision number starts at 1
return client.request("PUT", "/tss/" + tssUUID + "/tx/" + transactionUUID, body.getBytes(), Collections.singletonMap("tx_revision", transactionRevision));
}

public static void createTransaction() throws Exception {
UUID uuid = UUID.randomUUID();
transactionUUID = uuid.toString();
final String body = "{\"state\": \"ACTIVE\",\n" +
" \"client_id\": \"" + clientUUID + "\"\n" +
" }";
System.out.println("Creating transaction");
System.out.println(body);
transactionRevision = 0;
final FiskalyHttpResponse response = transactionRequest(body);
System.out.println("Create Transaction response:");
System.out.println(response);
}

public static void updateTransaction() throws Exception {
final String body = "{\n" +
" \"schema\": {\n" +
" \"standard_v1\": {\n" +
" \"receipt\": {\n" +
" \"receipt_type\": \"RECEIPT\",\n" +
" \"amounts_per_vat_rate\": [\n" +
" {\n" +
" \"vat_rate\": \"NORMAL\",\n" +
" \"amount\": \"21.42\"\n" +
" }\n" +
" ],\n" +
" \"amounts_per_payment_type\": [\n" +
" {\n" +
" \"payment_type\": \"NON_CASH\",\n" +
" \"amount\": \"21.42\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" },\n" +
" \"state\": \"ACTIVE\",\n" +
" \"client_id\": \"" + clientUUID + "\"\n" +
" }";
System.out.println("Updating transaction");
System.out.println(body);
final FiskalyHttpResponse response = transactionRequest(body);
System.out.println("Update Transaction response:");
System.out.println(response);
}

public static void finishTransaction() throws Exception {
final String body = "{\n" +
" \"schema\": {\n" +
" \"standard_v1\": {\n" +
" \"receipt\": {\n" +
" \"receipt_type\": \"RECEIPT\",\n" +
" \"amounts_per_vat_rate\": [\n" +
" {\n" +
" \"vat_rate\": \"NORMAL\",\n" +
" \"amount\": \"21.42\"\n" +
" }\n" +
" ],\n" +
" \"amounts_per_payment_type\": [\n" +
" {\n" +
" \"payment_type\": \"NON_CASH\",\n" +
" \"amount\": \"21.42\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" }\n" +
" },\n" +
" \"state\": \"FINISHED\",\n" +
" \"client_id\": \"" + clientUUID+ "\"\n" +
" }";
System.out.println("Finishing transaction");
System.out.println(body);
final FiskalyHttpResponse response = transactionRequest(body);
System.out.println("Finish Transaction response:");
System.out.println(response);
}

public static void disableTSS() throws Exception {
setTSSState("DISABLED");
}

///V1 versions
public static void createTSSV1() throws Exception {
tssUUID = UUID.randomUUID();
final String body = "{\n" +
" \"description\": \"JRE Test TSS\",\n" +
" \"state\": \"INITIALIZED\"\n" +
" }";
final FiskalyHttpResponse response = client.request("PUT", "/tss/" + tssUUID, body.getBytes());
System.out.println("Create TSS response:");
System.out.println(response);
}

public static void createTransactionV1() throws Exception {
UUID uuid = UUID.randomUUID();
transactionUUID = uuid.toString();
final String body = "{\"state\": \"ACTIVE\",\n" +
" \"client_id\": \"" + clientUUID +"\"\n" +
" }";
System.out.println("Creating V1 transaction");
System.out.println(body);
transactionRevision = 0;
final FiskalyHttpResponse response = client.request("PUT", "/tss/" + tssUUID + "/tx/" + transactionUUID, body.getBytes());

System.out.println("Create V1 Transaction response:");
System.out.println(response);
}

public static void finishTransactionV1() throws Exception {
final String body = "{\n" +
" \"state\": \"FINISHED\",\n" +
" \"client_id\": \"" + clientUUID + "\",\n" +
" \"schema\": {\n" +
" \"standard_v1\": {\n" +
" \"receipt\": {\n" +
" \"receipt_type\": \"RECEIPT\",\n" +
" \"amounts_per_vat_rate\": [\n" +
" {\"vat_rate\": \"19\", \"amount\": \"14.28\"}\n" +
" ],\n" +
" \"amounts_per_payment_type\": [\n" +
" {\"payment_type\": \"NON_CASH\", \"amount\": \"14.28\"}\n" +
" ]\n" +
" }\n" +
" }\n" +
" }\n" +
" }";
System.out.println("Finishing transaction");
System.out.println(body);
final FiskalyHttpResponse response = client.request("PUT", "/tss/" + tssUUID + "/tx/" + transactionUUID, body.getBytes(), Collections.singletonMap("last_revision", 1));
System.out.println("Finish Transaction response:");
System.out.println(response);
}

}

class CreateTSSResponse {
public final String admin_puk;

public CreateTSSResponse(final String admin_puk) {
this.admin_puk = admin_puk;
}

@Override
public String toString() {
return "Response{"
+ "admin_puk="
+ admin_puk
+ '}';
}
}
3 changes: 2 additions & 1 deletion examples/jre/src/main/resources/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ First, go to https://developer.fiskaly.com/downloads and download the client lib
- Put `com.fiskaly.client-linux-386-vX.Y.Z.so` into `linux-x86/`
- Put `com.fiskaly.client-linux-amd64-vX.Y.Z.so` into `linux-x86-64/`
- Put `com.fiskaly.client-windows-386-vX.Y.Z.so` into `win32-x86/`
- Put `com.fiskaly.client-windiws-amd64-vX.Y.Z.so` into `win32-x86-64/`
- Put `com.fiskaly.client-windows-amd64-vX.Y.Z.so` into `win32-x86-64/`
- Put `com.fiskaly.client-darwin-amd64-vX.Y.Z.dylib` into `darwin/` and rename it to `libcom.fiskaly.client-darwin-amd64-vX.Y.Z.dylib`
23 changes: 19 additions & 4 deletions src/main/java/com/fiskaly/sdk/FiskalyHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ public FiskalyHttpClient(
final String email,
final String password,
final String organizationId,
final String environment)
final String environment,
final URI miceUrl)
throws IOException, FiskalyHttpTimeoutException, FiskalyClientException,
FiskalyHttpException {
final ParamCreateContext params =
new ParamCreateContext(
apiKey, apiSecret, baseUrl, email, password, organizationId, environment);
apiKey, apiSecret, baseUrl, email, password, organizationId, environment, miceUrl);
final JsonRpcRequest request = new JsonRpcRequest("create-context", params);
final JsonRpcResponse<ResultCreateContext> response =
doInvoke(request, ResultCreateContext.class);
Expand All @@ -45,13 +46,20 @@ public FiskalyHttpClient(
final String password)
throws IOException, FiskalyHttpTimeoutException, FiskalyClientException,
FiskalyHttpException {
this(apiKey, apiSecret, baseUrl, email, password, "", "");
this(apiKey, apiSecret, baseUrl, email, password, "", "", null);
}

public FiskalyHttpClient(final String apiKey, final String apiSecret, final URI baseUrl)
throws IOException, FiskalyHttpTimeoutException, FiskalyClientException,
FiskalyHttpException {
this(apiKey, apiSecret, baseUrl, "", "", "", "");
this(apiKey, apiSecret, baseUrl, "", "", "", "", null);
}

public FiskalyHttpClient(
final String apiKey, final String apiSecret, final URI baseUrl, final URI miceUrl)
throws IOException, FiskalyHttpTimeoutException, FiskalyClientException,
FiskalyHttpException {
this(apiKey, apiSecret, baseUrl, "", "", "", "", miceUrl);
}

public FiskalyHttpClient(final String apiKey, final String apiSecret, final String baseUrl)
Expand All @@ -60,6 +68,13 @@ public FiskalyHttpClient(final String apiKey, final String apiSecret, final Stri
this(apiKey, apiSecret, new URI(baseUrl));
}

public FiskalyHttpClient(
final String apiKey, final String apiSecret, final String baseUrl, final String miceUrl)
throws IOException, URISyntaxException, FiskalyHttpException, FiskalyClientException,
FiskalyHttpTimeoutException {
this(apiKey, apiSecret, new URI(baseUrl), new URI(miceUrl));
}

public ResultVersion version()
throws FiskalyHttpException, FiskalyHttpTimeoutException, FiskalyClientException,
IOException {
Expand Down
Loading