This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
statements.go
160 lines (140 loc) · 4.25 KB
/
statements.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
package p24
import (
"context"
"encoding/xml"
"fmt"
"net/http"
"strings"
"time"
"github.com/pkg/errors"
)
const (
statementsAPIURL = "https://api.privatbank.ua/p24api/rest_fiz"
statementsReqTimeLayout = "02.01.2006"
statementsRespDateLayout = "2006-01-02"
statementsRespTimeLayout = "15:04:05"
)
// StatementsOpts is sets of options required
// for performs p24 statements request
type StatementsOpts struct {
StartDate time.Time
EndDate time.Time
CardNumber string
CommonOpts
}
// Validate r for current date range and card number.
// P24 statements api provide date range with max 90 days
func (r StatementsOpts) Validate() error {
if r.StartDate.Unix() > r.EndDate.Unix() {
return errors.New("date range should be with start date <= end date")
}
// check date range <= 90 days
if r.EndDate.Sub(r.StartDate) > 90*24*time.Hour {
return errors.New("date range should be no longer than 90 days")
}
if err := CheckCardNumber(r.CardNumber); err != nil {
return errors.Wrap(err, "invalid card number")
}
return nil
}
// Statements struct for mapping p24 get statements response.
// Represents statements list of a p24 merchant
type Statements struct {
Status string `xml:"status,attr"`
Statements []Statement `xml:"statement"`
Credit Amount `xml:"credit,attr"`
Debet Amount `xml:"debet,attr"`
}
// Statement represents a Statement of a p24 merchant
type Statement struct {
Card string `xml:"card,attr"`
Appcode string `xml:"appcode,attr"`
Date time.Time `xml:"-"`
Terminal string `xml:"terminal,attr"`
Description string `xml:"description,attr"`
Amount Funds `xml:"amount,attr"`
CardAmount Funds `xml:"cardamount,attr"`
Rest Funds `xml:"rest,attr"`
}
type (
statementAlias Statement
statementXML struct {
XMLName xml.Name `xml:"statement"`
TranTimeStr string `xml:"trantime,attr"`
TranDateStr string `xml:"trandate,attr"`
statementAlias
}
)
// UnmarshalXML implements xml.Unmarshaler interface for s
func (s *Statement) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
statement := &statementXML{}
if err := d.DecodeElement(statement, &start); err != nil {
return err
}
// parse tran date/time
layout := fmt.Sprintf("%s %s", statementsRespDateLayout, statementsRespTimeLayout)
tranDate, err := time.ParseInLocation(layout, fmt.Sprintf("%s %s", statement.TranDateStr, statement.TranTimeStr), kievLocation)
if err != nil {
return err
}
*s = Statement(statement.statementAlias)
s.Date = tranDate
return nil
}
// MarshalXML implements xml.Marshaler interface for s
func (s Statement) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
start.Name.Local = strings.ToLower(start.Name.Local)
if start.Name.Local != "statement" {
return errors.New("invalid start elem name")
}
statements := &statementXML{
statementAlias: statementAlias(s),
TranTimeStr: s.Date.Format(statementsRespTimeLayout),
TranDateStr: s.Date.Format(statementsRespDateLayout),
}
return e.EncodeElement(statements, start)
}
// GetStatements returns Statements for given opts.
// Performs p24 orders api call.
// see: https://api.privatbank.ua/#p24/orders
func (c *Client) GetStatements(ctx context.Context, opts StatementsOpts) (Statements, error) {
if err := opts.Validate(); err != nil {
return Statements{}, errors.Wrap(err, "invalid request options")
}
reqData := RequestData{
CommonOpts: opts.CommonOpts,
Payment: struct {
ID string "xml:\"id,attr\""
Prop []struct {
Name string "xml:\"name,attr\""
Value string "xml:\"value,attr\""
} "xml:\"prop\""
}{
Prop: []struct {
Name string "xml:\"name,attr\""
Value string "xml:\"value,attr\""
}{
{
Name: "sd",
Value: opts.StartDate.Format(statementsReqTimeLayout),
},
{
Name: "ed",
Value: opts.EndDate.Format(statementsReqTimeLayout),
},
{
Name: "card",
Value: opts.CardNumber,
},
},
},
}
type info struct {
Statements Statements `xml:"statements"`
}
resp := Response{Data: ResponseData{Info: info{}}}
if err := c.DoContext(ctx, statementsAPIURL, http.MethodPost, NewRequest(c.merchant, reqData), &resp); err != nil {
return Statements{}, err
}
return resp.Data.Info.(info).Statements, nil
}