Class: ActiveMerchant::Billing::Check
- Inherits:
-
Object
- Object
- ActiveMerchant::Billing::Check
- Includes:
- Validateable
- 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. Currently, only BrainTreeGateway
supports the Check object.
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.
-
#name ⇒ Object
Returns the value of attribute name.
-
#routing_number ⇒ Object
Returns the value of attribute routing_number.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ Check
constructor
A new instance of Check.
- #type ⇒ Object
-
#valid_routing_number? ⇒ Boolean
Routing numbers may be validated by calculating a checksum and dividing it by 10.
- #validate ⇒ Object
Methods included from Validateable
Constructor Details
#initialize(args = {}) ⇒ Check
Returns a new instance of Check.
14 15 16 17 18 19 20 21 |
# File 'lib/active_merchant/billing/check.rb', line 14 def initialize(args={}) args = args.with_indifferent_access self.name = args[:name] self.routing_number = args[:routing_number] self.account_number = args[:account_number] self.account_holder_type = args[:account_holder_type] self.account_type = args[:account_type] end |
Instance Attribute Details
#account_holder_type ⇒ Object
Returns the value of attribute account_holder_type.
12 13 14 |
# File 'lib/active_merchant/billing/check.rb', line 12 def account_holder_type @account_holder_type end |
#account_number ⇒ Object
Returns the value of attribute account_number.
12 13 14 |
# File 'lib/active_merchant/billing/check.rb', line 12 def account_number @account_number end |
#account_type ⇒ Object
Returns the value of attribute account_type.
12 13 14 |
# File 'lib/active_merchant/billing/check.rb', line 12 def account_type @account_type end |
#name ⇒ Object
Returns the value of attribute name.
12 13 14 |
# File 'lib/active_merchant/billing/check.rb', line 12 def name @name end |
#routing_number ⇒ Object
Returns the value of attribute routing_number.
12 13 14 |
# File 'lib/active_merchant/billing/check.rb', line 12 def routing_number @routing_number end |
Instance Method Details
#type ⇒ Object
37 38 39 |
# File 'lib/active_merchant/billing/check.rb', line 37 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
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_merchant/billing/check.rb', line 45 def valid_routing_number? d = routing_number.to_s.split('').map(&:to_i).select { |d| (0..9).include?(d) } case d.size when 9 then checksum = ((3 * (d[0] + d[3] + d[6])) + (7 * (d[1] + d[4] + d[7])) + (d[2] + d[5] + d[8])) % 10 case checksum when 0 then true else false end else false end end |
#validate ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/active_merchant/billing/check.rb', line 23 def validate for attr in [:name, :routing_number, :account_number] errors.add(attr, "cannot be empty") if self.send(attr).blank? end errors.add(:routing_number, "is invalid") unless valid_routing_number? errors.add(:account_holder_type, "must be personal or business") if !account_holder_type.blank? && !%w[business personal].include?(account_holder_type.to_s) errors.add(:account_type, "must be checking or savings") if !account_type.blank? && !%w[checking savings].include?(account_type.to_s) end |