Class: AdyenNotification

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/adyen_notification.rb

Overview

The AdyenNotification class handles notifications sent by Adyen to your servers.

Because notifications contain important payment status information, you should store these notifications in your database. For this reason, AdyenNotification inherits from ActiveRecord::Base, and a migration is included to simply create a suitable table to store the notifications in.

Adyen can either send notifications to you via HTTP POST requests, or SOAP requests. Because SOAP is not really well supported in Rails and setting up a SOAP server is not trivial, only handling HTTP POST notifications is currently supported.

Examples:

@notification = AdyenNotification.log(request)
if @notification.successful_authorisation?
  @invoice = Invoice.find(@notification.merchant_reference)
  @invoice.set_paid!
end

Constant Summary collapse

AUTO_CAPTURE_ONLY_METHODS =
[
  "ideal",
  "c_cash",
  "directEbanking",
  "trustly",
  "giropay",
  "bcmc"
].freeze
AUTHORISATION =
"AUTHORISATION".freeze
CANCELLATION =
"CANCELLATION".freeze
REFUND =
"REFUND".freeze
CANCEL_OR_REFUND =
"CANCEL_OR_REFUND".freeze
CAPTURE =
"CAPTURE".freeze
CAPTURE_FAILED =
"CAPTURE_FAILED".freeze
REFUND_FAILED =
"REFUND_FAILED".freeze
REFUNDED_REVERSED =
"REFUNDED_REVERSED".freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.build(params) ⇒ Adyen::Notification

Logs an incoming notification into the database.

Parameters:

  • params (Hash)

    The notification parameters that should be stored in the database.

Returns:

  • (Adyen::Notification)

    The initiated and persisted notification instance.

Raises:

  • This method will raise an exception if the notification cannot be stored.

See Also:

  • Adyen::Notification::HttpPost.log


70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/adyen_notification.rb', line 70

def self.build(params)
  # Assign explicit each attribute from CamelCase notation to notification
  # For example, merchantReference will be converted to merchant_reference
  self.new.tap do |notification|
    params.each do |key, value|
      setter = "#{key.to_s.underscore}="

      # don't assign if value is empty string.
      if notification.respond_to?(setter) && value.present?
        notification.send(setter, value)
      end
    end
  end
end

Instance Method Details

#actionsObject



123
124
125
126
127
128
129
130
131
132
133
# File 'app/models/adyen_notification.rb', line 123

def actions
  if operations
    operations.
      split(",").
      map(&:downcase)

  else
    []

  end
end

#authorisation?true, false Also known as: authorization?

Returns true if this notification is an AUTHORISATION notification

Returns:

  • (true, false)

    true iff event_code == ‘AUTHORISATION’

See Also:

  • Adyen.notification#successful_authorisation?


103
104
105
# File 'app/models/adyen_notification.rb', line 103

def authorisation?
  event_code == AUTHORISATION
end

#auto_captured?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'app/models/adyen_notification.rb', line 163

def auto_captured?
  payment_method_auto_capture_only? || bank_transfer?
end

#bank_transfer?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'app/models/adyen_notification.rb', line 151

def bank_transfer?
  self.payment_method.match(/^bankTransfer/)
end

#cancel_or_refund?Boolean

Returns:

  • (Boolean)


111
112
113
# File 'app/models/adyen_notification.rb', line 111

def cancel_or_refund?
  event_code == CANCEL_OR_REFUND
end

#capture?Boolean

Returns:

  • (Boolean)


107
108
109
# File 'app/models/adyen_notification.rb', line 107

def capture?
  event_code == CAPTURE
end

#duplicate?Boolean

Returns:

  • (Boolean)


155
156
157
158
159
160
161
# File 'app/models/adyen_notification.rb', line 155

def duplicate?
  self.class.exists?(
    psp_reference: self.psp_reference,
    event_code: self.event_code,
    success: self.success
  )
end

#modification_event?Boolean

Returns:

  • (Boolean)


136
137
138
139
140
141
142
143
144
145
# File 'app/models/adyen_notification.rb', line 136

def modification_event?
  [ CANCELLATION,
    REFUND,
    CANCEL_OR_REFUND,
    CAPTURE,
    CAPTURE_FAILED,
    REFUND_FAILED,
    REFUNDED_REVERSED
  ].member? self.event_code
end

#moneyObject



171
172
173
# File 'app/models/adyen_notification.rb', line 171

def money
  ::Money.new(value, currency)
end

#normal_event?Boolean

Returns:

  • (Boolean)


147
148
149
# File 'app/models/adyen_notification.rb', line 147

def normal_event?
  AUTHORISATION == self.event_code
end

#paymentObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/models/adyen_notification.rb', line 85

def payment
  reference = original_reference || psp_reference
  payment_with_reference = Spree::Payment.find_by response_code: reference
  return payment_with_reference if payment_with_reference

  # If no reference take the last payment in the associated order where the
  # response_code was nil.
  if order
    order
      .payments
      .where(source_type: "Spree::Adyen::HppSource", response_code: nil)
      .last
  end
end

#payment_method_auto_capture_only?Boolean

Returns:

  • (Boolean)


167
168
169
# File 'app/models/adyen_notification.rb', line 167

def payment_method_auto_capture_only?
  AUTO_CAPTURE_ONLY_METHODS.member?(self.payment_method)
end

#processed!Object



119
120
121
# File 'app/models/adyen_notification.rb', line 119

def processed!
  update! processed: true
end

#refund?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'app/models/adyen_notification.rb', line 115

def refund?
  event_code == REFUND
end