bankserv 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/lib/bankserv.rb +1 -1
  2. data/lib/bankserv/account_holder_verification.rb +3 -9
  3. data/lib/bankserv/credit.rb +13 -2
  4. data/lib/bankserv/debit.rb +12 -2
  5. data/lib/bankserv/eft.rb +0 -8
  6. data/lib/bankserv/engine.rb +11 -12
  7. data/lib/bankserv/request.rb +1 -0
  8. data/lib/bankserv/service.rb +88 -0
  9. data/lib/bankserv/transmission/input_document.rb +18 -16
  10. data/lib/bankserv/transmission/set/account_holder_verification.rb +22 -2
  11. data/lib/bankserv/transmission/set/credit.rb +8 -0
  12. data/lib/bankserv/transmission/set/debit.rb +11 -0
  13. data/lib/bankserv/transmission/set/document.rb +2 -2
  14. data/lib/bankserv/transmission/set/eft.rb +9 -8
  15. data/lib/bankserv/version.rb +1 -1
  16. data/lib/generators/active_record/templates/migration.rb +17 -11
  17. data/spec/examples/INPUT.120411110604.txt +1 -1
  18. data/spec/examples/INPUT.120411124123.txt +8 -0
  19. data/spec/factories.rb +0 -15
  20. data/spec/internal/db/schema.rb +4 -11
  21. data/spec/lib/bankserv/account_holder_verification_spec.rb +8 -7
  22. data/spec/lib/bankserv/credit_spec.rb +8 -4
  23. data/spec/lib/bankserv/debit_spec.rb +8 -6
  24. data/spec/lib/bankserv/engine/engine_spec.rb +35 -28
  25. data/spec/lib/bankserv/service_spec.rb +27 -0
  26. data/spec/lib/bankserv/transmission/input_document_spec.rb +13 -37
  27. data/spec/lib/bankserv/transmission/output_document_spec.rb +4 -3
  28. data/spec/lib/bankserv/transmission/reply_document_spec.rb +3 -3
  29. data/spec/lib/bankserv/transmission/set/account_holder_verification_spec.rb +11 -11
  30. data/spec/lib/bankserv/transmission/set/credit_spec.rb +10 -10
  31. data/spec/lib/bankserv/transmission/set/debit_spec.rb +2 -2
  32. data/spec/lib/bankserv/transmission/statement_spec.rb +1 -1
  33. data/spec/support/helpers.rb +32 -13
  34. metadata +19 -17
  35. data/lib/bankserv/configuration.rb +0 -83
  36. data/spec/lib/bankserv/configuration_spec.rb +0 -34
data/lib/bankserv.rb CHANGED
@@ -14,7 +14,7 @@ module Bankserv
14
14
 
15
15
  end
16
16
 
17
- require "bankserv/configuration"
17
+ require "bankserv/service"
18
18
  require "bankserv/eft"
19
19
  require "bankserv/request"
20
20
  require "bankserv/bank_account"
@@ -1,7 +1,6 @@
1
1
  module Bankserv
2
2
 
3
3
  class AccountHolderVerification < ActiveRecord::Base
4
-
5
4
  self.inheritance_column = :_type_disabled
6
5
  serialize :response
7
6
 
@@ -22,14 +21,9 @@ module Bankserv
22
21
  def self.generate_reference_number(ahv)
23
22
  "AHV#{ahv.id}"
24
23
  end
25
-
26
- def self.request(options = {})
27
- options.merge!(type: 'ahv')
28
- Request.create!(options)
29
- end
30
24
 
31
- def self.test_request(options)
32
- Request.create!(options.merge(test: true))
25
+ def self.service
26
+ Bankserv::AHVService.where(active: true).last
33
27
  end
34
28
 
35
29
  def self.for_reference(reference)
@@ -42,7 +36,7 @@ module Bankserv
42
36
 
43
37
  def self.build!(options)
44
38
  bank_account = BankAccount.new options[:bank_account].filter_attributes(BankAccount)
45
- is_internal = bank_account.branch_code == Bankserv::Configuration.internal_branch_code
39
+ is_internal = bank_account.branch_code == self.service.config[:internal_branch_code]
46
40
  options = options.filter_attributes(self).merge(bank_account: bank_account, internal: is_internal)
