Repository navigation
Expand file tree
/
Copy pathmain.rb
More file actions
247 lines (219 loc) · 6.44 KB
/
Copy pathmain.rb
File metadata and controls
247 lines (219 loc) · 6.44 KB
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
# coding: utf-8
# Copyright 2020 The Moov Authors
# Use of this source code is governed by an Apache License
# license that can be found in the LICENSE file.
require "date"
require "fileutils"
require "json"
require "net/http"
require "rainbow"
require "securerandom"
require "uri"
VERSION = File.read(File.expand_path("version", __dir__)).strip
ACH_URL = (ENV["ACH_URL"] || "http://localhost:8080").sub(/\/+\z/, "")
OUTPUT_PATH = ENV["OUTPUT_PATH"] || File.expand_path("payroll.ach", __dir__)
# Example PPD payroll credits. Amounts are in cents.
EMPLOYEES = [
{
identification_number: "EMP001",
individual_name: "Alice Rivera",
routing_number: "031300012",
account_number: "123456789",
amount: 150_000,
},
{
identification_number: "EMP002",
individual_name: "Ben Carter",
routing_number: "231380104",
account_number: "987654321",
amount: 175_000,
},
{
identification_number: "EMP003",
individual_name: "Chris Nguyen",
routing_number: "121042882",
account_number: "111222333",
amount: 200_000,
},
].freeze
IMMEDIATE_DESTINATION = "231380104"
IMMEDIATE_ORIGIN = "121042882"
ODFI_IDENTIFICATION = IMMEDIATE_ORIGIN[0, 8]
def main
puts Rainbow("Moov ACH Ruby Demo (version %s)" % VERSION).green
ping_ach
file_id = create_payroll_file
puts "File %s created" % file_id
write_contents(file_id)
validate_file(file_id)
end
def next_weekday(date)
nxt = date + 1
nxt += 1 while nxt.saturday? || nxt.sunday?
nxt
end
def ping_ach
last_error = nil
30.times do
begin
resp = request("GET", "/ping")
return if resp.body.to_s.include?("PONG")
last_error = "status: %s: %s" % [status_text(resp), error_detail(resp)]
rescue StandardError => e
last_error = e.message
end
sleep 0.5
end
abort_red("Problem connecting to ACH service at %s/ping: %s" % [ACH_URL, last_error])
end
def create_payroll_file
file_id = SecureRandom.hex
resp = request("POST", "/files/create", body: build_file(file_id).to_json)
error = json_error(resp)
unless resp.code == "200" && error.nil?
abort_red("Problem creating ACH file (status: %s): %s" % [status_text(resp), error || error_detail(resp)])
end
created = parse_json(resp)
id = created.is_a?(Hash) ? created["id"] : nil
if id.nil? || id.to_s.empty?
abort_red("Problem creating ACH file: response did not include an id: %s" % resp.body)
end
id
end
def write_contents(file_id)
resp = request("GET", "/files/%s/contents" % file_id)
unless resp.code == "200"
abort_red("Problem downloading ACH file contents (status: %s): %s" % [status_text(resp), error_detail(resp)])
end
FileUtils.mkdir_p(File.dirname(OUTPUT_PATH))
File.write(OUTPUT_PATH, resp.body)
puts "Wrote NACHA file to %s" % OUTPUT_PATH
end
def validate_file(file_id)
resp = request("GET", "/files/%s/validate" % file_id)
error = json_error(resp)
unless resp.code == "200" && error.nil?
abort_red("Problem validating ACH file (status: %s): %s" % [status_text(resp), error || error_detail(resp)])
end
puts "File validated successfully"
end
def build_file(file_id)
now = Time.now
effective = next_weekday(Date.today)
entries = EMPLOYEES.each_with_index.map do |employee, index|
routing = employee[:routing_number]
{
id: "%s-%d" % [file_id, index + 1],
transactionCode: 22,
RDFIIdentification: routing[0, 8],
checkDigit: routing[8, 1],
DFIAccountNumber: employee[:account_number],
amount: employee[:amount],
identificationNumber: employee[:identification_number],
individualName: employee[:individual_name],
discretionaryData: "",
addendaRecordIndicator: 0,
traceNumber: "%s%07d" % [ODFI_IDENTIFICATION, index + 1],
category: "Forward",
}
end
entry_count = entries.length
total_credit = EMPLOYEES.sum { |employee| employee[:amount] }
entry_hash = EMPLOYEES.sum { |employee| employee[:routing_number][0, 8].to_i } % 10_000_000_000
record_count = 4 + entry_count
block_count = (record_count + 9) / 10
{
id: file_id,
fileHeader: {
id: file_id,
immediateDestinationName: "Citadel",
immediateDestination: IMMEDIATE_DESTINATION,
immediateOriginName: "Wells Fargo",
immediateOrigin: IMMEDIATE_ORIGIN,
fileCreationDate: now.strftime("%y%m%d"),
fileCreationTime: now.strftime("%H%M"),
fileIDModifier: "A",
},
batches: [
{
batchHeader: {
id: "%s-1" % file_id,
serviceClassCode: 220,
companyName: "Wells Fargo",
companyIdentification: IMMEDIATE_ORIGIN,
standardEntryClassCode: "PPD",
companyEntryDescription: "PAYROLL",
effectiveEntryDate: effective.strftime("%y%m%d"),
originatorStatusCode: 1,
ODFIIdentification: ODFI_IDENTIFICATION,
batchNumber: 1,
},
entryDetails: entries,
batchControl: {
id: "%s-1" % file_id,
serviceClassCode: 220,
entryAddendaCount: entry_count,
entryHash: entry_hash,
totalDebit: 0,
totalCredit: total_credit,
companyIdentification: IMMEDIATE_ORIGIN,
ODFIIdentification: ODFI_IDENTIFICATION,
},
},
],
fileControl: {
id: file_id,
batchCount: 1,
blockCount: block_count,
entryAddendaCount: entry_count,
entryHash: entry_hash,
totalDebit: 0,
totalCredit: total_credit,
},
}
end
def request(method, path, body: nil)
uri = URI.parse(ACH_URL + path)
req = case method
when "GET"
Net::HTTP::Get.new(uri)
when "POST"
Net::HTTP::Post.new(uri)
else
raise ArgumentError, "unsupported HTTP method: %s" % method
end
if body
req["Content-Type"] = "application/json"
req.body = body
end
Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https") do |http|
http.request(req)
end
rescue StandardError => e
raise e, "%s %s: %s" % [method, uri, e.message]
end
def parse_json(resp)
JSON.parse(resp.body)
rescue JSON::ParserError
nil
end
def json_error(resp)
data = parse_json(resp)
return nil unless data.is_a?(Hash)
data["error"]
end
def error_detail(resp)
error = json_error(resp)
return error unless error.nil? || error.to_s.empty?
body = resp.body.to_s.strip
return body unless body.empty?
resp.msg
end
def status_text(resp)
"%s %s" % [resp.code, resp.msg]
end
def abort_red(message)
puts Rainbow(message).red
exit 1
end
main