Skip to content

Commit

Permalink
ad all
Browse files Browse the repository at this point in the history
  • Loading branch information
Navigatoroleg committed Sep 24, 2023
1 parent af788bc commit 1c0d449
Show file tree
Hide file tree
Showing 42 changed files with 870 additions and 0 deletions.
31 changes: 31 additions & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from .account.account import Account
from .account.balance_details import BalanceDetails

from .operation.operation import Operation

from yoomoney.operation_details.operation_details import OperationDetails
from yoomoney.operation_details.digital_bonus import DigitalBonus
from yoomoney.operation_details.digital_product import DigitalProduct
from yoomoney.operation_details.digital_good import DigitalGood

from .history.history import History

from .authorize.authorize import Authorize

from .quickpay.quickpay import Quickpay

from .client import Client

__all__ = [
'Client',
'Account',
'BalanceDetails',
'Operation',
'History',
'Authorize',
"OperationDetails",
"DigitalBonus",
"DigitalProduct",
"DigitalGood",
"Quickpay",
]
Binary file added __pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/client.cpython-311.pyc
Binary file not shown.
Binary file added __pycache__/exceptions.cpython-311.pyc
Binary file not shown.
Empty file added account/__init__.py
Empty file.
Binary file added account/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added account/__pycache__/account.cpython-311.pyc
Binary file not shown.
Binary file added account/__pycache__/balance_details.cpython-311.pyc
Binary file not shown.
Binary file added account/__pycache__/card.cpython-311.pyc
Binary file not shown.
67 changes: 67 additions & 0 deletions account/account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import requests
import json
from typing import TYPE_CHECKING, Optional, List

from yoomoney_async.account.balance_details import BalanceDetails
from yoomoney_async.account.card import Card
from yoomoney_async.exceptions import InvalidToken

class Account:

def __init__(self,
base_url: str = None,
token: str = None,
method: str = None,

):

self.__private_method = method

self.__private_base_url = base_url
self.__private_token = token

data = self._request()

if len(data) != 0:
self.account = data['account']
self.balance = data['balance']
self.currency = data['currency']
self.account_status = data['account_status']
self.account_type = data['account_type']

self.balance_details = BalanceDetails()
if 'balance_details' in data:
if 'available' in data['balance_details']:
self.balance_details.available = float(data['balance_details']['available'])
if 'blocked' in data['balance_details']:
self.balance_details.blocked = float(data['balance_details']['blocked'])
if 'debt' in data['balance_details']:
self.balance_details.debt = float(data['balance_details']['debt'])
if 'deposition_pending' in data['balance_details']:
self.balance_details.deposition_pending = float(data['balance_details']['deposition_pending'])
if 'total' in data['balance_details']:
self.balance_details.total = float(data['balance_details']['total'])
if 'hold' in data['balance_details']:
self.balance_details.hold = float(data['balance_details']['hold'])

self.cards_linked = []
if 'cards_linked' in data:
for card_linked in data['cards_linked']:
card = Card(pan_fragment=card_linked['pan_fragment'], type=card_linked['type'])
self.cards_linked.append(card)
else:
raise InvalidToken()

def _request(self):

access_token = str(self.__private_token)
url = self.__private_base_url + self.__private_method

headers = {
'Authorization': 'Bearer ' + str(access_token),
'Content-Type': 'application/x-www-form-urlencoded'
}

response = requests.request("POST", url, headers=headers)

return response.json()
27 changes: 27 additions & 0 deletions account/balance_details.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@


class BalanceDetails:
def __init__(self,
total: float = None,
available: float = None,
deposition_pending: float = None,
blocked: float = None,
debt: float = None,
hold: float = None,
):

self.total = total

self.available = available

self.deposition_pending = deposition_pending

self.blocked = blocked

self.debt = debt

self.hold = hold




11 changes: 11 additions & 0 deletions account/card.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@



class Card:
def __init__(self,
pan_fragment: str = None,
type: str = None,
):
self.pan_fragment = pan_fragment
self.type = type

Empty file added authorize/__init__.py
Empty file.
Binary file added authorize/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added authorize/__pycache__/authorize.cpython-311.pyc
Binary file not shown.
62 changes: 62 additions & 0 deletions authorize/authorize.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
from typing import List
import requests

from yoomoney_async.exceptions import (
InvalidRequest,
UnauthorizedClient,
InvalidGrant,
EmptyToken
)

class Authorize:
def __init__(
self,
client_id: str,
redirect_uri: str,
scope: List[str]
):

url = "https://yoomoney.ru/oauth/authorize?client_id={client_id}&response_type=code" \
"&redirect_uri={redirect_uri}&scope={scope}".format(client_id=client_id,
redirect_uri=redirect_uri,
scope='%20'.join([str(elem) for elem in scope]),
)

