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

- (Object) encrypted_data

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

- (Object) number

Returns the value of attribute number



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

def number
  @number
end

- (Object) verification_value

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

+ (Object) default



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

- (Object) address_attributes=(attributes)



66
67
68
# File 'app/models/spree/credit_card.rb', line 66

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

- (Object) cc_type=(type)

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



108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/spree/credit_card.rb', line 108

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

- (Object) default



47
48
49
50
# 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)
  user.wallet.default_wallet_payment_source.payment_source == self
end

- (Object) default=(set_as_default)



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

def default=(set_as_default)
  Spree::Deprecation.warn("CreditCard.default= is deprecated. Please use user.wallet.default_wallet_payment_source= instead.", caller)
  if 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

- (String) display_number

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”



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

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

- (Object) expiry=(expiry)

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”



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

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

- (String) first_name

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



160
161
162
# File 'app/models/spree/credit_card.rb', line 160

def first_name
  name.to_s.split(/[[:space:]]/, 2)[0]
end

- (Boolean) has_payment_profile?

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



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

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

- (String) last_name

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



169
170
171
# File 'app/models/spree/credit_card.rb', line 169

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

- (Boolean) reusable?

Returns:

  • (Boolean)


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

def reusable?
  has_payment_profile?
end

- (Object) set_last_digits

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



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

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

- (ActiveMerchant::Billing::CreditCard) to_active_merchant

Returns an ActiveMerchant credit card that represents this credit card

Returns:

  • (ActiveMerchant::Billing::CreditCard)

    an ActiveMerchant credit card that represents this credit card



175
176
177
178
179
180
181
182
183
184
# File 'app/models/spree/credit_card.rb', line 175

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

- (String) try_type_from_number

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



127
128
129
130
131
132
# File 'app/models/spree/credit_card.rb', line 127

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

- (Boolean) verification_value?

Returns true when a verification value is present

Returns:

  • (Boolean)

    true when a verification value is present



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

def verification_value?
  verification_value.present?
end