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



112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/adyen_notification.rb', line 112

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?


92
93
94
# File 'app/models/adyen_notification.rb', line 92

def authorisation?
  event_code == AUTHORISATION
end

#auto_captured?Boolean

Returns:

  • (Boolean)


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

def auto_captured?
  payment_method_auto_capture_only? || bank_transfer?
end

#bank_transfer?Boolean

Returns:

  • (Boolean)


140
141
142
# File 'app/models/adyen_notification.rb', line 140

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

#cancel_or_refund?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'app/models/adyen_notification.rb', line 100

def cancel_or_refund?
  event_code == CANCEL_OR_REFUND
end

#capture?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'app/models/adyen_notification.rb', line 96

def capture?
  event_code == CAPTURE
end

#duplicate?Boolean

Returns:

  • (Boolean)


144
145
146
147
148
149
150
# File 'app/models/adyen_notification.rb', line 144

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

#modification_event?Boolean

Returns:

  • (Boolean)


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

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

#moneyObject



160
161
162
# File 'app/models/adyen_notification.rb', line 160

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

#normal_event?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'app/models/adyen_notification.rb', line 136

def normal_event?
  AUTHORISATION == self.event_code
end

#paymentObject



85
86
87
# File 'app/models/adyen_notification.rb', line 85

def payment
  Spree::Payment.find_by response_code: original_reference || psp_reference
end

#payment_method_auto_capture_only?Boolean

Returns:

  • (Boolean)


156
157
158
# File 'app/models/adyen_notification.rb', line 156

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

#processed!Object



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

def processed!
  update! processed: true
end

#refund?Boolean

Returns:

  • (Boolean)


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

def refund?
  event_code == REFUND
end