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

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

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



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

Class Method Details

.defaultObject



24
25
26
27
# File 'app/models/spree/credit_card.rb', line 24

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



71
72
73
# File 'app/models/spree/credit_card.rb', line 71

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



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

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



49
50
51
52
53
# File 'app/models/spree/credit_card.rb', line 49

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



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

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”



146
147
148
# File 'app/models/spree/credit_card.rb', line 146

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”



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

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



165
166
167
# File 'app/models/spree/credit_card.rb', line 165

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



156
157
158
# File 'app/models/spree/credit_card.rb', line 156

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



174
175
176
# File 'app/models/spree/credit_card.rb', line 174

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

#reusable?Boolean

Returns:

  • (Boolean)


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

def reusable?
  has_payment_profile?
end

#set_last_digitsObject

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



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

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



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

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



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

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



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

def verification_value?
  verification_value.present?
end