Class: Kojn::Ipn

Inherits:
Object
  • Object
show all
Defined in:
lib/kojn/ipn.rb

Constant Summary collapse

ENCRYPTION =
1
INTEGRITY =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Ipn

Returns a new instance of Ipn.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/kojn/ipn.rb', line 8

def initialize params
  self.content = params

  self.ensure_secure(params)

  if params['token']
    self.mode = Kojn::Ipn::INTEGRITY
    self.invoice = params['invoice']
    self.token = params['token']

    raise AuthenticityException.new("ERROR -*- IPN Could not be verified. Please notify Kojn authors. This message has been fabricated. -*- ERROR") unless self.authentic?
  elsif params['iv']
    self.mode = Kojn::Ipn::ENCRYPTION
    self.decrypt
  end

  # Create a real object.
  self.invoice = Kojn::Invoice.new(self.invoice)
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/kojn/ipn.rb', line 6

def content
  @content
end

#invoiceObject

Returns the value of attribute invoice.



6
7
8
# File 'lib/kojn/ipn.rb', line 6

def invoice
  @invoice
end

#modeObject

Returns the value of attribute mode.



6
7
8
# File 'lib/kojn/ipn.rb', line 6

def mode
  @mode
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/kojn/ipn.rb', line 6

def token
  @token
end

Instance Method Details

#authentic?Boolean

Returns:

  • (Boolean)

Raises:



28
29
30
31
32
33
34
35
# File 'lib/kojn/ipn.rb', line 28

def authentic?
  raise AuthenticityException.new("Checking if message is authentic while mode is set to Encryption") unless self.mode == Kojn::Ipn::INTEGRITY

  sha = Digest::SHA2.hexdigest "#{Kojn.api_key}#{self.invoice['internal_id']}#{self.invoice['received_amount']}"
  Rails.logger.info "sha: #{sha} tokne: #{self.token}"

  sha == self.token
end

#decryptObject



37
38
39
# File 'lib/kojn/ipn.rb', line 37

def decrypt
  self.invoice = Kojn::crypto.decrypt_params(self.content)
end

#ensure_secure(params) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/kojn/ipn.rb', line 41

def ensure_secure(params)
  if (params['token'] && Kojn.ipn_sec == :integrity) || (params['iv'] && Kojn.ipn_sec == :encryption)
    true
  else
    raise Kojn::AuthenticityException.new("ERROR -*- IPN security expected: #{Kojn.ipn_sec}. Opposite was found. -*- ERROR")
  end
end