Class: KhipuRails::NotificationValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/khipu_rails/notification_validator.rb

Class Method Summary collapse

Class Method Details

.is_valid?(notification, mode = :local) ⇒ Boolean

Returns:

  • (Boolean)


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/khipu_rails/notification_validator.rb', line 7

def self.is_valid? notification, mode = :local
  data = {
    "api_version"     => notification[:api_version],
    "receiver_id"     => notification[:receiver_id],
    "notification_id" => notification[:notification_id],
    "subject"         => notification[:subject],
    "amount"          => notification[:amount],
    "currency"        => notification[:currency],
    "transaction_id"  => notification[:transaction_id],
    "payer_email"     => notification[:payer_email],
    "custom"          => notification[:custom]
  }

  signature = { "notification_signature" => notification[:notification_signature] }

  self.send mode, data, signature
end

.local(data, signature) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/khipu_rails/notification_validator.rb', line 25

def self.local data, signature
  receiver = KhipuRails.config.receivers.find{|r| r.id == data["receiver_id"]}
  if receiver.present?
    to_validate = data.map{|key, val| key + "=" + val}.join("&")
    signature   = Base64.decode64(signature["notification_signature"])
    digest      = OpenSSL::Digest::SHA1.new

    receiver.pkey.verify digest, signature, to_validate
  else
    # Raise exception
    false
  end
end

.webservice(data, signature) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/khipu_rails/notification_validator.rb', line 39

def self.webservice data, signature
  url      = 'https://khipu.com/api/1.1/verifyPaymentNotification'
  to_send  = data.merge signature
  client   = HTTPClient.new
  response = client.post url, to_send
  response.body == "VERIFIED"
end