class Digistore24::Notification

The main notification class which represents an Instant Payment Notification (IPN).

Attributes

payload[R]

Public Class Methods

new(params) click to toggle source

Initialize the notification.

@param params [Hash] The request parameters from Digistore24. @return [Notification]

# File lib/digistore24/notification.rb, line 18
def initialize(params)
  @payload = RecursiveOpenStruct.new(params)
end

Public Instance Methods

signature() click to toggle source

Calculate SHA512 signature for payload.

@return [String] The signature.

# File lib/digistore24/notification.rb, line 25
def signature
  # Remove 'sha_sign' key from request params and concatenate all
  # key value pairs
  params = payload.to_h
    .reject { |key, value| key == :sha_sign }
    .reject { |key, value| value == '' || value == false }.sort
    .map { | key, value| "#{key}=#{value}#{passphrase}" }.join

  # Calculate SHA512 and upcase all letters, since Digistore will
  # also return upcased letters in the signature.
  Digest::SHA512.hexdigest(params).upcase
end
to_h() click to toggle source
# File lib/digistore24/notification.rb, line 58
def to_h
  payload.to_h
end
Also aliased as: to_hash
to_hash()
Alias for: to_h
valid?() click to toggle source

Checks if the provided sha_sign is valid.

@return [Booleand] The answer

# File lib/digistore24/notification.rb, line 41
def valid?
  # If payload does not contain the sha_sign definitely return false.
  return false unless payload.sha_sign

  signature == payload.sha_sign
end
validate!() click to toggle source

Checks the request parameters signature and raises an error if it does not equal to the calculated signature.

@return [NilClass] @raise [Digistore24::NotificationError]

# File lib/digistore24/notification.rb, line 53
def validate!
  return if valid?
  raise NotificationError, 'Request signature invalid!'
end

Private Instance Methods

passphrase() click to toggle source

Return the passphrase from the global configuration.

@return [String] The passphrase

# File lib/digistore24/notification.rb, line 69
def passphrase
  Digistore24.configuration.passphrase
end