-
Notifications
You must be signed in to change notification settings - Fork 155
/
addenda98_refused.go
227 lines (191 loc) · 7.58 KB
/
addenda98_refused.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
// Licensed to The Moov Authors under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. The Moov Authors licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package ach
import (
"strings"
"unicode/utf8"
)
type Addenda98Refused struct {
// ID is an identifier only used by the moov-io/ach HTTP server as a way to identify a batch.
ID string `json:"id"`
// TypeCode Addenda types code '98'
TypeCode string `json:"typeCode"`
// RefusedChangeCode is the code specifying why the Notification of Change is being refused.
RefusedChangeCode string `json:"refusedChangeCode"`
// OriginalTrace This field contains the Trace Number as originally included on the forward Entry or Prenotification.
// The RDFI must include the Original Entry Trace Number in the Addenda Record of an Entry being returned to an ODFI,
// in the Addenda Record of an 98, within an Acknowledgment Entry, or with an RDFI request for a copy of an authorization.
OriginalTrace string `json:"originalTrace"`
// OriginalDFI field contains the Receiving DFI Identification (addenda.RDFIIdentification) as originally included on the
// forward Entry or Prenotification that the RDFI is returning or correcting.
OriginalDFI string `json:"originalDFI"`
// CorrectedData is the corrected data
CorrectedData string `json:"correctedData"`
// ChangeCode field contains a standard code used by an ACH Operator or RDFI to describe the reason for a change Entry.
ChangeCode string `json:"changeCode"`
// TraceSequenceNumber is the last seven digits of the TraceNumber in the original Notification of Change
TraceSequenceNumber string `json:"traceSequenceNumber"`
// TraceNumber matches the Entry Detail Trace Number of the entry being returned.
//
// Use TraceNumberField for a properly formatted string representation.
TraceNumber string `json:"traceNumber"`
// validator is composed for data validation
validator
// converters is composed for ACH to GoLang Converters
converters
}
// NewAddenda98Refused returns an reference to an instantiated Addenda98Refused with default values
func NewAddenda98Refused() *Addenda98Refused {
addenda98Refused := &Addenda98Refused{
TypeCode: "98",
}
return addenda98Refused
}
// Parse takes the input record string and parses the Addenda98Refused 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 (addenda98Refused *Addenda98Refused) Parse(record string) {
runeCount := utf8.RuneCountInString(record)
if runeCount != 94 {
return
}
buf := getBuffer()
defer saveBuffer(buf)
reset := func() string {
out := buf.String()
buf.Reset()
return out
}
// We're going to process the record rune-by-rune and at each field cutoff save the value.
var idx int
for _, r := range record {
idx++
// Append rune to buffer
buf.WriteRune(r)
// At each cutoff save the buffer and reset
switch idx {
case 0, 1:
// 1-1 Always 7
reset()
case 3:
// 2-3 Always "98"
addenda98Refused.TypeCode = strings.TrimSpace(reset())
case 6:
addenda98Refused.RefusedChangeCode = strings.TrimSpace(reset())
case 21:
addenda98Refused.OriginalTrace = strings.TrimSpace(reset())
case 27:
// Positions 22-27 are Reserved
reset()
case 35:
addenda98Refused.OriginalDFI = addenda98Refused.parseStringField(reset())
case 64:
addenda98Refused.CorrectedData = strings.TrimSpace(reset())
case 67:
addenda98Refused.ChangeCode = strings.TrimSpace(reset())
case 74:
addenda98Refused.TraceSequenceNumber = strings.TrimSpace(reset())
case 79:
// Positions 75-79 are Reserved
reset()
case 94:
addenda98Refused.TraceNumber = strings.TrimSpace(reset())
}
}
}
// String writes the Addenda98 struct to a 94 character string
func (addenda98Refused *Addenda98Refused) String() string {
if addenda98Refused == nil {
return ""
}
buf := getBuffer()
defer saveBuffer(buf)
buf.WriteString(entryAddendaPos)
buf.WriteString(addenda98Refused.TypeCode)
buf.WriteString(addenda98Refused.RefusedChangeCode)
buf.WriteString(addenda98Refused.OriginalTraceField())
buf.WriteString(strings.Repeat(" ", 6))
buf.WriteString(addenda98Refused.OriginalDFIField())
buf.WriteString(addenda98Refused.CorrectedDataField())
buf.WriteString(addenda98Refused.ChangeCode)
buf.WriteString(addenda98Refused.TraceSequenceNumberField())
buf.WriteString(strings.Repeat(" ", 5))
buf.WriteString(addenda98Refused.TraceNumberField())
return buf.String()
}
// Validate verifies NACHA rules for Addenda98
func (addenda98Refused *Addenda98Refused) Validate() error {
if addenda98Refused.TypeCode == "" {
return fieldError("TypeCode", ErrConstructor, addenda98Refused.TypeCode)
}
// Type Code must be 98
if addenda98Refused.TypeCode != "98" {
return fieldError("TypeCode", ErrAddendaTypeCode, addenda98Refused.TypeCode)
}
// RefusedChangeCode must be valid
_, ok := changeCodeDict[addenda98Refused.RefusedChangeCode]
if !ok {
return fieldError("RefusedChangeCode", ErrAddenda98RefusedChangeCode, addenda98Refused.RefusedChangeCode)
}
// Addenda98 Record must contain the corrected information corresponding to the Change Code used
if addenda98Refused.CorrectedData == "" {
return fieldError("CorrectedData", ErrAddenda98CorrectedData, addenda98Refused.CorrectedData)
}
// ChangeCode must be valid
_, ok = changeCodeDict[addenda98Refused.ChangeCode]
if !ok {
return fieldError("ChangeCode", ErrAddenda98ChangeCode, addenda98Refused.ChangeCode)
}
// TraceSequenceNumber must be valid
if addenda98Refused.TraceSequenceNumber == "" {
return fieldError("TraceSequenceNumber", ErrAddenda98RefusedTraceSequenceNumber, addenda98Refused.TraceSequenceNumber)
}
return nil
}
func (addenda98Refused *Addenda98Refused) RefusedChangeCodeField() *ChangeCode {
code, ok := changeCodeDict[addenda98Refused.RefusedChangeCode]
if ok {
return code
}
return nil
}
// OriginalTraceField returns a zero padded OriginalTrace string
func (addenda98Refused *Addenda98Refused) OriginalTraceField() string {
return addenda98Refused.stringField(addenda98Refused.OriginalTrace, 15)
}
// OriginalDFIField returns a zero padded OriginalDFI string
func (addenda98Refused *Addenda98Refused) OriginalDFIField() string {
return addenda98Refused.stringField(addenda98Refused.OriginalDFI, 8)
}
// CorrectedDataField returns a space padded CorrectedData string
func (addenda98Refused *Addenda98Refused) CorrectedDataField() string {
return addenda98Refused.alphaField(addenda98Refused.CorrectedData, 29)
}
func (addenda98Refused *Addenda98Refused) ChangeCodeField() *ChangeCode {
code, ok := changeCodeDict[addenda98Refused.ChangeCode]
if ok {
return code
}
return nil
}
func (addenda98Refused *Addenda98Refused) TraceSequenceNumberField() string {
return addenda98Refused.stringField(addenda98Refused.TraceSequenceNumber, 7)
}
// TraceNumberField returns a zero padded traceNumber string
func (addenda98Refused *Addenda98Refused) TraceNumberField() string {
return addenda98Refused.stringField(addenda98Refused.TraceNumber, 15)
}