-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMicr.cs
322 lines (274 loc) · 9.74 KB
/
Micr.cs
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using System;
namespace com.shepherdchurch.ImageCashLetter
{
/// <summary>
/// A helper class for working with MICR data from Rock.
/// </summary>
public class Micr
{
/// <summary>
/// Enum for readability to fetch out the data we are after
/// </summary>
protected enum FIELD
{
/// <summary>
/// Field 1
/// </summary>
CHECK_AMOUNT,
/// <summary>
/// Field 2
/// </summary>
CHECK_NUMBER,
/// <summary>
/// Field 3
/// </summary>
ACCOUNT_NUMBER,
/// <summary>
/// Field 4
/// </summary>
ROUTING_NUMBER,
/// <summary>
/// Field 5
/// </summary>
EXTERNAL_PROCESSING_CODE,
/// <summary>
/// Field 6
/// </summary>
AUX_ON_US,
/// <summary>
/// Field 7
/// </summary>
ON_US
}
#region Fields
/// <summary>
/// The content of the MICR data after it has been justified.
/// </summary>
private string _content;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="Micr"/> class.
/// </summary>
/// <param name="content">The content of the MICR data, expected to be in Ranger Driver format.</param>
/// <exception cref="System.ArgumentException">Argument does not contain valid micr data - content</exception>
public Micr( string content )
{
//
// Verify the data is valid.
//
if ( content == null )
{
_content = string.Empty;
}
else if ( !content.Contains( "d" ) || !content.Contains( "c" ) )
{
throw new ArgumentException( "Argument does not contain valid micr data.", "content" );
}
else
{
//
// MICR data should be aligned to have the left-most Routing Number symbol
// at position 43 (from the right-side of the string). So we need to adjust
// the length of the string so that the first 'd' we find is followed by 42
// characters.
//
int index = content.IndexOf( 'd' );
int length = content.Length - ( content.Length - 43 - index );
if ( content.Length > length )
{
content = content.Substring( 0, length );
}
else if ( content.Length < length )
{
content = content.PadRight( length );
}
_content = content;
}
}
#endregion
#region Static Methods
/// <summary>
/// Returns true if the MICR content is valid. This is not a perfect check, but
/// it should give a strong indication on the MICR string being valid or not.
/// </summary>
/// <param name="content">The content.</param>
/// <returns>
/// <c>true</c> if the specified content is valid; otherwise, <c>false</c>.
/// </returns>
public static bool IsValid( string content )
{
if ( content.Contains( "!" ) )
{
return false;
}
try
{
var micr = new Micr( content );
if ( micr.GetRoutingNumber().Length != 9 )
{
return false;
}
if ( string.IsNullOrWhiteSpace( micr.GetOnUs() ) )
{
return false;
}
return true;
}
catch
{
return false;
}
}
#endregion
#region Internal Methods
/// <summary>
/// Gets the characters in the specified range from the MICR.
/// </summary>
/// <param name="start">The starting position in the MICR, from the right.</param>
/// <param name="end">The ending position in the MICR (inclusive), from the right.</param>
/// <returns>A string containing the characters found in the specified range.</returns>
protected string GetCharacterFields( int start, int end )
{
if ( start > _content.Length )
{
return string.Empty;
}
if ( end > _content.Length )
{
end = _content.Length;
}
return _content.Substring( _content.Length - end, end - start + 1 );
}
/// <summary>
/// Get a specific MICR field from the MICR data.
/// </summary>
/// <param name="FieldType">The MICR field to be retrieved.</param>
/// <returns>String containing the component value from the MICR line.</returns>
protected string GetField( FIELD FieldType )
{
var f = string.Empty;
try
{
switch ( FieldType )
{
// Account number
case FIELD.ACCOUNT_NUMBER:
{
f = GetCharacterFields( 13, 32 );
if ( f.IndexOf( 'c' ) != f.LastIndexOf( 'c' ) )
{
return f.Substring( f.IndexOf( 'c' ) + 1, f.LastIndexOf( 'c' ) - f.IndexOf( 'c' ) - 1 ).Trim();
}
else
{
return f.Substring( 0, f.IndexOf( 'c' ) ).Trim();
}
}
// AUX OnUs
case FIELD.AUX_ON_US:
{
return GetCharacterFields( 45, _content.Length ).Replace( "c", "" ).Trim();
}
// Check Amount
case FIELD.CHECK_AMOUNT:
{
return GetCharacterFields( 2, 11 ).Trim();
}
// Check Number
case FIELD.CHECK_NUMBER:
{
f = GetCharacterFields( 13, 32 );
if ( f.IndexOf( 'c' ) != f.LastIndexOf( 'c' ) )
{
return f.Substring( 0, f.IndexOf( 'c' ) - 1 ).Trim() + f.Substring( f.LastIndexOf( 'c' ) + 1 ).Trim();
}
else
{
return f.Substring( f.IndexOf( 'c' ) + 1 ).Trim();
}
}
// External Processing code
case FIELD.EXTERNAL_PROCESSING_CODE:
{
return GetCharacterFields( 44, 44 ).Trim();
}
// The full raw ON_US field (that typically contains the account number and check number)
case FIELD.ON_US:
{
return GetCharacterFields( 13, 31 ).Trim();
}
// Routing Number
case FIELD.ROUTING_NUMBER:
{
return GetCharacterFields( 34, 42 ).Trim();
}
}
return string.Empty;
}
catch
{
return string.Empty;
}
}
#endregion
#region Methods
/// <summary>
/// Gets the amount of the check as specified on the MICR line.
/// </summary>
/// <returns>A string containing the amount characters.</returns>
public string GetCheckAmount()
{
return GetField( FIELD.CHECK_AMOUNT );
}
/// <summary>
/// Gets the routing number from the MICR line.
/// </summary>
/// <returns>A string containing the routing number characters.</returns>
public string GetRoutingNumber()
{
return GetField( FIELD.ROUTING_NUMBER );
}
/// <summary>
/// Gets the check number from the MICR line.
/// </summary>
/// <returns>A string containing, what is normally, the check number characters.</returns>
public string GetCheckNumber()
{
return GetField( FIELD.CHECK_NUMBER );
}
/// <summary>
/// Gets the account number from the MICR line.
/// </summary>
/// <returns>A string containing the account number this check will draw on.</returns>
public string GetAccountNumber()
{
return GetField( FIELD.ACCOUNT_NUMBER );
}
/// <summary>
/// Gets the EPC from the MICR line.
/// </summary>
/// <returns></returns>
public string GetExternalProcessingCode()
{
return GetField( FIELD.EXTERNAL_PROCESSING_CODE );
}
/// <summary>
/// GetAuxOnUs
/// </summary>
/// <returns>string</returns>
public string GetAuxOnUs()
{
return GetField( FIELD.AUX_ON_US );
}
/// <summary>
/// Gets the full raw ON_US field (that typically contains the account and check number)
/// </summary>
/// <returns>string</returns>
public string GetOnUs()
{
return GetField( FIELD.ON_US );
}
#endregion
}
}