Class: SpreeAdyen::Webhooks::EventProcessors::AuthorisationEventProcessor

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

Instance Method Summary collapse

Constructor Details

#initialize(event) ⇒ AuthorisationEventProcessor

Returns a new instance of AuthorisationEventProcessor.



5
6
7
# File 'app/services/spree_adyen/webhooks/event_processors/authorisation_event_processor.rb', line 5

def initialize(event)
  @event = event
end

Instance Method Details

#callObject



9
10
11
12
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
40
41
42
43
44
45
46
47
48
49
# File 'app/services/spree_adyen/webhooks/event_processors/authorisation_event_processor.rb', line 9

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)
    # session_id is available only for session based payments so payment_session can be nil
    payment_session = order.adyen_payment_sessions.find_by(adyen_id: event.session_id)
    source = SpreeAdyen::Webhooks::Actions::CreateSource.new(event: event, payment_method: payment_method, user: order.user).call
    # create or find payment
    # atm payment should be already created for web channel (but there is no payment for mobile channels)
    # for web channel payment response code is updated from session_id to psp_reference
    # psp_reference is required for refund flow but is not available before this event
    payment_session&.lock!

    payment = find_or_initialize_payment(payment_method)
    payment.assign_attributes(
      response_code: event.psp_reference,
      amount: event.amount.to_d,
      order: order,
      source: source
    )
    payment.save!

    payment.started_processing! if payment.checkout?

    if event.success?
      payment_session&.complete
      payment.confirm!
      Spree::Dependencies.checkout_complete_service.constantize.call(order: order) unless order.completed?
    else
      payment.failure!
      payment_session&.refuse
      if order.completed?
        Rails.error.unexpected('Payment failed for previously completed order', context: { order_id: order.id, event: event.payload },
                                                                                source: 'spree_adyen')
      end
    end
  end
  Rails.logger.info("[SpreeAdyen][#{event_id}]: Finished processing")
end