Skip to content

Commit

Permalink
Merge pull request #7 from lolkofka/lolkofka
Browse files Browse the repository at this point in the history
add method get_pay_url
  • Loading branch information
kewldan committed Aug 6, 2024
2 parents 98f5006 + bf03826 commit a59abdf
Show file tree
Hide file tree
Showing 19 changed files with 111 additions and 57 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
/src/aaio.egg-info
/dist/
/site/
main.py
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@

## About

This library is a wrapper for the https://aaio.so API **from enthusiasts**. All methods are described and all types are
This library is a wrapper for the https://aaio.so API **from enthusiast**. All methods are described and all types are
**explicitly** defined. Methods that create requests to
aaio.so
return a pydantic's models for each response. Please write about all problems related to the library
to [issues](https://github.com/kewldan/AAIO/issues)

API is up-to-date as of *01 March 2024*.
API is up-to-date as of *07 August 2024*.

* PyPl - https://pypi.org/project/aaio/
* Github - https://github.com/kewldan/AAIO
Expand Down Expand Up @@ -79,10 +79,21 @@ from aaio import AAIO

async def main():
client = AAIO('MERCHANT ID', 'SECRET KEY', 'API KEY')


# New way to create payments
payment_url = await client.get_pay_url(100, 'my_order_id', 'My order description', 'qiwi', '[email protected]',
'referral code', currency='USD',
language='en')
print(payment_url['url']) # Prints payment url for customer


# DEPRECATED METHOD
payment_url = client.create_payment(100, 'my_order_id', 'My order description', 'qiwi', '[email protected]',
'referral code', currency='USD',
language='en')
print(payment_url) # Prints payment url for customer
print(payment_url)
###################


asyncio.run(main())
Expand Down
9 changes: 6 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.hatch.build.targets.wheel]
packages = ["src/aaio"]

[project]
name = "aaio"
version = "1.3.1"
version = "1.4.0"
description = "Async AAIO api wrapper for python"
readme = "README.md"
authors = [{ name = "kewldan", email = "[email protected]" }]
Expand Down
15 changes: 4 additions & 11 deletions src/aaio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
from .AAIO import create_invoice, is_valid_payoff_webhook, AAIO
from .exceptions.aaio_bad_request import AAIOBadRequest
from .types.balance import Balance
from .types.create_payoff import CreatePayoff
from .types.payment_info import PaymentInfo
from .types.payment_methods import PaymentMethods, PaymentMethod, PaymentMethodAmounts
from .types.payment_webhook import PaymentWebhookData
from .types.payoff_info import PayoffInfo
from .types.payoff_methods import PayoffMethods, PayoffMethod
from .types.payoff_rates import PayoffRates
from .types.payoff_webhook import PayoffWebhookData
from .client import AAIO
from .exceptions import AAIOBadRequest
from .models import *
from .utils import create_invoice, is_valid_payoff_webhook
97 changes: 58 additions & 39 deletions src/aaio/AAIO.py → src/aaio/client.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,13 @@
import hashlib
from typing import Optional, List
import logging
from typing import List, Optional
from urllib.parse import urlencode

import aiohttp

from aaio.exceptions.aaio_bad_request import AAIOBadRequest
from aaio.types.balance import Balance
from aaio.types.create_payoff import CreatePayoff
from aaio.types.payment_info import PaymentInfo
from aaio.types.payment_methods import PaymentMethods
from aaio.types.payment_webhook import PaymentWebhookData
from aaio.types.payoff_info import PayoffInfo
from aaio.types.payoff_methods import PayoffMethods
from aaio.types.payoff_rates import PayoffRates
from aaio.types.payoff_sbp_banks import PayoffSbpBanks
from aaio.types.payoff_webhook import PayoffWebhookData


async def create_invoice(payment_url: str):
async with aiohttp.ClientSession() as session:
async with session.get(payment_url) as request:
return request.url


def is_valid_payoff_webhook(data: PayoffWebhookData, secret_key: str) -> bool:
return hashlib.sha256(
f'{data.id}:{secret_key}:{data.amount_down}'.encode()).hexdigest() == data.sign
from .exceptions import AAIOBadRequest
from .models import PaymentInfo, Balance, CreatePayoff, PayoffSbpBanks, PayoffInfo, PayoffRates, PayoffMethods, \
PaymentMethods, PaymentWebhookData


