Skip to content

Commit

Permalink
Fix issue #1
Browse files Browse the repository at this point in the history
  • Loading branch information
I-ToSa-I committed Dec 31, 2023
1 parent 79b60a3 commit 2b25733
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 226 deletions.
79 changes: 40 additions & 39 deletions AsyncPayments/aaio/api.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import hashlib
from .models import *

from typing import Optional, Union
from AsyncPayments.requests import RequestsClient
from models import Order, OrderMethod, WithdrawalMethod, CreateWithdrawalInfo, Withdrawal, Balance
from typing import Optional, Union, List
from urllib.parse import urlencode

from AsyncPayments.requests import RequestsClient
import hashlib


class AsyncAaio(RequestsClient):
API_HOST = "https://aaio.io"
Expand All @@ -26,8 +26,23 @@ def __init__(self,
self.__secret_key = secretkey
self.__headers = {
"Accept": "application/json",
"X-Api-Key": self.__api_key
"X-Api-Key": self.__api_key,
}
self.__post_method = "POST"
self.__payment_name = "aaio"
self.check_values()

def check_values(self):
if not self.__secret_key or not self.__shop_id:
raise ValueError('No SecretKey or ShopID specified')

def __create_sign(self, amount: Union[float, int], currency: str, order_id: str) -> str:
params_for_sing = ':'.join(map(
str,
[self.__shop_id, amount, currency, self.__secret_key, order_id])
)

return hashlib.sha256(params_for_sing.encode('utf-8')).hexdigest()

async def create_payment_url(
self,
Expand All @@ -39,7 +54,7 @@ async def create_payment_url(
email: Optional[str] = None,
lang: Optional[str] = None,
referal: Optional[str] = None,
us_key: Optional[str] = None
us_key: Optional[str] = None,
) -> str:

"""Generate payment url.
Expand All @@ -56,9 +71,6 @@ async def create_payment_url(
:param referal: Referral code
:param us_key: Parameter that you want to get in the notification"""

if not self.__secret_key or not self.__shop_id:
raise Exception('Не указан SecretKey или ShopID')

params = {
'merchant_id': self.__shop_id,
'amount': amount,
Expand All @@ -69,21 +81,13 @@ async def create_payment_url(
'email': email,
'lang': lang,
'referal': referal,
'us_key': us_key
'us_key': us_key,
'sign': self.__create_sign(amount, currency, order_id),
}

for key, value in params.copy().items():
if value is None:
del params[key]

params_for_sing = ':'.join(map(
str,
[self.__shop_id, amount, currency, self.__secret_key, order_id])
)

sign = hashlib.sha256(params_for_sing.encode('utf-8')).hexdigest()

params['sign'] = sign
params.pop(key)

return f"{self.API_HOST}/merchant/pay?" + urlencode(params)

Expand All @@ -94,7 +98,7 @@ async def get_balance(self) -> Balance:

url = f'{self.API_HOST}/api/balance'

response = await self._request("aaio", "POST", url, headers=self.__headers)
response = await self._request(self.__payment_name, self.__post_method, url, headers=self.__headers)

return Balance(**response)

Expand All @@ -108,27 +112,24 @@ async def get_order_info(self,
:param order_id: OrderID (in your system)"""

if not self.__shop_id:
raise Exception('Не указан ShopID.')

url = f'{self.API_HOST}/api/info-pay'

params = {
'merchant_id': self.__shop_id,
'order_id': order_id
'order_id': order_id,
}

for key, value in params.copy().items():
if value is None:
del params[key]
params.pop(key)

response = await self._request("aaio", "POST", url, data=params, headers=self.__headers)
response = await self._request(self.__payment_name, self.__post_method, url, data=params, headers=self.__headers)

return Order(**response)

async def get_withdrawal_methods(self,
method: Optional[str] = None
) -> Union[dict, WithdrawalMethod]:
) -> Union[List[WithdrawalMethod], WithdrawalMethod]:

"""Get available methods for withdrawal.
Expand All @@ -142,15 +143,15 @@ async def get_withdrawal_methods(self,

url = f'{self.API_HOST}/api/methods-payoff'

response = await self._request("aaio", "POST", url, headers=self.__headers)
response = await self._request(self.__payment_name, self.__post_method, url, headers=self.__headers)

if method is not None:
return WithdrawalMethod(**response['list'][method])
return response['list']
return [WithdrawalMethod(**method) for method in response["list"].values()]

async def get_order_methods(self,
method: Optional[str] = None
) -> Union[dict, OrderMethod]:
) -> Union[List[OrderMethod], OrderMethod]:

"""Get available methods for order.
Expand All @@ -165,14 +166,14 @@ async def get_order_methods(self,
url = f'{self.API_HOST}/api/methods-pay'

params = {
'merchant_id': self.__shop_id
'merchant_id': self.__shop_id,
}

response = await self._request("aaio", "POST", url, data=params, headers=self.__headers)
response = await self._request(self.__payment_name, self.__post_method, url, data=params, headers=self.__headers)

if method is not None:
return OrderMethod.model_validate(response['list'][method])
return response['list']
return [OrderMethod(**method) for method in response["list"].values()]

async def get_withdrawal_info(self,
my_id: Union[int, str],
Expand All @@ -187,10 +188,10 @@ async def get_withdrawal_info(self,
url = f'{self.API_HOST}/api/info-payoff'

params = {
'my_id': my_id
'my_id': my_id,
}

response = await self._request("aaio", "POST", url, data=params, headers=self.__headers)
response = await self._request(self.__payment_name, self.__post_method, url, data=params, headers=self.__headers)

return Withdrawal(**response)

Expand Down Expand Up @@ -222,6 +223,6 @@ async def create_withdrawal(self,
'commission_type': commission_type,
}

response = await self._request("aaio", "POST", url, data=params, headers=self.__headers)
response = await self._request(self.__payment_name, self.__post_method, url, data=params, headers=self.__headers)

return CreateWithdrawalInfo(**response)
return CreateWithdrawalInfo(**response)
28 changes: 12 additions & 16 deletions AsyncPayments/aaio/models.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
from pydantic import BaseModel
from typing import Union
from typing import Union, Optional


# Balance
class Balance(BaseModel):
balance: float
referral: float
hold: float


# Orders
class Order(BaseModel):
id: str
order_id: Union[int, str]
desc: str
merchant_id: str
merchant_domain: str
method: str = None
method: Optional[str] = None
amount: float
currency: str
profit: float = None
commission: float = None
commission_client: float = None
commission_type: str = None
email: str = None
profit: Optional[float] = None
commission: Optional[float] = None
commission_client: Optional[float] = None
commission_type: Optional[str] = None
email: Optional[str] = None
status: str
date: str
expired_date: str
complete_date: str = None
complete_date: Optional[str] = None
us_vars: dict

class OrderMethodCurrencies(BaseModel):
Expand All @@ -37,12 +34,11 @@ class OrderMethodCurrencies(BaseModel):
EUR: float

class OrderMethod(BaseModel):
name: str
min: OrderMethodCurrencies
max: OrderMethodCurrencies
commission_percent: float


# Withdrawals
class Withdrawal(BaseModel):
id: str
my_id: Union[int, str]
Expand All @@ -53,12 +49,12 @@ class Withdrawal(BaseModel):
commission: float
commission_type: int
status: str
cancel_message: str = None
cancel_message: Optional[str] = None
date: str
complete_date: str = None

complete_date: Optional[str] = None

class WithdrawalMethod(BaseModel):
name: str
min: float
max: float
commission_percent: float
Expand Down

0 comments on commit 2b25733

Please sign in to comment.