Class: ActiveMerchant::Billing::CreditCard

### Home Classes Methods

### In Files

- lib/active_merchant/billing/credit_card.rb
- lib/active_merchant/billing/expiry_date.rb

### Parent

Object

### Methods

- ::requires_verification_value?
- #display_number
- #expired?
- #expiry_date
- #first_name?
- #last_digits
- #last_name?
- #name
- #name=
- #name?
- #validate
- #verification_value?

### Included Modules

- ActiveMerchant::Billing::CreditCardMethods
- Validateable

### Class/Module Index

Quicksearch

- ActiveMerchant
- ActiveMerchant::Billing
- ActiveMerchant::Billing::AVSResult
- ActiveMerchant::Billing::BarclaysEpdqGateway
- ActiveMerchant::Billing::BarclaysEpdqGateway::Document
- ActiveMerchant::Billing::BarclaysEpdqGateway::Parser
- ActiveMerchant::Billing::Base
- ActiveMerchant::Billing::BeanstreamGateway
- ActiveMerchant::Billing::BeanstreamInteracGateway
- ActiveMerchant::Billing::BeanstreamInteracResponse
- ActiveMerchant::Billing::BluePayGateway
- ActiveMerchant::Billing::BogusGateway
- ActiveMerchant::Billing::BraintreeBlueGateway
- ActiveMerchant::Billing::BraintreeGateway
- ActiveMerchant::Billing::BraintreeOrangeGateway
- ActiveMerchant::Billing::CVVResult
- ActiveMerchant::Billing::CardSaveGateway
- ActiveMerchant::Billing::CardStreamGateway
- ActiveMerchant::Billing::Check
- ActiveMerchant::Billing::CreditCard
- ActiveMerchant::Billing::CreditCardFormatting
- ActiveMerchant::Billing::CreditCardMethods
- ActiveMerchant::Billing::CreditCardMethods::ClassMethods
- ActiveMerchant::Billing::CyberSourceGateway
- ActiveMerchant::Billing::DataCashGateway
- ActiveMerchant::Billing::EfsnetGateway
- ActiveMerchant::Billing::ElavonGateway
- ActiveMerchant::Billing::EpayGateway
- ActiveMerchant::Billing::EwayGateway
- ActiveMerchant::Billing::EwayManagedGateway
- ActiveMerchant::Billing::EwayManagedGateway::EwayResponse
- ActiveMerchant::Billing::ExactGateway
- ActiveMerchant::Billing::FederatedCanadaGateway
- ActiveMerchant::Billing::FirstPayGateway
- ActiveMerchant::Billing::FirstPayGateway::FirstPayPostData
- ActiveMerchant::Billing::GarantiGateway
- ActiveMerchant::Billing::Gateway
- ActiveMerchant::Billing::IdealBaseGateway
- ActiveMerchant::Billing::IdealRabobankGateway
- ActiveMerchant::Billing::IdealResponse
- ActiveMerchant::Billing::InspireGateway
- ActiveMerchant::Billing::InstapayGateway
- ActiveMerchant::Billing::Integrations
- ActiveMerchant::Billing::Integrations::ActionViewHelper
- ActiveMerchant::Billing::Integrations::Bogus
- ActiveMerchant::Billing::Integrations::Bogus::Helper
- ActiveMerchant::Billing::Integrations::Bogus::Notification
- ActiveMerchant::Billing::Integrations::Bogus::Return
- ActiveMerchant::Billing::Integrations::Chronopay
- ActiveMerchant::Billing::Integrations::Chronopay::Helper
- ActiveMerchant::Billing::Integrations::Chronopay::Return
- ActiveMerchant::Billing::Integrations::DirecPay
- ActiveMerchant::Billing::Integrations::DirecPay::Helper
- ActiveMerchant::Billing::Integrations::DirecPay::Notification
- ActiveMerchant::Billing::Integrations::DirecPay::Return
- ActiveMerchant::Billing::Integrations::DirecPay::Status
- ActiveMerchant::Billing::Integrations::Directebanking
- ActiveMerchant::Billing::Integrations::Directebanking::Helper
- ActiveMerchant::Billing::Integrations::Directebanking::Notification
- ActiveMerchant::Billing::Integrations::Directebanking::Return
- ActiveMerchant::Billing::Integrations::Dwolla
- ActiveMerchant::Billing::Integrations::Dwolla::Helper
- ActiveMerchant::Billing::Integrations::Dwolla::Notification
- ActiveMerchant::Billing::Integrations::Dwolla::Return
- ActiveMerchant::Billing::Integrations::EPaymentPlans
- ActiveMerchant::Billing::Integrations::EPaymentPlans::Helper
- ActiveMerchant::Billing::Integrations::EPaymentPlans::Notification
- ActiveMerchant::Billing::Integrations::Gestpay
- ActiveMerchant::Billing::Integrations::Gestpay::Common
- ActiveMerchant::Billing::Integrations::Gestpay::Helper
- ActiveMerchant::Billing::Integrations::Gestpay::Notification
- ActiveMerchant::Billing::Integrations::Gestpay::Return
- ActiveMerchant::Billing::Integrations::HiTrust
- ActiveMerchant::Billing::Integrations::HiTrust::Notification
- ActiveMerchant::Billing::Integrations::HiTrust::Return
- ActiveMerchant::Billing::Integrations::Moneybookers
- ActiveMerchant::Billing::Integrations::Moneybookers::Helper
- ActiveMerchant::Billing::Integrations::Nochex
- ActiveMerchant::Billing::Integrations::Nochex::Helper
- ActiveMerchant::Billing::Integrations::Nochex::Notification
- ActiveMerchant::Billing::Integrations::Nochex::Return
- ActiveMerchant::Billing::Integrations::Notification
- ActiveMerchant::Billing::Integrations::PayflowLink
- ActiveMerchant::Billing::Integrations::PayflowLink::Helper
- ActiveMerchant::Billing::Integrations::PayflowLink::Notification
- ActiveMerchant::Billing::Integrations::Paypal
- ActiveMerchant::Billing::Integrations::Paypal::Helper
- ActiveMerchant::Billing::Integrations::Paypal::Notification
- ActiveMerchant::Billing::Integrations::Paypal::Return
- ActiveMerchant::Billing::Integrations::Quickpay
- ActiveMerchant::Billing::Integrations::Quickpay::Helper
- ActiveMerchant::Billing::Integrations::Quickpay::Notification
- ActiveMerchant::Billing::Integrations::Return
- ActiveMerchant::Billing::Integrations::SagePayForm
- ActiveMerchant::Billing::Integrations::SagePayForm::Encryption
- ActiveMerchant::Billing::Integrations::SagePayForm::Helper
- ActiveMerchant::Billing::Integrations::SagePayForm::Notification
- ActiveMerchant::Billing::Integrations::SagePayForm::Notification::CryptError
- ActiveMerchant::Billing::Integrations::SagePayForm::Notification::InvalidCryptData
- ActiveMerchant::Billing::Integrations::SagePayForm::Notification::MissingCryptData
- ActiveMerchant::Billing::Integrations::SagePayForm::Notification::MissingCryptKey
- ActiveMerchant::Billing::Integrations::SagePayForm::Return
- ActiveMerchant::Billing::Integrations::TwoCheckout
- ActiveMerchant::Billing::Integrations::TwoCheckout::Helper
- ActiveMerchant::Billing::Integrations::TwoCheckout::Notification
- ActiveMerchant::Billing::Integrations::TwoCheckout::Return
- ActiveMerchant::Billing::Integrations::Valitor
- ActiveMerchant::Billing::Integrations::Valitor::Helper
- ActiveMerchant::Billing::Integrations::Valitor::Notification
- ActiveMerchant::Billing::Integrations::Valitor::ResponseFields
- ActiveMerchant::Billing::Integrations::Valitor::Return
- ActiveMerchant::Billing::Integrations::WorldPay
- ActiveMerchant::Billing::Integrations::WorldPay::Helper
- ActiveMerchant::Billing::Integrations::WorldPay::Notification
- ActiveMerchant::Billing::IridiumGateway
- ActiveMerchant::Billing::JetpayGateway
- ActiveMerchant::Billing::LinkpointGateway
- ActiveMerchant::Billing::MerchantESolutionsGateway
- ActiveMerchant::Billing::MerchantWareGateway
- ActiveMerchant::Billing::ModernPaymentsGateway
- ActiveMerchant::Billing::MonerisGateway
- ActiveMerchant::Billing::NetRegistryGateway
- ActiveMerchant::Billing::NetaxeptGateway
- ActiveMerchant::Billing::NetaxeptGateway::Response
- ActiveMerchant::Billing::NetbillingGateway
- ActiveMerchant::Billing::NmiGateway
- ActiveMerchant::Billing::OptimalPaymentGateway
- ActiveMerchant::Billing::OrbitalSoftDescriptors
- ActiveMerchant::Billing::PayJunctionGateway
- ActiveMerchant::Billing::PaySecureGateway
- ActiveMerchant::Billing::PayboxDirectGateway
- ActiveMerchant::Billing::PayflowCommonAPI
- ActiveMerchant::Billing::PayflowExpressResponse
- ActiveMerchant::Billing::PayflowExpressUkGateway
- ActiveMerchant::Billing::PayflowGateway
- ActiveMerchant::Billing::PayflowResponse
- ActiveMerchant::Billing::PayflowUkGateway
- ActiveMerchant::Billing::PaymentExpressGateway
- ActiveMerchant::Billing::PaymentExpressResponse
- ActiveMerchant::Billing::PaypalCaGateway
- ActiveMerchant::Billing::PaypalCommonAPI
- ActiveMerchant::Billing::PaypalExpressCommon
- ActiveMerchant::Billing::PaypalExpressGateway
- ActiveMerchant::Billing::PaypalExpressResponse
- ActiveMerchant::Billing::PaypalGateway
- ActiveMerchant::Billing::PlugnpayGateway
- ActiveMerchant::Billing::PlugnpayGateway::PlugnpayPostData
- ActiveMerchant::Billing::PsigateGateway
- ActiveMerchant::Billing::QbmsGateway
- ActiveMerchant::Billing::QuantumGateway
- ActiveMerchant::Billing::QuickpayGateway
- ActiveMerchant::Billing::RealexGateway
- ActiveMerchant::Billing::Response
- ActiveMerchant::Billing::SageGateway
- ActiveMerchant::Billing::SagePayGateway
- ActiveMerchant::Billing::SallieMaeGateway
- ActiveMerchant::Billing::SamuraiGateway
- ActiveMerchant::Billing::SecureNetGateway
- ActiveMerchant::Billing::SecurePayAuGateway
- ActiveMerchant::Billing::SecurePayGateway
- ActiveMerchant::Billing::SecurePayTechGateway
- ActiveMerchant::Billing::SecurePayTechGateway::SecurePayTechPostData
- ActiveMerchant::Billing::SkipJackGateway
- ActiveMerchant::Billing::StripeGateway
- ActiveMerchant::Billing::TransFirstGateway
- ActiveMerchant::Billing::TransaxGateway
- ActiveMerchant::Billing::TrustCommerceGateway
- ActiveMerchant::Billing::UsaEpayAdvancedGateway
- ActiveMerchant::Billing::UsaEpayGateway
- ActiveMerchant::Billing::UsaEpayTransactionGateway
- ActiveMerchant::Billing::VerifiGateway
- ActiveMerchant::Billing::VerifiGateway::VerifiPostData
- ActiveMerchant::Billing::ViaklixGateway
- ActiveMerchant::Billing::WirecardGateway
- ActiveMerchant::Billing::WorldpayGateway
- ActiveMerchant::Billing::WorldpayGateway::MultiResponse
- BraintreeCommon
- OutboundHosts

No matching classes.

# 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:

- 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

### 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.