Class: SpreeAdyen::Webhooks::EventProcessors::CancellationEventProcessor

Inherits:
Object
  • Object
show all
Defined in:
app/services/spree_adyen/webhooks/event_processors/cancellation_event_processor.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ CancellationEventProcessor

Returns a new instance of CancellationEventProcessor.



9
10
11
# File 'app/services/spree_adyen/webhooks/event_processors/cancellation_event_processor.rb', line 9

def initialize(event)
  @event = event
end

Instance Method Details

#callObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/services/spree_adyen/webhooks/event_processors/cancellation_event_processor.rb', line 13

def call
  Rails.logger.info("[SpreeAdyen][#{event_id}]: Started processing")
  order = Spree::Order.find_by!(number: event.order_number)

  order.with_lock do
    payment_method = SpreeAdyen::Gateway.find(event.payment_method_id)
    payment = Spree::Payment.find_by!(response_code: event.fetch('originalReference'), payment_method: payment_method)

    if event.success?
      payment.set_metafield(SpreeAdyen::Gateway::CANCELLATION_PSP_REFERENCE_METAFIELD_KEY, event.psp_reference)
      payment.started_processing! if payment.can_started_processing?
      payment.void! unless payment.void?
    else
      payment.add_gateway_processing_error("Cancellation failed: #{event.fetch('reason')}")
      payment.started_processing! if payment.can_started_processing?
      payment.pend! if payment.can_pend?

      Rails.error.report(
        SpreeAdyen::CancellationError.new(event.fetch('reason')),
        context: { order_id: order.id, event: event.payload },
        source: 'spree_adyen'
      )
    end
  end

  Rails.logger.info("[SpreeAdyen][#{event_id}]: Finished processing")
end