Class: Spree::Payment

Inherits:
Base
  • Object
show all
Extended by:
FriendlyId
Includes:
NumberGenerator, 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

Constants included from NumberGenerator

NumberGenerator::NUMBER_LENGTH, NumberGenerator::NUMBER_LETTERS, NumberGenerator::NUMBER_PREFIX

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from NumberGenerator

by_number

Methods included from Processing

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

Methods inherited from Base

page

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

#request_envObject

Returns the value of attribute request_env.



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

def request_env
  @request_env
end

#source_attributesObject

Returns the value of attribute source_attributes.



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

def source_attributes
  @source_attributes
end

Instance Method Details

#actionsObject



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

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



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

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



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

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)


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

def can_credit?
  credit_allowed > 0
end

#captured_amountObject



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

def captured_amount
  capture_events.sum(:amount)
end

#credit_allowedObject



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

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

#currencyObject



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

def currency
  order.currency
end

#editable?Boolean

Returns:

  • (Boolean)


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

def editable?
  checkout? || pending?
end

#generate_number(options = {}) ⇒ Object



9
10
11
12
13
14
# File 'app/models/spree/payment.rb', line 9

def generate_number(options = {})
  options[:prefix] ||= 'P'
  options[:letters] ||= true
  options[:length] ||= 7
  super(options)
end

#is_avs_risky?Boolean

Returns:

  • (Boolean)


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

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

#is_cvv_risky?Boolean

Returns:

  • (Boolean)


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

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



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

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

#offsets_totalObject



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

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

#payment_sourceObject



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

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

#transaction_idObject

transaction_id is much easier to understand



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

def transaction_id
  response_code
end

#uncaptured_amountObject



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

def uncaptured_amount
  amount - captured_amount
end