Class: Spree::Payment

Inherits:
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
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

Instance Attribute Summary collapse

Instance Method Summary collapse

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



30
31
32
# File 'app/models/spree/payment.rb', line 30

def request_env
  @request_env
end

#source_attributesObject

Returns the value of attribute source_attributes



30
31
32
# File 'app/models/spree/payment.rb', line 30

def source_attributes
  @source_attributes
end

Instance Method Details

#actionsObject



144
145
146
147
# File 'app/models/spree/payment.rb', line 144

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



112
113
114
115
116
117
118
119
120
# File 'app/models/spree/payment.rb', line 112

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



135
136
137
138
139
140
141
142
# File 'app/models/spree/payment.rb', line 135

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)


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

def can_credit?
  credit_allowed > 0
end

#credit_allowedObject



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

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

#currencyObject



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

def currency
  order.currency
end

#is_avs_risky?Boolean

Returns:

  • (Boolean)


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

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)


159
160
161
162
163
164
# File 'app/models/spree/payment.rb', line 159

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



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

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

#offsets_totalObject



122
123
124
# File 'app/models/spree/payment.rb', line 122

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

#payment_sourceObject



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

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

#persist_invalidObject



58
59
60
61
62
# File 'app/models/spree/payment.rb', line 58

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

#transaction_idObject

transaction_id is much easier to understand



54
55
56
# File 'app/models/spree/payment.rb', line 54

def transaction_id
  response_code
end

#uncaptured_amountObject



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

def uncaptured_amount
  amount - capture_events.sum(:amount)
end