Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add bank account validation service

  • Loading branch information...
commit 219e64c88b31faaa35b873e4189ab15fb68a0e30 1 parent 3b131b3
Morgan Christiansson authored
View
1  Gemfile
@@ -10,4 +10,5 @@ group :development do
gem "bundler"
gem "jeweler", "~> 1.6.2"
gem "rcov", ">= 0"
+ gem "webmock", :require => false
end
View
5 Gemfile.lock
@@ -3,6 +3,7 @@ GEM
specs:
activesupport (3.1.1)
multi_json (~> 1.0)
+ addressable (2.2.7)
crack (0.1.8)
diff-lcs (1.1.3)
git (1.2.5)
@@ -24,6 +25,9 @@ GEM
rspec-expectations (2.8.0)
diff-lcs (~> 1.1.2)
rspec-mocks (2.8.0)
+ webmock (1.8.0)
+ addressable (>= 2.2.7)
+ crack (>= 0.1.7)
PLATFORMS
ruby
@@ -36,3 +40,4 @@ DEPENDENCIES
jeweler (~> 1.6.2)
rcov
rspec
+ webmock
View
63 lib/postcode_anywhere/bank_account_validation.rb
@@ -0,0 +1,63 @@
+require 'json'
+#require 'active_support/inflector/methods'
+require 'active_support/core_ext/string/inflections'
+
+module PostcodeAnywhere
+ class BankAccountValidation
+ include HTTParty
+ base_uri 'https://services.postcodeanywhere.co.uk/BankAccountValidation/Interactive/Validate/v2.00/json3.ws'
+ def initialize(key)
+ self.class.default_params :Key => key
+ end
+ def validate(sort_code, account_number)
+ r = self.class.get('', :query => {:SortCode => sort_code, :AccountNumber => account_number})
+ r = JSON.parse(r)
+ raise unless r.length == 1
+ r = r["Items"]
+ raise unless r.length == 1
+ r = r[0]
+
+ r2 = BankAccountResult.new
+ r.each do |k, v|
+ r2.send "#{k.underscore}=", v
+ end
+
+ r2
+ end
+ end
+ class BankAccountResult
+ ATTRIBUTES = [:is_correct,
+ :is_direct_debit_capable,
+ :status_information,
+ :corrected_sort_code,
+ :corrected_account_number,
+ :iban,
+ :bank,
+ :bank_bic,
+ :branch,
+ :branch_bic,
+ :contact_address_line1,
+ :contact_address_line2,
+ :contact_post_town,
+ :contact_postcode,
+ :contact_phone,
+ :contact_fax,
+ :faster_payments_supported,
+ :chaps_supported]
+ attr_accessor *ATTRIBUTES
+
+ def correct?
+ is_correct
+ end
+ def direct_debit_capable?
+ is_direct_debit_capable
+ end
+ def faster_payments_supported?
+ faster_payments_supported
+ end
+ def chaps_supported?
+ chaps_supported
+ end
+ end
+end
+
View
3  lib/ruby-postcodeanywhere.rb
@@ -2,6 +2,7 @@
require 'active_support/core_ext/module/attribute_accessors.rb'
module PostcodeAnywhere
+ autoload :BankAccountValidation, 'postcode_anywhere/bank_account_validation'
# Account codes to access the PostcodeAnywhere Service
mattr_accessor :account_code
@@ -137,5 +138,5 @@ class AddressListItem
attr_accessor :id, :street_address, :place
end
+end
-end
View
64 spec/lib/postcode_anywhere/bank_account_validation_spec.rb
@@ -0,0 +1,64 @@
+require 'spec_helper'
+
+WebMock.disable_net_connect!
+
+describe PostcodeAnywhere::BankAccountValidation do
+ subject { PostcodeAnywhere::BankAccountValidation.new 'key' }
+ let :json_response do
+ <<-JSON
+{
+ "Items": [
+ {
+ "IsCorrect": false,
+ "IsDirectDebitCapable": false,
+ "StatusInformation": "UnknownSortCode",
+ "CorrectedSortCode": "",
+ "CorrectedAccountNumber": "",
+ "IBAN": "",
+ "Bank": "",
+ "BankBIC": "",
+ "Branch": "",
+ "BranchBIC": "",
+ "ContactAddressLine1": "",
+ "ContactAddressLine2": "",
+ "ContactPostTown": "",
+ "ContactPostcode": "",
+ "ContactPhone": "",
+ "ContactFax": "",
+ "FasterPaymentsSupported": false,
+ "CHAPSSupported": false
+ }
+ ]
+}
+ JSON
+ end
+
+ it "sends a valid request when validating a bank account" do
+ stub_request(:get,
+ 'https://services.postcodeanywhere.co.uk/BankAccountValidation/Interactive/Validate/v2.00/json3.ws?AccountNumber=account_number&Key=key&SortCode=sort_code'
+ ).to_return(:body => json_response)
+ ret = subject.validate('sort_code', 'account_number')
+
+ ret.should_not be_correct
+ ret.should_not be_direct_debit_capable
+ ret.status_information.should == 'UnknownSortCode'
+ ret.corrected_sort_code.should == ''
+ ret.corrected_account_number.should == ''
+ ret.iban.should == ''
+ ret.bank.should == ''
+ ret.bank_bic.should == ''
+ ret.branch.should == ''
+ ret.branch_bic.should == ''
+ ret.contact_address_line1.should == ''
+ ret.contact_address_line2.should == ''
+ ret.contact_post_town.should == ''
+ ret.contact_postcode.should == ''
+ ret.contact_phone.should == ''
+ ret.contact_fax.should == ''
+ ret.faster_payments_supported.should == false
+ ret.should_not be_faster_payments_supported
+ ret.chaps_supported.should == false
+ ret.should_not be_chaps_supported
+ end
+end
+
View
1  spec/spec_helper.rb
@@ -1,5 +1,6 @@
require 'rubygems'
require 'bundler/setup'
+require 'webmock/rspec'
require 'ruby-postcodeanywhere'
Please sign in to comment.
Something went wrong with that request. Please try again.