Sha256: a467eaf73e70dde6af145ecb38b13430f42f32e529b9adc9fad6dbe06ec605b7

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

class Spree::OrderCapturing
  # Allows for prioritizing payment methods in the order to be captured
  class_attribute :sorted_payment_method_classes
  self.sorted_payment_method_classes = []

  # Allows your store to void unused payments and release auths
  class_attribute :void_unused_payments
  self.void_unused_payments = false

  class_attribute :failure_handler
  self.failure_handler = ->(failures) { raise Spree::OrderCapturingFailures.new(failures.to_json) }

  def initialize(order, sorted_payment_method_classes = nil)
    @order = order
    @sorted_payment_method_classes = sorted_payment_method_classes || Spree::OrderCapturing.sorted_payment_method_classes
  end

  def capture_payments
    return if @order.paid?

    Spree::OrderMutex.with_lock!(@order) do
      uncaptured_amount = @order.display_total.cents

      begin
        sorted_payments(@order).each do |payment|
          amount = [uncaptured_amount, payment.money.cents].min

          if amount > 0
            payment.capture!(amount)
            uncaptured_amount -= amount
          elsif Spree::OrderCapturing.void_unused_payments
            payment.void_transaction!
          end
        end
      ensure
        @order.update!
      end
    end
  end

  private

  def sorted_payments(order)
    payments = order.pending_payments
    payments = payments.sort_by { |p| [@sorted_payment_method_classes.index(p.payment_method.class), p.id] }
  end
end

class Spree::OrderCapturingFailures < StandardError; end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
solidus_core-1.0.0.pre app/models/spree/order_capturing.rb