Class: Spree::CreditCard

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/spree/credit_card.rb

Direct Known Subclasses

TestCard

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#numberObject

Returns the value of attribute number.



7
8
9
# File 'app/models/spree/credit_card.rb', line 7

def number
  @number
end

#verification_valueObject

Returns the value of attribute verification_value.



7
8
9
# File 'app/models/spree/credit_card.rb', line 7

def verification_value
  @verification_value
end

Instance Method Details

#actionsObject



67
68
69
# File 'app/models/spree/credit_card.rb', line 67

def actions
  %w{capture void credit}
end

#can_capture?(payment) ⇒ Boolean

Indicates whether its possible to capture the payment

Returns:

  • (Boolean)


72
73
74
# File 'app/models/spree/credit_card.rb', line 72

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.

Returns:

  • (Boolean)


83
84
85
86
87
# File 'app/models/spree/credit_card.rb', line 83

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.

Returns:

  • (Boolean)


77
78
79
# File 'app/models/spree/credit_card.rb', line 77

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.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/models/spree/credit_card.rb', line 30

def cc_type=(type)
  real_type = case type
  when 'mastercard', 'maestro'
    'master'
  when 'amex'
    'american_express'
  when 'dinersclub'
    'diners_club'
  else
    type
  end
  self[:cc_type] = real_type
end

#display_numberObject

Show the card number, with all but last 4 numbers replace with “X”. (XXXX-XXXX-XXXX-4338)



63
64
65
# File 'app/models/spree/credit_card.rb', line 63

def display_number
  "XXXX-XXXX-XXXX-#{last_digits}"
end

#expiry=(expiry) ⇒ Object



19
20
21
22
# File 'app/models/spree/credit_card.rb', line 19

def expiry=(expiry)
  self[:month], self[:year] = expiry.split(" / ")
  self[:year] = "20" + self[:year]
end

#has_payment_profile?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'app/models/spree/credit_card.rb', line 89

def has_payment_profile?
  gateway_customer_profile_id.present?
end

#nameObject



54
55
56
# File 'app/models/spree/credit_card.rb', line 54

def name
  "#{first_name} #{last_name}"
end

#name?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/models/spree/credit_card.rb', line 50

def name?
  first_name? && last_name?
end

#set_last_digitsObject



44
45
46
47
48
# File 'app/models/spree/credit_card.rb', line 44

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_merchantObject



93
94
95
96
97
98
99
100
101
102
# File 'app/models/spree/credit_card.rb', line 93

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

Returns:

  • (Boolean)


58
59
60
# File 'app/models/spree/credit_card.rb', line 58

def verification_value?
  verification_value.present?
end