Class: Spree::Payments::HandleModificationWebhookJob

Inherits:
BaseJob
  • Object
show all
Defined in:
app/jobs/spree/payments/handle_modification_webhook_job.rb

Overview

Runs Spree::Payments::HandleModificationWebhook for an outcome the webhook endpoint accepted: the provider got its 200 already and will not send it again, so a failure is retried here.

An outcome can name a record Spree has not written yet: it arrived before the request that writes it finished, or that request's answer never reached Spree and a retry of it writes the record later. The job waits for the record as long as a retried request still gets the provider's first answer.

Defined Under Namespace

Classes: OutcomeFailed, RecordNeverWritten

Constant Summary collapse

CAPTURE_ACTIONS =
%w[capture_succeeded capture_failed].freeze
RECORD_WAIT =

How long a retried request still gets the provider's first answer: Adyen keeps an idempotency key for at least 7 days.

7.days
ANSWER_WINDOW =

Longer than any gateway call takes with its own retries, so after it no request for this outcome can still be running.

5.minutes

Constants inherited from BaseJob

BaseJob::TRANSIENT_ERRORS

Instance Method Summary collapse

Instance Method Details

#perform(payment_method_id:, action:, reference:, received_at:, payment_id: nil, metadata: {}) ⇒ Object

Parameters:

  • reference (String) —

    the provider reference the outcome names its record by

  • received_at (String) —

    when the endpoint accepted the outcome, ISO 8601

  • payment_id (Integer, nil) (defaults to: nil) —

    the payment a capture outcome's capture was asked for

Raises:



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/jobs/spree/payments/handle_modification_webhook_job.rb', line 37

def perform(payment_method_id:, action:, reference:, received_at:, payment_id: nil, metadata: {})
  payment_method = Spree::PaymentMethod.find(payment_method_id)
  Spree::Current.store = payment_method.store
  received_at = Time.zone.parse(received_at)

  record = payment_method.modification_record(action.to_sym, reference) ||
           unanswered_capture(payment_method, action, reference, payment_id, , received_at)
  return wait_for_record(payment_method, action, reference, received_at) if record.nil?

  result = Spree.payments_handle_modification_webhook_workflow.call(
    payment_method: payment_method,
    action: action.to_sym,
    record: record,
    metadata: 
  )

  raise OutcomeFailed, "#{action} #{reference}: #{result.error}" if result&.failure?
end