Sha256: 0485ce2e0fe4ed128e82b087e82aa458a958e7b4cfd093100cd9af44ce918e1a

Contents?: true

Size: 739 Bytes

Versions: 1

Compression:

Stored size: 739 Bytes

Contents

Spree::Payment.class_eval do
  has_one :adjustment, :as => :source, :dependent => :destroy
  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

1 entries across 1 versions & 1 rubygems

Version Path
spree_payment_calculator-1.1.0 app/model/spree/payment_decorator.rb