Class: Spree::Orders::Cancel

Inherits:
Object
  • Object
show all
Includes:
ServiceModule::Base
Defined in:
app/services/spree/orders/cancel.rb

Constant Summary collapse

DEFAULT_REASON =
'other'.freeze

Instance Method Summary collapse

Methods included from ServiceModule::Base

prepended

Instance Method Details

#call(order:, canceler: nil, canceled_at: nil, reason: DEFAULT_REASON, note: nil, restock_items: false, refund_payments: false, refund_amount: nil, notify_customer: false) ⇒ Spree::ServiceModule::Result

Cancels an order and records a Spree::OrderCancellation history record. Legacy ‘canceler:` and `canceled_at:` remain valid; new keywords are additive.

Parameters:

  • order (Spree::Order)
  • canceler (Object, nil) (defaults to: nil)

    the user/admin who initiated the cancellation

  • canceled_at (Time, nil) (defaults to: nil)

    timestamp (defaults to Time.current)

  • reason (String) (defaults to: DEFAULT_REASON)

    one of Spree::OrderCancellation::REASONS

  • note (String, nil) (defaults to: nil)

    staff-facing note

  • restock_items (Boolean) (defaults to: false)

    whether to return inventory

  • refund_payments (Boolean) (defaults to: false)

    whether to refund captured payments

  • refund_amount (BigDecimal, Numeric, nil) (defaults to: nil)

    amount to refund; when refund_payments is true and this is nil, defaults to order.payment_total

  • notify_customer (Boolean) (defaults to: false)

    hint for subscribers

Returns:



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/spree/orders/cancel.rb', line 22

def call(order:, canceler: nil, canceled_at: nil,
         reason: DEFAULT_REASON, note: nil,
         restock_items: false, refund_payments: false, refund_amount: nil,
         notify_customer: false)
  canceled_at ||= Time.current
  refund_amount ||= order.payment_total if refund_payments

  order.transaction do
    order.cancellations.create!(
      reason: reason,
      note: note,
      restock_items: restock_items,
      refund_payments: refund_payments,
      refund_amount: refund_amount,
      notify_customer: notify_customer,
      canceled_by: canceler,
      created_at: canceled_at
    )

    changes = { canceled_at: canceled_at }
    changes[:canceler_id] = canceler.id if canceler.present?
    order.update_columns(changes)
    order.cancel!
  end

  order.publish_event('order.canceled', order.event_payload.merge(notify_customer: notify_customer))
  success(order.reload)
rescue ActiveRecord::RecordInvalid, StateMachines::InvalidTransition
  failure(order)
end