Class: Spree::CreditCard
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- 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.
-
#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
- #has_payment_profile? ⇒ Boolean
- #set_last_digits ⇒ Object
- #to_active_merchant ⇒ Object
- #try_type_from_number ⇒ Object
- #verification_value? ⇒ Boolean
Instance Attribute Details
#encrypted_data ⇒ Object
Returns the value of attribute encrypted_data.
8 9 10 |
# File 'app/models/spree/credit_card.rb', line 8 def encrypted_data @encrypted_data end |
#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
82 83 84 |
# File 'app/models/spree/credit_card.rb', line 82 def actions %w{capture void credit} end |
#can_capture?(payment) ⇒ Boolean
Indicates whether its possible to capture the payment
87 88 89 |
# File 'app/models/spree/credit_card.rb', line 87 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.
98 99 100 101 102 |
# File 'app/models/spree/credit_card.rb', line 98 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.
92 93 94 |
# File 'app/models/spree/credit_card.rb', line 92 def can_void?(payment) !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.
52 53 54 55 56 57 58 59 60 |
# File 'app/models/spree/credit_card.rb', line 52 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)
78 79 80 |
# File 'app/models/spree/credit_card.rb', line 78 def display_number "XXXX-XXXX-XXXX-#{last_digits}" end |
#expiry=(expiry) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/models/spree/credit_card.rb', line 31 def expiry=(expiry) return unless expiry.present? self[:month], self[:year] = if expiry.match(/\d\s?\/\s?\d/) # 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 self[:year] = "20" + self[:year] if self[:year].length == 2 self[:year] = self[:year].to_i self[:month] = self[:month].to_i end |
#has_payment_profile? ⇒ Boolean
104 105 106 |
# File 'app/models/spree/credit_card.rb', line 104 def has_payment_profile? gateway_customer_profile_id.present? || gateway_payment_profile_id.present? end |
#set_last_digits ⇒ Object
62 63 64 65 66 |
# File 'app/models/spree/credit_card.rb', line 62 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
108 109 110 111 112 113 114 115 116 117 |
# File 'app/models/spree/credit_card.rb', line 108 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
68 69 70 71 |
# File 'app/models/spree/credit_card.rb', line 68 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
73 74 75 |
# File 'app/models/spree/credit_card.rb', line 73 def verification_value? verification_value.present? end |