-
Notifications
You must be signed in to change notification settings - Fork 149
/
addenda12.go
175 lines (163 loc) · 6.64 KB
/
addenda12.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
// 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 (
"fmt"
"strings"
)
// Addenda12 is an addenda which provides business transaction information for Addenda Type
// Code 12 in a machine readable format. It is usually formatted according to ANSI, ASC, X12 Standard.
//
// Addenda12 is mandatory for IAT entries
//
// The Addenda12 record identifies key information related to the Originator of
// the entry.
type Addenda12 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 Addenda12 types code '12'
TypeCode string `json:"typeCode"`
// Originator City & State / Province
// Data elements City and State / Province should be separated with an asterisk (*) as a delimiter
// and the field should end with a backslash (\).
// For example: San FranciscoCA.
OriginatorCityStateProvince string `json:"originatorCityStateProvince"`
// Originator Country & Postal Code
// Data elements must be separated by an asterisk (*) and must end with a backslash (\)
// For example: US10036\
OriginatorCountryPostalCode string `json:"originatorCountryPostalCode"`
// 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
}
// NewAddenda12 returns a new Addenda12 with default values for none exported fields
func NewAddenda12() *Addenda12 {
addenda12 := new(Addenda12)
addenda12.recordType = "7"
addenda12.TypeCode = "12"
return addenda12
}
// Parse takes the input record string and parses the Addenda12 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 (addenda12 *Addenda12) Parse(record string) {
// 1-1 Always "7"
addenda12.recordType = "7"
// 2-3 Always 12
addenda12.TypeCode = record[1:3]
// 4-38
addenda12.OriginatorCityStateProvince = strings.TrimSpace(record[3:38])
// 39-73
addenda12.OriginatorCountryPostalCode = strings.TrimSpace(record[38:73])
// 74-87 reserved - Leave blank
addenda12.reserved = " "
// 88-94 Contains the last seven digits of the number entered in the Trace Number field in the corresponding Entry Detail Record
addenda12.EntryDetailSequenceNumber = addenda12.parseNumField(record[87:94])
}
// String writes the Addenda12 struct to a 94 character string.
func (addenda12 *Addenda12) String() string {
var buf strings.Builder
buf.Grow(94)
buf.WriteString(addenda12.recordType)
buf.WriteString(addenda12.TypeCode)
buf.WriteString(addenda12.OriginatorCityStateProvinceField())
// ToDo Validator for backslash
buf.WriteString(addenda12.OriginatorCountryPostalCodeField())
buf.WriteString(addenda12.reservedField())
buf.WriteString(addenda12.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 (addenda12 *Addenda12) Validate() error {
if err := addenda12.fieldInclusion(); err != nil {
return err
}
if addenda12.recordType != "7" {
msg := fmt.Sprintf(msgRecordType, 7)
return &FieldError{FieldName: "recordType", Value: addenda12.recordType, Msg: msg}
}
if err := addenda12.isTypeCode(addenda12.TypeCode); err != nil {
return &FieldError{FieldName: "TypeCode", Value: addenda12.TypeCode, Msg: err.Error()}
}
// Type Code must be 12
if addenda12.TypeCode != "12" {
return &FieldError{FieldName: "TypeCode", Value: addenda12.TypeCode, Msg: msgAddendaTypeCode}
}
if err := addenda12.isAlphanumeric(addenda12.OriginatorCityStateProvince); err != nil {
return &FieldError{FieldName: "OriginatorCityStateProvince",
Value: addenda12.OriginatorCityStateProvince, Msg: err.Error()}
}
if err := addenda12.isAlphanumeric(addenda12.OriginatorCountryPostalCode); err != nil {
return &FieldError{FieldName: "OriginatorCountryPostalCode",
Value: addenda12.OriginatorCountryPostalCode, Msg: err.Error()}
}
return nil
}
// fieldInclusion validate mandatory fields are not default values. If fields are
// invalid the ACH transfer will be returned.
func (addenda12 *Addenda12) fieldInclusion() error {
if addenda12.recordType == "" {
return &FieldError{
FieldName: "recordType",
Value: addenda12.recordType,
Msg: msgFieldInclusion + ", did you use NewAddenda12()?",
}
}
if addenda12.TypeCode == "" {
return &FieldError{
FieldName: "TypeCode",
Value: addenda12.TypeCode,
Msg: msgFieldInclusion + ", did you use NewAddenda12()?",
}
}
if addenda12.OriginatorCityStateProvince == "" {
return &FieldError{
FieldName: "OriginatorCityStateProvince",
Value: addenda12.OriginatorCityStateProvince,
Msg: msgFieldInclusion + ", did you use NewAddenda12()?",
}
}
if addenda12.OriginatorCountryPostalCode == "" {
return &FieldError{
FieldName: "OriginatorCountryPostalCode",
Value: addenda12.OriginatorCountryPostalCode,
Msg: msgFieldInclusion + ", did you use NewAddenda12()?",
}
}
if addenda12.EntryDetailSequenceNumber == 0 {
return &FieldError{
FieldName: "EntryDetailSequenceNumber",
Value: addenda12.EntryDetailSequenceNumberField(),
Msg: msgFieldInclusion + ", did you use NewAddenda12()?",
}
}
return nil
}
// OriginatorCityStateProvinceField gets the OriginatorCityStateProvinceField left padded
func (addenda12 *Addenda12) OriginatorCityStateProvinceField() string {
return addenda12.alphaField(addenda12.OriginatorCityStateProvince, 35)
}
// OriginatorCountryPostalCodeField gets the OriginatorCountryPostalCode field left padded
func (addenda12 *Addenda12) OriginatorCountryPostalCodeField() string {
return addenda12.alphaField(addenda12.OriginatorCountryPostalCode, 35)
}
// reservedField gets reserved - blank space
func (addenda12 *Addenda12) reservedField() string {
return addenda12.alphaField(addenda12.reserved, 14)
}
// EntryDetailSequenceNumberField returns a zero padded EntryDetailSequenceNumber string
func (addenda12 *Addenda12) EntryDetailSequenceNumberField() string {
return addenda12.numericField(addenda12.EntryDetailSequenceNumber, 7)
}