Class: Spree::CreditCard

Inherits:
Object
  • Object
show all
Includes:
Metadata, Metafields, PaymentSourceConcern, Security::CreditCards
Defined in:
app/models/spree/credit_card.rb

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PaymentSourceConcern

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

Methods included from Metadata

#metadata, #metadata=, #public_metadata=

Instance Attribute Details

#encrypted_dataObject

Returns the value of attribute encrypted_data.



30
31
32
# File 'app/models/spree/credit_card.rb', line 30

def encrypted_data
  @encrypted_data
end

#importedObject

Returns the value of attribute imported.



30
31
32
# File 'app/models/spree/credit_card.rb', line 30

def imported
  @imported
end

#manual_entryObject

Returns the value of attribute manual_entry.



30
31
32
# File 'app/models/spree/credit_card.rb', line 30

def manual_entry
  @manual_entry
end

#numberObject

Returns the value of attribute number.



29
30
31
# File 'app/models/spree/credit_card.rb', line 29

def number
  @number
end

#verification_valueObject

Returns the value of attribute verification_value.



29
30
31
# File 'app/models/spree/credit_card.rb', line 29

def verification_value
  @verification_value
end

Class Method Details

.json_api_permitted_attributesObject



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

def self.json_api_permitted_attributes
  [
    'number', 'month', 'year', 'expiry', 'verification_value', 'first_name', 'last_name',
    'cc_type', 'gateway_customer_profile_id', 'gateway_payment_profile_id', 'last_digits',
    'name', 'encrypted_data', 'address_id', 'created_at', 'updated_at', 'user_id',
    'payment_method_id', 'default', 'deleted_at'
  ]
end

Instance Method Details

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

cc_type is set by jquery.payment, which helpfully provides different types from Active Merchant. Converting them is necessary.



102
103
104
105
106
107
108
109
110
# File 'app/models/spree/credit_card.rb', line 102

def cc_type=(type)
  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_brandString

Show the card brand, eg. “VISA”, “MASTERCARD”, etc.

Returns:

  • (String)


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

def display_brand
  brand.present? ? brand.upcase : Spree.t(:no_cc_type)
end

#display_numberString

Show the card number, with all but last 4 numbers replace with “X”. (XXXX-XXXX-XXXX-4338)

Returns:

  • (String)


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

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

#expiry=(expiry) ⇒ Object



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

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] / 100).zero?
    self[:year] = self[:year].to_i
  end
  self[:month] = self[:month].to_i if self[:month]
end

#first_nameString

Returns the first name of the cardholder.

Returns:

  • (String)


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

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

#last_nameString

Returns the last name of the cardholder.

Returns:

  • (String)


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

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

#set_last_digitsString

Returns the last 4 digits of the card number.

Returns:

  • (String)

    , eg. “4338”



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

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

#try_type_from_numberObject



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

def try_type_from_number
  numbers = number.delete(' ') if number
  CARD_TYPES.find { |type, pattern| return type.to_s if numbers =~ pattern }.to_s
end

#verification_value?Boolean

Returns:



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

def verification_value?
  verification_value.present?
end

#wallet_typeString

Returns the type of wallet the card is associated with, eg. “apple_pay”, “google_pay”, etc.

Returns:

  • (String)


57
58
59
# File 'app/models/spree/credit_card.rb', line 57

def wallet_type
  wallet&.[]('type')
end