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”.
- #first_name? ⇒ Boolean
- #has_payment_profile? ⇒ Boolean
- #last_name? ⇒ 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
81 82 83 |
# File 'app/models/spree/credit_card.rb', line 81 def actions %w{capture void credit} end |
#brand ⇒ Object
needed for some of the ActiveMerchant gateways (eg. SagePay)
75 76 77 |
# File 'app/models/spree/credit_card.rb', line 75 def brand spree_cc_type end |
#can_capture?(payment) ⇒ Boolean
Indicates whether its possible to capture the payment
86 87 88 |
# File 'app/models/spree/credit_card.rb', line 86 def can_capture?(payment) payment.state == 'pending' || payment.state == '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.
97 98 99 100 101 |
# File 'app/models/spree/credit_card.rb', line 97 def can_credit?(payment) return false unless payment.state == '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.
91 92 93 |
# File 'app/models/spree/credit_card.rb', line 91 def can_void?(payment) payment.state != 'void' end |
#display_number ⇒ Object
Show the card number, with all but last 4 numbers replace with “X”. (XXXX-XXXX-XXXX-4338)
70 71 72 |
# File 'app/models/spree/credit_card.rb', line 70 def display_number "XXXX-XXXX-XXXX-#{last_digits}" end |
#first_name? ⇒ Boolean
53 54 55 |
# File 'app/models/spree/credit_card.rb', line 53 def first_name? first_name.present? end |
#has_payment_profile? ⇒ Boolean
103 104 105 |
# File 'app/models/spree/credit_card.rb', line 103 def has_payment_profile? gateway_customer_profile_id.present? end |
#last_name? ⇒ Boolean
57 58 59 |
# File 'app/models/spree/credit_card.rb', line 57 def last_name? last_name.present? end |
#name ⇒ Object
61 62 63 |
# File 'app/models/spree/credit_card.rb', line 61 def name "#{first_name} #{last_name}" end |
#name? ⇒ Boolean
49 50 51 |
# File 'app/models/spree/credit_card.rb', line 49 def name? first_name? && last_name? end |
#set_card_type ⇒ Object
sets self.cc_type while we still have the card number
45 46 47 |
# File 'app/models/spree/credit_card.rb', line 45 def set_card_type self.cc_type ||= CardDetector.brand?(number) end |
#set_last_digits ⇒ Object
17 18 19 20 21 |
# File 'app/models/spree/credit_card.rb', line 17 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
107 108 109 110 |
# File 'app/models/spree/credit_card.rb', line 107 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.
33 34 35 36 37 38 39 40 41 42 |
# File 'app/models/spree/credit_card.rb', line 33 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
65 66 67 |
# File 'app/models/spree/credit_card.rb', line 65 def verification_value? verification_value.present? end |