-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile.go
More file actions
403 lines (374 loc) · 11.8 KB
/
file.go
File metadata and controls
403 lines (374 loc) · 11.8 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package cadeft
import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"
"github.com/hashicorp/go-multierror"
)
const (
maxLineLength = 1464
commonRecordDataLength = 24
segmentLength = 240
zRecordMinLength = 112
aRecordMinLength = 58
maxTxnsPerRecord = 6
)
type RecordType string
type DCSign string
type TransactionType string
const (
HeaderRecord RecordType = "A"
CreditRecord RecordType = "C"
DebitRecord RecordType = "D"
CreditReverseRecord RecordType = "E"
DebitReverseRecord RecordType = "F"
ReturnCreditRecord RecordType = "I"
ReturnDebitRecord RecordType = "J"
NoticeOfChangeRecord RecordType = "S"
NoticeOfChangeHeader RecordType = "U"
NoticeOfChangeFooter RecordType = "V"
FooterRecord RecordType = "Z"
)
type Transaction interface {
Parse(string) error
Build() (string, error)
Validate() error
GetType() RecordType
GetAmount() int64
GetBaseTxn() BaseTxn
GetAccountNo() string
GetDate() *time.Time
GetName() string
GetReturnInstitutionID() string
GetReturnAccountNo() string
GetOriginalInstitutionID() string
GetOriginalAccountNo() string
GetOriginalItemTraceNo() string
}
type Transactions []Transaction
type File struct {
Header *FileHeader `json:"file_header,omitempty"`
Txns Transactions `json:"transactions,omitempty"`
Footer *FileFooter `json:"file_footer,omitempty"`
}
func NewFile(header *FileHeader, txns []Transaction) File {
return File{
Header: header,
Txns: txns,
}
}
// Create returns a serialized EFT file as a string or an error.
// The serialized file will adhere to the EFT 005 payments canada specification but depending on individual fields the file may be rejected.
// Use the Validate function to catch any validation errors. Make sure to add the appropriate FileHeader and Transactions via NewFile before calling Create.
func (f *File) Create() (string, error) {
// 1. run validation checks
var sb strings.Builder
currentLine := 1
if f.Header == nil {
return "", errors.New("file header is missing")
}
serializedHeader, err := f.Header.buildHeader(currentLine)
if err != nil {
return "", err
}
sb.WriteString(serializedHeader)
currentLine++
serializedTxns, err := f.buildTransactions(f.Header.RecordHeader, ¤tLine)
if err != nil {
return "", err
}
sb.WriteString(serializedTxns)
// if the user provides a footer use that otherwise create a new one
if f.Footer != nil {
f.Footer.recordCount = int64(currentLine)
serializedFooter, err := f.Footer.Build()
if err != nil {
return "", fmt.Errorf("failed to build footer: %w", err)
}
sb.WriteString(serializedFooter)
} else {
// Build the footer
footerRecordHeader := RecordHeader{
RecordType: FooterRecord,
recordCount: int64(currentLine),
OriginatorID: f.Header.OriginatorID,
FileCreationNum: f.Header.FileCreationNum,
}
footer := NewFileFooter(footerRecordHeader, f.Txns)
if f.Footer == nil {
f.Footer = footer
}
serializedFooter, err := footer.Build()
if err != nil {
return "", err
}
sb.WriteString(serializedFooter)
}
return sb.String(), nil
}
// Returns all debit transactions or D records
func (f File) GetAllDebitTxns() []Debit {
txns := make([]Debit, 0)
for _, t := range f.Txns {
if txn, ok := t.(*Debit); ok {
txns = append(txns, *txn)
}
}
return txns
}
// Returns all credit transactions or C records
func (f File) GetAllCredits() []Credit {
txns := make([]Credit, 0)
for _, t := range f.Txns {
if txn, ok := t.(*Credit); ok {
txns = append(txns, *txn)
}
}
return txns
}
// Returns all debit return transactions or J records
func (f File) GetAllDebitReturns() []DebitReturn {
txns := make([]DebitReturn, 0)
for _, t := range f.Txns {
if txn, ok := t.(*DebitReturn); ok {
txns = append(txns, *txn)
}
}
return txns
}
// Returns all credit return transactions or I records
func (f File) GetAllCreditReturns() []CreditReturn {
txns := make([]CreditReturn, 0)
for _, t := range f.Txns {
if txn, ok := t.(*CreditReturn); ok {
txns = append(txns, *txn)
}
}
return txns
}
func (f File) buildTransactions(recordHeader RecordHeader, currentLine *int) (string, error) {
recTypeToTxs := map[RecordType][]Transaction{
DebitRecord: make([]Transaction, 0),
CreditRecord: make([]Transaction, 0),
CreditReverseRecord: make([]Transaction, 0),
DebitReverseRecord: make([]Transaction, 0),
ReturnCreditRecord: make([]Transaction, 0),
ReturnDebitRecord: make([]Transaction, 0),
}
for idx, t := range f.Txns {
switch t.GetType() {
case DebitRecord:
recTypeToTxs[DebitRecord] = append(recTypeToTxs[DebitRecord], t)
case CreditRecord:
recTypeToTxs[CreditRecord] = append(recTypeToTxs[CreditRecord], t)
case CreditReverseRecord:
recTypeToTxs[CreditReverseRecord] = append(recTypeToTxs[CreditReverseRecord], t)
case DebitReverseRecord:
recTypeToTxs[DebitReverseRecord] = append(recTypeToTxs[DebitReverseRecord], t)
case ReturnCreditRecord:
recTypeToTxs[ReturnCreditRecord] = append(recTypeToTxs[ReturnCreditRecord], t)
case ReturnDebitRecord:
recTypeToTxs[ReturnDebitRecord] = append(recTypeToTxs[ReturnDebitRecord], t)
case HeaderRecord, NoticeOfChangeRecord, NoticeOfChangeHeader, NoticeOfChangeFooter, FooterRecord:
return "", fmt.Errorf("transaction[%d] has unexpected record type: %v", idx, t.GetType())
}
}
var sb strings.Builder
for recType, entries := range recTypeToTxs {
recordHeader.RecordType = recType
entriesStr, err := f.buildTxnEntries(entries, recordHeader, currentLine)
if err != nil {
return "", fmt.Errorf("failed to build txn entries: %w", err)
}
if len(entriesStr) > 0 {
sb.WriteString(padStringWithBlanks(entriesStr, maxLineLength))
}
}
sb.WriteString("\n")
return sb.String(), nil
}
func (f File) buildTxnEntries(txns []Transaction, recordHeader RecordHeader, currentLine *int) (string, error) {
var sb strings.Builder
var txnSb strings.Builder
recordHeader.recordCount = int64(*currentLine)
recordHeaderStr, err := recordHeader.buildRecordHeader()
if err != nil {
return "", fmt.Errorf("failed to create Txn RecordHeader: %w", err)
}
// go through all the transactions and each line should contain at most 6 txns
for count, txn := range txns {
txnStr, err := txn.Build()
if err != nil {
return "", fmt.Errorf("failed to build transaction: %w", err)
}
txnSb.WriteString(txnStr)
if (count+1)%maxTxnsPerRecord == 0 {
//flush into sb
sb.WriteString("\n")
sb.WriteString(recordHeaderStr)
// pad txn with blanks to adhere to length requirement of MAX_LINE_LENGTH
blankPad := createFillerString(maxLineLength - txnSb.Len() - len(recordHeaderStr))
txnSb.WriteString(blankPad)
sb.WriteString(txnSb.String())
txnSb.Reset()
*currentLine++
recordHeader.recordCount = int64(*currentLine)
recordHeaderStr, err = recordHeader.buildRecordHeader()
if err != nil {
return "", fmt.Errorf("failed to create Txn RecordHeader: %w", err)
}
}
}
//left over debit txns txnSb should be populated
if txnSb.Len() > 0 {
sb.WriteString("\n")
sb.WriteString(recordHeaderStr)
sb.WriteString(txnSb.String())
// write the filler to make the length of the line MAX_LINE_LENGTH
filler := createFillerString(maxLineLength - txnSb.Len() - len(recordHeaderStr))
sb.WriteString(filler)
*currentLine++
}
return sb.String(), nil
}
// Validate runs validation on the entire file starting from the FileHeader then every Taransaction.
// Any error that is encountered will be appended to a multierror and returned to the caller.
func (f File) Validate() error {
var err error
headerErr := f.Header.Validate()
if headerErr != nil {
err = multierror.Append(err, headerErr)
}
for i, t := range f.Txns {
txnErr := t.Validate()
if txnErr != nil {
err = multierror.Append(err, fmt.Errorf("faild to validate txn %d: %w", i, txnErr))
}
}
return err
}
func NewTransaction(
recordType RecordType,
txnType TransactionType,
amount int64,
date *time.Time,
institutionID string,
payorPayeeAccountNo string,
itemTraceNo string,
originatorShortName string,
payorPayeeName string,
originatorLongName string,
originalOrReturnInstitutionID string,
originalOrReturnAccountNo string,
originalItemTraceNo string,
opts ...BaseTxnOpt,
) Transaction {
var txn Transaction
switch recordType {
case DebitRecord:
txn = Ptr(NewDebit(txnType, amount, date, institutionID, payorPayeeAccountNo, itemTraceNo, originatorShortName, payorPayeeName, originatorLongName, originalOrReturnInstitutionID, originalOrReturnAccountNo, opts...))
case CreditRecord:
txn = Ptr(NewCredit(txnType, amount, date, institutionID, payorPayeeAccountNo, itemTraceNo, originatorShortName, payorPayeeName, originatorLongName, originalOrReturnInstitutionID, originalOrReturnAccountNo, opts...))
case ReturnDebitRecord:
txn = Ptr(NewDebitReturn(txnType, amount, date, institutionID, payorPayeeAccountNo, itemTraceNo, originatorShortName, payorPayeeName, originatorLongName, originalOrReturnInstitutionID, originalOrReturnAccountNo, originalItemTraceNo, opts...))
case ReturnCreditRecord:
txn = Ptr(NewCreditReturn(txnType, amount, date, institutionID, payorPayeeAccountNo, itemTraceNo, originatorShortName, payorPayeeName, originatorLongName, originalOrReturnInstitutionID, originalOrReturnAccountNo, originalItemTraceNo, opts...))
case CreditReverseRecord:
txn = Ptr(NewCreditReverse(txnType, amount, date, institutionID, payorPayeeAccountNo, itemTraceNo, originatorShortName, payorPayeeName, originatorLongName, originalOrReturnInstitutionID, originalOrReturnAccountNo, originalItemTraceNo, opts...))
case DebitReverseRecord:
txn = Ptr(NewDebitReverse(txnType, amount, date, institutionID, payorPayeeAccountNo, itemTraceNo, originatorShortName, payorPayeeName, originatorLongName, originalOrReturnInstitutionID, originalOrReturnAccountNo, originalItemTraceNo, opts...))
case HeaderRecord, NoticeOfChangeRecord, NoticeOfChangeHeader, NoticeOfChangeFooter, FooterRecord:
return nil
}
return txn
}
func (f *Transactions) UnmarshalJSON(data []byte) error {
var txns []interface{}
if err := json.Unmarshal(data, &txns); err != nil {
return err
}
for _, t := range txns {
tMap, ok := t.(map[string]interface{})
if !ok {
return errors.New("failed to unmarshall transaction")
}
rawRecordType, ok := tMap["type"]
if !ok {
return errors.New("failed to unmarshall transaction")
}
recordType, ok := rawRecordType.(string)
if !ok {
return errors.New("failed to unmarshall transaction")
}
jsonStr, err := json.Marshal(tMap)
if err != nil {
return err
}
switch recordType {
case string(DebitRecord):
var d Debit
err = json.Unmarshal(jsonStr, &d)
if err != nil {
return err
}
*f = append(*f, &d)
case string(CreditRecord):
var c Credit
err = json.Unmarshal(jsonStr, &c)
if err != nil {
return err
}
*f = append(*f, &c)
case string(ReturnCreditRecord):
var cr CreditReturn
err = json.Unmarshal(jsonStr, &cr)
if err != nil {
return err
}
*f = append(*f, &cr)
case string(ReturnDebitRecord):
var dr DebitReturn
err = json.Unmarshal(jsonStr, &dr)
if err != nil {
return err
}
*f = append(*f, &dr)
}
}
return nil
}
func getTotalValueAndCount(recordType RecordType, txns []Transaction) (int64, int64) {
var totalValue int64
var totalCount int64
for _, t := range txns {
if t.GetType() == recordType {
totalValue += t.GetAmount()
totalCount++
}
}
return totalValue, totalCount
}
func parseRecordType(t string) (RecordType, error) {
switch t {
case "A":
return HeaderRecord, nil
case "D":
return DebitRecord, nil
case "C":
return CreditRecord, nil
case "E":
return CreditReverseRecord, nil
case "F":
return DebitReverseRecord, nil
case "I":
return ReturnCreditRecord, nil
case "J":
return ReturnDebitRecord, nil
case "Z":
return FooterRecord, nil
default:
return "", fmt.Errorf("unrecognized record type: %s", t)
}
}