Class: UnitPay

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

Instance Method Summary collapse

Constructor Details

#initialize(secretKey) ⇒ UnitPay

Returns a new instance of UnitPay.



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

def initialize(secretKey)
	@formUrl = 'https://unitpay.ru/pay/'
	@secretKey = secretKey

	@supportedUnitpayMethods = ['initPayment', 'getPayment']
	@requiredUnitpayMethodsParams = {
		'initPayment'	=>	['desc', 'account', 'sum', 'paymentType', 'projectId'],
		'getPayment'	=>	['paymentId']
	}
	@supportedPartnerMethods = ['check', 'pay', 'error']
	@supportedUnitpayIp = [
		'31.186.100.49',
       	'178.132.203.105',
       	'52.29.152.23',
       	'52.19.56.234',
       	'127.0.0.1' # for debug
	]
	@apiUrl = 'https://unitpay.ru/api'
end

Instance Method Details

#api(method, params = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/unitpay.rb', line 55

def api( method, params = {} )

	if !@supportedUnitpayMethods.include?(method)
		raise 'Method is not supported'
	end

	if !@requiredUnitpayMethodsParams[method].nil?
		@requiredUnitpayMethodsParams[method].each do |item|
			if params[item].nil?
				raise "param " + item + " is null"
			end
		end
	end

	params['secretKey'] = @secretKey

	querystring = params.map{|k,v| "params[#{CGI.escape(k)}]=#{CGI.escape(v)}"}.join("&")
	requestUrl = @apiUrl + '?method=' + method + '&' + querystring

	puts(requestUrl)
	data = Net::HTTP.get_response(URI.parse(requestUrl)).body
	json = JSON.parse(data)

	return json

end

#checkHandlerRequest(method, params, ip) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/unitpay.rb', line 82

def checkHandlerRequest(method, params, ip)
	if !@supportedPartnerMethods.include?(method)
		raise 'method is not supported'
	end
	signature = getSignature(params, method)
	if params['signature'] != signature
		raise 'wrong signature'
	end
	if !@supportedUnitpayIp.include?(ip)
		raise 'IP address error'
	end
	return true
end

#form(publicKey, sum, account, desc, currency = 'RUB', locale = 'ru') ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/unitpay.rb', line 26

def form(publicKey, sum, , desc, currency = 'RUB', locale = 'ru')

	params = {
           'account' => ,
           'currency' => currency,
           'desc' => desc,
           'sum' => sum,
       }
       if  (defined? @secretKey)
       	params['signature'] = getSignature(params, "check")
       end
       params['locale'] = locale

       querystring = params.map{|k,v| "#{CGI.escape(k)}=#{CGI.escape(v)}"}.join("&")
       return @formUrl + publicKey + '?' + querystring
end

#getErrorHandlerResponse(message) ⇒ Object



99
100
101
# File 'lib/unitpay.rb', line 99

def getErrorHandlerResponse( message )
	return JSON.generate({'error'=>{'message'=>message}})
end

#getSignature(params, method = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/unitpay.rb', line 42

def getSignature( params, method = nil )
	params.delete('sign')
	params.delete('signature')
	mas = params.sort
	mas.push(["1", @secretKey])
	if (!method.nil?)
		mas.unshift(["method",method])
	end
	params = Hash[mas]
	str = params.map{|k,v| "#{v}"}.join('{up}')
	h = Digest::SHA256.hexdigest str
	return h
end

#getSuccessHandlerResponse(message) ⇒ Object



96
97
98
# File 'lib/unitpay.rb', line 96

def getSuccessHandlerResponse( message )
	return JSON.generate({'result'=>{'message'=>message}})
end