-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcredit.go
270 lines (243 loc) · 9.27 KB
/
credit.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
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
// Copyright 2022 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package imagecashletter
import (
"encoding/json"
"fmt"
"strings"
"unicode/utf8"
)
// Errors specific to a Credit Record
// Credit Record
type Credit struct {
// ID is a client defined string used as a reference to this record.
ID string `json:"id"`
// RecordType defines the type of record.
recordType string
// AuxiliaryOnUs identifies a code used on commercial checks at the discretion of the payor bank.
AuxiliaryOnUs string `json:"auxiliaryOnUs,omitempty"`
// ExternalProcessingCode identifies a code used for special purposes as authorized by the AS Committee X9.
ExternalProcessingCode string `json:"externalProcessingCode,omitempty"`
// PayorBankRoutingNumber identifies a number that identifies the institution by or through
// which the item is payable.
PayorBankRoutingNumber string `json:"payorBankRoutingNumber"`
// CreditAccountNumberOnUs identifies data specified by the payor bank.
// Usually an account number, serial number or transaction code or both.
CreditAccountNumberOnUs string `json:"creditAccountNumberOnUs"`
// ItemAmount identifies amount of the credit in U.S. dollars.
ItemAmount int `json:"itemAmount"`
// InstitutionItemSequenceNumber identifies sequence number assigned by the ECE company/institution.
ECEInstitutionItemSequenceNumber string `json:"eceInstitutionItemSequenceNumber,omitempty"`
// DocumentationTypeIndicator identifies a code that indicates the type of documentation
// that supports the check record.
DocumentationTypeIndicator string `json:"documentationTypeIndicator,omitempty"`
// AccountTypeCode identifies a code to designate account type.
AccountTypeCode string `json:"accountTypeCode,omitempty"`
// SourceWorkCode identifies a code that identifies the incoming work.
SourceWorkCode string `json:"sourceWorkCode,omitempty"`
// WorkType identifies a code that identifies the type of work.
WorkType string `json:"workType,omitempty"`
// InstitutionItemSequenceNumber identifies a code that identifies whether this record represents
// a debit or credit adjustment.
DebitCreditIndicator string `json:"debitCreditIndicator,omitempty"`
// reserved is a field reserved for future use. Reserved should be blank.
reserved string
// validator is composed for image cash letter data validation
validator
// converters is composed for image cash letter to golang Converters
converters
}
// NewCredit returns a new credit with default values for non exported fields
func NewCredit() *Credit {
cr := &Credit{}
cr.setRecordType()
return cr
}
func (cr *Credit) setRecordType() {
if cr == nil {
return
}
cr.recordType = "61"
cr.reserved = " "
}
// Parse takes the input record string and parses the BundleControl values
func (cr *Credit) Parse(record string) {
if utf8.RuneCountInString(record) < 77 {
return
}
// Character position 1-2, Always "61"
cr.setRecordType()
// 03–17
cr.AuxiliaryOnUs = cr.parseStringField(record[2:17])
// 18
cr.ExternalProcessingCode = cr.parseStringField(record[17:18])
// 19–27
cr.PayorBankRoutingNumber = cr.parseStringField(record[18:27])
// 28–47
cr.CreditAccountNumberOnUs = cr.parseStringField(record[27:47])
// 48–57
cr.ItemAmount = cr.parseNumField(record[47:57])
// 58–72
cr.ECEInstitutionItemSequenceNumber = record[57:72]
// 73
cr.DocumentationTypeIndicator = cr.parseStringField(record[72:73])
// 74
cr.AccountTypeCode = cr.parseStringField(record[73:74])
// 75
cr.SourceWorkCode = cr.parseStringField(record[74:75])
// 76
cr.WorkType = cr.parseStringField(record[75:76])
// 77
cr.DebitCreditIndicator = cr.parseStringField(record[76:77])
// 78–80
cr.reserved = " "
}
func (cr *Credit) UnmarshalJSON(data []byte) error {
type Alias Credit
aux := struct {
*Alias
}{
(*Alias)(cr),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
cr.setRecordType()
return nil
}
// String writes the BundleControl struct to a string.
func (cr *Credit) String() string {
if cr == nil {
return ""
}
var buf strings.Builder
buf.Grow(80)
buf.WriteString(cr.recordType)
buf.WriteString(cr.AuxiliaryOnUsField())
buf.WriteString(cr.ExternalProcessingCodeField())
buf.WriteString(cr.PayorBankRoutingNumberField())
buf.WriteString(cr.CreditAccountNumberOnUsField())
buf.WriteString(cr.ItemAmountField())
buf.WriteString(cr.ECEInstitutionItemSequenceNumberField())
buf.WriteString(cr.DocumentationTypeIndicatorField())
buf.WriteString(cr.AccountTypeCodeField())
buf.WriteString(cr.SourceWorkCodeField())
buf.WriteString(cr.WorkTypeField())
buf.WriteString(cr.DebitCreditIndicatorField())
buf.WriteString(cr.reservedField())
return buf.String()
}
// Validate performs image cash letter format rule checks on the record and returns an error if not Validated
// The first error encountered is returned and stops the parsing.
func (cr *Credit) Validate() error {
if err := cr.fieldInclusion(); err != nil {
return err
}
if cr.recordType != "61" {
msg := fmt.Sprintf(msgRecordType, 61)
return &FieldError{FieldName: "recordType", Value: cr.recordType, Msg: msg}
}
if cr.SourceWorkCode != "" {
if cr.SourceWorkCode != "3" {
return &FieldError{FieldName: "SourceWorkCode", Value: cr.SourceWorkCode, Msg: msgInvalid}
}
}
if cr.AccountTypeCode != "" {
if err := cr.isAccountTypeCode(cr.AccountTypeCode); err != nil {
return &FieldError{FieldName: "AccountTypeCode", Value: cr.AccountTypeCode, Msg: err.Error()}
}
}
if cr.DocumentationTypeIndicator != "" {
if cr.DocumentationTypeIndicator != "G" {
return &FieldError{FieldName: "DocumentationTypeIndicator", Value: cr.DocumentationTypeIndicator, Msg: msgInvalid}
}
}
if cr.ECEInstitutionItemSequenceNumber != "" {
if err := cr.isNumeric(cr.ECEInstitutionItemSequenceNumber); err != nil {
return &FieldError{FieldName: "ECEInstitutionItemSequenceNumber", Value: cr.ECEInstitutionItemSequenceNumber, Msg: msgInvalid}
}
}
if err := cr.isNumeric(cr.PayorBankRoutingNumber); err != nil {
return &FieldError{FieldName: "PayorBankRoutingNumber",
Value: cr.PayorBankRoutingNumber, Msg: err.Error()}
}
// Should not contain forward or back slashes
if strings.Contains(cr.AuxiliaryOnUs, `\`) || strings.Contains(cr.AuxiliaryOnUs, `/`) {
return &FieldError{FieldName: "AuxiliaryOnUs", Value: cr.AuxiliaryOnUsField(), Msg: msgInvalid}
}
return nil
}
// fieldInclusion validate mandatory fields are not default values. If fields are
// invalid the Electronic Exchange will be returned.
func (cr *Credit) fieldInclusion() error {
if cr.recordType == "" {
return &FieldError{FieldName: "recordType",
Value: cr.recordType,
Msg: msgFieldInclusion + ", did you use Credit()?"}
}
if cr.PayorBankRoutingNumberField() == "000000000" {
return &FieldError{FieldName: "PayorBankRoutingNumber",
Value: cr.PayorBankRoutingNumberField(),
Msg: msgFieldInclusion + ", did you use Credit()?"}
}
if cr.CreditAccountNumberOnUs == "" {
return &FieldError{FieldName: "CreditAccountNumberOnUs",
Value: cr.CreditAccountNumberOnUsField(),
Msg: msgFieldInclusion + ", did you use Credit()?"}
}
if cr.ItemAmount == 0 {
return &FieldError{FieldName: "ItemAmount",
Value: cr.ItemAmountField(),
Msg: msgFieldInclusion + ", did you use Credit()?"}
}
return nil
}
// AuxiliaryOnUsField gets a string of the AuxiliaryOnUs
func (cr *Credit) AuxiliaryOnUsField() string {
return cr.alphaField(cr.AuxiliaryOnUs, 15)
}
// ExternalProcessingCodeField gets a string of the ExternalProcessingCode
func (cr *Credit) ExternalProcessingCodeField() string {
return cr.alphaField(cr.ExternalProcessingCode, 1)
}
// PayorBankRoutingNumberField gets a string of the PayorBankRoutingNumber zero padded
func (cr *Credit) PayorBankRoutingNumberField() string {
return cr.alphaField(cr.PayorBankRoutingNumber, 9)
}
// CreditAccountNumberOnUsField gets a string of the CreditAccountNumberOnUs
func (cr *Credit) CreditAccountNumberOnUsField() string {
return cr.alphaField(cr.CreditAccountNumberOnUs, 20)
}
// ItemAmountField gets a string of the ItemAmount zero padded
func (cr *Credit) ItemAmountField() string {
return cr.numericField(cr.ItemAmount, 10)
}
// ECEInstitutionItemSequenceNumberField gets a string of the ECEInstitutionItemSequenceNumber
func (cr *Credit) ECEInstitutionItemSequenceNumberField() string {
return cr.alphaField(cr.ECEInstitutionItemSequenceNumber, 15)
}
// DocumentationTypeIndicatorField gets a string of the DocumentationTypeIndicator
func (cr *Credit) DocumentationTypeIndicatorField() string {
return cr.alphaField(cr.DocumentationTypeIndicator, 1)
}
// AccountTypeCodeField gets a string of the AccountTypeCode
func (cr *Credit) AccountTypeCodeField() string {
return cr.alphaField(cr.AccountTypeCode, 1)
}
// SourceWorkCodeField gets a string of the SourceOfWorkCode
func (cr *Credit) SourceWorkCodeField() string {
return cr.alphaField(cr.SourceWorkCode, 1)
}
// WorkTypeField gets a string of the WorkType
func (cr *Credit) WorkTypeField() string {
return cr.alphaField(cr.WorkType, 1)
}
// DebitCreditIndicatorField gets a string of the DebitCreditIndicator
func (cr *Credit) DebitCreditIndicatorField() string {
return cr.alphaField(cr.DebitCreditIndicator, 1)
}
// reservedField gets reserved - blank space
func (cr *Credit) reservedField() string {
return cr.alphaField(cr.reserved, 3)
}