47
41
 
48
42
  create!(options)
@@ -11,6 +11,14 @@ module Bankserv
11
11
 
12
12
  after_create :generate_internal_user_ref
13
13
 
14
+ def self.bankserv_service
15
+ Bankserv::CreditService.where(active: true).last
16
+ end
17
+
18
+ def bankserv_service
19
+ Bankserv::Credit.bankserv_service
20
+ end
21
+
14
22
  def generate_internal_user_ref
15
23
  self.internal_user_ref = "CREDIT#{id}"
16
24
  save!
@@ -49,10 +57,13 @@ module Bankserv
49
57
  end
50
58
 
51
59
  def formatted_user_ref
60
+ abbreviated_name = bankserv_service.config[:client_abbreviated_name]
61
+
52
62
  if contra?
53
- Configuration.client_abbreviated_name.ljust(10, ' ') << "CONTRA#{user_ref}"
63
+ bankserv_service.config[:client_abbreviated_name]
64
+ abbreviated_name.ljust(10, ' ') << "CONTRA#{user_ref}"
54
65
  else
55
- Configuration.client_abbreviated_name.ljust(10, ' ') << user_ref
66
+ abbreviated_name.ljust(10, ' ') << user_ref
56
67
  end
57
68
  end
58
69
 
@@ -11,6 +11,14 @@ module Bankserv
11
11
 
12
12
  after_create :generate_internal_user_ref
13
13
 
14
+ def self.bankserv_service
15
+ Bankserv::DebitService.where(active: true).last
16
+ end
17
+
18
+ def bankserv_service
19
+ Bankserv::Debit.bankserv_service
20
+ end
21
+
14
22
  def generate_internal_user_ref
15
23
  self.internal_user_ref = "DEBIT#{id}"
16
24
  save!
@@ -25,10 +33,12 @@ module Bankserv
25
33
  end
26
34
 
27
35
  def formatted_user_ref
36
+ abbreviated_name = bankserv_service.config[:client_abbreviated_name]
37
+
28
38
  if contra?
29
- Configuration.client_abbreviated_name.ljust(10, ' ') << "CONTRA#{user_ref}"
39
+ abbreviated_name.ljust(10, ' ') << "CONTRA#{user_ref}"
30
40
  else
31
- Configuration.client_abbreviated_name.ljust(10, ' ') << user_ref
41
+ abbreviated_name.ljust(10, ' ') << user_ref
32
42
  end
33
43
  end
34
44
 
data/lib/bankserv/eft.rb CHANGED
@@ -4,14 +4,6 @@ module Bankserv
4
4
  # Any change here will ripple down...
5
5
  attr_accessor :request_id
6
6
 
7
- def request(options)
8
- Request.create!(options)
9
- end
10
-
11
- def test_request(options)
12
- Request.create!(options.merge(test: true))
13
- end
14
-
15
7
  def build!(options)
16
8
  @request_id = options[:bankserv_request_id]
17
9
 
@@ -66,23 +66,22 @@ module Bankserv
66
66
  end
67
67
  end
68
68
 
69
- def process_input_files
70
- unless self.expecting_reply_file?
69
+ def process_input_files
70
+ Bankserv::Service.active.each do |bankserv_service|
71
71
  begin
72
- document = Bankserv::InputDocument.generate!(
73
- client_code: Bankserv::Configuration.client_code,
74
- client_name: Bankserv::Configuration.client_name,
75
- th_for_use_of_ld_user: ""
76
- )
77
- @logs[:input_files] << "Input Document created with id: #{document.id}"
72
+ next if self.expecting_reply_file? # TODO: expecting per service
73
+
74
+ if document = Bankserv::InputDocument.generate!(bankserv_service)
75
+ @logs[:input_files] << "Input Document created with id: #{document.id}" if document
76
+
77
+ if self.write_file!(document)
78
+ document.mark_processed!
79
+ end
80
+ end
78
81
  rescue Exception => e
79
82
  @logs[:input_files] << "Error occured! #{e.message}"
80
83
  @success = false
