Class: Spree::OrderCapturing

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/order_capturing.rb

Instance Method Summary collapse

Constructor Details

#initialize(order, sorted_payment_method_classes = nil) ⇒ OrderCapturing

Returns a new instance of OrderCapturing



13
14
15
16
# File 'app/models/spree/order_capturing.rb', line 13

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

Instance Method Details

#capture_paymentsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/spree/order_capturing.rb', line 18

def capture_payments
  return if @order.paid?

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

    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
  end
end