class ActiveMerchant::Billing::Check

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.

Constants

CAN_INSTITUTION_NUMBERS

Canadian Institution Numbers Partial list found here: en.wikipedia.org/wiki/Routing_number_(Canada)

Attributes

account_holder_type[RW]
account_number[RW]
account_type[RW]
bank_name[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

checksum(digits) click to toggle source

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

# 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?() click to toggle source
# File lib/active_merchant/billing/check.rb, line 59
def credit_card?
  false
end
electronic_format_routing_number() click to toggle source

Always return electronic-formatted routing number for Canadian routing numbers, US routing numbers unchanged

# 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() click to toggle source

Always return MICR-formatted routing number for Canadian routing numbers, US routing numbers unchanged

# 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() click to toggle source
# File lib/active_merchant/billing/check.rb, line 26
def name
  @name ||= "#{first_name} #{last_name}".strip
end
name=(value) click to toggle source
# 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() click to toggle source
# File lib/active_merchant/billing/check.rb, line 55
def type
  'check'
end
valid_routing_number?() click to toggle source
# 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() click to toggle source
# 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