def pay(message, to, amount, con): cur = con.cursor() cur.execute('SELECT token FROM `users` WHERE cid=' + str(message.from_user.id)) con.commit() sql_result = cur.fetchall() if sql_result: token = sql_result[0][0] else: # no user in DB return 'Перейдите по <a href="http://95.163.114.6/?cid=' + str(message.from_user.id) + '&to=' + str(self.to_number) + '&amount=' + str(self.amount) + '">ссылке</a>' request_options = {"pattern_id": "p2p", "to": self.to_number, "amount_due": self.amount, "comment": "переведено через бота", "message": "переведено через бота", "label": "testPayment"} wallet = Wallet(token) request_result = wallet.request_payment(request_options) print(request_result) if 'error' in request_result: return 'Произошла ошибка\nВозможно, неверно введен номер или пользователю невозможно отправить перевод' else: # check status process_payment = wallet.process_payment({"request_id": request_result['request_id'],}) # check result if process_payment['status'] == "success": return 'Ваш платеж успешно проведен!' else: return 'К сожалению, что-то пошло не так =(\n Платеж не проведен\nМожете попробовать еще раз'
def send_money(number, amount): yandex_money_token = get_config_var('YandexMoneyToken') if not yandex_money_token: return False wallet = Wallet(yandex_money_token) options = { "pattern_id": "p2p", "to": number, "amount_due": amount } request_result = wallet.request_payment(options) process_payment = wallet.process_payment({"request_id": request_result['request_id']}) if process_payment['status'] == "success": return True else: ym_logger.warning(process_payment['error']) return False
def pay(message, to, amount, con): cur = con.cursor() cur.execute('SELECT token FROM `users` WHERE cid=' + str(message.from_user.id)) con.commit() sql_result = cur.fetchall() if sql_result: token = sql_result[0][0] else: # no user in DB return 'Перейдите по <a href="http://95.163.114.6/?cid=' + str( message.from_user.id) + '&to=' + str( self.to_number) + '&amount=' + str( self.amount) + '">ссылке</a>' request_options = { "pattern_id": "p2p", "to": self.to_number, "amount_due": self.amount, "comment": "переведено через бота", "message": "переведено через бота", "label": "testPayment" } wallet = Wallet(token) request_result = wallet.request_payment(request_options) print(request_result) if 'error' in request_result: return 'Произошла ошибка\nВозможно, неверно введен номер или пользователю невозможно отправить перевод' else: # check status process_payment = wallet.process_payment({ "request_id": request_result['request_id'], }) # check result if process_payment['status'] == "success": return 'Ваш платеж успешно проведен!' else: return 'К сожалению, что-то пошло не так =(\n Платеж не проведен\nМожете попробовать еще раз'
def do_GET(self): #DB connection con = sqlite3.connect('/home/user/bot/users.sqlite') cur = con.cursor() p = urlparse(self.path) q = parse_qs(p.query) self.send_response(302, 'Found') if 'cid' in q: scope = ['account-info', 'operation-history', 'payment-p2p'] auth_url = Wallet.build_obtain_token_url(config.client_id, config.redirect_uri, scope) self.send_header('Location', auth_url) if 'b' in q: self.send_header("Set-Cookie", "cid=" + q['cid'][0] + '&b=1') else: self.send_header("Set-Cookie", "cid=" + q['cid'][0] + '&to=' + q['to'][0] + '&amount=' + q['amount'][0]) elif 'code' in q: access_token = Wallet.get_access_token(config.client_id, q['code'][0], config.redirect_uri, client_secret=None) cookie = parse_qs(self.headers.get('Cookie')) cid = cookie['cid'][0] cur.execute('INSERT INTO users (cid, token) VALUES ("' + str(cid) +'", "' + access_token['access_token'] + '")') con.commit() wallet = Wallet(access_token['access_token']) if 'b' in cookie: queue.put({'cid': cid, 'b': wallet.account_info()['balance_details']['available']}) else: to = cookie['to'][0] amount = cookie['amount'][0] request_options = {"pattern_id": "p2p", "to": to, "amount_due": amount, "comment": "переведено через бота", "message": "переведено через бота", "label": "testPayment"} request_result = wallet.request_payment(request_options) # check status process_payment = wallet.process_payment({"request_id": request_result['request_id'],}) # check result if process_payment['status'] == "success": queue.put({'cid': cid, 'result':'+'}) else: queue.put({'cid': cid, 'result':'-'}) self.send_header('Location', 'http://telegram.me/GodMoneyBot') self.end_headers() con.close()
class WalletTestSuite(unittest.TestCase): def setUp(self): super(WalletTestSuite, self).setUp() self.api = Wallet(ACCESS_TOKEN) def assert_auth_header_present(self): pass def testAccountInfo(self): response = self.api.account_info() self.assert_auth_header_present() def testGetAuxToken(self): token = "some_aux_token" response = self.api.get_aux_token(["account-info", "operation-history"]) self.assertIn('aux_token', response) def testOperationHistory(self): options = {"records": 3} response = self.api.operation_history(options) def testOperationDetails(self): pass def testRequestPayment(self): options = { "pattern_id": "p2p", "to": "410011161616877", "amount_due": "0.02", "comment": "test payment comment from yandex-money-python", "message": "test payment message from yandex-money-python", "label": "testPayment", "test_payment": True, "test_result": "success" } response = self.api.request_payment(options) self.assertEqual(response['status'], 'success') def testResponsePayment(self): options = { "request_id": "test-p2p", "test_payment": True, "test_result": "success" } response = self.api.process_payment(options) self.assertEqual(response['status'], 'success') def testIncomingTransferAccept(self): #self.addResponse("incoming-transfer-accept", {"status": "success"}) operation_id = "some id" protection_code = "some code" # TODO: test when it's None response = self.api.incoming_transfer_accept( operation_id=operation_id, protection_code=protection_code ) self.assertEqual(response['status'], "refused") def testIncomingTransferReject(self): #self.addResponse("incoming-transfer-reject", {"status": "success"}) operation_id = "some operatoin id" response = self.api.incoming_transfer_reject( operation_id=operation_id, ) def testObtainTokenUrl(self): client_id = "client-id" url = Wallet.build_obtain_token_url( "client-id", "http://localhost/redirect", ["account-info", "operation_history"] ) # TODO: check url def testGetAccessToken(self): options = { "code": "code", "client_id": "client_id", "grant_type": "authorization_code", "redirect_uri": "redirect_uri", "client_secret": "client_secret" } response = Wallet.get_access_token( code=options["code"], client_id=options["client_id"], redirect_uri=options["redirect_uri"], client_secret=options["client_secret"] ) self.assertEqual(response['error'], 'unauthorized_client')
class WalletTestSuite(unittest.TestCase): def setUp(self): super(WalletTestSuite, self).setUp() self.api = Wallet(ACCESS_TOKEN) def assert_auth_header_present(self): pass def testAccountInfo(self): self.api.account_info() self.assert_auth_header_present() def testGetAuxToken(self): response = self.api.get_aux_token( ["account-info", "operation-history"]) self.assertIn('aux_token', response) def testOperationHistory(self): options = {"records": 3} self.api.operation_history(options) def testOperationDetails(self): self.api.operation_details("some-invalid-id") def testRequestPayment(self): options = { "pattern_id": "p2p", "to": "410011161616877", "amount_due": "0.02", "comment": "test payment comment from yandex-money-python", "message": "test payment message from yandex-money-python", "label": "testPayment", "test_payment": True, "test_result": "success" } response = self.api.request_payment(options) self.assertEqual(response['status'], 'success') def testResponsePayment(self): options = { "request_id": "test-p2p", "test_payment": True, "test_result": "success" } response = self.api.process_payment(options) self.assertEqual(response['status'], 'success') def testIncomingTransferAccept(self): operation_id = "some id" protection_code = "some code" # TODO: test when it's None response = self.api.incoming_transfer_accept( operation_id=operation_id, protection_code=protection_code) self.assertEqual(response['status'], "refused") def testIncomingTransferReject(self): operation_id = "some operatoin id" self.api.incoming_transfer_reject(operation_id=operation_id, ) def testObtainTokenUrl(self): Wallet.build_obtain_token_url("client-id", "http://localhost/redirect", ["account-info", "operation_history"]) # TODO: check url def testGetAccessToken(self): options = { "code": "code", "client_id": "client_id", "grant_type": "authorization_code", "redirect_uri": "redirect_uri", "client_secret": "client_secret" } response = Wallet.get_access_token( code=options["code"], client_id=options["client_id"], redirect_uri=options["redirect_uri"], client_secret=options["client_secret"]) self.assertEqual(response['error'], 'unauthorized_client')
def do_GET(self): #DB connection con = sqlite3.connect('/home/user/bot/users.sqlite') cur = con.cursor() p = urlparse(self.path) q = parse_qs(p.query) self.send_response(302, 'Found') if 'cid' in q: scope = [ 'account-info', 'operation-history', 'payment-p2p' ] auth_url = Wallet.build_obtain_token_url( config.client_id, config.redirect_uri, scope) self.send_header('Location', auth_url) if 'b' in q: self.send_header("Set-Cookie", "cid=" + q['cid'][0] + '&b=1') else: self.send_header( "Set-Cookie", "cid=" + q['cid'][0] + '&to=' + q['to'][0] + '&amount=' + q['amount'][0]) elif 'code' in q: access_token = Wallet.get_access_token(config.client_id, q['code'][0], config.redirect_uri, client_secret=None) cookie = parse_qs(self.headers.get('Cookie')) cid = cookie['cid'][0] cur.execute('INSERT INTO users (cid, token) VALUES ("' + str(cid) + '", "' + access_token['access_token'] + '")') con.commit() wallet = Wallet(access_token['access_token']) if 'b' in cookie: queue.put({ 'cid': cid, 'b': wallet.account_info()['balance_details'] ['available'] }) else: to = cookie['to'][0] amount = cookie['amount'][0] request_options = { "pattern_id": "p2p", "to": to, "amount_due": amount, "comment": "переведено через бота", "message": "переведено через бота", "label": "testPayment" } request_result = wallet.request_payment( request_options) # check status process_payment = wallet.process_payment({ "request_id": request_result['request_id'], }) # check result if process_payment['status'] == "success": queue.put({'cid': cid, 'result': '+'}) else: queue.put({'cid': cid, 'result': '-'}) self.send_header('Location', 'http://telegram.me/GodMoneyBot') self.end_headers() con.close()
class WalletTestSuite(ResponseMockTestCase): def setUp(self): super(WalletTestSuite, self).setUp() self.api = Wallet("TEST TOKEN") def assert_auth_header_present(self): self.assertEqual("Bearer TEST TOKEN", responses.calls[0].request.headers['Authorization'], ) def testAccountInfo(self): self.addResponse("account-info", {"status": "success"} ) response = self.api.account_info() self.assertEqual(response['status'], "success") self.assert_auth_header_present() def testGetAuxToken(self): token = "some_aux_token" self.addResponse("token-aux", {"aux_token": token}) response = self.api.get_aux_token(["account-info", "operation-history"]) self.assertEqual(response['aux_token'], token) self.assertEqual(responses.calls[0].request.body, "scope=account-info+operation-history") def testOperationHistory(self): options = {"foo": "bar", "foo2": "bar2"} self.addResponse("operation-history", []) response = self.api.operation_history(options) self.assertEqual(response, []) self.assertEqual(responses.calls[0].request.body, urlencode(options) ) def testRequestPayment(self): self.addResponse("request-payment", {"status": "success"}) options = { "foo": "bar", "foo2": "bar2", } response = self.api.request_payment(options) self.assertEqual(response, {"status": "success"}) self.assertEqual(responses.calls[0].request.body, urlencode(options) ) def testResponsePayment(self): self.addResponse("process-payment", {"status": "success"}) options = { "foo": "bar", "foo2": "bar2", } response = self.api.process_payment(options) self.assertEqual(response, {"status": "success"}) self.assertEqual(responses.calls[0].request.body, urlencode(options) ) def testIncomingTransferAccept(self): self.addResponse("incoming-transfer-accept", {"status": "success"}) options = { "foo": "bar", "foo2": "bar2", } operation_id = "some id" protection_code = "some code" # TODO: test when it's None response = self.api.incoming_transfer_accept( operation_id=operation_id, protection_code=protection_code ) self.assertEqual(response, {"status": "success"}) self.assertEqual( responses.calls[0].request.body, urlencode({ "operation_id": operation_id, "protection_code": protection_code }) ) def testIncomingTransferReject(self): self.addResponse("incoming-transfer-reject", {"status": "success"}) operation_id = "some id" response = self.api.incoming_transfer_reject( operation_id=operation_id, ) self.assertEqual(response, {"status": "success"}) self.assertEqual( responses.calls[0].request.body, urlencode({ "operation_id": operation_id, }) ) def testObtainTokenUrl(self): client_id = "client-id" url = Wallet.build_obtain_token_url( "client-id", "http://localhost/redirect", ["account-info", "operation_history"] ) # TODO: check url def testGetAccessToken(self): self.addResponse(Wallet.SP_MONEY_URL + "/oauth/token", {"status": "success"}, is_api_url=False ) options = { "code": "code", "client_id": "client_id", "grant_type": "authorization_code", "redirect_uri": "redirect_uri", "client_secret": "client_secret" } response = Wallet.get_access_token( code=options["code"], client_id=options["client_id"], redirect_uri=options["redirect_uri"], client_secret=options["client_secret"] ) self.assertEqual(response, {"status": "success"}) self.assertEqual( responses.calls[0].request.body, urlencode(options) ) def testRevokeToken(self): self.addResponse("revoke", {"status": "success"}) Wallet.revoke_token("TEST TOKEN") self.assert_auth_header_present()