Parent

Included Modules

Class/Module Index [+]

Quicksearch

ActiveMerchant::Billing::CreditCard

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:

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.

Testing With CreditCard

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?}

Example Usage

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

Attributes

brand[RW]

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

first_name[RW]

Returns or sets the first name of the card holder.

@return [String]

issue_number[RW]

Required for Switch / Solo cards

last_name[RW]

Returns or sets the last name of the card holder.

@return [String]

month[RW]

Returns or sets the expiry month for the card.

@return [Integer]

number[RW]

Returns or sets the credit card number.

@return [String]

start_month[RW]

Required for Switch / Solo cards

start_year[RW]

Required for Switch / Solo cards

type[RW]

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

verification_value[RW]

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

year[RW]

Returns or sets the expiry year for the card.

@return [Integer]

Public Class Methods

requires_verification_value?() click to toggle source
# File lib/active_merchant/billing/credit_card.rb, line 196
def self.requires_verification_value?
  require_verification_value
end

Public Instance Methods

display_number() click to toggle source

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
expired?() click to toggle source

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
expiry_date() click to toggle source

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
first_name?() click to toggle source

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
last_digits() click to toggle source
# File lib/active_merchant/billing/credit_card.rb, line 177
def last_digits
  self.class.last_digits(number)
end
last_name?() click to toggle source

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
name() click to toggle source

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
name=(full_name) click to toggle source
# 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
name?() click to toggle source

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
validate() click to toggle source

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
verification_value?() click to toggle source
# File lib/active_merchant/billing/credit_card.rb, line 159
def verification_value?
  !@verification_value.blank?
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.