Class: Paybox::System::Base

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

Constant Summary collapse

@@config =
{}

Class Method Summary collapse

Class Method Details

.check_response?(params, sign) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
44
45
46
# File 'lib/paybox_system.rb', line 41

def self.check_response?(params, sign)
  digest = OpenSSL::Digest::SHA1.new
  public_key = OpenSSL::PKey::RSA.new(File.read(File.expand_path(File.dirname(__FILE__) + '/../docs/pubkey.pem')))

  public_key.verify(digest, Base64.decode64(Rack::Utils.unescape(sign)), params)
end

.configObject



11
12
13
# File 'lib/paybox_system.rb', line 11

def self.config
  @@config
end

.config=(new_config) ⇒ Object



15
16
17
# File 'lib/paybox_system.rb', line 15

def self.config=(new_config)
  @@config = new_config
end

.hash_form_fields_from(options = {}) ⇒ Object

Raises:

  • (StandardError)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/paybox_system.rb', line 19

def self.hash_form_fields_from(options = {})
  raise StandardError, "missing :secret_key in config Hash" unless @@config[:secret_key]

  formatted_options = Hash[options.map { |k, v| ["PBX_#{k.to_s.upcase}", v] }]
  formatted_options["PBX_HASH"] = "SHA512"

  date_iso = Time.now.iso8601
  formatted_options["PBX_TIME"] = date_iso

  base_params_query = formatted_options.to_a.map { |a| a.join("=") }.join("&")

  key = @@config[:secret_key]
  
  binary_key = [key].pack("H*")
  signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha512'),
                binary_key, base_params_query).upcase

  formatted_options["PBX_HMAC"] = signature

  formatted_options
end