forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entryDetail.go
246 lines (218 loc) · 10.1 KB
/
entryDetail.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
// Copyright 2017 The ACH Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package ach
import "fmt"
// EntryDetail contains the actual transaction data for an individual entry.
// Fields include those designating the entry as a deposit (credit) or
// withdrawal (debit), the transit routing number for the entry recipient’s financial
// institution, the account number (left justify,no zero fill), name, and dollar amount.
type EntryDetail struct {
// RecordType defines the type of record in the block. 6
recordType string
// TransactionCode if the receivers account is:
// Credit (deposit) to checking account ‘22’
// Prenote for credit to checking account ‘23’
// Debit (withdrawal) to checking account ‘27’
// Prenote for debit to checking account ‘28’
// Credit to savings account ‘32’
// Prenote for credit to savings account ‘33’
// Debit to savings account ‘37’
// Prenote for debit to savings account ‘38’
TransactionCode int
// rdfiIdentification is the RDFI's routing number without the last digit.
// Receiving Depository Financial Institution
RDFIIdentification int
// CheckDigit the last digit of the RDFI's routing number
CheckDigit int
// DFIAccountNumber is the receiver's bank account number you are crediting/debiting.
// It important to note that this is an alphanumeric field, so its space padded, no zero padded
DFIAccountNumber string
// Amount Number of cents you are debiting/crediting this account
Amount int
// IndividualIdentificationNumber n internal identification (alphanumeric) that
// you use to uniquely identify this Entry Detail Record
IndividualIdentificationNumber string
// IndividualName The name of the receiver, usually the name on the bank account
IndividualName string
// DiscretionaryData allows ODFIs to include codes, of significance only to them,
// to enable specialized handling of the entry. There will be no
// standardized interpretation for the value of this field. It can either
// be a single two-character code, or two distince one-character codes,
// according to the needs of the ODFI and/or Originator involved. This
// field must be returned intact for any returned entry.
//
// WEB uses the Discretionary Data Field as the Payment Type Code
DiscretionaryData string
// AddendaRecordIndicator indicates the existence of an Addenda Record.
// A value of "1" indicates that one ore more addenda records follow,
// and "0" means no such record is present.
AddendaRecordIndicator int
// TraceNumber assigned by the ODFI in ascending sequence, is included in each
// Entry Detail Record, Corporate Entry Detail Record, and addenda Record.
// Trace Numbers uniquely identify each entry within a batch in an ACH input file.
// In association with the Batch Number, transmission (File Creation) Date,
// and File ID Modifier, the Trace Number uniquely identifies an entry within a given file.
// For addenda Records, the Trace Number will be identical to the Trace Number
// in the associated Entry Detail Record, since the Trace Number is associated
// with an entry or item rather than a physical record.
TraceNumber int
// Addendums a list of Addenda for the Entry Detail
Addendums []Addenda
// validator is composed for data validation
validator
// converters is composed for ACH to golang Converters
converters
}
// NewEntryDetail returns a new EntryDetail with default values for none exported fields
func NewEntryDetail() *EntryDetail {
return &EntryDetail{
recordType: "6",
}
}
// Parse takes the input record string and parses the EntryDetail values
func (ed *EntryDetail) Parse(record string) {
// 1-1 Always "6"
ed.recordType = "6"
// 2-3 is checking credit 22 debit 27 savings credit 32 debit 37
ed.TransactionCode = ed.parseNumField(record[1:3])
// 4-11 the RDFI's routing number without the last digit.
ed.RDFIIdentification = ed.parseNumField(record[3:11])
// 12-12 The last digit of the RDFI's routing number
ed.CheckDigit = ed.parseNumField(record[11:12])
// 13-29 The receiver's bank account number you are crediting/debiting
ed.DFIAccountNumber = record[12:29]
// 30-39 Number of cents you are debiting/crediting this account
ed.Amount = ed.parseNumField(record[29:39])
// 40-54 An internal identification (alphanumeric) that you use to uniquely identify this Entry Detail Record
ed.IndividualIdentificationNumber = record[39:54]
// 55-76 The name of the receiver, usually the name on the bank account
ed.IndividualName = record[54:76]
// 77-78 allows ODFIs to include codes of significance only to them
// normally blank
ed.DiscretionaryData = record[76:78]
// 79-79 1 if addenda exists 0 if it does not
ed.AddendaRecordIndicator = ed.parseNumField(record[78:79])
// 80-84 An internal identification (alphanumeric) that you use to uniquely identify
// this Entry Detail Recor This number should be unique to the transaction and will help identify the transaction in case of an inquiry
ed.TraceNumber = ed.parseNumField(record[79:94])
}
// String writes the EntryDetail struct to a 94 character string.
func (ed *EntryDetail) String() string {
return fmt.Sprintf("%v%v%v%v%v%v%v%v%v%v%v",
ed.recordType,
ed.TransactionCode,
ed.RDFIIdentificationField(),
ed.CheckDigit,
ed.DFIAccountNumberField(),
ed.AmountField(),
ed.IndividualIdentificationNumberField(),
ed.IndividualNameField(),
ed.DiscretionaryDataField(),
ed.AddendaRecordIndicator,
ed.TraceNumberField())
}
// Validate performs NACHA format rule checks on the record and returns an error if not Validated
// The first error encountered is returned and stops that parsing.
func (ed *EntryDetail) Validate() error {
if err := ed.fieldInclusion(); err != nil {
return err
}
if ed.recordType != "6" {
return &ValidateError{FieldName: "recordType", Value: ed.recordType, Err: ErrRecordType}
}
if err := ed.isTransactionCode(ed.TransactionCode); err != nil {
return &ValidateError{FieldName: "TransactionCode", Value: string(ed.TransactionCode), Err: err}
}
if err := ed.isAlphanumeric(ed.DFIAccountNumber); err != nil {
return &ValidateError{FieldName: "DFIAccountNumber", Value: ed.DFIAccountNumber, Err: err}
}
if err := ed.isAlphanumeric(ed.IndividualIdentificationNumber); err != nil {
return &ValidateError{FieldName: "IndividualIdentificationNumber", Value: ed.IndividualIdentificationNumber, Err: err}
}
if err := ed.isAlphanumeric(ed.IndividualName); err != nil {
return &ValidateError{FieldName: "IndividualName", Value: ed.IndividualName, Err: err}
}
if err := ed.isAlphanumeric(ed.DiscretionaryData); err != nil {
return &ValidateError{FieldName: "DiscretionaryData", Value: ed.DiscretionaryData, Err: err}
}
if err := ed.isCheckDigit(ed.RDFIIdentificationField(), ed.CheckDigit); err != nil {
return &ValidateError{FieldName: "CheckDigit", Value: string(ed.CheckDigit), Err: err}
}
return nil
}
// fieldInclusion validate mandatory fields are not default values. If fields are
// invalid the ACH transfer will be returned.
func (ed *EntryDetail) fieldInclusion() error {
if ed.recordType == "" {
return &ValidateError{FieldName: "recordType", Value: ed.recordType, Err: ErrRecordType}
}
if ed.TransactionCode == 0 {
return &ValidateError{FieldName: "TransactionCode", Value: string(ed.TransactionCode), Err: ErrValidFieldInclusion}
}
if ed.RDFIIdentification == 0 {
return &ValidateError{FieldName: "RDFIIdentification", Value: string(ed.RDFIIdentification), Err: ErrValidFieldInclusion}
}
if ed.CheckDigit == 0 {
return &ValidateError{FieldName: "CheckDigit", Value: string(ed.CheckDigit), Err: ErrValidFieldInclusion}
}
if ed.DFIAccountNumber == "" {
return &ValidateError{FieldName: "DFIAccountNumber", Value: ed.DFIAccountNumber, Err: ErrValidFieldInclusion}
}
if ed.Amount == 0 {
return &ValidateError{FieldName: "Amount", Value: string(ed.Amount), Err: ErrValidFieldInclusion}
}
if ed.IndividualName == "" {
return &ValidateError{FieldName: "IndividualName", Value: ed.IndividualName, Err: ErrValidFieldInclusion}
}
if ed.TraceNumber == 0 {
return &ValidateError{FieldName: "TraceNumber", Value: string(ed.TraceNumber), Err: ErrValidFieldInclusion}
}
return nil
}
// AddAddenda appends an EntryDetail to the Addendums
func (ed *EntryDetail) AddAddenda(addenda Addenda) []Addenda {
ed.AddendaRecordIndicator = 1
ed.Addendums = append(ed.Addendums, addenda)
return ed.Addendums
}
// SetRDFI takes the 9 digit RDFI account number and seperates it for RDFIIdentification and CheckDigit
func (ed *EntryDetail) SetRDFI(rdfi int) *EntryDetail {
s := ed.numericField(rdfi, 9)
ed.RDFIIdentification = ed.parseNumField(s[:8])
ed.CheckDigit = ed.parseNumField(s[8:9])
return ed
}
// setTraceNumber takes first 8 digits of RDFI and catinates a sequence number onto the TraceNumber
func (ed *EntryDetail) setTraceNumber(RDFIIdentification int, seq int) {
trace := ed.numericField(RDFIIdentification, 8) + ed.numericField(seq, 7)
ed.TraceNumber = ed.parseNumField(trace)
}
// RDFIIdentificationField get the rdfiIdentification with zero padding
func (ed *EntryDetail) RDFIIdentificationField() string {
return ed.numericField(ed.RDFIIdentification, 8)
}
// DFIAccountNumberField gets the DFIAccountNumber with space padding
func (ed *EntryDetail) DFIAccountNumberField() string {
return ed.alphaField(ed.DFIAccountNumber, 17)
}
// AmountField returns a zero padded string of amount
func (ed *EntryDetail) AmountField() string {
return ed.numericField(ed.Amount, 10)
}
// IndividualIdentificationNumberField returns a space padded string of IndividualIdentificationNumber
func (ed *EntryDetail) IndividualIdentificationNumberField() string {
return ed.alphaField(ed.IndividualIdentificationNumber, 15)
}
// IndividualNameField returns a space padded string of IndividualName
func (ed *EntryDetail) IndividualNameField() string {
return ed.alphaField(ed.IndividualName, 22)
}
// DiscretionaryDataField returns a space padded string of DiscretionaryData
func (ed *EntryDetail) DiscretionaryDataField() string {
return ed.alphaField(ed.DiscretionaryData, 2)
}
// TraceNumberField returns a zero padded traceNumber string
func (ed *EntryDetail) TraceNumberField() string {
return ed.numericField(ed.TraceNumber, 15)
}