Class: Spree::CreditCard

Inherits:
PaymentSource show all
Defined in:
app/models/spree/credit_card.rb

Overview

The default ‘source` of a `Spree::Payment`.

Constant Summary collapse

CARD_TYPES =
{
  'visa'               => /^4\d{12}(\d{3})?(\d{3})?$/,
  'master'             => /^(5[1-5]\d{4}|677189|222[1-9]\d{2}|22[3-9]\d{3}|2[3-6]\d{4}|27[01]\d{3}|2720\d{2})\d{10}$/,
  'discover'           => /^(6011|65\d{2}|64[4-9]\d)\d{12}|(62\d{14})$/,
  'american_express'   => /^3[47]\d{13}$/,
  'diners_club'        => /^3(0[0-5]|[68]\d)\d{11}$/,
  'jcb'                => /^35(28|29|[3-8]\d)\d{12}$/,
  'switch'             => /^6759\d{12}(\d{2,3})?$/,
  'solo'               => /^6767\d{12}(\d{2,3})?$/,
  'dankort'            => /^5019\d{12}$/,
  'maestro'            => /^(5[06-8]|6\d)\d{10,17}$/,
  'forbrugsforeningen' => /^600722\d{10}$/,
  'laser'              => /^(6304|6706|6709|6771(?!89))\d{8}(\d{4}|\d{6,7})?$/
}.freeze

Instance Attribute Summary collapse

Attributes inherited from PaymentSource

#imported

Instance Method Summary collapse

Methods inherited from PaymentSource

#actions, #can_capture?, #can_credit?, #can_void?

Methods inherited from Base

display_includes

Methods included from Spree::Core::Permalinks

#generate_permalink, #save_permalink

Instance Attribute Details

#encrypted_dataObject

Returns the value of attribute encrypted_data.



15
16
17
# File 'app/models/spree/credit_card.rb', line 15

def encrypted_data
  @encrypted_data
end

#numberObject

Returns the value of attribute number.



14
15
16
# File 'app/models/spree/credit_card.rb', line 14

def number
  @number
end

#verification_valueObject

Returns the value of attribute verification_value.



14
15
16
# File 'app/models/spree/credit_card.rb', line 14

def verification_value
  @verification_value
end

Instance Method Details

#address_attributes=(attributes) ⇒ Object



41
42
43
# File 'app/models/spree/credit_card.rb', line 41

def address_attributes=(attributes)
  self.address = Spree::Address.immutable_merge(address, attributes)
end

#cc_type=(type) ⇒ Object Also known as: brand=

Sets the credit card type, converting it to the preferred internal representation from jquery.payment’s representation when appropriate.

Parameters:

  • type (String)

    the desired credit card type



83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/spree/credit_card.rb', line 83

def cc_type=(type)
  # cc_type is set by jquery.payment, which helpfully provides different
  # types from Active Merchant. Converting them is necessary.
  self[:cc_type] = case type
                   when 'mastercard', 'maestro' then 'master'
                   when 'amex' then 'american_express'
                   when 'dinersclub' then 'diners_club'
                   when '' then try_type_from_number
  else type
  end
end

#display_numberString

Returns the card number, with all but last 4 numbers replace with “X”, as in “XXXX-XXXX-XXXX-4338”.

Returns:

  • (String)

    the card number, with all but last 4 numbers replace with “X”, as in “XXXX-XXXX-XXXX-4338”



122
123
124
# File 'app/models/spree/credit_card.rb', line 122

def display_number
  "XXXX-XXXX-XXXX-#{last_digits}"
end

#expiry=(expiry) ⇒ Object

Sets the expiry date on this credit card.

Parameters:

  • expiry (String)

    the desired new expiry date in one of the following formats: “mm/yy”, “mm / yyyy”, “mmyy”, “mmyyyy”



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/models/spree/credit_card.rb', line 49

def expiry=(expiry)
  return unless expiry.present?

  self[:month], self[:year] =
    if expiry =~ /\d{2}\s?\/\s?\d{2,4}/ # will match mm/yy and mm / yyyy
      expiry.delete(' ').split('/')
    elsif match = expiry.match(/(\d{2})(\d{2,4})/) # will match mmyy and mmyyyy
      [match[1], match[2]]
    end
  if self[:year]
    self[:year] = "20" + self[:year] if self[:year].length == 2
    self[:year] = self[:year].to_i
  end
  self[:month] = self[:month].to_i if self[:month]
end

#first_nameString

TODO:

We should probably be calling #to_active_merchant before passing the object to ActiveMerchant.

Note:

ActiveMerchant needs first_name/last_name because we pass it a Spree::CreditCard and it calls those methods on it.

Returns the first name on this credit card.

Returns:

  • (String)

    the first name on this credit card



141
142
143
# File 'app/models/spree/credit_card.rb', line 141

def first_name
  Spree::Address::Name.new(name).first_name
end

#has_payment_profile?Boolean

Returns true when there is a gateway customer or payment profile id present.

Returns:

  • (Boolean)

    true when there is a gateway customer or payment profile id present



132
133
134
# File 'app/models/spree/credit_card.rb', line 132

def has_payment_profile?
  gateway_customer_profile_id.present? || gateway_payment_profile_id.present?
end

#last_nameString

TODO:

We should probably be calling #to_active_merchant before passing the object to ActiveMerchant.

Note:

ActiveMerchant needs first_name/last_name because we pass it a Spree::CreditCard and it calls those methods on it.

Returns the last name on this credit card.

Returns:

  • (String)

    the last name on this credit card



150
151
152
# File 'app/models/spree/credit_card.rb', line 150

def last_name
  Spree::Address::Name.new(name).last_name
end

#reusable?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'app/models/spree/credit_card.rb', line 126

def reusable?
  has_payment_profile?
end

#set_last_digitsObject

Sets the last digits field based on the assigned credit card number.



102
103
104
# File 'app/models/spree/credit_card.rb', line 102

def set_last_digits
  self.last_digits ||= number.to_s.length <= 4 ? number : number.to_s.slice(-4..)
end

#to_active_merchantActiveMerchant::Billing::CreditCard

Returns an ActiveMerchant credit card that represents this credit card.

Returns:

  • (ActiveMerchant::Billing::CreditCard)

    an ActiveMerchant credit card that represents this credit card



156
157
158
159
160
161
162
163
164
165
# File 'app/models/spree/credit_card.rb', line 156

def to_active_merchant
  ActiveMerchant::Billing::CreditCard.new(
    number: number,
    month: month,
    year: year,
    verification_value: verification_value,
    first_name: first_name,
    last_name: last_name
  )
end

#try_type_from_numberString

Returns the credit card type if it can be determined from the number, otherwise the empty string.

Returns:

  • (String)

    the credit card type if it can be determined from the number, otherwise the empty string



108
109
110
111
112
113
# File 'app/models/spree/credit_card.rb', line 108

def try_type_from_number
  CARD_TYPES.each do |type, pattern|
    return type if number =~ pattern
  end
  ''
end

#verification_value?Boolean

Returns true when a verification value is present.

Returns:

  • (Boolean)

    true when a verification value is present



116
117
118
# File 'app/models/spree/credit_card.rb', line 116

def verification_value?
  verification_value.present?
end