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.



25
26
27
# File 'app/models/spree/payment.rb', line 25

def source_attributes
  @source_attributes
end

Instance Method Details

#actionsObject



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

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



81
82
83
84
85
86
87
88
89
# File 'app/models/spree/payment.rb', line 81

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



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

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)
  end
end

#can_credit?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'app/models/spree/payment.rb', line 99

def can_credit?
  credit_allowed > 0
end

#credit_allowedObject



95
96
97
# File 'app/models/spree/payment.rb', line 95

def credit_allowed
  amount - offsets_total.abs
end

#currencyObject



72
73
74
# File 'app/models/spree/payment.rb', line 72

def currency
  order.currency
end

#is_avs_risky?Boolean

Returns:

  • (Boolean)


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

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)


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

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



76
77
78
# File 'app/models/spree/payment.rb', line 76

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

#offsets_totalObject



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

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

#payment_sourceObject



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

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

#persist_invalidObject



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

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