Class: Payment

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

Instance Method Summary collapse

Instance Method Details

#actionsObject



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

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



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

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)


61
62
63
# File 'app/models/payment.rb', line 61

def can_credit?
  credit_allowed > 0
end

#credit(amount) ⇒ Object



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

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

#credit_allowedObject



57
58
59
# File 'app/models/payment.rb', line 57

def credit_allowed
  amount - offsets_total
end

#offsets_totalObject



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

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

#payment_sourceObject



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

def payment_source
  source.is_a?(Payment) ? source.source : source
end

#process!Object



78
79
80
81
82
83
# File 'app/models/payment.rb', line 78

def process!
  if !processing? and source and source.respond_to?(:process!)
    started_processing!
    source.process!(self) # source is responsible for updating the payment state when it's done processing
  end
end