Skip to content

Commit

Permalink
Add posting invoice into the CLI
Browse files Browse the repository at this point in the history
We want to be able to post invoices into the KSeF API and get back
the UPO that proves that the invoice has been accepted by the system.

Here we get the parameters from the CLI and use the requests we prepared
to post the invoice and save the UPO. The example command would look like

go run ./cmd/gobl.ksef send ./test/data/out/invoice-pl-pl.xml 1234567788 624A48824F01935DADE66C83D4874C0EF7AF0529CB5F0F412E6932F189D3864A test
  • Loading branch information
noplisu committed Feb 2, 2024
1 parent fc33468 commit e0e776b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
21 changes: 21 additions & 0 deletions cmd/gobl.ksef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,24 @@ func inputFilename(args []string) string {
}
return ""
}

func inputNip(args []string) string {
if len(args) > 1 && args[1] != "-" {
return args[1]
}
return ""
}

func inputToken(args []string) string {
if len(args) > 2 && args[2] != "-" {
return args[2]
}
return ""
}

func inputEnv(args []string) string {
if len(args) > 3 && args[3] != "-" {
return args[3]
}
return ""
}
1 change: 1 addition & 0 deletions cmd/gobl.ksef/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func (o *rootOpts) cmd() *cobra.Command {
}

cmd.AddCommand(versionCmd())
cmd.AddCommand(send(o).cmd())
cmd.AddCommand(convert(o).cmd())

return cmd
Expand Down
57 changes: 57 additions & 0 deletions cmd/gobl.ksef/send.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package main

import (
"fmt"
"io"
"os"

ksef_api "github.com/invopop/gobl.ksef/api"
"github.com/spf13/cobra"
)

type sendOpts struct {
*rootOpts
}

func send(o *rootOpts) *sendOpts {
return &sendOpts{rootOpts: o}
}

func (c *sendOpts) cmd() *cobra.Command {
cmd := &cobra.Command{
Use: "send [infile] [nip] [token] [env]",
Short: "Send a GOBL JSON to the KSeF API",
RunE: c.runE,
}

return cmd
}

func (c *sendOpts) runE(cmd *cobra.Command, args []string) error {
// ctx := commandContext(cmd)
token := inputToken(args)
env := inputEnv(args)
nip := inputNip(args)

input, err := openInput(cmd, args)
if err != nil {
return err
}
defer func() {
err = input.Close()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}()

data, err := io.ReadAll(input)
if err != nil {
return fmt.Errorf("reading input: %w", err)
}

_, err = ksef_api.SendInvoice(env, nip, token, data)
if err != nil {
return fmt.Errorf("sending invoices: %w", err)
}
return nil
}

0 comments on commit e0e776b

Please sign in to comment.