81
84
  end
82
-
83
- if self.write_file!(document)
84
- document.mark_processed!
85
- end
86
85
  end
87
86
  end
88
87
 
@@ -1,6 +1,7 @@
1
1
  module Bankserv
2
2
 
3
3
  class Request < ActiveRecord::Base
4
+ belongs_to :service
4
5
  serialize :data
5
6
 
6
7
  self.inheritance_column = :_type_disabled
@@ -0,0 +1,88 @@
1
+ module Bankserv
2
+
3
+ class Service < ActiveRecord::Base
4
+
5
+ has_many :requests
6
+ serialize :config
7
+
8
+ def self.register(params)
9
+ s = new
10
+ s.active = true
11
+ s.client_code = params.delete(:client_code)
12
+ s.config = params
13
+ s.save!
14
+ s
15
+ end
16
+
17
+ def self.active
18
+ where(active: true)
19
+ end
20
+
21
+ def set_generation_number!(number)
22
+ self.config[:generation_number] = number
23
+ save!
24
+ end
25
+
26
+ def reserve_generation_number!
27
+ reserved = config[:generation_number] || 1
28
+ set_generation_number!(reserved + 1)
29
+ reserved
30
+ end
31
+
32
+ def sequence_number
33
+ sequence_number = config[:sequence_number] || 1
34
+ sequence_number = 1 unless (config[:sequence_number_updated_at] || Time.now).to_date == Date.today
35
+ sequence_number
36
+ end
37
+
38
+ def set_sequence_number!(number)
39
+ self.config[:sequence_number] = number
40
+ self.config[:sequence_number_updated_at] = Time.now
41
+ save!
42
+ end
43
+
44
+ def reserve_sequence_number!(reserved = nil)
45
+ reserved ||= sequence_number
46
+ set_sequence_number!(reserved.to_i + 1)
47
+ reserved
48
+ end
49
+
50
+ def is_test_env?
51
+ config[:transmission_status] == "T"
52
+ end
53
+
54
+ end
55
+
56
+ class DebitService < Service
57
+ def request(params)
58
+ params.merge!(type: 'debit', service_id: id)
59
+ params.merge!(test: true) if is_test_env?
60
+ Request.create!(params)
61
+ end
62
+ end
63
+
64
+ class CreditService < Service
65
+ def request(params)
66
+ params.merge!(type: 'credit', service_id: id)
67
+ params.merge!(test: true) if is_test_env?
68
+ Request.create!(params)
69
+ end
70
+ end
71
+
72
+ class AHVService < Service
73
+ def request(params)
74
+ params.merge!(type: 'ahv', service_id: id)
75
+ params.merge!(test: true) if is_test_env?
76
+ Request.create!(params)
77
+ end
78
+ end
79
+
80
+ class StatementService < Service
81
+ def request(params)
82
+ params.merge!(type: 'statement', service_id: id)
83
+ params.merge!(test: true) if is_test_env?
84
+ Request.create!(params)
85
+ end
86
+ end
87
+
88
+ end
@@ -16,11 +16,6 @@ class Bankserv::InputDocument < Bankserv::Document
16
16
  document
17
17
  end
18
18
 
19
- def self.fetch_next_transmission_number
20
- transmission_status = Bankserv::Configuration.live_env? ? "L" : "T"
21
- where(type: 'input', reply_status: 'ACCEPTED', transmission_status: transmission_status).maximum(:transmission_number) or "1"
22
- end
23
-
24
19
  def self.sets_with_work
25
20
  defined_input_sets.select(&:has_work?)
26
21
  end
@@ -37,18 +32,22 @@ class Bankserv::InputDocument < Bankserv::Document
37
32
  sets_with_test_work.any?
38
33
  end
39
34
 
40
- def self.generate_test!(options = {})
41
- build!(options.merge(rec_status: "T")) if has_test_work?
42
- end
43
-
44
- def self.generate!(options = {})
45
- build!(options.merge(rec_status: "L")) if has_work?
46
- end
47
-
48
- def self.build!(options = {}) # move to private
49
- options[:transmission_no] ||= fetch_next_transmission_number
35
+ def self.generate!(bankserv_service)
36
+ if bankserv_service.is_test_env?
37
+ return unless has_test_work?
38
+ else
39
+ return unless has_work?
40
+ end
50
41
 
