π Tochka API
Π£Π΄ΠΎΠ±Π½Π°Ρ ΠΎΠ±ΡΡΡΠΊΠ° Π½Π°Π΄ ΠΎΡΠΊΡΡΡΡΠΌ API ΠΠ°Π½ΠΊΠ° Π’ΠΎΡΠΊΠ°
π₯ Π£ΡΡΠ°Π½ΠΎΠ²ΠΊΠ°
π¦ ΠΠ· pip:
python -m pip install -u tochka_api
π ΠΠ· ΡΠ΅ΠΏΠΎΠ·ΠΈΡΠΎΡΠΈΡ:
git clone https://github.com/WhiteApfel/tochka_api.git
cd tochka_api
python setup.py install
π§ ΠΡΠΎΠ³ΡΠ΅ΡΡ ΡΠ°Π·ΡΠ°Π±ΠΎΡΠΊΠΈ
- ΠΠ²ΡΠΎΡΠΈΠ·Π°ΡΠΈΡ
- ΠΠ°Π»Π°Π½ΡΡ
- Π‘ΡΠ΅ΡΠ°
- Webhooks
- ΠΡΠΏΠΈΡΠΊΠΈ
- ΠΠ°ΡΡΡ
- ΠΠ»ΠΈΠ΅Π½ΡΡ
- ΠΠ»Π°ΡΠ΅ΠΆΠΈ
- Π Π°Π·ΡΠ΅ΡΠ΅Π½ΠΈΡ
- Π‘ΠΏΠ΅ΡΠΈΠ°Π»ΡΠ½ΡΠ΅ ΡΡΠ΅ΡΠ°
-
Π‘ΠΠ
- QR
- Π’Π‘Π (Merchant)
- ΠΠΎΠΌΠΏΠ°Π½ΠΈΠΈ (Legal)
- ΠΠΎΠ·Π²ΡΠ°ΡΡ
π§βπ« ΠΠ°ΠΊ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ
-
Decimal
andstr
- amount in rubles -
int
- amount in kopecks
Π Π°Π·Π»ΠΈΡΠΈΡ user_code ΠΈ customer_code
-
user_code
ΡΡΠΎ ΠΊΠΎΠ΄ ΠΊΠ»ΠΈΠ΅Π½ΡΠ°, ΠΊΠΎΡΠΎΡΡΠΉ ΠΈΠΌΠ΅Π΅Ρ Π΄ΠΎΡΡΡΠΏ ΠΊ ΠΊΠΎΠΌΠΏΠ°Π½ΠΈΠΈ -
customer_code
ΡΡΠΎ ΠΊΠΎΠ΄ ΠΊΠΎΠΌΠΏΠ°Π½ΠΈΠΈ(ΠΠ), Π½Π° ΠΊΠΎΡΠΎΡΡΡ ΠΎΡΠΊΡΡΡΡ ΡΡΠ΅ΡΠ°
import asyncio
from decimal import Decimal
from tochka_api import TochkaAPI, context_user_code
from tochka_api.models import PermissionsEnum
client_id = "<<client_id>>"
client_secret = "<<client_secret>>"
redirect_uri = "https://tochka-api.pfel.cc/"
tochka = TochkaAPI(client_id, client_secret, redirect_uri=redirect_uri)
async def add_user():
consents_token, consents_expires_in = await tochka.get_consents_token()
consents_request = await tochka.create_consents(
consents_token, PermissionsEnum.all()
)
auth_url = tochka.generate_auth_url(consent_id=consents_request.consent_id)
print("Auth:", auth_url)
code = input("Code >>> ")
token_id = input("Token_id >>> ")
tokens = await tochka.get_access_token(
code=code, token_id=token_id
)
print(f"User {tokens.user_code=} are authorized.")
context_user_code.set(tokens.user_code)
asyncio.create_task(get_accounts())
merchant_id = await register_merchant(user_code=tokens.user_code)
await register_qr(merchant_id=merchant_id)
async def get_accounts():
# user_code Π±ΡΠ΄Π΅Ρ ΡΠ½Π°ΡΠ»Π΅Π΄ΠΎΠ²Π°Π½ ΠΈΠ· context_user_code
# ΠΡΠΎ thread-safe ΠΈ loop-safe
for _ in range(25):
await tochka.get_accounts()
await asyncio.sleep(2)
async def register_merchant(user_code) -> str:
# ΠΡΠ΄Π΅Ρ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡΡΡ ΡΠΊΠ°Π·Π°Π½Π½ΡΠΉ user_code
# ΠΠ°ΠΆΠ΅ Π΅ΡΠ»ΠΈ Π² context_user_code Π±ΡΠ»ΠΎ Π·Π°Π΄Π°Π½ΠΎ Π΄ΡΡΠ³ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅
accounts = await tochka.get_accounts(user_code=user_code)
customer_info = await tochka.sbp_get_customer_info(
customer_code=accounts[0].customer_code
)
legal_entity = await tochka.sbp_get_legal_entity(customer_info.legal_id)
merchant = await tochka.sbp_register_merchant(
legal_id=legal_entity.legal_id,
name="TochkaExample",
mcc="7277",
address=" 1-ΠΉ ΠΠ΅ΡΠ½ΡΠΊΠΎΠ²ΡΠΊΠΈΠΉ ΠΏΡΠΎΠ΅Π·Π΄, Π΄. 1, ΡΡΡ. 8, ΡΡΠ°ΠΆ 1, ΠΏΠΎΠΌΠ΅Ρ. 43",
city="ΠΠΎΡΠΊΠ²Π°",
region_code="45",
zip_code="109456",
phone_number="+78002000024",
)
print(f"New merchant {merchant.merchant_id=}")
print(
*(await tochka.sbp_get_merchants(legal_id=legal_entity.legal_id)).merchants,
sep="\n",
)
return merchant.merchant_id
async def register_qr(merchant_id):
# user_code Π±ΡΠ΄Π΅Ρ ΡΠ½Π°ΡΠ»Π΅Π΄ΠΎΠ²Π°Π½ ΠΈΠ· context_user_code
# ΠΡΠΎ thread-safe ΠΈ loop-safe
accounts = await tochka.get_accounts()
customer_info = await tochka.sbp_get_customer_info(
customer_code=accounts[0].customer_code
)
legal_entity = await tochka.sbp_get_legal_entity(customer_info.legal_id)
sbp_accounts = await tochka.sbp_get_accounts(legal_entity.legal_id)
qr = await tochka.sbp_register_qr(
merchant_id=merchant_id,
account=sbp_accounts[0].account,
is_static=True,
purpose="ΠΠ΅ΡΠ΅ΡΠΈΡΠ»Π΅Π½ΠΈΠ΅ ΠΏΠΎ Π΄ΠΎΠ³ΠΎΠ²ΠΎΡΡ ΠΌΠΈΠ½Π΅ΡΠ°",
media_type="image/svg+xml",
)
print("Π‘ΡΠ°ΡΠΈΡΠ½ΡΠΉ Π±Π΅Π· ΡΡΠΌΠΌΡ:\n",qr.image.content)
qr = await tochka.sbp_register_qr(
merchant_id=merchant_id,
account=sbp_accounts[0].account,
is_static=True,
amount=Decimal("100.00"), # ΠΈΠ»ΠΈ Decimal(100), ΠΈΠ»ΠΈ 10000
purpose="ΠΠΏΠ»Π°ΡΠ° Π°ΡΠ΅Π½Π΄Ρ Π±ΠΎΡΠ΄Π΅Π»Ρ",
media_type="image/svg+xml",
)
print("Π‘ΡΠ°ΡΠΈΡΠ½ΡΠΉ Ρ ΡΡΠΌΠΌΠΎΠΉ:\n",qr.image.content)
qr = await tochka.sbp_register_qr(
merchant_id=merchant_id,
account=sbp_accounts[0].account,
is_static=False,
amount=10000, # ΠΈΠ»ΠΈ Decimal(100)
ttl=60, # 0 - ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎ Π²ΠΎΠ·ΠΌΠΎΠΆΠ½ΠΎΠ΅ ΠΎΠ³ΡΠ°Π½ΠΈΡΠ΅Π½ΠΈΠ΅, ΠΈΠ½Π°ΡΠ΅ Π² ΠΌΠΈΠ½ΡΡΠ°Ρ
purpose="ΠΠΏΠ»Π°ΡΠ° ΠΏΠΎΡΡΠ°Π²ΠΊΠΈ ΠΏΡΠ΅Π·Π΅ΡΠ²Π°ΡΠΈΠ²ΠΎΠ²",
media_type="image/svg+xml",
)
print("ΠΠΈΠ½Π°ΠΌΠΈΡΠ΅ΡΠΊΠΈΠΉ Ρ ΡΡΠΌΠΌΠΎΠΉ:\n",qr.image.content)
async def refund():
accounts = await tochka.get_accounts()
customer_info = await tochka.sbp_get_customer_info(
customer_code=accounts[0].customer_code
)
sbp_accounts = await tochka.sbp_get_accounts(customer_info.legal_id)
payments = await tochka.sbp_get_payments(
customer_info.customer_code, from_date=1,
)
print(payments)
last_payment = payments.payments[0]
refund_response = await tochka.sbp_start_refund(
account=sbp_accounts[0].account,
amount=Decimal("1.25"), # ΠΈΠ»ΠΈ 125
qrc_id=last_payment.qrc_id,
trx_id=last_payment.trx_id,
)
print("Refund: ", refund_response)
async def main():
# ΠΠΎΠ±Π°Π²ΠΈΡΡ Π΄Π²Π° ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ.
# ΠΡΠΎ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π±ΡΡ
Π³Π°Π»ΡΠ΅Ρ ΠΈ Π²Π»Π°Π΄Π΅Π»Π΅Ρ ΠΎΠ΄Π½ΠΎΠΉ ΠΊΠΎΠΌΠΏΠ°Π½ΠΈΠΈ
# ΠΈΠ»ΠΈ ΡΠΎΠ²Π΅ΡΡΠ΅Π½Π½ΠΎ ΡΠ°Π·Π½ΡΠ΅ Π»ΡΠ΄ΠΈ ΠΈΠ· ΡΠ°Π·Π½ΡΡ
ΠΊΠΎΠΌΠΏΠ°Π½ΠΈΠΉ.
# ΠΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠ΅ ΠΌΠΎΠΆΠ΅Ρ ΡΠ°Π±ΠΎΡΠ°ΡΡ Ρ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΈΠΌΠΈ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΠΌΠΈ
# Π»ΠΈΠ±ΠΎ Π² ΠΎΠ΄Π½ΠΎΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»ΡΡΠΊΠΎΠΌ ΡΠ΅ΠΆΠΈΠΌΠ΅, ΡΠΎΠ³Π΄Π° Π½Π°Π΄ΠΎ ΡΠΊΠ°Π·Π°ΡΡ
# tochka = TochkaAPI(client_id, client_secret, redirect_uri=redirect_uri, one_customer_mode=True)
# ΠΈ ΡΠΎΠ³Π΄Π° ΠΏΠΎΡΠ»Π΅ Π΄ΠΎΠ±Π°Π²Π»Π΅Π½ΠΈΡ ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ, ΡΠΈΡΡΠ΅ΠΌΠ° Π΅Π³ΠΎ Π·Π°ΠΏΠΎΠΌΠ½ΠΈΡ.
# Π£ΠΊΠ°Π·ΡΠ²Π°ΡΡ context_user_code Π½Π΅ ΠΏΡΠΈΠ΄ΡΡΡΡ
print("ΠΠ²Π΅Π΄ΠΈΡΠ΅ ΡΡΠΎ-Π»ΠΈΠ±ΠΎ, ΡΡΠΎΠ±Ρ Π΄ΠΎΠ±Π°Π²ΠΈΡΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Π΅ΠΉ")
print("ΠΠ»Ρ ΠΏΡΠΎΠΏΡΡΠΊΠ° Π½Π°ΠΆΠΌΠΈΡΠ΅ Enter")
if input(">>> "):
await add_user()
await add_user()
else:
# ΠΌΠΎΠΆΠ½ΠΎ Π½Π΅ ΡΠΊΠ°Π·ΡΠ²Π°ΡΡ, Π΅ΡΠ»ΠΈ one_customer_mode=True
context_user_code.set("212332030")
await refund()
balances = await tochka.get_balances()
print(balances[0].amount)
asyncio.run(main())