Parent

Included Modules

Class 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

number[RW]

Returns or sets the credit card number.

@return [String]

month[RW]

Returns or sets the expiry month for the card.

@return [Integer]

year[RW]

Returns or sets the expiry year for the card.

@return [Integer]

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

first_name[RW]

Returns or sets the first name of the card holder.

@return [String]

last_name[RW]

Returns or sets the last name of the card holder.

@return [String]

start_month[RW]

Required for Switch / Solo cards

start_year[RW]

Required for Switch / Solo cards

issue_number[RW]

Required for Switch / Solo cards

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

Public Class Methods

requires_verification_value?() click to toggle source
     # File lib/active_merchant/billing/credit_card.rb, line 196
196:       def self.requires_verification_value?
197:         require_verification_value
198:       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
173:       def display_number
174:         self.class.mask(number)
175:       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
127:       def expired?
128:         expiry_date.expired?
129:       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
120:       def expiry_date
121:         ExpiryDate.new(@month, @year)
122:       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
137:       def first_name?
138:         @first_name.present?
139:       end
last_digits() click to toggle source
     # File lib/active_merchant/billing/credit_card.rb, line 177
177:       def last_digits
178:         self.class.last_digits(number)
179:       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
142:       def last_name?
143:         @last_name.present?
144:       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
149:       def name
150:         [@first_name, @last_name].compact.join(' ')
151:       end
name=(full_name) click to toggle source
     # File lib/active_merchant/billing/credit_card.rb, line 153
153:       def name=(full_name)
154:         names = full_name.split
155:         self.last_name  = names.pop
156:         self.first_name = names.join(" ")
157:       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
132:       def name?
133:         first_name? || last_name?
134:       end
validate() click to toggle source

Validates the credit card details.

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

     # File lib/active_merchant/billing/credit_card.rb, line 184
184:       def validate
185:         validate_essential_attributes
186: 
187:         # Bogus card is pretty much for testing purposes. Lets just skip these extra tests if its used
188:         return if type == 'bogus'
189: 
190:         validate_card_type
191:         validate_card_number
192:         validate_verification_value
193:         validate_switch_or_solo_attributes
194:       end
verification_value?() click to toggle source
     # File lib/active_merchant/billing/credit_card.rb, line 159
159:       def verification_value?
160:         !@verification_value.blank?
161:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.