Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes an issue with shipment state after editing a paid order.
[spree#1574 state:resolved]
  • Loading branch information
schof committed Aug 6, 2010
1 parent 7852a3b commit 6a5ae06
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions app/models/payment.rb
Expand Up @@ -5,14 +5,14 @@ class Payment < ActiveRecord::Base

has_many :transactions
alias :txns :transactions

after_save :create_payment_profile, :if => :payment_profiles_supported?
after_save :check_payments, :if => :order_payment?
after_destroy :check_payments, :if => :order_payment?

accepts_nested_attributes_for :source
validate :amount_is_valid_for_outstanding_balance_or_credit, :if => :order_payment?

validate :amount_is_valid_for_outstanding_balance_or_credit, :if => :order_payment?
validates_presence_of :payment_method, :if => Proc.new { |payable| payable.is_a? Checkout }

named_scope :from_creditcard, :conditions => {:source_type => 'Creditcard'}
Expand All @@ -27,23 +27,23 @@ def build_source(params)
self.source = payment_method.payment_source_class.new(params)
end
end

def process!
source.process!(self) if source and source.respond_to?(:process!)
end
end

def can_finalize?
!finalized?
end

def finalize!
return unless can_finalize?
source.finalize!(self) if source and source.respond_to?(:finalize!)
self.payable = payable.order
save!
payable.save!
end

def finalized?
payable.is_a?(Order)
end
Expand All @@ -54,27 +54,28 @@ def actions
end

private

def check_payments
return unless order.checkout_complete
#sorting by created_at.to_f to ensure millisecond percsision, plus ID - just in case
events = order.state_events.sort_by { |e| [e.created_at.to_f, e.id] }.reverse


if order.returnable_units.nil? && order.return_authorizations.size >0
order.return!
elsif events.present? and %w(over_paid under_paid).include?(events.first.name)
events.each do |event|
if %w(shipped paid new).include?(event.previous_state)
order.update_attribute("state", event.previous_state)
order.pay!
order.update_attribute("state", event.previous_state) if %w(shipped returned).include?(event.previous_state)
return
end
end
elsif order.payment_total >= order.total
order.pay!
end
end

def amount_is_valid_for_outstanding_balance_or_credit
if amount < 0
if amount.abs > order.outstanding_credit
Expand All @@ -84,12 +85,12 @@ def amount_is_valid_for_outstanding_balance_or_credit
if amount > order.outstanding_balance
errors.add(:amount, "Is greater than the outstanding balance (#{order.outstanding_balance})")
end
end
end
end

def order_payment?
payable_type == "Order"
end
end

def payment_profiles_supported?
source && source.payment_gateway && source.payment_gateway.payment_profiles_supported?
Expand All @@ -101,5 +102,5 @@ def create_payment_profile
rescue ActiveMerchant::ConnectionError => e
gateway_error I18n.t(:unable_to_connect_to_gateway)
end

end

0 comments on commit 6a5ae06

Please sign in to comment.