bakong-khqr
A lightweight, robust Python library for generating EMVCo-compliant Bakong KHQR strings, checking transaction payment statuses, generating mobile banking deep links, and rendering styled QR images.
Features
- Offline / Local Generation: Generate valid EMVCo KHQR payloads locally without external network dependencies.
- Custom Gateway Integration: Easily connect to custom or self-hosted payment relay endpoints via
base_urlandKHQRConfig. - Backward Compatible: Seamlessly supports existing implementations and legacy token methods without breaking changes.
- Smart Polling: Adaptive interval calculation to verify payment statuses while optimizing API requests.
- Bulk Verification: Check up to 50 transactions simultaneously in a single API call.
- Mobile Deep Linking: Generate direct URLs to launch mobile banking apps on smartphones.
- Built-in Image Tools: Render official-style KHQR templates as PNG, JPEG, WebP, or Base64 Data URIs (optional extra).
Installation
Standard Installation
pip install bakong-khqr
Installation with Image Generation Support
If you plan to use qr_image() to generate styled PNG/JPEG images:
pip install "bakong-khqr[image]"
Initialization & Configuration
KHQR can be initialized in multiple flexible ways depending on your architectural setup:
1. Custom Gateway / Relay Mode (Recommended for Custom API Servers)
If you deploy your own relay API server or use a custom endpoint, specify base_url:
Option A: Using KHQRConfig Dataclass
from bakong_khqr import KHQR, KHQRConfig
config = KHQRConfig(
token="your_api_token",
base_url="https://api.yourgateway.com/v1"
)
khqr = KHQR(config=config)
Option B: Using Direct Arguments
from bakong_khqr import KHQR
khqr = KHQR(
token="your_api_token",
base_url="https://api.yourgateway.com/v1"
)
2. Standard Official NBC Mode
For standard offline QR generation or connecting directly to official endpoints:
from bakong_khqr import KHQR
khqr = KHQR("your_bakong_token_here")
Quickstart & Usage Examples
1. Generating a KHQR Code
A. Gateway / Relay Mode
When using a configured base_url, merchant credentials and currency can be resolved on the server side:
from bakong_khqr import KHQR, KHQRConfig
config = KHQRConfig(
token="your_token",
base_url="https://api.yourgateway.com/v1"
)
khqr = KHQR(config=config)
response = khqr.create_qr(
amount=10.0,
return_url="https://yourstore.com/checkout/return",
bill_number="INV-2026-001"
)
# KHQRResponse acts directly as the raw QR string:
print("QR Code:", response)
print("MD5 Checksum:", response.md5)
print("Transaction ID:", response.tran_id)
print("Checkout URL:", response.checkout_url)
B. Offline / Direct EMVCo Generation
Generate QR strings locally by supplying complete merchant specifications:
from bakong_khqr import KHQR
khqr = KHQR()
qr_string = khqr.create_qr(
account_id="merchant_name@bank",
merchant_name="Sample Store",
merchant_city="Phnom Penh",
amount=15000,
currency="KHR",
bill_number="TRX-1002"
)
print("Generated KHQR:", qr_string)
2. Checking Payment Status & Smart Polling
Verify whether a payment has been captured using the 32-character MD5 checksum.
Single Status Check
md5_hash = khqr.generate_md5(qr_string)
status = khqr.check_payment(md5_hash)
print("Payment Status:", status)
# Returns: 'PAID', 'SCANNED', 'EXPIRED', or 'UNPAID'
Smart Polling Loop (Optimized Polling Overhead)
Pass start_time into check_payment() to dynamically calculate the optimal sleep delay:
import time
start_time = time.time()
timeout_seconds = 10 * 60 # 10 minutes
while True:
status, next_delay = khqr.check_payment(md5_hash, start_time=start_time)
if status == "PAID":
print("Payment Confirmed!")
# Fulfill order here
break
if status == "EXPIRED" or (time.time() - start_time) >= timeout_seconds:
print("Payment session expired.")
break
time.sleep(next_delay)
3. Retrieving Confirmed Transaction Details
Retrieve payment receipt information once a transaction is completed:
payment_info = khqr.get_payment(md5_hash)
if payment_info:
print("Amount Paid:", payment_info.get("amount"))
print("Currency:", payment_info.get("currency"))
print("Payer Bank:", payment_info.get("payerBank"))
4. Bulk Payment Status Verification
Verify up to 50 transactions simultaneously in one network call:
md5_list = [
"c4ca4238a0b923820dcc509a6f75849b",
"c81e728d9d4c2f636f067f89cc14862c",
"eccbc87e4b5ce2fe28308fd9f2a7baf3"
]
paid_md5s = khqr.check_bulk_payments(md5_list)
print("Paid Transactions:", paid_md5s)
5. Mobile Deep Link Generation
Generate deep links to open mobile banking applications directly:
deeplink = khqr.generate_deeplink(
qr=qr_string,
appName="MyStoreApp",
appIconUrl="https://mystore.com/icon.png",
appDeepLinkCallback="https://mystore.com/orders/verify"
)
print("Mobile Deep Link:", deeplink)
6. Styled KHQR Image Generation
Render branded KHQR templates locally as files or Base64 URIs:
# Save as a PNG file
png_path = khqr.qr_image(qr_string, format="png", output_path="checkout_qr.png")
# Output as Base64 Data URI (ideal for web embedding)
base64_data_uri = khqr.qr_image(qr_string, format="base64_uri")
print('<img src="' + base64_data_uri + '" alt="Scan to Pay" />')
Supported formats: png, jpeg, webp, bytes, base64, base64_uri.
API Reference
KHQRConfig (Dataclass)
| Field | Type | Default | Description |
|---|---|---|---|
token |
str | None |
None |
Authentication Bearer token. |
base_url |
str | None |
None |
Base URL for custom relay/gateway endpoints. |
KHQR(bakong_token=None, base_url=None, token=None, config=None)
config(KHQRConfig, optional): Configuration instance containingtokenandbase_url.token(str, optional): API Bearer token.base_url(str, optional): Custom Gateway base URL (e.g.,https://api.yourgateway.com/v1).bakong_token(str \| KHQRConfig, optional): Positional token for backward compatibility.
create_qr(...) Parameters
| Parameter | Type | Required | Description |
|---|---|---|---|
amount |
float |
Yes | Transaction amount (>= 0.01 USD, >= 100 KHR). |
return_url |
str | None |
No | Destination URL after checkout completion. |
success_url |
str | None |
No | Redirect destination specifically upon successful payment. |
cancel_url |
str | None |
No | Redirect destination if cancelled or expired. |
account_id |
str | None |
Conditional | Payee account identifier (e.g. merchant@bank). |
merchant_name |
str | None |
Conditional | Merchant display name. |
merchant_city |
str | None |
Conditional | Merchant location city (default: Phnom Penh). |
currency |
str | None |
Conditional | Currency code (USD or KHR). |
bill_number |
str | None |
No | Order / Invoice reference identifier. |
store_label |
str | None |
No | Branch / store description. |
phone_number |
str | None |
No | Contact phone number. |
terminal_label |
str | None |
No | Terminal identifier. |
static |
bool |
No | Generate static QR (no fixed amount). Default: False. |
expiration |
int |
No | Expiration duration in days. Default: 1. |
return_dict |
bool |
No | Return raw dictionary instead of KHQRResponse. |
License
This project is licensed under the MIT License.
Disclaimer
This package is an independent open-source project and is not affiliated with, officially endorsed by, or authorized by the National Bank of Cambodia (NBC).
Metadata
Release files for bakong-khqr 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bakong_khqr-0.7.0.tar.gz | 214.1 kB | With provenance | Details for bakong_khqr-0.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bakong_khqr-0.7.0-py3-none-any.whl | Python 3 | none | any | Details for bakong_khqr-0.7.0-py3-none-any.whl |
Total release size: 427.7 kB
Release files / bakong_khqr-0.7.0.tar.gz
| Download URL | bakong_khqr-0.7.0.tar.gz |
|---|---|
| Size | 214.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b5fe8d46605133478bea2e735d10c83ce570f9534c714bad632244bfc52c1640
|
|
BLAKE2b-256 checksum How to use checksums |
a7d1e2fe598db2d65961c777e2389395a74e223351be95e6fc6f543aa699d244
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logRelease files / bakong_khqr-0.7.0-py3-none-any.whl
| Download URL | bakong_khqr-0.7.0-py3-none-any.whl |
|---|---|
| Size | 213.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1e40fabd703b295e82cb172694f2c7f70e489e83dce36052ad29dddbf80643b5
|
|
BLAKE2b-256 checksum How to use checksums |
6688ea4f8ac257f1bbf30f0e37d6b0f9488ca8711bedd9dc00c8807389640f34
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logSecurity for bakong-khqr 0.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for bakong_khqr-0.7.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Nov 8, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.