Class: Spree::Adyen::NotificationProcessor

Inherits:
Object
  • Object
show all
Defined in:
app/models/spree/adyen/notification_processor.rb

Overview

Class responsible for taking in a notification from Adyen and applying some form of modification to the associated payment.

I would in the future like to refactor this by breaking this into separate classes that are only aware of how to process specific kinds of notifications (auth, capture, refund, etc.).

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(notification, payment = nil) ⇒ NotificationProcessor

Returns a new instance of NotificationProcessor.



12
13
14
15
16
# File 'app/models/spree/adyen/notification_processor.rb', line 12

def initialize(notification, payment = nil)
  self.notification = notification
  self.order = notification.order
  self.payment = payment ? payment : notification.payment
end

Instance Attribute Details

#notificationObject

Returns the value of attribute notification.



10
11
12
# File 'app/models/spree/adyen/notification_processor.rb', line 10

def notification
  @notification
end

#orderObject

Returns the value of attribute order.



10
11
12
# File 'app/models/spree/adyen/notification_processor.rb', line 10

def order
  @order
end

#paymentObject

Returns the value of attribute payment.



10
11
12
# File 'app/models/spree/adyen/notification_processor.rb', line 10

def payment
  @payment
end

Class Method Details

.process_outstanding!(payment) ⇒ Object

for the given payment, process all notifications that are currently unprocessed in the order that they were dispatched.



20
21
22
23
24
25
26
27
28
29
# File 'app/models/spree/adyen/notification_processor.rb', line 20

def self.process_outstanding!(payment)
  payment.
    source.
    notifications(true). # bypass caching
    unprocessed.
    as_dispatched.
    map do |notification|
      new(notification, payment).process!
    end
end

Instance Method Details

#process!Object

only process the notification if there is a matching payment there’s a number of reasons why there may not be a matching payment such as test notifications, reports etc, we just log them and then accept



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/spree/adyen/notification_processor.rb', line 34

def process!
  return notification if order.nil?

  order.with_lock do
    if should_create_payment?
      self.payment = create_missing_payment
    end

    if !notification.success?
      handle_failure

    elsif notification.modification_event?
      handle_modification_event

    elsif notification.normal_event?
      handle_normal_event

    end
  end

  return notification
end