headers = {
'Content-Type': 'application/x-www-form-urlencoded'
}

response = requests.request("POST", url, headers=headers)

if response.status_code == 200:
print("Visit this website and confirm the application authorization request:")
print(response.url)

code = str(input("Enter redirected url (https://yourredirect_uri?code=XXXXXXXXXXXXX) or just code: "))
try:
code = code[code.index("code=") + 5:].replace(" ","")
except:
pass

url = "https://yoomoney.ru/oauth/token?code={code}&client_id={client_id}&" \
"grant_type=authorization_code&redirect_uri={redirect_uri}".format(code=str(code),
client_id=client_id,
redirect_uri=redirect_uri,
)

response = requests.request("POST", url, headers=headers)

if "error" in response.json():
error = response.json()["error"]
if error == "invalid_request":
raise InvalidRequest()
elif error == "unauthorized_client":
raise UnauthorizedClient()
elif error == "invalid_grant":
raise InvalidGrant()

if response.json()['access_token'] == "":
raise EmptyToken()

print("Your access token:")
print(response.json()['access_token'])
36 changes: 36 additions & 0 deletions client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import datetime
from yoomoney_async.history.history import History

class Client:
def __init__(self,
token: str = None,
base_url: str = None,
):

if base_url is None:
self.base_url = "https://yoomoney.ru/api/"

if token is not None:
self.token = token

async def operation_history(self,
type: str = None,
label: str = None,
from_date: datetime.datetime = None,
till_date: datetime.datetime = None,
start_record: str = None,
records: int = None,
details: bool = None,
):
method = "operation-history"
return await History(base_url=self.base_url,
token=self.token,
method=method,
type=type,
label=label,
from_date=from_date,
till_date=till_date,
start_record=start_record,
records=records,
details=details,
).start()
89 changes: 89 additions & 0 deletions exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@


class YooMoneyError(Exception):
"""Basic class"""


class InvalidToken(YooMoneyError):

message = "Token is not valid, or does not have the appropriate rights"
def __init__(self, ):
super().__init__(self.message)


class IllegalParamType(YooMoneyError):

message = "Invalid parameter value 'type'"
def __init__(self, ):
super().__init__(self.message)

class IllegalParamStartRecord(YooMoneyError):

message = "Invalid parameter value 'start_record'"
def __init__(self, ):
super().__init__(self.message)


class IllegalParamRecords(YooMoneyError):

message = "Invalid parameter value 'records'"
def __init__(self, ):
super().__init__(self.message)

class IllegalParamLabel(YooMoneyError):

message = "Invalid parameter value 'label'"
def __init__(self, ):
super().__init__(self.message)

class IllegalParamFromDate(YooMoneyError):

message = "Invalid parameter value 'from_date'"
def __init__(self, ):
super().__init__(self.message)


class IllegalParamTillDate(YooMoneyError):

message = "Invalid parameter value 'till_date'"
def __init__(self, ):
super().__init__(self.message)

class IllegalParamOperationId(YooMoneyError):

message = "Invalid parameter value 'operation_id'"
def __init__(self, ):
super().__init__(self.message)

class TechnicalError(YooMoneyError):

message = "Technical error, try calling the operation again later"
def __init__(self, ):
super().__init__(self.message)

class InvalidRequest(YooMoneyError):

message = "Required query parameters are missing or have incorrect or invalid values"
def __init__(self, ):
super().__init__(self.message)

class UnauthorizedClient(YooMoneyError):

message = "Invalid parameter value 'client_id' or 'client_secret', or the application" \
" does not have the right to request authorization (for example, YooMoney blocked it 'client_id')"
def __init__(self, ):
super().__init__(self.message)

class InvalidGrant(YooMoneyError):

message = "In issue 'access_token' denied. YuMoney did not issue a temporary token, " \
"the token is expired, or this temporary token has already been issued " \
"'access_token' (repeated request for an authorization token with the same temporary token)"
def __init__(self, ):
super().__init__(self.message)

class EmptyToken(YooMoneyError):

message = "Response token is empty. Repeated request for an authorization token"
def __init__(self, ):
super().__init__(self.message)
Empty file added history/__init__.py
Empty file.
Binary file added history/__pycache__/__init__.cpython-311.pyc
Binary file not shown.
Binary file added history/__pycache__/exceptions.cpython-311.pyc
Binary file not shown.
Binary file added history/__pycache__/history.cpython-311.pyc
Binary file not shown.
Loading

0 comments on commit 1c0d449

Please sign in to comment.