forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
addenda14.go
191 lines (177 loc) · 7.66 KB
/
addenda14.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
// Copyright 2018 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package ach
import (
"strings"
)
// Addenda14 is an addenda which provides business transaction information for Addenda Type
// Code 14 in a machine readable format. It is usually formatted according to ANSI, ASC, X14 Standard.
//
// Addenda14 is mandatory for IAT entries
//
// The Addenda14 identifies the Receiving financial institution holding the Receiver's account.
type Addenda14 struct {
// ID is a client defined string used as a reference to this record.
ID string `json:"id"`
// RecordType defines the type of record in the block.
recordType string
// TypeCode Addenda14 types code '14'
TypeCode string `json:"typeCode"`
// Receiving DFI Name
// Name of the Receiver's bank
RDFIName string `json:"RDFIName"`
// Receiving DFI Identification Number Qualifier
// The 2-digit code that identifies the numbering scheme used in the
// Receiving DFI Identification Number field::
// 01 = National Clearing System
// 02 = BIC Code
// 03 = IBAN Code
RDFIIDNumberQualifier string `json:"RDFIIDNumberQualifier"`
// Receiving DFI Identification
// This field contains the bank identification number of the DFI at which the
// Receiver maintains his account.
RDFIIdentification string `json:"RDFIIdentification"`
// Receiving DFI Branch Country Code
// USb” = United States
//(“b” indicates a blank space)
// This 3 position field contains a 2-character code as approved by the International
// Organization for Standardization (ISO) used to identify the country in which the
// branch of the bank that receives the entry is located. Values for other countries can
// be found on the International Organization for Standardization website: www.iso.org
RDFIBranchCountryCode string `json:"RDFIBranchCountryCode"`
// reserved - Leave blank
reserved string
// EntryDetailSequenceNumber contains the ascending sequence number section of the Entry
// Detail or Corporate Entry Detail Record's trace number This number is
// the same as the last seven digits of the trace number of the related
// Entry Detail Record or Corporate Entry Detail Record.
EntryDetailSequenceNumber int `json:"entryDetailSequenceNumber,omitempty"`
// validator is composed for data validation
validator
// converters is composed for ACH to GoLang Converters
converters
}
// NewAddenda14 returns a new Addenda14 with default values for none exported fields
func NewAddenda14() *Addenda14 {
addenda14 := new(Addenda14)
addenda14.recordType = "7"
addenda14.TypeCode = "14"
return addenda14
}
// Parse takes the input record string and parses the Addenda14 values
//
// Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm successful parsing and data validity.
func (addenda14 *Addenda14) Parse(record string) {
// 1-1 Always "7"
addenda14.recordType = "7"
// 2-3 Always 14
addenda14.TypeCode = record[1:3]
// 4-38 RDFIName
addenda14.RDFIName = strings.TrimSpace(record[3:38])
// 39-40 RDFIIDNumberQualifier
addenda14.RDFIIDNumberQualifier = record[38:40]
// 41-74 RDFIIdentification
addenda14.RDFIIdentification = addenda14.parseStringField(record[40:74])
// 75-77
addenda14.RDFIBranchCountryCode = strings.TrimSpace(record[74:77])
// 78-87 reserved - Leave blank
addenda14.reserved = " "
// 88-94 Contains the last seven digits of the number entered in the Trace Number field in the corresponding Entry Detail Record
addenda14.EntryDetailSequenceNumber = addenda14.parseNumField(record[87:94])
}
// String writes the Addenda14 struct to a 94 character string.
func (addenda14 *Addenda14) String() string {
var buf strings.Builder
buf.Grow(94)
buf.WriteString(addenda14.recordType)
buf.WriteString(addenda14.TypeCode)
buf.WriteString(addenda14.RDFINameField())
buf.WriteString(addenda14.RDFIIDNumberQualifierField())
buf.WriteString(addenda14.RDFIIdentificationField())
buf.WriteString(addenda14.RDFIBranchCountryCodeField())
buf.WriteString(addenda14.reservedField())
buf.WriteString(addenda14.EntryDetailSequenceNumberField())
return buf.String()
}
// 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 (addenda14 *Addenda14) Validate() error {
if err := addenda14.fieldInclusion(); err != nil {
return err
}
if addenda14.recordType != "7" {
return fieldError("recordType", NewErrRecordType(7), addenda14.recordType)
}
if err := addenda14.isTypeCode(addenda14.TypeCode); err != nil {
return fieldError("TypeCode", err, addenda14.TypeCode)
}
// Type Code must be 14
if addenda14.TypeCode != "14" {
return fieldError("TypeCode", ErrAddendaTypeCode, addenda14.TypeCode)
}
if err := addenda14.isAlphanumeric(addenda14.RDFIName); err != nil {
return fieldError("RDFIName", err, addenda14.RDFIName)
}
// Valid RDFI Identification Number Qualifier
if err := addenda14.isIDNumberQualifier(addenda14.RDFIIDNumberQualifier); err != nil {
return fieldError("RDFIIDNumberQualifier", ErrIDNumberQualifier, addenda14.RDFIIDNumberQualifier)
}
if err := addenda14.isAlphanumeric(addenda14.RDFIIdentification); err != nil {
return fieldError("RDFIIdentification", err, addenda14.RDFIIdentification)
}
if err := addenda14.isAlphanumeric(addenda14.RDFIBranchCountryCode); err != nil {
return fieldError("RDFIBranchCountryCode", err, addenda14.RDFIBranchCountryCode)
}
return nil
}
// fieldInclusion validate mandatory fields are not default values. If fields are
// invalid the ACH transfer will be returned.
func (addenda14 *Addenda14) fieldInclusion() error {
if addenda14.recordType == "" {
return fieldError("recordType", ErrConstructor, addenda14.recordType)
}
if addenda14.TypeCode == "" {
return fieldError("TypeCode", ErrConstructor, addenda14.TypeCode)
}
if addenda14.RDFIName == "" {
return fieldError("RDFIName", ErrConstructor, addenda14.RDFIName)
}
if addenda14.RDFIIDNumberQualifier == "" {
return fieldError("RDFIIDNumberQualifier", ErrConstructor, addenda14.RDFIIDNumberQualifier)
}
if addenda14.RDFIIdentification == "" {
return fieldError("RDFIIdentification", ErrConstructor, addenda14.RDFIIdentification)
}
if addenda14.RDFIBranchCountryCode == "" {
return fieldError("RDFIBranchCountryCode", ErrConstructor, addenda14.RDFIBranchCountryCode)
}
if addenda14.EntryDetailSequenceNumber == 0 {
return fieldError("EntryDetailSequenceNumber", ErrConstructor, addenda14.EntryDetailSequenceNumberField())
}
return nil
}
// RDFINameField gets the RDFIName field left padded
func (addenda14 *Addenda14) RDFINameField() string {
return addenda14.alphaField(addenda14.RDFIName, 35)
}
// RDFIIDNumberQualifierField gets the RDFIIDNumberQualifier field left padded
func (addenda14 *Addenda14) RDFIIDNumberQualifierField() string {
return addenda14.alphaField(addenda14.RDFIIDNumberQualifier, 2)
}
// RDFIIdentificationField gets the RDFIIdentificationCode field left padded
func (addenda14 *Addenda14) RDFIIdentificationField() string {
return addenda14.alphaField(addenda14.RDFIIdentification, 34)
}
// RDFIBranchCountryCodeField gets the RDFIBranchCountryCode field left padded
func (addenda14 *Addenda14) RDFIBranchCountryCodeField() string {
return addenda14.alphaField(addenda14.RDFIBranchCountryCode, 3)
}
// reservedField gets reserved - blank space
func (addenda14 *Addenda14) reservedField() string {
return addenda14.alphaField(addenda14.reserved, 10)
}
// EntryDetailSequenceNumberField returns a zero padded EntryDetailSequenceNumber string
func (addenda14 *Addenda14) EntryDetailSequenceNumberField() string {
return addenda14.numericField(addenda14.EntryDetailSequenceNumber, 7)
}