def get_ym_token(discodes_id, code, url): log = logging.getLogger('payment') wallet = PaymentWallet.get_valid_by_discodes_id(discodes_id) if not wallet: return False try: info = Wallet.get_access_token( client_id=YandexMoneyConfig.CLIENT_ID, code=code, redirect_uri=url, client_secret=YandexMoneyConfig.OAUTH_KEY) except Exception as e: log.error(e) return False else: if 'error' in info: log.error(info) return False try: PaymentCard.set_archiv(wallet.id) card = PaymentCard.add_ym_wallet(wallet, info['access_token']) card.save() except Exception as e: log.error(e) return False else: wallet.blacklist = PaymentWallet.ACTIVE_ON wallet.save() return True
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 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 oauth_confirm(): code = request.args.get("code") user_id = request.args.get("user_id") if not code: raise ValueError("The code is missing, and it's sucks :(") ym_redirect_url = "{}/{}".format(BASE_URL, REDIRECT_TO) token = Wallet.get_access_token(client_id=YM_CLIENT_ID, code=code, redirect_uri=ym_redirect_url) account_info = Wallet(access_token=token['access_token']).account_info() service = ModelService(session) try: service.add_wallet(username=user_id, auth_token=token['access_token'], account_id=int(account_info["account"])) except IntegrityError: return redirect("{}/auth_confirmed?user_id={}".format(BASE_URL, user_id)) except Exception as e: # TODO handle exceptions with invalid user_id! logger.exception(e) return redirect("{}/auth_failed?user_id={}".format(BASE_URL, user_id)) return redirect("{}/auth_confirmed?user_id={}".format(BASE_URL, user_id))
def wallet(request): if request.method == 'POST': wallet = Wallets.objects.get(pk=request.POST.get('wallet')) if wallet.name == 'Yandex Money': if not settings.YANDEX_MONEY_CLIENT_ID or not settings.YANDEX_MONEY_REDIRECT_URI or not \ settings.YANDEX_MONEY_CLIENT_SECRET: return HttpResponse('You must define yandex money settings', status=404) scope = ['account-info', 'operation-history', 'operation-details'] auth_url = Wallet.build_obtain_token_url( client_id=settings.YANDEX_MONEY_CLIENT_ID, redirect_uri=settings.YANDEX_MONEY_REDIRECT_URI, scope=scope) + '&response_type=code' return redirect(auth_url) else: uw = UserWallet() uw.user = request.user uw.address = request.POST.get('address') uw.wallet = wallet uw.save() elif request.method == 'GET': if not settings.YANDEX_MONEY_CLIENT_ID or not settings.YANDEX_MONEY_REDIRECT_URI or not \ settings.YANDEX_MONEY_CLIENT_SECRET: return HttpResponse('You must define yandex money settings', status=404) access_token = Wallet.get_access_token( client_id=settings.YANDEX_MONEY_CLIENT_ID, redirect_uri=settings.YANDEX_MONEY_REDIRECT_URI, code=request.GET.get('code'), client_secret=settings.YANDEX_MONEY_CLIENT_SECRET) access_token = access_token['access_token'] wallet = Wallet(access_token) account_info = wallet.account_info() uw = UserWallet() uw.wallet = Wallets.objects.get(name='Yandex Money') uw.access_token = access_token uw.user = request.user uw.address = account_info['account'] uw.balance = account_info['balance'] uw.save() return redirect(index)
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()
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) )
from yandex_money.api import Wallet, ExternalPayment scope = ['account-info', 'operation-history'] # etc.. auth_url = Wallet.build_obtain_token_url(client_id, redirect_uri, scope) access_token = Wallet.get_access_token(client_id, code, redirect_uri, client_secret=None) account_info = api.account_info() balance = account_info['balance'] # and so on request_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" }; request_result = api.request(request_options) # check status process_payment = api.process({ "request_id": request_result['request_id'], }) # check result if process_payment['status'] == "success": # show success page
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()