Sha256: 8ea37d0473bce6101ea10433a4ba03d1a1a858ea81456f5e0d6ac3535ba2794e

Contents?: true

Size: 708 Bytes

Versions: 2

Compression:

Stored size: 708 Bytes

Contents

Payment.class_eval do
  has_one :adjustment, :as => :source
  before_save :remove_old_adjustment
  after_save :ensure_correct_adjustment, :update_order, :if => Proc.new {|p| !p.payment_method.calculator.blank?}
  
  private
  def remove_old_adjustment
    order.adjustments.each do |adjustment|
      adjustment.destroy if adjustment.source_type == "Payment"
    end
  end
  
  def ensure_correct_adjustment
    if adjustment
      adjustment.originator = payment_method
      adjustment.save
    else
      payment_method.create_adjustment(I18n.t(:payment_adjustment), order, self, true)
      reload #ensure adjustment is present on later saves
    end
  end

  def update_order
    order.update!
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
spree_payment_calculator-0.70.1 app/model/payment_decorator.rb
spree_payment_calculator-0.70.0 app/model/payment_decorator.rb