Class: Spree::Exchanges::Fulfill

Inherits:
Workflow
  • Object
show all
Includes:
Fulfillments::Replacements, Refunds::OrderPayments, Refunds::TaxCredit
Defined in:
app/workflows/spree/exchanges/fulfill.rb

Overview

Ships the replacements and settles the difference in price.

Both halves of the swap carry tax: the units that came back are credited the tax the customer paid on them, and the replacements are taxed as a new sale. The difference between the two, tax included, is what settles.

The money branch mirrors Returns::Refund: a credit the customer is owed can be store credit (an internal ledger write, inside the transaction) or a gateway refund (outside it). A balance the customer owes is put on the order as a fee for the merchant to collect — charging a stored card without a fresh authorization is not something core should do silently.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Workflow

#call, define_argument_readers, #errors, #halt!, hooks, inherited, model_name, #read_attribute_for_validation, #reject!, workflow_key

Methods included from ServiceModule::Base

#call, prepended

Methods included from Workflow::ControlFlow

#failure

Instance Attribute Details

#fulfillments ⇒ Object (readonly)

Fulfillments created for the replacement items.



22
23
24
# File 'app/workflows/spree/exchanges/fulfill.rb', line 22

def fulfillments
  @fulfillments
end

#refunds ⇒ Object (readonly)

Fulfillments created for the replacement items.



22
23
24
# File 'app/workflows/spree/exchanges/fulfill.rb', line 22

def refunds
  @refunds
end

Instance Method Details

#perform(exchange:, refund_method: 'store_credit', refunder: nil) ⇒ Object

Parameters:

  • exchange (Spree::Exchange) —

    must be received

  • refund_method (String) (defaults to: 'store_credit') —

    how a credit is returned when the replacements are cheaper: 'store_credit' or 'original_payment'

  • refunder (Object, nil) (defaults to: nil)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/workflows/spree/exchanges/fulfill.rb', line 28

def perform(exchange:, refund_method: 'store_credit', refunder: nil)
  super

  @fulfillments = []
  @refunds = []
  step :ensure_received
  step :settle_tax
  step :ensure_refund_method
  run_hooks :validate

  ApplicationRecord.transaction do
    step :build_replacement_fulfillments
    run_hooks :before_settle
    step :issue_store_credit if credit_due? && internal_refund?
    step :charge_balance if balance_owed?
    step :mark_fulfilled
  end

  external_step :refund_at_gateway if credit_due? && !internal_refund?
  external_step :refund_tax
  external_step :commit_replacement

  step :recalculate_order
  run_hooks :after_fulfill
  exchange.publish_event('exchange.fulfilled')
  success(exchange.reload)
end