Class: Spree::Refunds::Create

Inherits:
Workflow
  • Object
show all
Defined in:
app/workflows/spree/refunds/create.rb

Overview

Creates a refund and credits it back at the gateway.

In the workflow tier because the credit is network I/O. The refund row commits first, then the gateway is called from an external_step — a row with no transaction_id is recoverable, a gateway credit with no record is not. On a clean gateway failure the uncredited row is destroyed (credit_allowed sums every refund row, so a dangling one would block the retry); a crash between the two leaves the row for reconciliation.

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

#refund ⇒ Object (readonly)

The created refund — hook handlers read it (nil during :validate).



15
16
17
# File 'app/workflows/spree/refunds/create.rb', line 15

def refund
  @refund
end

Instance Method Details

#perform(payment:, amount: nil, reason: nil, refunder: nil, originator: nil, order: nil, tax_amount: 0) ⇒ Object

Parameters:

  • payment (Spree::Payment) —

    the captured payment to reverse

  • amount (BigDecimal, Numeric, nil) (defaults to: nil) —

    defaults to the full creditable balance

  • reason (Spree::RefundReason, nil) (defaults to: nil) —

    defaults to the store's return-processing reason

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

    who is issuing it (see Spree.actor_classes)

  • originator (Object, nil) (defaults to: nil) —

    what triggered the refund — a Spree::Return, Exchange or Claim; nil for a manual refund

  • order (Spree::Order, nil) (defaults to: nil) —

    which order is being put right. Required only when the payment is shared by a split checkout, where it covers several orders and the payment cannot say which one this refund is for; otherwise the payment's own order is taken.

  • tax_amount (BigDecimal, Numeric) (defaults to: 0) —

    the tax inside amount, when what is being refunded carried tax (see Spree::Refund#tax_amount)



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

def perform(payment:, amount: nil, reason: nil, refunder: nil, originator: nil, order: nil, tax_amount: 0)
  super

  step :ensure_refundable
  @amount_to_refund = amount || payment.credit_allowed
  step :ensure_amount_within_balance

  # Veto point — before the refund record exists and before any money
  # moves back.
  run_hooks :validate
  run_hooks :before_refund

  step :create_refund, on_flow_failure: :destroy_uncredited_refund
  external_step :credit_at_gateway

  run_hooks :after_refund
  # A requested refund is confirmed by the provider's webhook, which
  # publishes payment.refunded then.
  if refund.requested?
    refund.publish_event('refund.requested')
  else
    payment.publish_event('payment.refunded', nil, refund_id: refund.prefixed_id)
  end
  success(refund)
end