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

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PaymentSource

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

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

#default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Attribute Details

#encrypted_dataObject

Returns the value of attribute encrypted_data



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

def encrypted_data
  @encrypted_data
end

#numberObject

Returns the value of attribute number



12
13
14
# File 'app/models/spree/credit_card.rb', line 12

def number
  @number
end

#verification_valueObject

Returns the value of attribute verification_value



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

def verification_value
  @verification_value
end

Class Method Details

.defaultObject



22
23
24
25
# File 'app/models/spree/credit_card.rb', line 22

def self.default
  Spree::Deprecation.warn("CreditCard.default is deprecated. Please use Spree::Wallet instead.")
  joins(:wallet_payment_sources).where(spree_wallet_payment_sources: { default: true })
end

Instance Method Details

#address_attributes=(attributes) ⇒ Object



69
70
71
# File 'app/models/spree/credit_card.rb', line 69

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

#cc_type=(type) ⇒ Object

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



111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/spree/credit_card.rb', line 111

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

#defaultObject



47
48
49
50
51
# File 'app/models/spree/credit_card.rb', line 47

def default
  Spree::Deprecation.warn("CreditCard.default is deprecated. Please use user.wallet.default_wallet_payment_source instead.", caller)
  return false if user.nil?
  user.wallet.default_wallet_payment_source.try!(:payment_source) == self
end

#default=(set_as_default) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/models/spree/credit_card.rb', line 53

def default=(set_as_default)
  Spree::Deprecation.warn("CreditCard.default= is deprecated. Please use user.wallet.default_wallet_payment_source= instead.", caller)
  if user.nil?
    raise "Cannot set 'default' on a credit card without a user"
  elsif set_as_default # setting this card as default
    wallet_payment_source = user.wallet.add(self)
    user.wallet.default_wallet_payment_source = wallet_payment_source
    true
  else # removing this card as default
    if user.wallet.default_wallet_payment_source.try!(:payment_source) == self
      user.wallet.default_wallet_payment_source = nil
    end
    false
  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”



144
145
146
# File 'app/models/spree/credit_card.rb', line 144

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”



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/spree/credit_card.rb', line 77

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



163
164
165
# File 'app/models/spree/credit_card.rb', line 163

def first_name
  name.to_s.split(/[[:space:]]/, 2)[0]
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



154
155
156
# File 'app/models/spree/credit_card.rb', line 154

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



172
173
174
# File 'app/models/spree/credit_card.rb', line 172

def last_name
  name.to_s.split(/[[:space:]]/, 2)[1]
end

#reusable?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'app/models/spree/credit_card.rb', line 148

def reusable?
  has_payment_profile?
end

#set_last_digitsObject

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



124
125
126
# File 'app/models/spree/credit_card.rb', line 124

def set_last_digits
  self.last_digits ||= number.to_s.length <= 4 ? number : number.to_s.slice(-4..-1)
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



178
179
180
181
182
183
184
185
186
187
# File 'app/models/spree/credit_card.rb', line 178

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



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

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



138
139
140
# File 'app/models/spree/credit_card.rb', line 138

def verification_value?
  verification_value.present?
end