Inspired by ksef-java-rest-client
project, based on Resty, KSeF API client for Go.
Very early project status
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"go-ksef/ksef/api"
"go-ksef/ksef/model"
"go-ksef/ksef/util"
)
func main() {
client := api.New(api.Test)
session := api.NewSessionService(client)
sessionToken, err := session.LoginByToken(
util.GetEnvOrFailed("KSEF_NIP"),
model.ONIP,
util.GetEnvOrFailed("KSEF_TOKEN"),
"data/mfkeys/test/publicKey.pem")
if err != nil {
re, ok := err.(*api.RequestError)
if ok {
log.Errorf("request error %d responce body %s", re.StatusCode, re.Body)
}
panic(err)
}
fmt.Printf("session token: %s\n", sessionToken.SessionToken.Token)
}
The only difference is to create session object with NewSessionServiceWithEncryption and pass initialized AES cipher
aes, err := cipher.AesWithRandomKey(32)
if err != nil {
panic("can't prepare AES Encryptor: " + err)
}
sessionEncrypted := NewSessionServiceWithEncryption(apiClient, aes)
Important: When session is open with encryption, all invoices have to be sent encrypted, and all incoming invoices will be encrypted with AES key given on init session call.
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"go-ksef/ksef/api"
"go-ksef/ksef/model"
"go-ksef/ksef/util"
"os"
)
func main() {
content, err := os.ReadFile("../../data/testing/faktura1.xml")
if err != nil {
panic(err)
}
client := api.New(api.Test)
invoiceService := api.NewInvoiceService(client)
sendInvoiceResp, err := invoiceService.SendInvoice(content, "_sessionToken_")
if err != nil {
panic(err)
}
fmt.Printf("Invoice responce: %#v", *sendInvoiceResp)
}
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"go-ksef/ksef/api"
"go-ksef/ksef/cipher"
"go-ksef/ksef/model"
"go-ksef/ksef/util"
"os"
)
func main() {
content, err := os.ReadFile("../../data/testing/faktura1.xml")
if err != nil {
panic(err)
}
aes, err := cipher.AesWithRandomKey(32)
client := api.New(api.Test)
invoiceService := api.NewInvoiceService(client)
sessionEncrypted := api.NewSessionServiceWithEncryption(client, aes)
sessionToken, err := sessionEncrypted.LoginByToken(
util.GetEnvOrFailed("KSEF_NIP"),
model.ONIP,
util.GetEnvOrFailed("KSEF_TOKEN"),
"../../data/mfkeys/test/publicKey.pem")
if err != nil {
panic(err)
}
fmt.Printf("session token: %s\n", sessionToken.SessionToken.Token)
sendInvoiceResp, err := invoiceService.EncryptAndSend(content, aes, sessionToken.SessionToken.Token)
if err != nil {
panic(err)
}
fmt.Printf("Invoice responce: %#v", *sendInvoiceResp)
}
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"go-ksef/ksef/api"
"go-ksef/ksef/cipher"
"go-ksef/ksef/model"
"go-ksef/ksef/util"
"os"
)
func main() {
client := api.New(api.Test)
invoiceService := api.NewInvoiceService(client)
session := api.NewSessionService(client)
status, err := session.Terminate("__session_token__")
if err != nil {
panic(err)
}
fmt.Printf("%#v", *status)
upo, err := invoiceService.GetUpo("__session_reference_number__")
if err != nil {
panic(err)
}
fmt.Printf("ReferenceNumber: %s, ProcessingCode: %d, ProcessingDescription: %s\n", upo.ReferenceNumber, upo.ProcessingCode, upo.ProcessingDescription)
err = os.WriteFile("../../upo.xml", upo.Upo, 0644)
if err != nil {
panic(err)
}
}
package main
import (
"fmt"
"go-ksef/ksef/api"
"go-ksef/ksef/model"
"go-ksef/ksef/util"
)
func main() {
client := api.New(api.Test)
session := api.NewSessionService(client)
challenge, err := session.AuthorisationChallenge(util.GetEnvOrFailed("KSEF_NIP"), model.ONIP)
if err != nil {
panic(err)
}
fmt.Printf("res: %#v\n", challenge)
}
To enable log debug just set KSEF_DEBUG=true
environment variable.
To enable http trace info, just set KSEF_HTTP_TRACE=true
environment variable
Tests require some environment variables to run:
KSEF_NIP
organization tax identifierKSEF_TOKEN
authorisation token to KSeF test environment