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
/
Copy pathstanding_orders.go
66 lines (56 loc) · 1.99 KB
/
standing_orders.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
package sbanken
import (
"context"
"encoding/json"
"fmt"
"net/http"
"github.com/engvik/sbanken-go/internal/transport"
)
// StandingOrder represents a standing order (repeated transfers and payments).
// Sbanken API documentation: https://publicapi.sbanken.no/openapi/apibeta/index.html#/StandingOrders
type StandingOrder struct {
FreeTerms []string `json:"freeTerms"`
BeneficiaryName string `json:"beneficiaryName"`
CID string `json:"cId"`
CreditAccountNumber string `json:"creditAccountNumber"`
DebitAccountNumber string `json:"debitAccountNumber"`
Frequency string `json:"frequency"`
LastPaymentDate string `json:"lastPaymentDate"`
NextDueDate string `json:"nextDueDate"`
StandingOrderEndDate string `json:"standingOrderEndDate"`
StandingOrderStartDate string `json:"standingOrderStartDate"`
StandingOrderType string `json:"standingOrderType"`
Amount float32 `json:"amount"`
StandingOrderID int `json:"standingOrderId"`
}
// ListStandingOrders lists the standing orders for repeated transfers and payments. The accoundID are required.
func (c *Client) ListStandingOrders(ctx context.Context, accountID string) ([]StandingOrder, error) {
if accountID == "" {
return nil, ErrMissingAccountID
}
url := fmt.Sprintf("%s/v2/StandingOrders/%s", c.bankBaseURL, accountID)
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 {
StandingOrders []StandingOrder `json:"items"`
transport.HTTPResponse
}{}
if err := json.Unmarshal(res, &data); err != nil {
return data.StandingOrders, fmt.Errorf("Unmarshal: %w", err)
}
if data.IsError || sc != http.StatusOK {
return data.StandingOrders, &Error{
"ListStandingOrders",
data.ErrorType,
data.ErrorMessage,
data.ErrorCode,
sc,
}
}
return data.StandingOrders, nil
}