51
- transmission_status = Bankserv::Configuration.live_env? ? "L" : "T"
42
+ options = {}
43
+ options.merge! rec_status: bankserv_service.config[:transmission_status]
44
+ options.merge! client_code: bankserv_service.client_code
45
+ options.merge! client_name: bankserv_service.config[:client_name]
46
+ options.merge! th_for_use_of_ld_user: ""
47
+
48
+ transmission_status = bankserv_service.config[:transmission_status]
49
+ raise "Transmission status not specified" if transmission_status.nil?
50
+ options[:transmission_no] ||= bankserv_service.config[:transmission_number]
52
51
 
53
52
  document = new(transmission_status: transmission_status, rec_status: options[:rec_status], type: 'input', transmission_number: options[:transmission_no])
54
53
  document.set = Bankserv::Transmission::UserSet::Document.generate(options.merge(rec_status: document.rec_status))
@@ -60,6 +59,9 @@ class Bankserv::InputDocument < Bankserv::Document
60
59
  sets_with_test_work
61
60
  end
62
61
 
62
+ input_sets.select!{|s| s.bankserv_service.client_code == bankserv_service.client_code}
63
+ return unless input_sets.count > 0
64
+
63
65
  input_sets.each do |set|
64
66
  document.set.sets << set.generate(rec_status: document.rec_status)
65
67
  document.set.sets[-1].set = document.set # whaaaaaa?
@@ -61,11 +61,31 @@ module Bankserv
61
61
  end
62
62
 
63
63
  private
64
+
65
+ # def bankserv_service
66
+ # Bankserv::Service.where(active: true, type: 'ahv').last
67
+ # end
68
+ #
69
+ # def self.bankserv_service
70
+ # Bankserv::Service.where(active: true, type: 'ahv').last
71
+ # end
72
+
73
+ def bankserv_service
74
+ self.class.bankserv_service
75
+ #Bankserv::Transmission::UserSet::AccountHolderVerification.bankserv_service
76
+ #Bankserv::Service.where(active: true, type: 'debit').last
77
+ end
78
+
79
+
80
+ def self.bankserv_service
81
+ Bankserv::AHVService.where(active: true).last
82
+ #Bankserv::Service.where(active: true, type: 'debit').last
83
+ end
64
84
 
65
85
  def set_header
66
- self.generation_number = Bankserv::Configuration.reserve_user_generation_number!.to_s
86
+ self.generation_number = bankserv_service.reserve_generation_number!.to_s
67
87
  header.data[:gen_no] = generation_number
68
- header.data[:dept_code] = Bankserv::Configuration.department_code
88
+ header.data[:dept_code] = bankserv_service.config[:department_code]
69
89
  header.save!
70
90
  end
71
91
 
@@ -38,6 +38,14 @@ module Bankserv
38
38
  def credit_records
39
39
  standard_records
40
40
  end
41
+
42
+ def bankserv_service
43
+ Bankserv::Transmission::UserSet::Credit.bankserv_service
44
+ end
45
+
46
+ def self.bankserv_service
47
+ Bankserv::CreditService.where(active: true).last
48
+ end
41
49
 
42
50
  end
43
51
  end
@@ -38,6 +38,17 @@ module Bankserv
38
38
  def credit_records
39
39
  contra_records
40
40
  end
41
+
42
+ def bankserv_service
43
+ Bankserv::Transmission::UserSet::Debit.bankserv_service
44
+ #Bankserv::Service.where(active: true, type: 'debit').last
45
+ end
46
+
47
+
48
+ def self.bankserv_service
49
+ Bankserv::DebitService.where(active: true).last
50
+ #Bankserv::Service.where(active: true, type: 'debit').last
51
+ end
41
52
 
42
53
  end
43
54
  end
@@ -15,8 +15,8 @@ module Bankserv
15
15
 
16
16
  def build_header(options = {})
