Class: Spree::CreditCard
- Inherits:
-
PaymentSource
- Object
- ActiveRecord::Base
- Base
- PaymentSource
- Spree::CreditCard
- 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
-
#encrypted_data ⇒ Object
Returns the value of attribute encrypted_data.
-
#number ⇒ Object
Returns the value of attribute number.
-
#verification_value ⇒ Object
Returns the value of attribute verification_value.
Attributes inherited from PaymentSource
Instance Method Summary collapse
- #address_attributes=(attributes) ⇒ Object
-
#cc_type=(type) ⇒ Object
(also: #brand=)
Sets the credit card type, converting it to the preferred internal representation from jquery.payment’s representation when appropriate.
-
#display_number ⇒ String
The card number, with all but last 4 numbers replace with “X”, as in “XXXX-XXXX-XXXX-4338”.
-
#expiry=(expiry) ⇒ Object
Sets the expiry date on this credit card.
-
#first_name ⇒ String
The first name on this credit card.
-
#has_payment_profile? ⇒ Boolean
True when there is a gateway customer or payment profile id present.
-
#last_name ⇒ String
The last name on this credit card.
- #reusable? ⇒ Boolean
-
#set_last_digits ⇒ Object
Sets the last digits field based on the assigned credit card number.
-
#to_active_merchant ⇒ ActiveMerchant::Billing::CreditCard
An ActiveMerchant credit card that represents this credit card.
-
#try_type_from_number ⇒ String
The credit card type if it can be determined from the number, otherwise the empty string.
-
#verification_value? ⇒ Boolean
True when a verification value is present.
Methods inherited from PaymentSource
#actions, #can_capture?, #can_credit?, #can_void?
Methods inherited from Base
Methods included from Spree::Core::Permalinks
#generate_permalink, #save_permalink
Instance Attribute Details
#encrypted_data ⇒ Object
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 |
#number ⇒ Object
Returns the value of attribute number.
14 15 16 |
# File 'app/models/spree/credit_card.rb', line 14 def number @number end |
#verification_value ⇒ Object
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.
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_number ⇒ String
Returns 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.
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_name ⇒ String
We should probably be calling #to_active_merchant before passing the object to ActiveMerchant.
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.
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.
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_name ⇒ String
We should probably be calling #to_active_merchant before passing the object to ActiveMerchant.
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.
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
126 127 128 |
# File 'app/models/spree/credit_card.rb', line 126 def reusable? has_payment_profile? end |
#set_last_digits ⇒ Object
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_merchant ⇒ ActiveMerchant::Billing::CreditCard
Returns 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:, month:, year:, verification_value:, first_name:, last_name: ) end |
#try_type_from_number ⇒ String
Returns 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.
116 117 118 |
# File 'app/models/spree/credit_card.rb', line 116 def verification_value? verification_value.present? end |