Skip to main content

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

Project description

QPay API Integration client

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

Visit links:
Package document
QPay document

Made with ❤️

Features

  • Client manages the access & refresh tokens 🤖
  • Both sync and async/await support 🙈🙉
  • Pydantic data validation ✅
  • Retries for payment check 🔁
  • QPay error support 🔍

API coverage

Auth 🔐

  • token - Used to get token
  • refresh - Used to refresh token

Invoice 📜

  • get (Please make an issue if you need this!)
  • create (simple and complex)
  • cancel

Payment 💵

  • get
  • list
  • check
  • cancel
  • refund

Ebarimt 🧾

  • get
  • create

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
from qpay_client.v2.enums import ObjectTypeNum
from qpay_client.v2.schemas import InvoiceCreateSimpleRequest, PaymentCheckRequest

client = QPayClient(
    username="TEST_MERCHANT",  # or use your username
    password="123456",  # or use your password
    is_sandbox=True,  # or false for production
)

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=ObjectTypeNum.invoice,
            object_id=invoice_id,
        )
    )

    # do something with payment ...

    print(response)

    # This is important !
    return "SUCCESS"


asyncio.run(create_invoice())

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()

...

Run it

fastapi dev main.py

Methods

Invoice methods

invoice_create Used to create QPay invoice.

invoice_cancel Used to cancel a created invoice

Payment methods

payment_get Used to get payment details

payment_check Used to check payment after the callback invocation

payment_cancel Used to cancel payment (Use with caution ⚠️)

payment_refund Used to refund the payment back to the user

payment_list Used to list payments (e.g: for subscription 🔁)

Ebarimt methods

ebarimt_create Used to create Ebarimt (must be registered in Ebarimt platform first)

ebarimt_get Used to get Ebarimt (must be registered in Ebarimt platform first)

Schemas

Request/response payloads are strongly typed via Pydantic. See qpay_client.v2.schemas for models such as:

  • InvoiceCreateSimpleRequest
  • InvoiceCreateRequest
  • PaymentCheckRequest
  • EbarimtCreateRequest

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.2.4.tar.gz (9.2 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.2.4-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: qpay_client-0.2.4.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.14

File hashes

Hashes for qpay_client-0.2.4.tar.gz
Algorithm Hash digest
SHA256 f617aca985fb6097d99c2feeaf067232c065952449457d4588daf42a4ac6c8a0
MD5 3c96c349cd8f71b9c5d3086491aa839b
BLAKE2b-256 f4b7caf8c03a0676bc897676c9a6bb1e93f696bbd1e43575482c981dcafe6ac0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for qpay_client-0.2.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0c349114e1dc78c8f348d748403d726f22a5e2340c3e46407d84a9842b75bf3e
MD5 9371097197464d75cf56c5897977b998
BLAKE2b-256 94dd80340a106e7d0ca068f0348e4f4608000c0a06866ba1b1cd61e194a2dc32

See more details on using hashes here.

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