A CreditCard object represents a physical credit card, and is capable of validating the various data associated with these.
At the moment, the following credit card types are supported:
Visa
MasterCard
Discover
American Express
Diner’s Club
JCB
Switch
Solo
Dankort
Maestro
Forbrugsforeningen
Laser
For testing purposes, use the ‘bogus’ credit card type. This skips the vast majority of validations, allowing you to focus on your core concerns until you’re ready to be more concerned with the details of particular credit cards or your gateway.
Often when testing we don’t care about the particulars of a given card type. When using the ‘test’ mode in your {Gateway}, there are six different valid card numbers: 1, 2, 3, ‘success’, ‘fail’, and ‘error’.
For details, see {CreditCardMethods::ClassMethods#valid_number?}
cc = CreditCard.new( :first_name => 'Steve', :last_name => 'Smith', :month => '9', :year => '2010', :type => 'visa', :number => '4242424242424242' ) cc.valid? # => true cc.display_number # => XXXX-XXXX-XXXX-4242
Returns or sets the credit card type.
Valid card types are
+‘visa’+
+‘master’+
+‘discover’+
+‘american_express’+
+‘diners_club’+
+‘jcb’+
+‘switch’+
+‘solo’+
+‘dankort’+
+‘maestro’+
+‘forbrugsforeningen’+
+‘laser’+
Or, if you wish to test your implementation, +‘bogus’+.
@return (String) the credit card type
Returns or sets the credit card type.
Valid card types are
+‘visa’+
+‘master’+
+‘discover’+
+‘american_express’+
+‘diners_club’+
+‘jcb’+
+‘switch’+
+‘solo’+
+‘dankort’+
+‘maestro’+
+‘forbrugsforeningen’+
+‘laser’+
Or, if you wish to test your implementation, +‘bogus’+.
@return (String) the credit card type
Returns or sets the card verification value.
This attribute is optional but recommended. The verification value is a card security code. If provided, the gateway will attempt to validate the value.
@return [String] the verification value
Returns a display-friendly version of the card number.
All but the last 4 numbers are replaced with an “X”, and hyphens are inserted in order to improve legibility.
@example
credit_card = CreditCard.new(:number => "2132542376824338") credit_card.display_number # "XXXX-XXXX-XXXX-4338"
@return [String] a display-friendly version of the card number
# File lib/active_merchant/billing/credit_card.rb, line 173 def display_number self.class.mask(number) end
Returns whether the credit card has expired.
@return true if the card has expired, false otherwise
# File lib/active_merchant/billing/credit_card.rb, line 127 def expired? expiry_date.expired? end
Provides proxy access to an expiry date object
@return [ExpiryDate]
# File lib/active_merchant/billing/credit_card.rb, line 120 def expiry_date ExpiryDate.new(@month, @year) end
Returns whether the first_name attribute has been set.
# File lib/active_merchant/billing/credit_card.rb, line 137 def first_name? @first_name.present? end
# File lib/active_merchant/billing/credit_card.rb, line 177 def last_digits self.class.last_digits(number) end
Returns whether the last_name attribute has been set.
# File lib/active_merchant/billing/credit_card.rb, line 142 def last_name? @last_name.present? end
Returns the full name of the card holder.
@return [String] the full name of the card holder
# File lib/active_merchant/billing/credit_card.rb, line 149 def name [@first_name, @last_name].compact.join(' ') end
# File lib/active_merchant/billing/credit_card.rb, line 153 def name=(full_name) names = full_name.split self.last_name = names.pop self.first_name = names.join(" ") end
Returns whether either the first_name or the last_name attributes has been set.
# File lib/active_merchant/billing/credit_card.rb, line 132 def name? first_name? || last_name? end
Validates the credit card details.
Any validation errors are added to the {errors} attribute.
# File lib/active_merchant/billing/credit_card.rb, line 184 def validate validate_essential_attributes # Bogus card is pretty much for testing purposes. Lets just skip these extra tests if its used return if type == 'bogus' validate_card_type validate_card_number validate_verification_value validate_switch_or_solo_attributes end
Generated with the Darkfish Rdoc Generator 2.