Class: Spree::CreditCard

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

Direct Known Subclasses

TestCard

Defined Under Namespace

Classes: CardDetector

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#numberObject

Returns the value of attribute number.



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

def number
  @number
end

#verification_valueObject

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

#actionsObject



74
75
76
# File 'app/models/spree/credit_card.rb', line 74

def actions
  %w{capture void credit}
end

#brandObject

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

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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

def can_void?(payment)
  !payment.void?
end

#display_numberObject

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

Returns:

  • (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

#nameObject



56
57
58
# File 'app/models/spree/credit_card.rb', line 56

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

#name?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/spree/credit_card.rb', line 52

def name?
  first_name? && last_name?
end

#set_card_typeObject

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_digitsObject



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_typeObject



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_merchantObject

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

Returns:

  • (Boolean)


60
61
62
# File 'app/models/spree/credit_card.rb', line 60

def verification_value?
  verification_value.present?
end