class AAIO:
Expand Down Expand Up @@ -81,12 +63,30 @@ def __generate_sign(self, amount: float, order_id: str, currency: str) -> str:
sign = hashlib.sha256(params.encode('utf-8')).hexdigest()
return sign

def __generate_payment_params(self, amount: float, order_id: str, description: str = None, method: str = None,
email: str = None,
referral: str = None, us_key: str = None, currency: str = None,
language: str = 'ru'):
return {
'merchant_id': self._merchant_id,
'amount': amount,
'currency': currency,
'order_id': order_id,
'sign': self.__generate_sign(amount, order_id, currency or self._default_currency),
'desc': description,
'lang': language,
'method': method,
'email': email,
'referral': referral,
'us_key': us_key
}

def create_payment(self, amount: float, order_id: str, description: str = None, method: str = None,
email: str = None,
referral: str = None, us_key: str = None, currency: str = None,
language: str = 'ru') -> str:
"""
Creates payment URL (Not a request)
Generates payment URL (DEPRECATED)
See https://wiki.aaio.so/priem-platezhei/sozdanie-zakaza for more detailed information
Args:
Expand All @@ -104,24 +104,42 @@ def create_payment(self, amount: float, order_id: str, description: str = None,
"""

if not currency:
currency = self._default_currency
params = {
'merchant_id': self._merchant_id,
'amount': amount,
'currency': currency,
'order_id': order_id,
'sign': self.__generate_sign(amount, order_id, currency),
'desc': description,
'lang': language,
'method': method,
'email': email,
'referral': referral,
'us_key': us_key
}
logging.warning('This method is deprecated, consider using create_pay method instead')

params = self.__generate_payment_params(amount, order_id, description, method, email, referral, us_key,
currency, language)

return f'{self._base_url}/merchant/pay?' + urlencode({k: v for k, v in params.items() if v is not None})

async def get_pay_url(self, amount: float, order_id: str, description: str = None, method: str = None,
email: str = None,
referral: str = None, us_key: str = None, currency: str = None,
language: str = 'ru') -> dict:
"""
Creates payment URL
See https://wiki.aaio.so/priem-platezhei/sozdanie-zakaza-zaprosom-rekomenduem for more detailed information
Args:
amount: Payment amount
order_id: Your order id
description: Payment description (Optional)
method: Payment method, can be overwritten by customer (Optional)
email: Client E-Mail (Optional)
referral: Referral code for cookies (Optional)
us_key: Custom parameters (Optional)
currency: Payment currency, default - default client currency (Optional)
language: Page language (Optional)
Returns: dict {"type": "...", "url": "https://......"}
"""

params = self.__generate_payment_params(amount, order_id, description, method, email, referral, us_key,
currency, language)

response = await self.__create_request('/merchant/get_pay_url', params)

return response

async def get_ips(self) -> List[str]:
response = await self.__create_request('/api/public/ips')

Expand Down Expand Up @@ -283,6 +301,7 @@ async def __create_request(self, uri: str, params: dict = None) -> Optional[dict

headers = {
'Accept': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
'X-Api-Key': self._api_key
}

Expand Down
1 change: 1 addition & 0 deletions src/aaio/exceptions/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .bad_request import AAIOBadRequest
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class AAIOBadRequest(BaseException):
class AAIOBadRequest(Exception):
def __init__(self, code: int, message: str) -> None:
self.code = code
self.message = message
Expand Down
10 changes: 10 additions & 0 deletions src/aaio/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .balance import Balance
from .create_payoff import CreatePayoff
from .payment_info import PaymentInfo
from .payment_methods import PaymentMethods, PaymentMethod, PaymentMethodAmounts
from .payment_webhook import PaymentWebhookData
from .payoff_info import PayoffInfo
from .payoff_methods import PayoffMethods, PayoffMethod
from .payoff_rates import PayoffRates
from .payoff_sbp_banks import PayoffSbpBanks, SbpBank
from .payoff_webhook import PayoffWebhookData
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
16 changes: 16 additions & 0 deletions src/aaio/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import hashlib

import aiohttp

from .models import PayoffWebhookData


async def create_invoice(payment_url: str):
async with aiohttp.ClientSession() as session:
async with session.get(payment_url) as request:
return request.url


def is_valid_payoff_webhook(data: PayoffWebhookData, secret_key: str) -> bool:
return hashlib.sha256(
f'{data.id}:{secret_key}:{data.amount_down}'.encode()).hexdigest() == data.sign

0 comments on commit a59abdf

Please sign in to comment.