Class: ActiveModel::Validations::CreditCardFieldsValidator

Inherits:
Validator
  • Object
show all
Defined in:
lib/validate_credit_card_fields.rb

Constant Summary collapse

ERROR_TYPES =
[:invalid, :blank, :not_supported]
PROVIDERS =
{
  visa: /^4[0-9]{12}(?:[0-9]{3})?$/,
  master_card: /^5[1-5][0-9]{14}$/,
  maestro: /^(?:5[0678]\d\d|6304|6390|67\d\d)\d{8,15}$/,
  diners_club: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/,
  amex: /^3[47][0-9]{13}$/,
  discover: /^6(?:011|5[0-9]{2})[0-9]{12}$/,
  jcb: /^(?:2131|1800|35\d{3})\d{11}$/,
  solo: /(^(6334)[5-9](\d{11}$|\d{13,14}$)) |(^(6767)(\d{12}$|\d{14,15}$))/,
  china_union: /^62[0-5]\d{13,16}$/,
  dankort: /^5019\d{12}$/
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CreditCardFieldsValidator

Returns a new instance of CreditCardFieldsValidator.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/validate_credit_card_fields.rb', line 30

def initialize(options={})
  @options = options
  @custom_messages = {}
  @cc_number = init_option(:number, :cc_number)
  @cc_cvv = init_option(:cvv, :cc_cvv)
  @cc_month = init_option(:month, :cc_month)
  @cc_year = init_option(:year, :cc_year)
  @cc_providers = options[:providers]
  @using_owner = !options[:owner].nil? || options[:first_name].nil? || options[:last_name].nil?
  if @using_owner
    @cc_owner = init_option(:owner, :cc_owner)
  else
    @cc_first_name = init_option(:first_name, :cc_first_name)
    @cc_last_name = init_option(:last_name, :cc_last_name)
  end
end

Instance Attribute Details

#cc_cvvObject

Returns the value of attribute cc_cvv.



26
27
28
# File 'lib/validate_credit_card_fields.rb', line 26

def cc_cvv
  @cc_cvv
end

#cc_first_nameObject

Returns the value of attribute cc_first_name.



26
27
28
# File 'lib/validate_credit_card_fields.rb', line 26

def cc_first_name
  @cc_first_name
end

#cc_last_nameObject

Returns the value of attribute cc_last_name.



26
27
28
# File 'lib/validate_credit_card_fields.rb', line 26

def cc_last_name
  @cc_last_name
end

#cc_monthObject

Returns the value of attribute cc_month.



26
27
28
# File 'lib/validate_credit_card_fields.rb', line 26

def cc_month
  @cc_month
end

#cc_numberObject

Returns the value of attribute cc_number.



26
27
28
# File 'lib/validate_credit_card_fields.rb', line 26

def cc_number
  @cc_number
end

#cc_ownerObject

Returns the value of attribute cc_owner.



26
27
28
# File 'lib/validate_credit_card_fields.rb', line 26

def cc_owner
  @cc_owner
end

#cc_providersObject

Returns the value of attribute cc_providers.



26
27
28
# File 'lib/validate_credit_card_fields.rb', line 26

def cc_providers
  @cc_providers
end

#cc_typeObject

Returns the value of attribute cc_type.



26
27
28
# File 'lib/validate_credit_card_fields.rb', line 26

def cc_type
  @cc_type
end

#cc_yearObject

Returns the value of attribute cc_year.



26
27
28
# File 'lib/validate_credit_card_fields.rb', line 26

def cc_year
  @cc_year
end

#custom_messagesObject

Returns the value of attribute custom_messages.



26
27
28
# File 'lib/validate_credit_card_fields.rb', line 26

def custom_messages
  @custom_messages
end

#optionsObject

Returns the value of attribute options.



26
27
28
# File 'lib/validate_credit_card_fields.rb', line 26

def options
  @options
end

#using_ownerObject

Returns the value of attribute using_owner.



26
27
28
# File 'lib/validate_credit_card_fields.rb', line 26

def using_owner
  @using_owner
end

Instance Method Details

#init_option(key, default) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/validate_credit_card_fields.rb', line 47

def init_option(key, default)
  if options[key].is_a? Hash
    init_option_from_hash options[key], default
  else
    options[key] || default
  end
end

#init_option_from_hash(hash, default) ⇒ Object



55
56
57
58
59
# File 'lib/validate_credit_card_fields.rb', line 55

def init_option_from_hash(hash, default)
  field_name = hash[:field] || default
  custom_messages[field_name] = hash.select{ |k,_| ERROR_TYPES.include? k }
  field_name
end

#validate(record) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/validate_credit_card_fields.rb', line 61

def validate(record)
  @record = record
  check_fields_format
  @cc_type = get_cc_type
  validate_fields_presence
  validate_cc_number
  validate_cc_cvv
  validate_cc_month
  validate_cc_year
  validate_cc_expiry_date
end