Class: Spree::Payment

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Processing
Defined in:
app/models/spree/payment.rb,
app/models/spree/payment/processing.rb

Defined Under Namespace

Modules: Processing

Constant Summary collapse

IDENTIFIER_CHARS =
(('A'..'Z').to_a + ('0'..'9').to_a - %w(0 1 I O)).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Processing

#authorize!, #cancel!, #capture!, #credit!, #gateway_options, #partial_credit, #process!, #purchase!, #void_transaction!

Instance Attribute Details

#source_attributesObject

Returns the value of attribute source_attributes.



28
29
30
# File 'app/models/spree/payment.rb', line 28

def source_attributes
  @source_attributes
end

Instance Method Details

#actionsObject



124
125
126
127
# File 'app/models/spree/payment.rb', line 124

def actions
  return [] unless payment_source and payment_source.respond_to? :actions
  payment_source.actions.select { |action| !payment_source.respond_to?("can_#{action}?") or payment_source.send("can_#{action}?", self) }
end

#amount=(amount) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'app/models/spree/payment.rb', line 92

def amount=(amount)
  self[:amount] =
    case amount
    when String
      separator = I18n.t('number.currency.format.separator')
      number    = amount.delete("^0-9-#{separator}").tr(separator, '.')
      number.to_d if number.present?
    end || amount
end

#build_sourceObject



115
116
117
118
119
120
121
122
# File 'app/models/spree/payment.rb', line 115

def build_source
  return unless new_record?
  if source_attributes.present? && source.blank? && payment_method.try(:payment_source_class)
    self.source = payment_method.payment_source_class.new(source_attributes)
    self.source.payment_method_id = payment_method.id
    self.source.user_id = self.order.user_id if self.order
  end
end

#can_credit?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'app/models/spree/payment.rb', line 110

def can_credit?
  credit_allowed > 0
end

#credit_allowedObject



106
107
108
# File 'app/models/spree/payment.rb', line 106

def credit_allowed
  amount - offsets_total.abs
end

#currencyObject



83
84
85
# File 'app/models/spree/payment.rb', line 83

def currency
  order.currency
end

#is_avs_risky?Boolean

Returns:

  • (Boolean)


134
135
136
137
138
# File 'app/models/spree/payment.rb', line 134

def is_avs_risky?
  return false if avs_response == "D"
  return false if avs_response.blank?
  return true
end

#is_cvv_risky?Boolean

Returns:

  • (Boolean)


140
141
142
143
144
145
# File 'app/models/spree/payment.rb', line 140

def is_cvv_risky?
  return false if cvv_response_code == "M"
  return false if cvv_response_code.nil?
  return false if cvv_response_message.present?
  return true
end

#moneyObject Also known as: display_amount



87
88
89
# File 'app/models/spree/payment.rb', line 87

def money
  Spree::Money.new(amount, { currency: currency })
end

#offsets_totalObject



102
103
104
# File 'app/models/spree/payment.rb', line 102

def offsets_total
  offsets.pluck(:amount).sum
end

#payment_sourceObject



129
130
131
132
# File 'app/models/spree/payment.rb', line 129

def payment_source
  res = source.is_a?(Payment) ? source.source : source
  res || payment_method
end

#persist_invalidObject



42
43
44
45
46
# File 'app/models/spree/payment.rb', line 42

def persist_invalid
  return unless ['failed', 'invalid'].include?(state)
  state_will_change!
  save
end