Class: S6kPay::Client

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Client

Returns a new instance of Client.



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

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.



17
18
19
# File 'lib/s6kpay.rb', line 17

def options
  @options
end

Instance Method Details

#get_order_redirect_url(order) ⇒ Object



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

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



37
38
39
40
41
42
43
44
45
46
# File 'lib/s6kpay.rb', line 37

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