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 pathpayments.go
145 lines (119 loc) · 3.46 KB
/
payments.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
package sbanken
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"github.com/engvik/sbanken-go/internal/transport"
)
// Payment represents a payment.
// Sbanken API documentation: https://publicapi.sbanken.no/openapi/apibeta/index.html#/Payments
type Payment struct {
AllowedNewStatusTypes []string `json:"allowedNewStatusTypes"`
ID string `json:"paymentId"`
RecipientAccountNumber string `json:"recipientAccountNumber"`
DueDate string `json:"dueDate"`
KID string `json:"kid"`
Text string `json:"text"`
Status string `json:"status"`
StatusDetails string `json:"statusDetails"`
ProductType string `json:"productType"`
PaymentType string `json:"paymentType"`
BeneficiaryName string `json:"beneficiaryName"`
Amount float32 `json:"amount"`
PaymentNumber int `json:"paymentNumber"`
IsActive bool `json:"isActive"`
}
// PaymentListQuery represents query parameters for querying payments.
type PaymentListQuery struct {
Index string
Length string
}
// QueryString translates the query into a query string.
func (q *PaymentListQuery) QueryString(u string) (string, error) {
parsedURL, err := url.Parse(u)
if err != nil {
return u, err
}
query := parsedURL.Query()
if q.Index != "" {
query.Add("index", q.Index)
}
if q.Length != "" {
query.Add("length", q.Length)
}
return query.Encode(), nil
}
// ListPayments list the payments. The accountID are required.
func (c *Client) ListPayments(ctx context.Context, accountID string, q *PaymentListQuery) ([]Payment, error) {
if accountID == "" {
return nil, ErrMissingAccountID
}
url := fmt.Sprintf("%s/v2/Payments/%s", c.bankBaseURL, accountID)
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 {
Payments []Payment `json:"items"`
transport.HTTPResponse
}{}
if err := json.Unmarshal(res, &data); err != nil {
return data.Payments, fmt.Errorf("Unmarshal: %w", err)
}
if data.IsError || sc != http.StatusOK {
return data.Payments, &Error{
"ListPayments",
data.ErrorType,
data.ErrorMessage,
data.ErrorCode,
sc,
}
}
return data.Payments, nil
}
// ReadPayment reads a payment. The accountID and paymentID are required.
func (c *Client) ReadPayment(ctx context.Context, accountID string, paymentID string) (Payment, error) {
if accountID == "" {
return Payment{}, ErrMissingAccountID
}
if paymentID == "" {
return Payment{}, ErrMissingPaymentID
}
url := fmt.Sprintf("%s/v2/Payments/%s/%s", c.bankBaseURL, accountID, paymentID)
res, sc, err := c.transport.Request(ctx, &transport.HTTPRequest{
Method: http.MethodGet,
URL: url,
})
if err != nil {
return Payment{}, fmt.Errorf("request: %w", err)
}
data := struct {
Payment Payment `json:"item"`
transport.HTTPResponse
}{}
if err := json.Unmarshal(res, &data); err != nil {
return data.Payment, fmt.Errorf("Unmarshal: %w", err)
}
if data.IsError || sc != http.StatusOK {
return data.Payment, &Error{
"ReadPayment",
data.ErrorType,
data.ErrorMessage,
data.ErrorCode,
sc,
}
}
return data.Payment, nil
}