Class: Spree::BankPayments::HealthReporter

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

Overview

Owns the health log line and its event, for every provider.

This lives in core rather than in each provider gem on purpose: health already gates checkout and the expiry job, so if providers each logged their own way, every new provider would reinvent it and every downstream alert rule would need another clause.

Constant Summary collapse

RELOG_AFTER =
1.hour
REASONS =

A closed set. A provider hands us one of these symbols; anything else becomes :unknown. Reasons are NEVER built from an exception message or a response body -- that is how a bearer token reaches a log aggregator.

Deliberately only the four values core can actually produce. Widening a published enum later is non-breaking; narrowing it is not, and every value here is a promise to alert rules that key on reason=. Nothing hands a provider a way to supply its own reason yet, so a richer vocabulary would be advertising states no code path can reach.

%i[ok provider_error consent_revoked unknown].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payment_method:, status:, reason:) ⇒ HealthReporter

Returns a new instance of HealthReporter.



27
28
29
30
31
# File 'app/services/spree/bank_payments/health_reporter.rb', line 27

def initialize(payment_method:, status:, reason:)
  @payment_method = payment_method
  @status = clamp(status.to_s.to_sym, Reconcilers::Base::HEALTH_STATES, :transient)
  @reason = clamp(reason.to_s.to_sym, REASONS, :unknown)
end

Class Method Details

.call(payment_method:, status:, reason:) ⇒ Object



23
24
25
# File 'app/services/spree/bank_payments/health_reporter.rb', line 23

def self.call(payment_method:, status:, reason:)
  new(payment_method: payment_method, status: status, reason: reason).call
end

Instance Method Details

#callObject



33
34
35
36
37
38
# File 'app/services/spree/bank_payments/health_reporter.rb', line 33

def call
  logged = should_log?
  emit if logged
  state.record_health!(status: status, reason: reason, logged: logged)
  logged
end