Class: Spree::Payment

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

Defined Under Namespace

Modules: Processing Classes: GatewayOptions

Constant Summary collapse

NON_RISKY_AVS_CODES =
['B', 'D', 'H', 'J', 'M', 'Q', 'T', 'V', 'X', 'Y'].freeze
RISKY_AVS_CODES =
['A', 'C', 'E', 'F', 'G', 'I', 'K', 'L', 'N', 'O', 'P', 'R', 'S', 'U', 'W', 'Z'].freeze
INVALID_STATES =
%w(failed invalid).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NumberAsParam

#to_param

Methods included from Processing

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

Methods inherited from Base

belongs_to_required_by_default, page, spree_base_scopes

Methods included from Spree::Preferences::Preferable

#clear_preferences, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

Instance Attribute Details

#capture_on_dispatchObject

Returns the value of attribute capture_on_dispatch


39
40
41
# File 'app/models/spree/payment.rb', line 39

def capture_on_dispatch
  @capture_on_dispatch
end

#request_envObject

Returns the value of attribute request_env


39
40
41
# File 'app/models/spree/payment.rb', line 39

def request_env
  @request_env
end

#source_attributesObject

Returns the value of attribute source_attributes


39
40
41
# File 'app/models/spree/payment.rb', line 39

def source_attributes
  @source_attributes
end

Instance Method Details

#actionsObject


148
149
150
151
# File 'app/models/spree/payment.rb', line 148

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

#amount=(amount) ⇒ Object


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

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


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

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)
    source.payment_method_id = payment_method.id
    source.user_id = order.user_id if order
  end
end

#can_credit?Boolean


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

def can_credit?
  credit_allowed > 0
end

#captured_amountObject


170
171
172
# File 'app/models/spree/payment.rb', line 170

def captured_amount
  capture_events.sum(:amount)
end

#credit_allowedObject


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

def credit_allowed
  amount - (offsets_total.abs + refunds.sum(:amount))
end

#editable?Boolean


178
179
180
# File 'app/models/spree/payment.rb', line 178

def editable?
  checkout? || pending?
end

#is_avs_risky?Boolean


158
159
160
161
# File 'app/models/spree/payment.rb', line 158

def is_avs_risky?
  return false if avs_response.blank? || NON_RISKY_AVS_CODES.include?(avs_response)
  true
end

#is_cvv_risky?Boolean


163
164
165
166
167
168
# File 'app/models/spree/payment.rb', line 163

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?
  true
end

#moneyObject Also known as: display_amount


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

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

#offsets_totalObject


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

def offsets_total
  offsets.sum(:amount)
end

#payment_sourceObject


153
154
155
156
# File 'app/models/spree/payment.rb', line 153

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

#transaction_idObject

transaction_id is much easier to understand


66
67
68
# File 'app/models/spree/payment.rb', line 66

def transaction_id
  response_code
end

#uncaptured_amountObject


174
175
176
# File 'app/models/spree/payment.rb', line 174

def uncaptured_amount
  amount - captured_amount
end