Class: Spree::CreditCard
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Spree::CreditCard
- Defined in:
- app/models/spree/credit_card.rb
Direct Known Subclasses
Defined Under Namespace
Classes: CardDetector
Instance Attribute Summary collapse
-
#number ⇒ Object
Returns the value of attribute number.
-
#verification_value ⇒ Object
Returns the value of attribute verification_value.
Instance Method Summary collapse
- #actions ⇒ Object
-
#brand ⇒ Object
needed for some of the ActiveMerchant gateways (eg. SagePay).
-
#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.
-
#display_number ⇒ Object
Show the card number, with all but last 4 numbers replace with “X”.
- #has_payment_profile? ⇒ Boolean
- #name ⇒ Object
- #name? ⇒ Boolean
-
#set_card_type ⇒ Object
sets self.cc_type while we still have the card number.
- #set_last_digits ⇒ Object
- #spree_cc_type ⇒ Object
-
#to_active_merchant ⇒ Object
Some payment gateways, such as USA EPay, only support an ActiveMerchant::Billing::CreditCard object, rather than an object like that.
- #verification_value? ⇒ Boolean
Instance Attribute Details
#number ⇒ Object
Returns the value of attribute number.
8 9 10 |
# File 'app/models/spree/credit_card.rb', line 8 def number @number end |
#verification_value ⇒ Object
Returns the value of attribute verification_value.
8 9 10 |
# File 'app/models/spree/credit_card.rb', line 8 def verification_value @verification_value end |
Instance Method Details
#actions ⇒ Object
74 75 76 |
# File 'app/models/spree/credit_card.rb', line 74 def actions %w{capture void credit} end |
#brand ⇒ Object
needed for some of the ActiveMerchant gateways (eg. SagePay)
70 71 72 |
# File 'app/models/spree/credit_card.rb', line 70 def brand spree_cc_type end |
#can_capture?(payment) ⇒ Boolean
Indicates whether its possible to capture the payment
79 80 81 |
# File 'app/models/spree/credit_card.rb', line 79 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.
90 91 92 93 94 |
# File 'app/models/spree/credit_card.rb', line 90 def can_credit?(payment) return false unless payment.completed? return false unless payment.order.payment_state == 'credit_owed' payment.credit_allowed > 0 end |
#can_void?(payment) ⇒ Boolean
Indicates whether its possible to void the payment.
84 85 86 |
# File 'app/models/spree/credit_card.rb', line 84 def can_void?(payment) !payment.void? end |
#display_number ⇒ Object
Show the card number, with all but last 4 numbers replace with “X”. (XXXX-XXXX-XXXX-4338)
65 66 67 |
# File 'app/models/spree/credit_card.rb', line 65 def display_number "XXXX-XXXX-XXXX-#{last_digits}" end |
#has_payment_profile? ⇒ Boolean
96 97 98 |
# File 'app/models/spree/credit_card.rb', line 96 def has_payment_profile? gateway_customer_profile_id.present? || gateway_payment_profile_id.present? end |
#name ⇒ Object
56 57 58 |
# File 'app/models/spree/credit_card.rb', line 56 def name "#{first_name} #{last_name}" end |
#name? ⇒ Boolean
52 53 54 |
# File 'app/models/spree/credit_card.rb', line 52 def name? first_name? && last_name? end |
#set_card_type ⇒ Object
sets self.cc_type while we still have the card number
48 49 50 |
# File 'app/models/spree/credit_card.rb', line 48 def set_card_type self.cc_type ||= CardDetector.brand?(number) end |
#set_last_digits ⇒ Object
20 21 22 23 24 |
# File 'app/models/spree/credit_card.rb', line 20 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 |
#spree_cc_type ⇒ Object
100 101 102 103 |
# File 'app/models/spree/credit_card.rb', line 100 def spree_cc_type return 'visa' if Rails.env.development? cc_type end |
#to_active_merchant ⇒ Object
Some payment gateways, such as USA EPay, only support an ActiveMerchant::Billing::CreditCard object, rather than an object like that. So we need to convert it.
36 37 38 39 40 41 42 43 44 45 |
# File 'app/models/spree/credit_card.rb', line 36 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 |
#verification_value? ⇒ Boolean
60 61 62 |
# File 'app/models/spree/credit_card.rb', line 60 def verification_value? verification_value.present? end |