Class: Digistore24::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/digistore24/notification.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Notification

Initialize the notification.

Parameters:

  • params (Hash) —

    The request parameters from Digistore24.



18
19
20
# File 'lib/digistore24/notification.rb', line 18

def initialize(params)
  @payload = RecursiveOpenStruct.new(params)
end

Instance Attribute Details

#payload ⇒ Object (readonly)

Returns the value of attribute payload.



12
13
14
# File 'lib/digistore24/notification.rb', line 12

def payload
  @payload
end

Instance Method Details

#signature ⇒ String

Calculate SHA512 signature for payload.

Returns:

  • (String) —

    The signature.



25
26
27
28
29
30
31
32
33
34
35
36
# 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 ⇒ Object Also known as: to_hash



58
59
60
# File 'lib/digistore24/notification.rb', line 58

def to_h
  payload.to_h
end

#valid? ⇒ Booleand

Checks if the provided sha_sign is valid.

Returns:

  • (Booleand) —

    The answer



41
42
43
44
45
46
# 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! ⇒ NilClass

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

Returns:

  • (NilClass)

Raises:



53
54
55
56
# File 'lib/digistore24/notification.rb', line 53

def validate!
  return if valid?
  raise NotificationError, 'Request signature invalid!'
end