Skip to main content

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

Project description

QPay API Integration client

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

QPay API integration made simpler and safer with data validation and auto token refresh.

This python package includes async and sync client. You can choose which ever suits your project.

Visit links:
Package document
QPay document

Features

  • Client manages the access & refresh tokens.
  • Both sync and async/await support.
  • Data validation with Pydantic.
  • Retries on payment check failures.
  • Retries on server error >=500.
  • Retries on network error.
  • Clear QPay error code and with details.
  • QPay Client settings with .env support.
  • async with & with statement support.

For more on async with QPayClient() as client: visit project documentation.

API coverage

All QPay APIs on their official document is supported.

Authentication

  • token
  • refresh

Invoice

  • Get invoice
  • Create simple invoice
  • Create detailed invoice
  • Create subscription invoice
  • Cancel invoice

Payment

  • get
  • list
  • check
  • cancel
  • refund

Ebarimt

  • get
  • create

Subscription

  • Get subscription
  • Cancel subscription

Installation

Using pip:

pip install qpay-client

Using poetry:

poetry add qpay-client

Using uv:

uv add qpay-client

Usage

Basic Example

Lets implement basic payment flow described in QPay developer document.

Process diagram image

Important to note:

You are free to implement the callback API's URI and query/params in anyway you want. But the callback you implement must return Response(status_code = 200, body="SUCCESS").

How to implement (Async example)

You don't have to worry about authentication and managing tokens. QPay client manages this behind the scene so you can focus on the important parts.

You can use any web framework. I am using Fastapi for the example just to create a simple callback API.

import asyncio
from decimal import Decimal

from fastapi import FastAPI, status

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

# Qpay client settings
settings = QPaySettings()

# Init async client
client = QPayClient(settings=settings)

# init FastAPI app
app = FastAPI()

# Just a dummy db
payment_database = {}


async def create_invoice():
    response = await client.invoice_create(
        InvoiceCreateSimpleRequest(
            invoice_code="TEST_INVOICE",
            sender_invoice_no="1234567",
            invoice_receiver_code="terminal",
            invoice_description="test",
            sender_branch_code="SALBAR1",
            amount=Decimal(1500),
            callback_url="https://api.your-domain.mn/payments?payment_id=1234567",
        )
    )

    # keep the qpay invoice_id in database, used for checking payment later!
    payment_database["1234567"] = {
        "id": "1234567",
        "invoice_id": response.invoice_id,
        "amount": Decimal(1500),
    }

    # Showing QPay invoice to the user ...
    print(response.qPay_shortUrl)


# You define the uri and query/param of your callback
# Your callback API must return
#   Response(status_code=200, body="SUCCESS")
@app.get("/payments", status_code=status.HTTP_200_OK)
async def qpay_callback(payment_id: str):
    data = payment_database.get(payment_id)
    if not data:
        raise ValueError("Payment not found")
    invoice_id = str(data["invoice_id"])
    response = await client.payment_check(
        PaymentCheckRequest(
            object_type=ObjectType.invoice, object_id=invoice_id, offset=Offset(page_number=1, page_limit=100)
        )
    )

    # do something with payment ...

    print(response)

    # This is important !
    return "SUCCESS"


if __name__ == "__main__":
    asyncio.run(create_invoice())

Run with fastapi.

fastapi dev main.py

Sync client

There is also sync flavour of the client which you can simply use as follows. All the implementation in Async client is also in the Sync client.

from qpay_client.v2 import QPayClientSync

client = QPayClientSync()

...

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-0.4.1.tar.gz (17.3 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-0.4.1-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for qpay_client-0.4.1.tar.gz
Algorithm Hash digest
SHA256 84b76487f04ecc90e74cc0cf25c5269069ce1204f1befacd50455b8711308270
MD5 0e513d554042a99922efc09f9905254e
BLAKE2b-256 9a6f8d57f7bffe7eadac75cfa3ac550f64d8f07cb0ae8ee91bab7ff020b0d4f6

See more details on using hashes here.

Provenance

The following attestation bundles were made for qpay_client-0.4.1.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-0.4.1-py3-none-any.whl.

File metadata

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

File hashes

Hashes for qpay_client-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 16d4e48c329a0230d4205b892987b3cd3f7b180c1ab2f7348f05dc0733e57eca
MD5 8fe3ec3a82160e99b37d2b4d77fc72ed
BLAKE2b-256 15074d773751485846fdd97a0e3b8d4d2b0f3a2ab7105d04dbbe73a609b0e397

See more details on using hashes here.

Provenance

The following attestation bundles were made for qpay_client-0.4.1-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