Class: CreditOfficer::CreditCard
- 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
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
-
#expiration_month ⇒ Object
- Integer
-
the integer based representation of the month when the credit card expires (1-12 e.g).
-
#expiration_year ⇒ Object
- Integer
-
the year when the credit card expires.
-
#issue_number ⇒ Object
- String
-
Solo or Switch Card attribute representing the issue number found on the card.
-
#name_on_card ⇒ Object
- String
-
the name found on the front of the card.
-
#number ⇒ Object
- String
-
the number found on card.
-
#provider_name ⇒ Object
(also: #brand)
- String
-
downcased name of the credit card provider (see PROVIDERS_AND_FORMATS for a valid list.
-
#start_month ⇒ Object
- Integer
-
Solo or Switch card attribute representing the start date found on the card.
-
#start_year ⇒ Object
- Integer
-
Solo or Switch card attribute representing the start year found on the card.
-
#verification_value ⇒ Object
(also: #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.
Class Method Summary collapse
-
.supported_providers ⇒ Array<String>
List of providers.
-
.supported_providers=(providers) ⇒ Object
configure your list of supported providers.
-
.verification_value_required? ⇒ Boolean
checks the configuration setting require_verification_value to see if verification is required.
Instance Method Summary collapse
- #derive_provider_name ⇒ Object
-
#expiration_date ⇒ CreditOfficer::MonthYearPair
Month year pair that represents the expiration date.
- #masked_number ⇒ Object
-
#start_date ⇒ CreditOfficer::MonthYearPair
Month year pair that represents the start date.
-
#switch_or_solo? ⇒ Boolean
Whether or not the provider name indicates the card is a switch or solo card.
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_month ⇒ Object
- 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_year ⇒ Object
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_number ⇒ Object
- 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_card ⇒ Object
- 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 |
#number ⇒ Object
- String
-
the number found on card
50 51 52 |
# File 'lib/credit_officer/credit_card.rb', line 50 def number @number end |
#provider_name ⇒ Object 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_month ⇒ Object
- 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_year ⇒ Object
- 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_value ⇒ Object 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_providers ⇒ Array<String>
defaults to PROVIDERS_AND_FORMATS.keys
Returns 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
matches specified providers against the supported whitelist PROVIDERS_AND_FORMATS
configure your list of supported providers
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
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_name ⇒ Object
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_date ⇒ CreditOfficer::MonthYearPair
Returns month year pair that represents the expiration date.
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_number ⇒ Object
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_date ⇒ CreditOfficer::MonthYearPair
this applies to switch and solo cards only
Returns month year pair that represents the start date.
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.
171 172 173 |
# File 'lib/credit_officer/credit_card.rb', line 171 def switch_or_solo? SWITCH_OR_SOLO_PROVIDERS.include?(provider_name) end |