|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import urllib.parse |
| 4 | +import hashlib |
| 5 | +import os |
| 6 | +from urllib.request import urlopen |
| 7 | +import json |
| 8 | +import re |
| 9 | +import copy |
| 10 | +from unitpay_lib import * |
| 11 | + |
| 12 | +class UnitPay: |
| 13 | + formUrl = 'https://unitpay.ru/pay/' |
| 14 | + apiUrl = 'https://unitpay.ru/api'; |
| 15 | + secretKey = '' |
| 16 | + supportedUnitpayMethods = ['initPayment', 'getPayment'] |
| 17 | + requiredUnitpayMethodsParams = {'initPayment' : ['desc', 'account', 'sum'],'getPayment' : ['paymentId']} |
| 18 | + supportedPartnerMethods = ['check', 'pay', 'error']; |
| 19 | + supportedUnitpayIp = [ |
| 20 | + '31.186.100.49', |
| 21 | + '178.132.203.105', |
| 22 | + '52.29.152.23', |
| 23 | + '52.19.56.234', |
| 24 | + '127.0.0.1' # for debug |
| 25 | + ]; |
| 26 | + def __init__(self, secretKey): |
| 27 | + self.secretKey = secretKey |
| 28 | + def form( self, publicKey, summ, account, desc, currency='RUB', locale='ru'): |
| 29 | + params = { |
| 30 | + 'account' : account, |
| 31 | + 'currency' : currency, |
| 32 | + 'desc' : desc, |
| 33 | + 'sum' : summ |
| 34 | + } |
| 35 | + params['signature'] = self.getSignature(params) |
| 36 | + params['locale'] = locale |
| 37 | + |
| 38 | + return self.formUrl + publicKey + '?' + urllib.parse.urlencode(params) |
| 39 | + |
| 40 | + def getSignature( self, params, method = None ): |
| 41 | + paramss = copy.copy(params) |
| 42 | + if 'signature' in paramss: |
| 43 | + del paramss['signature'] |
| 44 | + if 'sign' in paramss: |
| 45 | + del paramss['sign'] |
| 46 | + paramss = ksort(paramss) |
| 47 | + paramss.append([0,self.secretKey]) |
| 48 | + if method: |
| 49 | + paramss.insert(0,['method',method]) |
| 50 | + |
| 51 | + #list of dict to str |
| 52 | + res_p = [] |
| 53 | + for p in paramss: |
| 54 | + res_p.append(str(p[1])) |
| 55 | + strr = '{up}'.join(res_p) |
| 56 | + strr = strr.encode('utf-8') |
| 57 | + h = hashlib.sha256(strr).hexdigest() |
| 58 | + return h |
| 59 | + def checkHandlerRequest(self): |
| 60 | + ip = os.environ.get('REMOTE_ADDR', '') |
| 61 | + qs = os.environ.get('QUERY_STRING', '') |
| 62 | + val = urllib.parse.parse_qs(qs) |
| 63 | + params = parseParams(val); |
| 64 | + method = val['method'][0] |
| 65 | + if not 'method' in val: |
| 66 | + raise Exception('Method is null') |
| 67 | + if not params: |
| 68 | + raise Exception('Params is null') |
| 69 | + if not method in self.supportedPartnerMethods: |
| 70 | + raise Exception('Method is not supported') |
| 71 | + signature = self.getSignature(params, method); |
| 72 | + if not 'signature' in params: |
| 73 | + raise Exception('signature params is null') |
| 74 | + if params['signature'] != signature: |
| 75 | + raise Exception('Wrong signature') |
| 76 | + if not ip in self.supportedUnitpayIp: |
| 77 | + raise Exception ('IP address error') |
| 78 | + return True |
| 79 | + def getErrorHandlerResponse(self, message): |
| 80 | + return json.dumps({'error': {'message': message}}) |
| 81 | + def getSuccessHandlerResponse(self, message): |
| 82 | + return json.dumps({'result': {'message': message}}) |
| 83 | + def api(self, method, params = {}): |
| 84 | + if (not(method in self.supportedUnitpayMethods)): |
| 85 | + raise Exception('Method is not supported') |
| 86 | + for rParam in self.requiredUnitpayMethodsParams[method]: |
| 87 | + if (not rParam in params): |
| 88 | + raise Exception('Param ' + rParam + ' is null') |
| 89 | + params['secretKey'] = self.secretKey |
| 90 | + requestUrl = self.apiUrl + '?method=' + method + '&' + self.insertUrlEncode('params', params) |
| 91 | + response = urlopen(requestUrl) |
| 92 | + data = response.read().decode('utf-8') |
| 93 | + jsons = json.loads(data) |
| 94 | + return jsons |
| 95 | + def insertUrlEncode(self, inserted, params): |
| 96 | + result = '' |
| 97 | + first = True |
| 98 | + for p in params: |
| 99 | + if first: |
| 100 | + first = False |
| 101 | + else: |
| 102 | + result += '&' |
| 103 | + result += inserted + '[' + p + ']=' + str(params[p]) |
| 104 | + return result |
0 commit comments