Class: Payment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/payment.rb

Instance Method Summary collapse

Instance Method Details

#actionsObject



84
85
86
87
# File 'app/models/payment.rb', line 84

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

#build_source(params) ⇒ Object

With nested attributes, Rails calls build_ for the nested model which won’t work for a polymorphic association



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

def build_source(params)
  if payment_method and payment_method.payment_source_class
    self.source = payment_method.payment_source_class.new(params)
  end
end

#can_credit?Boolean

Returns:

  • (Boolean)


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

def can_credit?
  credit_allowed > 0
end

#credit(amount) ⇒ Object



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

def credit(amount)
  return if amount > credit_allowed
  started_processing!
  source.credit(self, amount)
end

#credit_allowedObject



50
51
52
# File 'app/models/payment.rb', line 50

def credit_allowed
  amount - offsets_total
end

#offsets_totalObject



46
47
48
# File 'app/models/payment.rb', line 46

def offsets_total
  offsets.map(&:amount).sum
end

#payment_sourceObject



89
90
91
92
# File 'app/models/payment.rb', line 89

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

#process!Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/payment.rb', line 71

def process!
  if payment_method && payment_method.source_required?
    if source
      if !processing? && source.respond_to?(:process!)
        started_processing!
        source.process!(self) # source is responsible for updating the payment state when it's done processing
      end
    else
      raise Spree::GatewayError.new(I18n.t(:payment_processing_failed))
    end
  end
end