Class: CreditOfficer::CreditCard

Inherits:
Base
  • Object
show all
Defined in:
lib/credit_officer/credit_card.rb

Overview

ActiveModel compliant class that represents credit card information Use this to populate and validate credit card details

It is not recommended that you persist credit card information unless you absolutely must. Many payment processors proviider you with a mechanism to store this private information

Examples:

cc = CreditOfficer::CreditCard.new({
  :number => "411111111111111",
  :provider_name => "visa",
  :name_on_card => "John Doe",
  :expiration_year => 2010,
  :expiration_month => 1
}).valid? => true

cc.number = ""
cc.valid? => false 
cc.errors.full_messages => ["Number can't be blank"]

Constant Summary collapse

PROVIDERS_AND_FORMATS =
[
  ['visa'               , /^4\d{12}(\d{3})?$/],
  ['master'             , /^(5[1-5]\d{4}|677189)\d{10}$/],
  ['discover'           , /^(6011|65\d{2}|64[4-9]\d)\d{12}|(62\d{14})$/],
  ['american_express'   , /^3[47]\d{13}$/],
  ['diners_club'        , /^3(0[0-5]|[68]\d)\d{11}$/],
  ['jcb'                , /^35(28|29|[3-8]\d)\d{12}$/],
  ['switch'             , /^6759\d{12}(\d{2,3})?$/],
  ['solo'               , /^6767\d{12}(\d{2,3})?$/],
  ['dankort'            , /^5019\d{12}$/],
  ['maestro'            , /^(5[06-8]|6\d)\d{10,17}$/],
  ['forbrugsforeningen' , /^600722\d{10}$/],
  ['laser'              , /^(6304|6706|6771|6709)\d{8}(\d{4}|\d{6,7})?$/]
].inject(ActiveSupport::OrderedHash.new) do |ordered_hash, name_format_pair|
  ordered_hash[name_format_pair[0]] = name_format_pair[1]
  ordered_hash
end
SWITCH_OR_SOLO_PROVIDERS =
[
  'switch',
  'solo'
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize, #persisted?, #to_key, #to_model, #to_param

Constructor Details

This class inherits a constructor from CreditOfficer::Base

Instance Attribute Details

#expiration_monthObject

Integer

the integer based representation of the month when the credit card expires (1-12 e.g)



56
57
58
# File 'lib/credit_officer/credit_card.rb', line 56

def expiration_month
  @expiration_month
end

#expiration_yearObject

Note:

paired with the month, this must be in the future for the credit card to be valid

Integer

the year when the credit card expires



60
61
62
# File 'lib/credit_officer/credit_card.rb', line 60

def expiration_year
  @expiration_year
end

#issue_numberObject

String

Solo or Switch Card attribute representing the issue number found on the card



77
78
79
# File 'lib/credit_officer/credit_card.rb', line 77

def issue_number
  @issue_number
end

#name_on_cardObject

String

the name found on the front of the card



53
54
55
# File 'lib/credit_officer/credit_card.rb', line 53

def name_on_card
  @name_on_card
end

#numberObject

String

the number found on card



50
51
52
# File 'lib/credit_officer/credit_card.rb', line 50

def number
  @number
end

#provider_nameObject Also known as: brand

String

downcased name of the credit card provider

(see PROVIDERS_AND_FORMATS for a valid list



68
69
70
# File 'lib/credit_officer/credit_card.rb', line 68

def provider_name
  @provider_name
end

#start_monthObject

Integer

Solo or Switch card attribute representing the start date found on the card



71
72
73
# File 'lib/credit_officer/credit_card.rb', line 71

def start_month
  @start_month
end

#start_yearObject

Integer

Solo or Switch card attribute representing the start year found on the card



74
75
76
# File 'lib/credit_officer/credit_card.rb', line 74

def start_year
  @start_year
end

#verification_valueObject Also known as: cvv

String

the CVV/CVV2 value found on the back or front of cards depending on their brand

validation of this string can be turned off via class setting require_verification_value



64
65
66
# File 'lib/credit_officer/credit_card.rb', line 64

def verification_value
  @verification_value
end

Class Method Details

.supported_providersArray<String>

Note:

defaults to PROVIDERS_AND_FORMATS.keys

Returns list of providers.

Returns:

  • (Array<String>)

    list of providers



164
165
166
# File 'lib/credit_officer/credit_card.rb', line 164

def self.supported_providers
  @supported_providers
end

.supported_providers=(providers) ⇒ Object

Note:

matches specified providers against the supported whitelist PROVIDERS_AND_FORMATS

configure your list of supported providers

Parameters:



158
159
160
# File 'lib/credit_officer/credit_card.rb', line 158

def self.supported_providers=(providers)
  @supported_providers = providers.collect{|i| i.downcase} & PROVIDERS_AND_FORMATS.keys
end

.verification_value_required?Boolean

checks the configuration setting require_verification_value to see if verification is required

Returns:

  • (Boolean)


127
128
129
# File 'lib/credit_officer/credit_card.rb', line 127

def self.verification_value_required?
  require_verification_value
end

Instance Method Details

#derive_provider_nameObject



175
176
177
178
179
180
181
182
# File 'lib/credit_officer/credit_card.rb', line 175

def derive_provider_name
  self.class.supported_providers_and_formats.each do |name, format|
    if number =~ format
      self.provider_name = name
      return
    end
  end
end

#expiration_dateCreditOfficer::MonthYearPair

Returns month year pair that represents the expiration date.

Returns:



132
133
134
135
# File 'lib/credit_officer/credit_card.rb', line 132

def expiration_date
  CreditOfficer::MonthYearPair.new(:month => expiration_month, 
    :year => expiration_year)
end

#masked_numberObject



184
185
186
187
188
# File 'lib/credit_officer/credit_card.rb', line 184

def masked_number
  if number.present? && number.size >= 4
    "X" * (number.size - 4) + number[-4..-1]
  end
end

#start_dateCreditOfficer::MonthYearPair

Note:

this applies to switch and solo cards only

Returns month year pair that represents the start date.

Returns:



139
140
141
142
# File 'lib/credit_officer/credit_card.rb', line 139

def start_date
  CreditOfficer::MonthYearPair.new(:month => start_month,
    :year => start_year)
end

#switch_or_solo?Boolean

Returns whether or not the provider name indicates the card is a switch or solo card.

Returns:

  • (Boolean)

    whether or not the provider name indicates the card is a switch or solo card



171
172
173
# File 'lib/credit_officer/credit_card.rb', line 171

def switch_or_solo?
  SWITCH_OR_SOLO_PROVIDERS.include?(provider_name)
end