Class ActiveMerchant::Billing::Check
In: lib/active_merchant/billing/check.rb
Parent: Object

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. Currently, only BrainTreeGateway supports the Check object.

Methods

Included Modules

Validateable

Attributes

account_holder_type  [RW] 
account_number  [RW] 
account_type  [RW] 
first_name  [RW] 
institution_number  [RW]  Used for Canadian bank accounts
last_name  [RW] 
number  [RW] 
routing_number  [RW] 
transit_number  [RW]  Used for Canadian bank accounts

Public Instance methods

[Source]

    # File lib/active_merchant/billing/check.rb, line 17
17:       def name
18:         @name ||= "#{@first_name} #{@last_name}".strip
19:       end

[Source]

    # File lib/active_merchant/billing/check.rb, line 21
21:       def name=(value)
22:         return if value.blank?
23: 
24:         @name = value
25:         segments = value.split(' ')
26:         @last_name = segments.pop
27:         @first_name = segments.join(' ')
28:       end

[Source]

    # File lib/active_merchant/billing/check.rb, line 44
44:       def type
45:         'check'
46:       end

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

[Source]

    # File lib/active_merchant/billing/check.rb, line 52
52:       def valid_routing_number?
53:         d = routing_number.to_s.split('').map(&:to_i).select { |d| (0..9).include?(d) }
54:         case d.size
55:           when 9 then
56:             checksum = ((3 * (d[0] + d[3] + d[6])) +
57:                         (7 * (d[1] + d[4] + d[7])) +
58:                              (d[2] + d[5] + d[8])) % 10
59:             case checksum
60:               when 0 then true
61:               else        false
62:             end
63:           else false
64:         end
65:       end

[Source]

    # File lib/active_merchant/billing/check.rb, line 30
30:       def validate
31:         [:name, :routing_number, :account_number].each do |attr|
32:           errors.add(attr, "cannot be empty") if self.send(attr).blank?
33:         end
34:         
35:         errors.add(:routing_number, "is invalid") unless valid_routing_number?
36:         
37:         errors.add(:account_holder_type, "must be personal or business") if
38:             !account_holder_type.blank? && !%w[business personal].include?(account_holder_type.to_s)
39:         
40:         errors.add(:account_type, "must be checking or savings") if
41:             !account_type.blank? && !%w[checking savings].include?(account_type.to_s)
42:       end

[Validate]