Module: Spree::Purchase::PaymentProcessing

Included in:
Cart, Order
Defined in:
app/models/concerns/spree/purchase/payment_processing.rb

Overview

Payment processing shared by Spree::Cart and Spree::Order.

Instance Method Summary collapse

Instance Method Details

#amount_left_to_pay ⇒ BigDecimal

What is still owed once every live payment is counted, settled or not — the amount a new payment should be for. Never negative.

Returns:

  • (BigDecimal)


69
70
71
# File 'app/models/concerns/spree/purchase/payment_processing.rb', line 69

def amount_left_to_pay
  [outstanding_balance - valid_payments.reject { |payment| payment.status == 'completed' }.sum(BigDecimal('0'), &:amount), BigDecimal('0')].max
end

#authorize_payments! ⇒ Object



22
23
24
# File 'app/models/concerns/spree/purchase/payment_processing.rb', line 22

def authorize_payments!
  process_payments_with(:authorize)
end

#capture_payments! ⇒ Object



26
27
28
# File 'app/models/concerns/spree/purchase/payment_processing.rb', line 26

def capture_payments!
  process_payments_with(:purchase)
end

#confirmation_required? ⇒ Boolean

Whether a confirm/review pass is expected before completion — computed from data only.

Returns:

  • (Boolean)


56
57
58
59
# File 'app/models/concerns/spree/purchase/payment_processing.rb', line 56

def confirmation_required?
  Spree::StorePreferences.read(store, :always_include_confirm_step) ||
    valid_payments.filter_map(&:payment_method).any?(&:confirmation_required?)
end

#payment_methods ⇒ Array<Spree::PaymentMethod>

Active front-end payment methods available for this record. Store credit is never one of them: it is applied through its own endpoint before the buyer chooses how to pay the rest.

Returns:



39
40
41
42
43
# File 'app/models/concerns/spree/purchase/payment_processing.rb', line 39

def payment_methods
  store.payment_methods.active.storefront_visible.select do |payment_method|
    !payment_method.store_credit? && payment_method.available_for_order?(self)
  end
end

#payment_required? ⇒ Boolean

Free checkouts have no money to collect.

Returns:

  • (Boolean)


48
49
50
# File 'app/models/concerns/spree/purchase/payment_processing.rb', line 48

def payment_required?
  total.to_d.positive?
end

#pending_payments ⇒ Object



30
31
32
# File 'app/models/concerns/spree/purchase/payment_processing.rb', line 30

def pending_payments
  payments.pending
end

#process_payments! ⇒ Object

Processes any pending payments, recording a gateway failure on errors. The completion workflows check whether payments actually cover the total rather than trusting the return value.

Deliberately not locked: holding the order's row lock across gateway round trips is what the workflow external_step boundary exists to prevent. Concurrent processing of the same payment is guarded twice — the workflow's atomic claim refuses a payment another writer settled, and the gateway idempotency key (spree-<payment.number>) makes a duplicate charge a no-op at the provider.



15
16
17
18
19
20
# File 'app/models/concerns/spree/purchase/payment_processing.rb', line 15

def process_payments!
  # with_lock used to reload the record as a side effect; callers create
  # payments right before processing, so the association must refresh.
  payments.reset
  process_payments_with(nil)
end

#unprocessed_payments ⇒ Object



61
62
63
# File 'app/models/concerns/spree/purchase/payment_processing.rb', line 61

def unprocessed_payments
  payments.select(&:checkout?)
end