This repository has been archived by the owner on Feb 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
efakturas.go
225 lines (184 loc) · 5.29 KB
/
efakturas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package sbanken
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"github.com/engvik/sbanken-go/internal/transport"
)
// Efaktura represents an efaktura.
// Sbanken API documentation: https://publicapi.sbanken.no/openapi/apibeta/index.html#/Efaktura
type Efaktura struct {
ID string `json:"eFakturaId"`
IssuerID string `json:"issuerId"`
Reference string `json:"eFakturaReference"`
DocumentType string `json:"documentType"`
Status string `json:"status"`
KID string `json:"kid"`
OriginalDueDate string `json:"originalDueDate"`
UpdatedDueDate string `json:"updatedDueDate"`
NotificationDate string `json:"notificationDate"`
IssuerName string `json:"issuerName"`
CreditAccountNumber string `json:"creditAccountNumber"`
OriginalAmount float32 `json:"originalAmount"`
UpdatedAmount float32 `json:"updatedAmount"`
MinimumAmount float32 `json:"minimumAmount"`
}
// EfakturaListQuery represents query parameters for querying efakturas.
type EfakturaListQuery struct {
StartDate time.Time
EndDate time.Time
Status string
Index string
Length string
}
// QueryString translates the query into a query string.
func (q *EfakturaListQuery) QueryString(u string) (string, error) {
parsedURL, err := url.Parse(u)
if err != nil {
return u, err
}
query := parsedURL.Query()
if !q.StartDate.IsZero() {
query.Add("startDate", q.StartDate.Format(time.RFC3339))
}
if !q.EndDate.IsZero() {
query.Add("endDate", q.EndDate.Format(time.RFC3339))
}
if q.Status != "" {
query.Add("status", q.Status)
}
if q.Index != "" {
query.Add("index", q.Index)
}
if q.Length != "" {
query.Add("length", q.Length)
}
return query.Encode(), nil
}
// EfakturaPayQuery represents a payment query.
type EfakturaPayQuery struct {
ID string `json:"eFakturaId"`
AccountID string `json:"accountId"`
PayOnlyMinimumAmount bool `json:"PayOnlyMinimumAmount"`
}
// ListEfakturas lists efakturas.
func (c *Client) ListEfakturas(ctx context.Context, q *EfakturaListQuery) ([]Efaktura, error) {
url := fmt.Sprintf("%s/v2/Efaktura", c.bankBaseURL)
return c.listEfakturas(ctx, url, q, "ListEfakturas")
}
// PayEfaktura pays an efaktura. The EfakturaPayQuery are required.
func (c *Client) PayEfaktura(ctx context.Context, q *EfakturaPayQuery) error {
if q == nil {
return ErrMissingEfakturaPayQuery
}
payload, err := json.Marshal(q)
if err != nil {
return fmt.Errorf("Marshal: %w", err)
}
url := fmt.Sprintf("%s/v2/Efaktura", c.bankBaseURL)
res, sc, err := c.transport.Request(ctx, &transport.HTTPRequest{
Method: http.MethodPost,
URL: url,
PostPayload: payload,
})
if err != nil {
return fmt.Errorf("request: %w", err)
}
var data transport.HTTPResponse
if err := json.Unmarshal(res, &data); err != nil {
return fmt.Errorf("Unmarshal: %w", err)
}
if data.IsError || sc != http.StatusOK {
return &Error{
"PayEfaktura",
data.ErrorType,
data.ErrorMessage,
data.ErrorCode,
sc,
}
}
return nil
}
// ListNewEfakturas lists efakturas that have not yet been processed by the customer.
func (c *Client) ListNewEfakturas(ctx context.Context, q *EfakturaListQuery) ([]Efaktura, error) {
url := fmt.Sprintf("%s/v2/Efaktura/new", c.bankBaseURL)
if q != nil {
if !q.StartDate.IsZero() {
return nil, ErrNotValidOptionStartDate
}
if !q.EndDate.IsZero() {
return nil, ErrNotValidOptionEndDate
}
if q.Status != "" {
return nil, ErrNotValidOptionStatus
}
}
return c.listEfakturas(ctx, url, q, "ListNewEfakturas")
}
// ReadEfaktura reads an efaktura. The efakturaID are required.
func (c *Client) ReadEfaktura(ctx context.Context, efakturaID string) (Efaktura, error) {
if efakturaID == "" {
return Efaktura{}, ErrMissingEfakturaID
}
url := fmt.Sprintf("%s/v2/Efaktura/%s", c.bankBaseURL, efakturaID)
res, sc, err := c.transport.Request(ctx, &transport.HTTPRequest{
Method: http.MethodGet,
URL: url,
})
if err != nil {
return Efaktura{}, fmt.Errorf("request: %w", err)
}
data := struct {
Efaktura Efaktura `json:"item"`
transport.HTTPResponse
}{}
if err := json.Unmarshal(res, &data); err != nil {
return data.Efaktura, fmt.Errorf("Unmarshal: %w", err)
}
if data.IsError || sc != http.StatusOK {
return data.Efaktura, &Error{
"ReadEfaktura",
data.ErrorType,
data.ErrorMessage,
data.ErrorCode,
sc,
}
}
return data.Efaktura, nil
}
func (c *Client) listEfakturas(ctx context.Context, url string, q *EfakturaListQuery, caller string) ([]Efaktura, error) {
if q != nil {
qs, err := q.QueryString(url)
if err != nil {
return nil, fmt.Errorf("QueryString: %w", err)
}
url = fmt.Sprintf("%s?%s", url, qs)
}
res, sc, err := c.transport.Request(ctx, &transport.HTTPRequest{
Method: http.MethodGet,
URL: url,
})
if err != nil {
return nil, fmt.Errorf("request: %w", err)
}
data := struct {
Efakturas []Efaktura `json:"items"`
transport.HTTPResponse
}{}
if err := json.Unmarshal(res, &data); err != nil {
return data.Efakturas, fmt.Errorf("Unmarshal: %w", err)
}
if data.IsError || sc != http.StatusOK {
return data.Efakturas, &Error{
caller,
data.ErrorType,
data.ErrorMessage,
data.ErrorCode,
sc,
}
}
return data.Efakturas, nil
}