Class ActiveMerchant::Billing::CreditCard
In: lib/active_merchant/billing/credit_card.rb
lib/active_merchant/billing/expiry_date.rb
Parent: Object

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.

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

Methods

Included Modules

CreditCardMethods Validateable

External Aliases

type -> brand

Attributes

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

Public Instance methods

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

Returns whether the credit card has expired.

@return true if the card has expired, false otherwise

Provides proxy access to an expiry date object

@return [ExpiryDate]

Returns whether the first_name attribute has been set.

Returns whether the last_name attribute has been set.

Returns the full name of the card holder.

@return [String] the full name of the card holder

Returns whether either the first_name or the last_name attributes has been set.

Validates the credit card details.

Any validation errors are added to the {errors} attribute.

[Validate]