17
17
  defaults = {
18
- client_code: Bankserv::Configuration.active.client_code,
19
- client_name: Bankserv::Configuration.active.client_name,
18
+ client_code: options[:client_code],
19
+ client_name: options[:client_name],
20
20
  date: Date.today.strftime("%Y%m%d"),
21
21
  destination: "0"
22
22
  }
@@ -27,7 +27,7 @@ module Bankserv
27
27
  efts = self.unprocessed_efts(options[:rec_status])
28
28
 
29
29
  if efts.count > 0
30
- set = self.new
30
+ set = self.new
31
31
  set.type_of_service = efts.first.request.data[:type_of_service]
32
32
  set.accepted_report = efts.first.request.data[:accepted_report] || ""
33
33
  set.account_type_correct = efts.first.request.data[:account_type_correct] || ""
@@ -56,7 +56,7 @@ module Bankserv
56
56
  end
57
57
 
58
58
  def get_user_generation_number
59
- Bankserv::Configuration.reserve_user_generation_number!.to_s #Equal to the last accepted user gen number + 1
59
+ bankserv_service.reserve_generation_number!.to_s #Equal to the last accepted user gen number + 1
60
60
  end
61
61
 
62
62
  def build_header(options = {})
@@ -100,7 +100,7 @@ module Bankserv
100
100
  rec_id: rec_id,
101
101
  user_nominated_account: transaction.contra_bank_details.account_number,
102
102
  user_branch: transaction.contra_bank_details.branch_code,
103
- user_code: Bankserv::Configuration.active.user_code,
103
+ user_code: bankserv_service.config[:user_code],
104
104
  bankserv_record_identifier: standard_bankserv_record_identifier,
105
105
  homing_branch: homing_branch_code,
106
106
  homing_account_number: homing_account_number.length <= 11 ? homing_account_number : "0",
@@ -125,7 +125,7 @@ module Bankserv
125
125
  bankserv_record_identifier: contra_bankserv_record_identifier,
126
126
  user_branch: transaction.bank_account.branch_code,
127
127
  user_nominated_account: transaction.bank_account.account_number.to_i.to_s,
128
- user_code: Bankserv::Configuration.active.user_code,
128
+ user_code: bankserv_service.config[:user_code],
129
129
  homing_branch: transaction.bank_account.branch_code,
130
130
  homing_account_number: transaction.bank_account.account_number,
131
131
  type_of_account: "1",
@@ -180,7 +180,7 @@ module Bankserv
180
180
  private
181
181
 
182
182
  def set_sequence_numbers
183
- sequence_number = (header.data[:first_sequence_number] || Bankserv::Configuration.reserve_eft_sequence_number!).to_i
183
+ sequence_number = (header.data[:first_sequence_number] || bankserv_service.reserve_sequence_number!).to_i
184
184
 
185
185
  header.data[:first_sequence_number] = sequence_number.to_s
186
186
  trailer.data[:first_sequence_number] = sequence_number.to_s
@@ -191,11 +191,12 @@ module Bankserv
191
191
  end
192
192
 
193
193
  trailer.data[:last_sequence_number] = (sequence_number - 1).to_s
194
- Bankserv::Configuration.reserve_eft_sequence_number!(sequence_number - 1)
194
+
195
+ bankserv_service.reserve_sequence_number!(sequence_number - 1)
195
196
  end
196
197
 
197
198
  def decorate_header
198
- header.data[:bankserv_user_code] = Bankserv::Configuration.active.user_code
199
+ header.data[:bankserv_user_code] = bankserv_service.config[:user_code]
199
200
  header.data[:bankserv_purge_date] = purge_date
200
201
  header.data[:first_action_date] = first_action_date
201
202
  header.data[:last_action_date] = last_action_date
@@ -203,7 +204,7 @@ module Bankserv
203
204
  end
204
205
 
205
206
  def decorate_trailer
206
- trailer.data[:bankserv_user_code] = Bankserv::Configuration.active.user_code
207
+ trailer.data[:bankserv_user_code] = header.data[:bankserv_user_code]
207
208
  trailer.data[:first_action_date] = first_action_date
208
209
  trailer.data[:last_action_date] = last_action_date
209
210
  trailer.data[:no_debit_records] = no_debit_records