Class: PaymentReferenceValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Extended by:
ActiveSupport::Concern
Defined in:
lib/elfproef/payment_reference_validator.rb

Overview

Validates if the specified value is a valid payment reference number using a weighted modulus 11

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.validate_payment_reference(value, options = {}) ⇒ Object

Takes the payment reference number and returns true if:

* The number is exact 7 digits (no verification is possible)
* Is between 9 and 14 digits, has the correct check digit and has the correct length digit
* Is exact 16 digits and has the correct check digit


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/elfproef/payment_reference_validator.rb', line 21

def self.validate_payment_reference(value, options = {})
  number = value.to_s.gsub(/\D/, '').strip

  # Not valid if payment reference length is < 7, 8, 15 or > 16
  return false if number.length < 7 || number.length == 8 || number.length == 15 || number.length > 16

  # No further verification possible for 7 digit payment references
  return true if number.length == 7

  # For payment references between 9 and 14 digits validate it has the correct length digit
  self.validate_length_digit(number) if (9..14).include?(number.length)

  # Validate the check digit
  self.validate_check_digit(number)
end

Instance Method Details

#validate_each(record, attribute, value) ⇒ Object



9
10
11
12
13
# File 'lib/elfproef/payment_reference_validator.rb', line 9

def validate_each(record, attribute, value)
  unless self.class.validate_payment_reference(value, options)
    record.errors.add(attribute, :invalid_payment_reference, options)
  end
end