Skip to content

Instantly share code, notes, and snippets.

@pbrdmn
Last active March 13, 2023 21:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pbrdmn/378f9f05f92bda64b1ebd6c1e3d74545 to your computer and use it in GitHub Desktop.
Save pbrdmn/378f9f05f92bda64b1ebd6c1e3d74545 to your computer and use it in GitHub Desktop.
require './ctb'
require 'csv'
## New CTB Instance, write empty file
ctb = CTB.new("Organisation", "ID_123", "A11111111")
ctb_output_file = "ctb-#{Time.now.strftime("%Y%m%d")}.txt"
File.write(ctb_output_file, '');
# Define input files
svb_input_file = 'svb.txt'
# Loop through row in the SVB file
CSV.foreach(svb_input_file) do |record|
if record[0] == "R"
# Write CTB entry from CSV data
File.write(ctb_output_file, ctb.entry(
# record[6],
record[3],
record[4],
record[7],
record[1],
record[2]
), mode: 'a');
end
end
class CTB
RECORD_LENGTH = 85
SEPARATOR = '#'
TERMINATOR = "\r\n"
def initialize (company_name, company_id, company_account, date = Time.now.strftime("%Y%m%d"))
@company_name = company_name
@company_id = company_id
@company_account = company_account
@date = date
end
def entry (
receiver_bank_code,
receiver_account,
amount,
username,
receiver_name
)
r = Array.new(RECORD_LENGTH)
r[2] = "US" # Must contain "US"
r[3] = "ACH" # Must contain "ACH"
r[4] = @date # Value date
r[10] = format('%.2f', amount.to_f) # Must have explicit decimal - 8.2 format
r[12] = @company_account # DEBIT_ACCOUNT - 8 digits
r[23] = r[4] # Processing date
r[25] = "" # Transaction Reference Number
r[30] = "PPD" # PPD or CCD
r[31] = "Payment" # Entry Description (10)
r[32] = username # (15)
r[33] = "Y" # "Y" if payment amount = 0
r[35] = @company_name # Originating Company Name
r[36] = @company_id # Originating Company ID
r[44] = receiver_account # Beneficiary Account or other id (17x)
r[45] = receiver_name # Beneficiary Name (30x)
r[51] = receiver_bank_code # Beneficiary Bank Routing Code
# remove element [0] because these fields are 1-indexed
r.shift
r.join(SEPARATOR) + TERMINATOR
end
end
require_relative 'ctb'
RSpec.describe CTB do
before(:each) do
@date = "19991215"
@ctb = CTB.new("ABC Corp", "123456789", "98765432", @date)
end
describe "#entry" do
it "generates a correct CTB line for a given set of inputs" do
receiver_bank_code = "12345678"
receiver_account = "9876543210"
amount = 100.0
username = "johndoe"
receiver_name = "Jane Doe"
expected_line = "#US#ACH##{@date}######100.00##98765432############{@date}#######PPD#Payment##{username}#Y##ABC Corp#123456789########9876543210#Jane Doe######12345678#################################\r\n"
expect(@ctb.entry(receiver_bank_code, receiver_account, amount, username, receiver_name)).to eq(expected_line)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment