Skip to main content

QPay Integration made easy (async & sync, typed schemas, auto token refresh)

Project description

qpay-client

Tests codecov PyPI - Version Python PyPI - License PyPI - Downloads Documentation Status

Монгол хувилбар — README_MN.md

qpay-client is a production-ready Python client for the QPay v2 payment API — Mongolia's leading payment provider. Built and used in production systems, it supports both async and sync clients, Pydantic v2-validated schemas, automatic token management, retry logic with exponential backoff, and typed wrappers for all common endpoints.

Documentation: qpay-client.readthedocs.io

QPay developer portal: developer.qpay.mn

Features

  • Both AsyncQPayClient and QPayClient (sync) supported
  • Authentication and token refresh managed automatically
  • Request/response validation via Pydantic v2 schemas
  • Retry logic for network errors and transient server failures
  • Configurable payment_check polling with exponential backoff
  • with and async with context manager support
  • Structured QPayError exceptions with error codes and descriptions

Installation

Using pip:

pip install qpay-client

Using uv:

uv add qpay-client

Using poetry:

poetry add qpay-client

Quickstart

Async client

from decimal import Decimal

from qpay_client.v2 import AsyncQPayClient, QPaySettings
from qpay_client.v2.schemas import InvoiceCreateSimpleRequest

settings = QPaySettings.sandbox()

async def main():
    async with AsyncQPayClient(settings=settings) as client:
        invoice = await client.invoice_create(
            InvoiceCreateSimpleRequest(
                sender_invoice_no="ORDER-1001",
                invoice_receiver_code="terminal",
                invoice_description="Test invoice",
                amount=Decimal("1500"),
                callback_url="https://example.com/qpay/callback?payment_id=ORDER-1001",
            )
        )

        print(invoice.invoice_id)
        print(invoice.qPay_shortUrl)

Sync client

from decimal import Decimal

from qpay_client.v2 import QPayClient, QPaySettings
from qpay_client.v2.schemas import InvoiceCreateSimpleRequest

settings = QPaySettings.sandbox()

with QPayClient(settings=settings) as client:
    invoice = client.invoice_create(
        InvoiceCreateSimpleRequest(
            sender_invoice_no="ORDER-1002",
            invoice_receiver_code="terminal",
            invoice_description="Sync test invoice",
            amount=Decimal("2500"),
            callback_url="https://example.com/qpay/callback?payment_id=ORDER-1002",
        )
    )

    print(invoice.invoice_id)

Configuration

Sandbox

from qpay_client.v2 import QPaySettings

settings = QPaySettings.sandbox()

Production

from qpay_client.v2 import QPaySettings

settings = QPaySettings.production(
    username="your-merchant-username",
    password="your-merchant-password",
    invoice_code="YOUR_INVOICE_CODE",
)

Retry and delay settings

settings = QPaySettings.sandbox(
    client_retries=2,
    client_delay=0.25,
    client_jitter=0.1,
    payment_check_retries=8,
    payment_check_delay=0.5,
    payment_check_jitter=0.2,
)

Checking a payment

from qpay_client.v2.enums import ObjectType
from qpay_client.v2.schemas import Offset, PaymentCheckRequest

check_request = PaymentCheckRequest(
    object_type=ObjectType.invoice,
    object_id="YOUR_INVOICE_ID",
    offset=Offset(page_number=1, page_limit=100),
)

result = await client.payment_check(check_request)

if result.count > 0:
    print("Payment found")

FastAPI callback flow

examples/quickstart.py contains a working async example with a QPay callback endpoint.

The general pattern is:

  1. Create an invoice
  2. Store the invoice_id in your database
  3. On QPay callback, call payment_check to verify the payment
  4. Return "SUCCESS" with HTTP 200

To run the example:

fastapi dev examples/quickstart.py

Returning HTTP 200 with body "SUCCESS" from your callback endpoint is required by QPay.

Import paths

Import clients and settings from qpay_client.v2:

from qpay_client.v2 import AsyncQPayClient, QPayClient, QPaySettings, QPayError

Import schemas and enums from their respective modules:

from qpay_client.v2.enums import ObjectType
from qpay_client.v2.schemas import InvoiceCreateSimpleRequest, Offset, PaymentCheckRequest

Supported endpoints

Authentication

  • token
  • refresh

Invoice

  • invoice_get
  • invoice_create
  • invoice_cancel

Payment

  • payment_get
  • payment_list
  • payment_check
  • payment_cancel
  • payment_refund

Ebarimt

  • ebarimt_get
  • ebarimt_create

Subscription

  • subscription_get
  • subscription_cancel

Notes

  • Never call QPaySettings() directly — use sandbox() or production() factory methods.
  • All public endpoint methods check and refresh authentication automatically; no need to manage tokens manually.
  • payment_check polls with exponential backoff — tune retry/delay settings to your use case.
  • Do not commit production credentials to source control.

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qpay_client-2.0.3.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

qpay_client-2.0.3-py3-none-any.whl (25.0 kB view details)

Uploaded Python 3

File details

Details for the file qpay_client-2.0.3.tar.gz.

File metadata

  • Download URL: qpay_client-2.0.3.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for qpay_client-2.0.3.tar.gz
Algorithm Hash digest
SHA256 d222a476fcfe6ffc7e2000956b03f03c0b26af55f4915f09c926fbe298cc858b
MD5 a84f662e5d714f472cb556fe6fdf064b
BLAKE2b-256 796158012e140551c4d7d1dd55c6c3fe1dde8fe7c13055e17d98512b0e21f45b

See more details on using hashes here.

Provenance

The following attestation bundles were made for qpay_client-2.0.3.tar.gz:

Publisher: release.yml on Amraa1/qpay_client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file qpay_client-2.0.3-py3-none-any.whl.

File metadata

  • Download URL: qpay_client-2.0.3-py3-none-any.whl
  • Upload date:
  • Size: 25.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for qpay_client-2.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 1e4e0b3ae017038ef2662cb045e33c8af509c94d18ad5a4039f4cfbe8a2ad64e
MD5 c24c4c0543e3820e093bf56bbc923096
BLAKE2b-256 fc3d1d9df3a5c6828532af5a3976dae5c2814aa9afbb867533c5c5b4606836ac

See more details on using hashes here.

Provenance

The following attestation bundles were made for qpay_client-2.0.3-py3-none-any.whl:

Publisher: release.yml on Amraa1/qpay_client

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page