-
Notifications
You must be signed in to change notification settings - Fork 2
/
p24api.rb
65 lines (57 loc) · 1.81 KB
/
p24api.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'base64'
require 'cgi'
require 'digest/md5'
require 'digest/sha1'
require 'net/https'
class P24
attr_reader :response
def initialize(options={})
options = defaults.merge(options)
@merchant_id, @password, @url = options[:merchant_id], options[:password], URI.parse(options[:url])
end
def defaults
@options ||= {
:url => 'https://privat24.privatbank.ua/p24/accountorder?oper=prp&PUREXML'
}
end
def self.define_methods(type,*methods)
methods.each do |method|
define_method method do |*args|
@params = method == :bank_mfo ? [] : [method.to_s.gsub('_','=')]
args.first.each {|k,v| @params << "#{k}=#{rawurlencode(v)}"} if args.first.is_a?(Hash)
query(type,@params)
end
end
end
define_methods 'get', :exchange, :apicour, :deposit, :bank_mfo, :avias_price, :avias_avias, :atm, :pboffice, :peoplenet, :bonus, :kontech, :konauto, :konbez, :wifi
# define_methods 'post', :privat24, :liqpay
private
def query(type,data=nil)
connection = Net::HTTP.new(@url.host,@url.port)
connection.use_ssl = true
connection.verify_mode = OpenSSL::SSL::VERIFY_NONE
connection.start do |http|
url = type == 'post' ? @url.request_uri : @url.request_uri + '&' + (data.is_a?(Array) ? data.join('&') : (data.is_a?(String) ? data : ''))
@response = http.send(type,url,(data unless type == 'get')).body if ['get','post'].include?(type)
end
end
# def signature(data)
# sha1(md5(data + @password))
# end
#
# def liqpay_signature(merch_sign,xml)
# Base64.encode64(sha1(merch_sign + xml + merch_sign))
# end
#
def rawurlencode(str)
CGI.escape(str).gsub('+','%20') rescue str
end
#
# def sha1(str)
# Digest::SHA1.hexdigest(str)
# end
#
# def md5(str)
# Digest::MD5.hexdigest(str)
# end
end