Async Python library for RoboKassa payment gateway
Project description
aiorobokassa
Async Python library for RoboKassa payment gateway integration
aiorobokassa is a modern async Python library for integrating with RoboKassa payment gateway. The library provides full support for RoboKassa API, including payment link generation, notification handling, invoice creation, refunds, fiscalization, and more.
โจ Features
- ๐ Full async/await support with
aiohttpfor high performance - ๐ณ Payment link generation with customizable parameters
- ๐ Notification handling (ResultURL, SuccessURL) with signature verification
- ๐ Invoice creation via Invoice API (JWT-based)
- ๐ฐ Refund operations (full and partial) via legacy XML API and modern JWT API
- ๐งพ Fiscalization support (Receipt) with Pydantic models and enums for ะคะ-54 compliance
- ๐ Signature verification (MD5, SHA256, SHA512)
- ๐ก๏ธ Type hints throughout the codebase
- โ Pydantic validation for all requests and responses
- ๐งช Test mode support for development
- ๐๏ธ Clean architecture (SOLID, DRY, KISS principles)
๐ Links
- ๐ Documentation: aiorobokassa.readthedocs.io
- ๐ Issue Tracker: GitHub Issues
- ๐ฆ PyPI: pypi.org/project/aiorobokassa
- ๐ฑ๏ธ Developer contacts:
- ๐ Support project:
๐ฆ Dependencies
| Library | Description |
|---|---|
| aiohttp | Asynchronous HTTP Client/Server for asyncio and Python. |
| pydantic | JSON Data Validator |
๐ Project Structure
aiorobokassa/
โโโ api/ # API mixins
โ โโโ base.py # Base API client
โ โโโ invoice.py # Invoice operations
โ โโโ payment.py # Payment operations
โ โโโ refund.py # Refund operations
โโโ models/ # Pydantic models
โ โโโ receipt.py # Receipt models for fiscalization
โ โโโ requests.py # Request/response models
โโโ utils/ # Utility functions
โ โโโ helpers.py # Helper functions
โ โโโ jwt.py # JWT token creation
โ โโโ signature.py # Signature calculation
โ โโโ xml.py # XML parsing
โโโ client.py # Main RoboKassa client
โโโ constants.py # Constants
โโโ enums.py # Enums
โโโ exceptions.py # Custom exceptions
๐ Quick Start
Installation
pip install aiorobokassa
Basic Usage
import asyncio
from decimal import Decimal
from aiorobokassa import RoboKassaClient
async def main():
# Initialize client
client = RoboKassaClient(
merchant_login="your_merchant_login",
password1="password1",
password2="password2",
test_mode=True, # Use test mode for development
)
# Create payment URL
payment_url = client.create_payment_url(
out_sum=Decimal("100.00"),
description="Test payment",
inv_id=123,
email="customer@example.com",
)
print(f"Payment URL: {payment_url}")
# Close client session
await client.close()
asyncio.run(main())
๐ฏ Supported Features
The library supports all RoboKassa API features:
- ๐ณ Payments โ payment link generation with customizable parameters
- ๐ Notifications โ ResultURL and SuccessURL signature verification
- ๐ Invoices โ create and manage invoices via Invoice API (JWT-based)
- ๐ฐ Refunds โ full and partial refunds via legacy XML API and modern JWT API
- ๐งพ Fiscalization โ receipt generation for ะคะ-54 compliance
- ๐ Signatures โ MD5, SHA256, SHA512 signature algorithms
- ๐งช Test Mode โ development and testing support
๐ Main Methods
๐ณ Payments
from decimal import Decimal
from aiorobokassa import RoboKassaClient
# Create payment URL
payment_url = client.create_payment_url(
out_sum=Decimal("100.00"),
description="Payment for order #12345",
inv_id=12345,
email="customer@example.com",
culture="ru",
user_parameters={"user_id": "123", "order_id": "456"},
)
# Verify ResultURL notification
params = client.parse_result_url_params(request_params)
client.verify_result_url(
out_sum=params["out_sum"],
inv_id=params["inv_id"],
signature_value=params["signature_value"],
shp_params=params.get("shp_params"),
)
# Verify SuccessURL redirect
params = client.parse_success_url_params(request_params)
client.verify_success_url(
out_sum=params["out_sum"],
inv_id=params["inv_id"],
signature_value=params["signature_value"],
shp_params=params.get("shp_params"),
)
๐ Invoices
from aiorobokassa import RoboKassaClient, InvoiceType
from aiorobokassa.models.requests import InvoiceItem
from aiorobokassa.enums import TaxRate, PaymentMethod, PaymentObject
# Create simple invoice
result = await client.create_invoice(
out_sum=Decimal("100.00"),
description="Invoice payment",
invoice_type=InvoiceType.ONE_TIME,
inv_id=123,
culture="ru",
)
# Create invoice with fiscalization
invoice_items = [
InvoiceItem(
name="Service 1",
quantity=1,
cost=100.0,
tax=TaxRate.VAT20,
payment_method=PaymentMethod.FULL_PAYMENT,
payment_object=PaymentObject.SERVICE,
)
]
result = await client.create_invoice(
out_sum=Decimal("100.00"),
description="Invoice with items",
invoice_items=invoice_items,
)
# Deactivate invoice
await client.deactivate_invoice(inv_id=123)
# Get invoice information list
invoices = await client.get_invoice_information_list(
current_page=1,
page_size=10,
invoice_statuses=["paid", "notpaid"],
)
๐ฐ Refunds
from decimal import Decimal
# Legacy XML API - Full refund
refund_result = await client.create_refund(invoice_id=123)
# Legacy XML API - Partial refund
partial_refund = await client.create_refund(
invoice_id=123,
amount=Decimal("50.00"),
)
# Legacy XML API - Check refund status
status = await client.get_refund_status(invoice_id=123)
# Modern JWT API - Create refund (requires password3)
refund = await client.create_refund_v2(
op_key="operation_key_from_payment",
refund_sum=Decimal("50.00"),
)
# Modern JWT API - Get refund status
refund_status = await client.get_refund_status_v2(
request_id=refund.request_id
)
๐งพ Fiscalization (Receipt) - ะคะ-54
For clients using RoboKassa's cloud or cash solutions, fiscalization is required:
from aiorobokassa import (
RoboKassaClient,
Receipt,
ReceiptItem,
TaxRate,
TaxSystem,
PaymentMethod,
PaymentObject,
)
# Create receipt item
item = ReceiptItem(
name="ะขะพะฒะฐั 1",
quantity=1,
sum=Decimal("100.00"),
tax=TaxRate.VAT10,
payment_method=PaymentMethod.FULL_PAYMENT,
payment_object=PaymentObject.COMMODITY,
)
# Create receipt
receipt = Receipt(
items=[item],
sno=TaxSystem.OSN,
)
# Create payment URL with receipt
url = client.create_payment_url(
out_sum=Decimal("100.00"),
description="Payment with receipt",
receipt=receipt,
)
๐ Handling Notifications
ResultURL (Server-to-Server Notification)
from aiorobokassa import RoboKassaClient, SignatureError
# In your web framework (FastAPI, Django, etc.)
async def handle_result_url(request_params: dict):
client = RoboKassaClient(
merchant_login="your_merchant_login",
password1="password1",
password2="password2",
)
# Parse parameters
params = client.parse_result_url_params(request_params)
try:
# Verify signature
client.verify_result_url(
out_sum=params["out_sum"],
inv_id=params["inv_id"],
signature_value=params["signature_value"],
shp_params=params.get("shp_params"),
)
# Payment is valid, update order status
invoice_id = params["inv_id"]
amount = params["out_sum"]
# ... update your database
return "OK" + invoice_id # RoboKassa expects this response
except SignatureError:
# Invalid signature, reject payment
return "ERROR"
SuccessURL (User Redirect)
from aiorobokassa import RoboKassaClient, SignatureError
async def handle_success_url(request_params: dict):
client = RoboKassaClient(
merchant_login="your_merchant_login",
password1="password1",
password2="password2",
)
params = client.parse_success_url_params(request_params)
try:
client.verify_success_url(
out_sum=params["out_sum"],
inv_id=params["inv_id"],
signature_value=params["signature_value"],
shp_params=params.get("shp_params"),
)
# Show success page to user
return "Payment successful!"
except SignatureError:
return "Payment verification failed"
๐ง Context Manager
import asyncio
from decimal import Decimal
from aiorobokassa import RoboKassaClient
async with RoboKassaClient(
merchant_login="your_merchant_login",
password1="password1",
password2="password2",
test_mode=True,
) as client:
payment_url = client.create_payment_url(
out_sum=Decimal("100.00"),
description="Test payment",
)
print(f"Payment URL: {payment_url}")
# Client automatically closes
๐ ๏ธ Installation and Setup
Requirements
- Python 3.8+
- aiohttp >= 3.8.0
- pydantic >= 2.0.0
Installation via pip
pip install aiorobokassa
Installation via Poetry
poetry add aiorobokassa
๐ Documentation
๐ Full documentation is available at aiorobokassa.readthedocs.io
The documentation includes:
- ๐ Installation guide
- ๐ Quick start tutorial
- ๐ Detailed guides (payments, notifications, invoices, refunds, fiscalization)
- ๐ง API reference
- ๐ก Code examples (FastAPI, Django, Flask)
- โ Error handling guide
For more information about RoboKassa API, visit official RoboKassa documentation.
๐ค Supporting the Project
If the library was helpful, you can support the project:
- ๐ Tribute โ support through Telegram
- ๐ Report a bug โ GitHub Issues
- ๐ฌ Contact developer โ
๐ Contributing
We welcome contributions to the library! Here's how you can help:
Quick Start for Developers
# Clone the repository
git clone https://github.com/masasibata/aiorobokassa.git
cd aiorobokassa
# Install dependencies for development
poetry install --extras dev
# Or with pip
pip install -e ".[dev]"
Available Commands
# Testing
make test # Run tests
make test-cov # Tests with code coverage
make test-fast # Fast tests without coverage
# Code Quality
make lint # Linting (Black)
make format # Format code
make type-check # Type checking (MyPy)
make all-checks # All quality checks
# Build and Publish
make build # Build package
make clean # Clean artifacts
# Documentation
make docs # Build documentation
make docs-serve # Local documentation server
Contribution Process
- Fork the repository on GitHub
- Create a branch for your changes:
git checkout -b feature/your-feature-name
- Make changes and ensure all checks pass:
make all-checks - Commit your changes:
git add . git commit -m "feat: add new feature"
- Push your changes:
git push origin feature/your-feature-name
- Create a Pull Request on GitHub
Pull Request Requirements
- โ
All tests pass (
make test) - โ
Code is formatted (
make format) - โ
Linting passes (
make lint) - โ
Type checking passes (
make type-check) - โ Documentation updated (if necessary)
- โ Descriptive commit message
Contribution Types
- ๐ Bug fixes โ fixing errors in code
- โจ New features โ adding new functionality
- ๐ Documentation โ improving documentation and examples
- โก Optimization โ improving performance
- ๐งช Tests โ adding or improving tests
- ๐ง Infrastructure โ improving development tools
Commit Conventions
Use Conventional Commits:
feat: add new payment method support
fix: resolve timeout issue in payment creation
docs: update API documentation
test: add tests for refund functionality
refactor: improve error handling
Getting Help
- ๐ฌ Questions โ GitHub Issues
- ๐ Problems โ GitHub Issues
๐ License
MIT License - see LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiorobokassa-1.1.3.tar.gz.
File metadata
- Download URL: aiorobokassa-1.1.3.tar.gz
- Upload date:
- Size: 27.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.12.11 Linux/6.14.0-36-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
400ce4caaa200b59acf5161284e711e63a1040c23132bf2cced8af62baa063b3
|
|
| MD5 |
8374ff681ac24691c79159a704a7f0a4
|
|
| BLAKE2b-256 |
3ce0bd3a6925168ab618bf23d6a50b5893a014d5a82e7ffb32a71d0a0f17af44
|
File details
Details for the file aiorobokassa-1.1.3-py3-none-any.whl.
File metadata
- Download URL: aiorobokassa-1.1.3-py3-none-any.whl
- Upload date:
- Size: 32.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.12.11 Linux/6.14.0-36-generic
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e0eb2c3a83b59ed0b23d7c4fc8733a162cf6190f823e6d28a5ec380622a289d7
|
|
| MD5 |
ff5190a77313eebe73f7c62e4df52f00
|
|
| BLAKE2b-256 |
c86f5141dc58b1a3f3e2358c1f4a07760de4599562463f2410cab270d1a19159
|