Class: Spree::BankPayments::ApplyDiscount

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree/bank_payments/apply_discount.rb

Overview

Called for every payment (see PaymentDecorator), not just bank transfer ones, so switching away from bank transfer removes the discount rather than leaving it behind -- unless money has already arrived via a completed bank-transfer payment, in which case removal is skipped (see settled_by_bank_transfer?).

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(order:, payment_method:) ⇒ ApplyDiscount

Returns a new instance of ApplyDiscount.



13
14
15
16
# File 'app/services/spree/bank_payments/apply_discount.rb', line 13

def initialize(order:, payment_method:)
  @order = order
  @payment_method = payment_method
end

Class Method Details

.call(order:, payment_method:) ⇒ Object



9
10
11
# File 'app/services/spree/bank_payments/apply_discount.rb', line 9

def self.call(order:, payment_method:)
  new(order: order, payment_method: payment_method).call
end

Instance Method Details

#callObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/services/spree/bank_payments/apply_discount.rb', line 18

def call
  # If money has already arrived via a completed bank-transfer payment,
  # a later payment on a different method (store credit, an admin-added
  # card payment, etc.) must not strip the discount: that would raise
  # order.total above what was actually collected and flip an
  # already-paid order back to balance_due.
  return if settled_by_bank_transfer? && !bank_transfer?

  remove_existing
  return unless bank_transfer?
  return if percent.zero?

  allocation.each do |line_item, amount|
    next if amount.zero?

    Spree::Adjustment.create!(
      adjustable: line_item,
      order: order,
      source: payment_method,
      amount: amount,
      label: label,
      eligible: true,
      included: false
    )
  end

  order.update_with_updater!
end