tochka-api

Simple Tochka Bank Open API client


Keywords
tochka, openapi, api, bank
License
MPL-2.0
Install
pip install tochka-api==0.0.5

Documentation

πŸŽ‰ 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 and str - 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())