Skip to content

Commit

Permalink
Merge pull request #198 from gocardless/joesouthan/fix-ca-numbers
Browse files Browse the repository at this point in the history
Fix US and CA account number padding
  • Loading branch information
JoeSouthan authored Mar 11, 2021
2 parents b14a679 + b093781 commit 646e27c
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 38 deletions.
4 changes: 2 additions & 2 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ inherit_gem:
AllCops:
TargetRubyVersion: 2.4

# Limit lines to 80 characters.
# Limit lines to 90 characters.
Layout/LineLength:
Max: 80
Max: 90

Metrics/ClassLength:
Max: 400
Expand Down
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
## 1.5.0 - March 11, 2021

- Fix issue with padding on Canadian and USA pseudo IBANs #198

## 1.4.1 - March 3, 2021

- Update BLZ data - BLZVZ FTSEX MD 652


## 1.4.0 - February 22, 2021

- [Breaking] Correct `swift_national_id` for Canadian Psudo Ibans. Before `swift_national_id`
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ iban.iban # => nil
iban = Ibandit::IBAN.new('USZZ026073150_______2715500356')
iban.country_code # => "US"
iban.bank_code # => "026073150"
iban.account_number # => "_______2715500356"
iban.account_number # => "2715500356"
iban.iban # => nil
```

Expand Down
10 changes: 8 additions & 2 deletions data/structures.yml
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,10 @@ AU:
CA:
:bank_code_length: 4
:branch_code_length: 5
:account_number_length: 12
:account_number_length: !ruby/range
begin: 7
end: 12
excl: false
:bank_code_format: "\\d{4}"
:branch_code_format: "\\d{5}"
:account_number_format: "\\d{7,12}"
Expand All @@ -1015,7 +1018,10 @@ NZ:
US:
:bank_code_length: 9
:branch_code_length: 0
:account_number_length: 17
:account_number_length: !ruby/range
begin: 1
end: 17
excl: false
:bank_code_format: "\\d{9}"
:account_number_format: "_*\\d{1,17}"
:national_id_length: 9
Expand Down
4 changes: 2 additions & 2 deletions ibandit.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ require File.expand_path("lib/ibandit/version", __dir__)
Gem::Specification.new do |gem|
gem.add_development_dependency "gc_ruboconfig", "~> 2.24.0"
gem.add_development_dependency "nokogiri", "~> 1.6"
gem.add_development_dependency "pry", "~> 0.10"
gem.add_development_dependency "pry-nav", "~> 0.2"
gem.add_development_dependency "pry", "~> 0.13"
gem.add_development_dependency "pry-byebug", "~> 3.9"
gem.add_development_dependency "rspec", "~> 3.3"
gem.add_development_dependency "rspec-its", "~> 1.2"
gem.add_development_dependency "rspec_junit_formatter", "~> 0.4.1"
Expand Down
13 changes: 8 additions & 5 deletions lib/ibandit/iban.rb
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,7 @@ def valid_bank_code_length?

def valid_branch_code_length?
return unless valid_country_code?
if swift_branch_code.to_s.length == structure[:branch_code_length]
return true
end
return true if swift_branch_code.to_s.length == structure[:branch_code_length]

if structure[:branch_code_length]&.zero?
@errors[:branch_code] = Ibandit.translate(:not_used_in_country,
Expand All @@ -199,8 +197,13 @@ def valid_account_number_length?
return false
end

if swift_account_number.length == structure[:account_number_length]
return true
case structure[:account_number_length]
when Range
if structure[:account_number_length].include?(swift_account_number.length)
return true
end
else
return true if swift_account_number.length == structure[:account_number_length]
end

@errors[:account_number] =
Expand Down
10 changes: 4 additions & 6 deletions lib/ibandit/local_details_cleaner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def self.clean_be_details(local_details)
def self.clean_ca_details(local_details)
account_number = local_details[:account_number].tr("-", "")

return {} if account_number.length < 7 # minimum length
return {} unless (7..12).cover?(account_number.length)

bank_code = if local_details[:bank_code].length == 3
local_details[:bank_code].rjust(4, "0")
Expand All @@ -100,7 +100,7 @@ def self.clean_ca_details(local_details)
end

{
account_number: account_number.rjust(12, "0"),
account_number: account_number,
bank_code: bank_code,
}
end
Expand All @@ -113,7 +113,7 @@ def self.clean_us_details(local_details)

{
bank_code: local_details[:bank_code],
account_number: account_number.rjust(17, "_"),
account_number: account_number,
}
end

Expand Down Expand Up @@ -330,9 +330,7 @@ def self.clean_hr_details(local_details)
def self.clean_hu_details(local_details)
# This method supports being passed the component IBAN parts, as defined
# by SWIFT, or a single 16 or 24 digit string.
if local_details[:bank_code] || local_details[:branch_code]
return local_details
end
return local_details if local_details[:bank_code] || local_details[:branch_code]

cleaned_acct_number = local_details[:account_number].gsub(/[-\s]/, "")

Expand Down
2 changes: 1 addition & 1 deletion lib/ibandit/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Ibandit
VERSION = "1.4.1"
VERSION = "1.5.0"
end
30 changes: 15 additions & 15 deletions spec/ibandit/iban_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -418,12 +418,12 @@
its(:country_code) { is_expected.to eq("CA") }
its(:bank_code) { is_expected.to eq("0036") }
its(:branch_code) { is_expected.to eq("00063") }
its(:account_number) { is_expected.to eq("000000123456") }
its(:account_number) { is_expected.to eq("0123456") }
its(:swift_bank_code) { is_expected.to eq("0036") }
its(:swift_branch_code) { is_expected.to eq("00063") }
its(:swift_account_number) { is_expected.to eq("000000123456") }
its(:swift_account_number) { is_expected.to eq("0123456") }
its(:swift_national_id) { is_expected.to eq("003600063") }
its(:pseudo_iban) { is_expected.to eq("CAZZ003600063000000123456") }
its(:pseudo_iban) { is_expected.to eq("CAZZ003600063_____0123456") }

its(:iban) { is_expected.to be_nil }
its(:valid?) { is_expected.to eq(true) }
Expand All @@ -437,12 +437,12 @@
its(:country_code) { is_expected.to eq("CA") }
its(:bank_code) { is_expected.to eq("36") }
its(:branch_code) { is_expected.to eq("00063") }
its(:account_number) { is_expected.to eq("000000123456") }
its(:account_number) { is_expected.to eq("0123456") }
its(:swift_bank_code) { is_expected.to eq("36") }
its(:swift_branch_code) { is_expected.to eq("00063") }
its(:swift_account_number) { is_expected.to eq("000000123456") }
its(:swift_account_number) { is_expected.to eq("0123456") }
its(:swift_national_id) { is_expected.to eq("3600063") }
its(:pseudo_iban) { is_expected.to eq("CAZZ__3600063000000123456") }
its(:pseudo_iban) { is_expected.to eq("CAZZ__3600063_____0123456") }

its(:iban) { is_expected.to be_nil }
its(:valid?) { is_expected.to eq(false) }
Expand All @@ -456,12 +456,12 @@
its(:country_code) { is_expected.to eq("CA") }
its(:bank_code) { is_expected.to eq("0036") }
its(:branch_code) { is_expected.to eq("00063") }
its(:account_number) { is_expected.to eq("000000123456") }
its(:account_number) { is_expected.to eq("0123456") }
its(:swift_bank_code) { is_expected.to eq("0036") }
its(:swift_branch_code) { is_expected.to eq("00063") }
its(:swift_account_number) { is_expected.to eq("000000123456") }
its(:swift_account_number) { is_expected.to eq("0123456") }
its(:swift_national_id) { is_expected.to eq("003600063") }
its(:pseudo_iban) { is_expected.to eq("CAZZ003600063000000123456") }
its(:pseudo_iban) { is_expected.to eq("CAZZ003600063_____0123456") }

its(:iban) { is_expected.to be_nil }
its(:valid?) { is_expected.to eq(true) }
Expand Down Expand Up @@ -501,11 +501,11 @@
its(:country_code) { is_expected.to eq("CA") }
its(:bank_code) { is_expected.to eq("0036") }
its(:branch_code) { is_expected.to eq("00063") }
its(:account_number) { is_expected.to eq("000000123456") }
its(:account_number) { is_expected.to eq("0123456") }
its(:swift_bank_code) { is_expected.to eq("0036") }
its(:swift_branch_code) { is_expected.to eq("00063") }
its(:swift_account_number) { is_expected.to eq("000000123456") }
its(:pseudo_iban) { is_expected.to eq("CAZZ003600063000000123456") }
its(:swift_account_number) { is_expected.to eq("0123456") }
its(:pseudo_iban) { is_expected.to eq("CAZZ003600063_____0123456") }

its(:iban) { is_expected.to be_nil }
its(:valid?) { is_expected.to be true }
Expand All @@ -518,7 +518,7 @@
it "is invalid and has the correct errors" do
expect(subject.valid?).to eq(false)
expect(subject.errors).
to eq(account_number: "is the wrong length (should be 12 characters)")
to eq(account_number: "is the wrong length (should be 7..12 characters)")
end
end

Expand Down Expand Up @@ -709,10 +709,10 @@

its(:country_code) { is_expected.to eq("US") }
its(:bank_code) { is_expected.to eq(bank_code) }
its(:account_number) { is_expected.to eq("__________0123456") }
its(:account_number) { is_expected.to eq("0123456") }
its(:swift_bank_code) { is_expected.to eq(bank_code) }
its(:swift_branch_code) { is_expected.to eq(nil) }
its(:swift_account_number) { is_expected.to eq("__________0123456") }
its(:swift_account_number) { is_expected.to eq("0123456") }
its(:swift_national_id) { is_expected.to eq(bank_code) }

its(:pseudo_iban) do
Expand Down
6 changes: 3 additions & 3 deletions spec/ibandit/local_details_cleaner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,15 +163,15 @@
let(:bank_code) { "0036" }
let(:branch_code) { "00063" }

its([:account_number]) { is_expected.to eq("000000123456") }
its([:account_number]) { is_expected.to eq("0123456") }
its([:country_code]) { is_expected.to eq(country_code) }
its([:bank_code]) { is_expected.to eq("0036") }
its([:branch_code]) { is_expected.to eq("00063") }

context "with a hyphen" do
let(:account_number) { "0123456-789" }

its([:account_number]) { is_expected.to eq("000123456789") }
its([:account_number]) { is_expected.to eq("0123456789") }
end
end

Expand Down Expand Up @@ -1311,7 +1311,7 @@
let(:account_number) { "0123456789" }

its([:bank_code]) { is_expected.to eq(bank_code) }
its([:account_number]) { is_expected.to eq("_______0123456789") }
its([:account_number]) { is_expected.to eq("0123456789") }
end
end
end

0 comments on commit 646e27c

Please sign in to comment.