Class: S6kPay::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



21
22
23
24
# File 'lib/s6kpay/client.rb', line 21

def initialize(options)
  options[:webhost] = options[:test] ? $Constants[:WEB_TEST] : $Constants[:WEB]
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



19
20
21
# File 'lib/s6kpay/client.rb', line 19

def options
  @options
end

Instance Method Details

#get_order_redirect_url(order) ⇒ Object



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

def get_order_redirect_url(order)
  data = {
    merchantId: options[:code],
    amount: order[:amount],
    merchantOrder: order[:orderId],
  }
  data[:merchantUser] = order[:userId] if order[:userId]
  sorted_params = data.sort.to_h
  query = sorted_params.map { |key, value| "#{key}=#{value}" }.join('&')
  signed_data = OpenSSL::HMAC.hexdigest("sha256", options[:secret], query)
  return "#{@options[:test] ? $Constants[:WEB_TEST] : $Constants[:WEB]}?#{query}&secureHash=#{signed_data}&returnUrl=#{@options[:returnUrl]}"
end

#verify_result(query) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/s6kpay/client.rb', line 39

def verify_result(query)
  params = URI.decode_www_form(query).to_h
  return false unless params.key?("secureHash")
  secure_hash = params["secureHash"]
  params.delete("secureHash")
  sorted_params = params.sort.to_h
  query_str = sorted_params.map { |k, v| "#{k}=#{v}" }.join("&")
  signed_data = OpenSSL::HMAC.hexdigest("sha256", @options[:secret], query_str)
  secure_hash == signed_data
end