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



81
82
83
# File 'app/models/spree/credit_card.rb', line 81

def actions
  %w{capture void credit}
end

#brandObject

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

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


91
92
93
# File 'app/models/spree/credit_card.rb', line 91

def can_void?(payment)
  payment.state != 'void'
end

#display_numberObject

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

Returns:

  • (Boolean)


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

def first_name?
  first_name.present?
end

#has_payment_profile?Boolean

Returns:

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

Returns:

  • (Boolean)


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

def last_name?
  last_name.present?
end

#nameObject



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

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

#name?Boolean

Returns:

  • (Boolean)


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

def name?
  first_name? && last_name?
end

#set_card_typeObject

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_digitsObject



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_typeObject



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_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.



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

Returns:

  • (Boolean)


65
66
67
# File 'app/models/spree/credit_card.rb', line 65

def verification_value?
  verification_value.present?
end