Class: Spree::CreditCard
- Defined in:
- app/models/spree/credit_card.rb
Constant Summary collapse
- CARD_TYPES =
{ visa: /^4[0-9]{12}(?:[0-9]{3})?$/, master: /(^5[1-5][0-9]{14}$)|(^6759[0-9]{2}([0-9]{10})$)|(^6759[0-9]{2}([0-9]{12})$)|(^6759[0-9]{2}([0-9]{13})$)/, diners_club: /^3(?:0[0-5]|[68][0-9])[0-9]{11}$/, american_express: /^3[47][0-9]{13}$/, discover: /^6(?:011|5[0-9]{2})[0-9]{12}$/, jcb: /^(?:2131|1800|35\d{3})\d{11}$/ }
Instance Attribute Summary collapse
-
#encrypted_data ⇒ Object
Returns the value of attribute encrypted_data.
-
#imported ⇒ Object
Returns the value of attribute imported.
-
#number ⇒ Object
Returns the value of attribute number.
-
#verification_value ⇒ Object
Returns the value of attribute verification_value.
Instance Method Summary collapse
- #actions ⇒ Object
-
#can_capture?(payment) ⇒ Boolean
Indicates whether its possible to capture the payment.
-
#can_credit?(payment) ⇒ Boolean
Indicates whether its possible to credit the payment.
-
#can_void?(payment) ⇒ Boolean
Indicates whether its possible to void the payment.
-
#cc_type=(type) ⇒ Object
cc_type is set by jquery.payment, which helpfully provides different types from Active Merchant.
-
#display_number ⇒ Object
Show the card number, with all but last 4 numbers replace with “X”.
- #expiry=(expiry) ⇒ Object
-
#first_name ⇒ Object
ActiveMerchant needs first_name/last_name because we pass it a Spree::CreditCard and it calls those methods on it.
- #has_payment_profile? ⇒ Boolean
- #last_name ⇒ Object
- #set_last_digits ⇒ Object
- #to_active_merchant ⇒ Object
- #try_type_from_number ⇒ Object
- #verification_value? ⇒ Boolean
Methods inherited from Base
Methods included from Preferences::Preferable
#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference
Instance Attribute Details
#encrypted_data ⇒ Object
Returns the value of attribute encrypted_data.
11 12 13 |
# File 'app/models/spree/credit_card.rb', line 11 def encrypted_data @encrypted_data end |
#imported ⇒ Object
Returns the value of attribute imported.
11 12 13 |
# File 'app/models/spree/credit_card.rb', line 11 def imported @imported end |
#number ⇒ Object
Returns the value of attribute number.
11 12 13 |
# File 'app/models/spree/credit_card.rb', line 11 def number @number end |
#verification_value ⇒ Object
Returns the value of attribute verification_value.
11 12 13 |
# File 'app/models/spree/credit_card.rb', line 11 def verification_value @verification_value end |
Instance Method Details
#actions ⇒ Object
90 91 92 |
# File 'app/models/spree/credit_card.rb', line 90 def actions %w{capture void credit} end |
#can_capture?(payment) ⇒ Boolean
Indicates whether its possible to capture the payment
95 96 97 |
# File 'app/models/spree/credit_card.rb', line 95 def can_capture?(payment) payment.pending? || payment.checkout? end |
#can_credit?(payment) ⇒ Boolean
Indicates whether its possible to credit the payment. Note that most gateways require that the payment be settled first which generally happens within 12-24 hours of the transaction.
106 107 108 |
# File 'app/models/spree/credit_card.rb', line 106 def can_credit?(payment) payment.completed? && payment.credit_allowed > 0 end |
#can_void?(payment) ⇒ Boolean
Indicates whether its possible to void the payment.
100 101 102 |
# File 'app/models/spree/credit_card.rb', line 100 def can_void?(payment) !payment.failed? && !payment.void? end |
#cc_type=(type) ⇒ Object
cc_type is set by jquery.payment, which helpfully provides different types from Active Merchant. Converting them is necessary.
60 61 62 63 64 65 66 67 68 |
# File 'app/models/spree/credit_card.rb', line 60 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_number ⇒ Object
Show the card number, with all but last 4 numbers replace with “X”. (XXXX-XXXX-XXXX-4338)
86 87 88 |
# File 'app/models/spree/credit_card.rb', line 86 def display_number "XXXX-XXXX-XXXX-#{last_digits}" end |
#expiry=(expiry) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/spree/credit_card.rb', line 38 def expiry=(expiry) return unless expiry.present? self[:month], self[:year] = if expiry.match(/\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 ⇒ Object
ActiveMerchant needs first_name/last_name because we pass it a Spree::CreditCard and it calls those methods on it. Looking at the ActiveMerchant source code we should probably be calling #to_active_merchant before passing the object to ActiveMerchant but this should do for now.
117 118 119 |
# File 'app/models/spree/credit_card.rb', line 117 def first_name name.to_s.split(/[[:space:]]/, 2)[0] end |
#has_payment_profile? ⇒ Boolean
110 111 112 |
# File 'app/models/spree/credit_card.rb', line 110 def has_payment_profile? gateway_customer_profile_id.present? || gateway_payment_profile_id.present? end |
#last_name ⇒ Object
121 122 123 |
# File 'app/models/spree/credit_card.rb', line 121 def last_name name.to_s.split(/[[:space:]]/, 2)[1] end |
#set_last_digits ⇒ Object
70 71 72 73 74 |
# File 'app/models/spree/credit_card.rb', line 70 def set_last_digits number.to_s.gsub!(/\s/,'') verification_value.to_s.gsub!(/\s/,'') self.last_digits ||= number.to_s.length <= 4 ? number : number.to_s.slice(-4..-1) end |
#to_active_merchant ⇒ Object
125 126 127 128 129 130 131 132 133 134 |
# File 'app/models/spree/credit_card.rb', line 125 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_number ⇒ Object
76 77 78 79 |
# File 'app/models/spree/credit_card.rb', line 76 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
81 82 83 |
# File 'app/models/spree/credit_card.rb', line 81 def verification_value? verification_value.present? end |