Class: ActiveMerchant::Billing::Check

Inherits:
Model
  • Object
show all
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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Model

#initialize

Methods included from ActiveMerchant::Billing::Compatibility::Model

#errors, #valid?

Constructor Details

This class inherits a constructor from ActiveMerchant::Billing::Model

Instance Attribute Details

#account_holder_typeObject

Returns the value of attribute account_holder_type.



9
10
11
# File 'lib/active_merchant/billing/check.rb', line 9

def 
  @account_holder_type
end

#account_numberObject

Returns the value of attribute account_number.



9
10
11
# File 'lib/active_merchant/billing/check.rb', line 9

def 
  @account_number
end

#account_typeObject

Returns the value of attribute account_type.



9
10
11
# File 'lib/active_merchant/billing/check.rb', line 9

def 
  @account_type
end

#bank_nameObject

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_nameObject

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_numberObject

Used for Canadian bank accounts



14
15
16
# File 'lib/active_merchant/billing/check.rb', line 14

def institution_number
  @institution_number
end

#last_nameObject

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

#numberObject

Returns the value of attribute number.



9
10
11
# File 'lib/active_merchant/billing/check.rb', line 9

def number
  @number
end

#routing_numberObject

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_numberObject

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

#nameObject



16
17
18
# File 'lib/active_merchant/billing/check.rb', line 16

def name
  @name ||= "#{first_name} #{last_name}".strip
end

#name=(value) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/active_merchant/billing/check.rb', line 20

def name=(value)
  return if empty?(value)

  @name = value
  segments = value.split(' ')
  @last_name = segments.pop
  @first_name = segments.join(' ')
end

#typeObject



49
50
51
# File 'lib/active_merchant/billing/check.rb', line 49

def type
  'check'
end

#valid_routing_number?Boolean

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

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/active_merchant/billing/check.rb', line 57

def valid_routing_number?
  digits = routing_number.to_s.split('').map(&:to_i).select{|d| (0..9).include?(d)}
  case digits.size
  when 9
    checksum = ((3 * (digits[0] + digits[3] + digits[6])) +
                (7 * (digits[1] + digits[4] + digits[7])) +
                     (digits[2] + digits[5] + digits[8])) % 10
    case checksum
    when 0
      true
    else
      false
    end
  else
    false
  end
end

#validateObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/active_merchant/billing/check.rb', line 29

def validate
  errors = []

  [: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?

  if(!empty?() && !%w[business personal].include?(.to_s))
    errors << [:account_holder_type, "must be personal or business"]
  end

  if(!empty?() && !%w[checking savings].include?(.to_s))
    errors << [:account_type, "must be checking or savings"]
  end

  errors_hash(errors)
end