Class: ActiveMerchant::Billing::Check
- Defined in:
- lib/active_merchant/billing/check.rb
Overview
The Check object is a plain old Ruby object, similar to CreditCard. It supports validation of necessary attributes such as checkholder’s name, routing and account numbers, but it is not backed by any database.
You may use Check in place of CreditCard with any gateway that supports it.
Constant Summary collapse
- CAN_INSTITUTION_NUMBERS =
Canadian Institution Numbers Partial list found here: en.wikipedia.org/wiki/Routing_number_(Canada)
%w( 001 002 003 004 006 010 016 030 039 117 127 177 219 245 260 269 270 308 309 310 315 320 338 340 509 540 608 614 623 809 815 819 828 829 837 839 865 879 889 899 241 242 248 250 265 275 277 290 294 301 303 307 311 314 321 323 327 328 330 332 334 335 342 343 346 352 355 361 362 366 370 372 376 378 807 853 890 618 842 )
Instance Attribute Summary collapse
-
#account_holder_type ⇒ Object
Returns the value of attribute account_holder_type.
-
#account_number ⇒ Object
Returns the value of attribute account_number.
-
#account_type ⇒ Object
Returns the value of attribute account_type.
-
#bank_name ⇒ Object
Returns the value of attribute bank_name.
-
#first_name ⇒ Object
Returns the value of attribute first_name.
-
#institution_number ⇒ Object
Used for Canadian bank accounts.
-
#last_name ⇒ Object
Returns the value of attribute last_name.
-
#number ⇒ Object
Returns the value of attribute number.
-
#routing_number ⇒ Object
Returns the value of attribute routing_number.
-
#transit_number ⇒ Object
Used for Canadian bank accounts.
Instance Method Summary collapse
-
#checksum(digits) ⇒ Object
Routing numbers may be validated by calculating a checksum and dividing it by 10.
- #credit_card? ⇒ Boolean
-
#electronic_format_routing_number ⇒ Object
Always return electronic-formatted routing number for Canadian routing numbers, US routing numbers unchanged.
-
#micr_format_routing_number ⇒ Object
Always return MICR-formatted routing number for Canadian routing numbers, US routing numbers unchanged.
- #name ⇒ Object
- #name=(value) ⇒ Object
- #type ⇒ Object
- #valid_routing_number? ⇒ Boolean
- #validate ⇒ Object
Methods inherited from Model
Methods included from ActiveMerchant::Billing::Compatibility::Model
Constructor Details
This class inherits a constructor from ActiveMerchant::Billing::Model
Instance Attribute Details
#account_holder_type ⇒ Object
Returns the value of attribute account_holder_type.
9 10 11 |
# File 'lib/active_merchant/billing/check.rb', line 9 def account_holder_type @account_holder_type end |
#account_number ⇒ Object
Returns the value of attribute account_number.
9 10 11 |
# File 'lib/active_merchant/billing/check.rb', line 9 def account_number @account_number end |
#account_type ⇒ Object
Returns the value of attribute account_type.
9 10 11 |
# File 'lib/active_merchant/billing/check.rb', line 9 def account_type @account_type end |
#bank_name ⇒ Object
Returns the value of attribute bank_name.
9 10 11 |
# File 'lib/active_merchant/billing/check.rb', line 9 def bank_name @bank_name end |
#first_name ⇒ Object
Returns the value of attribute first_name.
9 10 11 |
# File 'lib/active_merchant/billing/check.rb', line 9 def first_name @first_name end |
#institution_number ⇒ Object
Used for Canadian bank accounts
14 15 16 |
# File 'lib/active_merchant/billing/check.rb', line 14 def institution_number @institution_number end |
#last_name ⇒ Object
Returns the value of attribute last_name.
9 10 11 |
# File 'lib/active_merchant/billing/check.rb', line 9 def last_name @last_name end |
#number ⇒ Object
Returns the value of attribute number.
9 10 11 |
# File 'lib/active_merchant/billing/check.rb', line 9 def number @number end |
#routing_number ⇒ Object
Returns the value of attribute routing_number.
9 10 11 |
# File 'lib/active_merchant/billing/check.rb', line 9 def routing_number @routing_number end |
#transit_number ⇒ Object
Used for Canadian bank accounts
14 15 16 |
# File 'lib/active_merchant/billing/check.rb', line 14 def transit_number @transit_number end |
Instance Method Details
#checksum(digits) ⇒ Object
Routing numbers may be validated by calculating a checksum and dividing it by 10. The formula is:
(3(d1 + d4 + d7) + 7(d2 + d5 + d8) + 1(d3 + d6 + d9))mod 10 = 0
See en.wikipedia.org/wiki/Routing_transit_number#Internal_checksums
79 80 81 82 83 |
# File 'lib/active_merchant/billing/check.rb', line 79 def checksum(digits) ((3 * (digits[0] + digits[3] + digits[6])) + (7 * (digits[1] + digits[4] + digits[7])) + (digits[2] + digits[5] + digits[8])) % 10 end |
#credit_card? ⇒ Boolean
59 60 61 |
# File 'lib/active_merchant/billing/check.rb', line 59 def credit_card? false end |
#electronic_format_routing_number ⇒ Object
Always return electronic-formatted routing number for Canadian routing numbers, US routing numbers unchanged
101 102 103 104 105 106 107 108 109 |
# File 'lib/active_merchant/billing/check.rb', line 101 def electronic_format_routing_number digits = routing_number.to_s.split('').map(&:to_i).select { |d| (0..9).cover?(d) } case digits.size when 9 return routing_number when 8 return '0' + routing_number[5..7] + routing_number[0..4] end end |
#micr_format_routing_number ⇒ Object
Always return MICR-formatted routing number for Canadian routing numbers, US routing numbers unchanged
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/active_merchant/billing/check.rb', line 86 def micr_format_routing_number digits = routing_number.to_s.split('').map(&:to_i).select { |d| (0..9).cover?(d) } case digits.size when 9 if checksum(digits) == 0 return routing_number else return routing_number[4..8] + routing_number[1..3] end when 8 return routing_number end end |
#name ⇒ Object
26 27 28 |
# File 'lib/active_merchant/billing/check.rb', line 26 def name @name ||= "#{first_name} #{last_name}".strip end |
#name=(value) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/active_merchant/billing/check.rb', line 30 def name=(value) return if empty?(value) @name = value segments = value.split(' ') @last_name = segments.pop @first_name = segments.join(' ') end |
#type ⇒ Object
55 56 57 |
# File 'lib/active_merchant/billing/check.rb', line 55 def type 'check' end |
#valid_routing_number? ⇒ Boolean
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/active_merchant/billing/check.rb', line 63 def valid_routing_number? digits = routing_number.to_s.split('').map(&:to_i).select { |d| (0..9).cover?(d) } case digits.size when 9 return checksum(digits) == 0 || CAN_INSTITUTION_NUMBERS.include?(routing_number[1..3]) when 8 return CAN_INSTITUTION_NUMBERS.include?(routing_number[5..7]) end false end |
#validate ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/active_merchant/billing/check.rb', line 39 def validate errors = [] %i[name routing_number account_number].each do |attr| errors << [attr, 'cannot be empty'] if empty?(self.send(attr)) end errors << [:routing_number, 'is invalid'] unless valid_routing_number? errors << [:account_holder_type, 'must be personal or business'] if !empty?(account_holder_type) && !%w[business personal].include?(account_holder_type.to_s) errors << [:account_type, 'must be checking or savings'] if !empty?(account_type) && !%w[checking savings].include?(account_type.to_s) errors_hash(errors) end |