Class: InvoiceCaptureFundsWorker

Inherits:
Object
  • Object
show all
Includes:
Sidekiq::Job
Defined in:
app/workers/invoice_capture_funds_worker.rb

Overview

Sidekiq worker that runs Invoice#capture_funds? for a single
invoice id, used by the on-demand "capture now" CRM action and as
the retry vehicle for transient gateway failures.

Instance Method Summary collapse

Instance Method Details

#perform(invoice_id) ⇒ void

This method returns an undefined value.

Skips already-paid or non-unpaid invoices (idempotent retries),
then triggers funds capture. State-machine InvalidTransition
errors are logged but not retried.

Parameters:

  • invoice_id (Integer)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/workers/invoice_capture_funds_worker.rb', line 16

def perform(invoice_id)
  invoice = Invoice.find(invoice_id)

  if invoice.paid?
    logger.info "Invoice id: #{invoice_id} is already paid, skipping capture"
    return
  end

  unless invoice.unpaid?
    logger.warn "Invoice id: #{invoice_id} is in state '#{invoice.state}', cannot capture funds (must be 'unpaid')"
    return
  end

  invoice.capture_funds?
  logger.info "Invoice capture finished for invoice id: #{invoice_id}, state: #{invoice.state}"
rescue StateMachines::InvalidTransition => e
  # Log but don't retry - invoice state doesn't allow this transition
  logger.warn "Invoice id: #{invoice_id} state transition failed: #{e.message}"
  ErrorReporting.warning(e, source: :background, invoice_id: invoice_id, invoice_state: invoice.state)
end