Skip to main content

Generate styled Bakong KHQR card images with correct USD/KHR amount formatting.

Project description

bakong-khqr-image

PyPI version Python versions License: MIT

Generate styled Bakong KHQR card images that match the official NBC design — with correct USD and KHR amount formatting.

Built from production experience: the official bakong-khqr library formats USD amounts with a comma (0,01 USD) instead of a period (0.01 USD). This library fixes that and ships a clean, standalone implementation.


Features

  • ✅ EMV-compliant KHQR payload generation (no external bakong dependency)
  • ✅ Correct amount formatting — 1,234.56 USD · 4,000 KHR
  • ✅ Styled 300 × 450 px card (Bakong red header, merchant info, dashed separator, rounded corners)
  • ✅ Bundled assets — works out of the box, no manual font/icon setup
  • ✅ Multiple export formats: PNG · JPEG · WebP · bytes · Base64 · data URI
  • ✅ Static (open-amount) and dynamic QR code support
  • ✅ Typed, documented, tested

Installation

pip install bakong-khqr-image

Dependencies (Pillow and qrcode) are installed automatically.


Quick Start

from bakong_khqr_image import KHQRImage

# Create one instance per merchant — reuse it for every payment
khqr = KHQRImage(
    bakong_account_id="yourname@aclb",
    merchant_name="My Coffee Shop",
    merchant_city="Phnom Penh",   # optional, default "Phnom Penh"
    currency="USD",               # optional, default "USD"
)

# Generate a styled QR card image
result = khqr.generate_image(amount=2.50)

# Save as PNG
result.save_png("payment_qr.png")

# Or get bytes / Base64 / data URI for web / API responses
png_bytes = result.to_bytes()
b64       = result.to_base64()
data_uri  = result.to_data_uri()

API Reference

KHQRImage

KHQRImage(
    bakong_account_id: str,
    merchant_name: str,
    merchant_city: str = "Phnom Penh",
    currency: str = "USD",           # "USD" or "KHR"
)

generate_image(amount, *, bill_number, store_label, terminal_label, mobile_number, static) → QRImageResult

Renders the full styled card image.

Parameter Type Description
amount float Transaction amount. 0 = open-amount / static QR
bill_number str Unique reference ID (auto-generated if omitted)
store_label str Store label shown in the Bakong app
terminal_label str Terminal identifier
mobile_number str Merchant phone number
static bool True → static QR (no amount in payload)

generate_payload(amount, *, ...) → (payload, bill_number)

Returns only the raw EMV KHQR string — useful for custom renderers or the Bakong payment-verification API.

payload, ref_id = khqr.generate_payload(amount=5.00)
md5_hash = KHQRImage.md5(payload)   # pass to Bakong API to check payment status

KHQRImage.md5(payload) → str

Computes the MD5 hash of a payload string required by the Bakong transaction-check API.


QRImageResult

Method Returns Description
save_png(path) str Save PNG, returns absolute path
save_jpeg(path) str Save JPEG, returns absolute path
save_webp(path) str Save lossless WebP, returns absolute path
to_bytes() bytes PNG-encoded bytes
to_base64() str Base64-encoded PNG string
to_data_uri() str data:image/png;base64,… URI
.image PIL.Image Raw PIL Image for custom processing

Examples

KHR currency

from bakong_khqr_image import KHQRImage

khqr = KHQRImage(
    bakong_account_id="yourname@aclb",
    merchant_name="ហាងកាហ្វេ",
    currency="KHR",
)
result = khqr.generate_image(amount=4_000)
result.save_png("payment_khr.png")

Static (open-amount) QR

result = khqr.generate_image(amount=0, static=True)
result.save_png("static_qr.png")

Web / API response

result = khqr.generate_image(amount=1.50)
# Return as JSON
import json
response = {"qr_image": result.to_data_uri()}

Access the raw PIL image

result = khqr.generate_image(amount=2.50)
pil_image = result.image          # PIL.Image.Image
pil_image.show()                  # open in system viewer

Card Layout

┌─────────────────────────────┐  ◥ red fold
│  [KHQR / Bakong logo]       │  ← red header (60 px)
│                             │
│  Merchant Name              │
│  2.50 USD                   │
│ - - - - - - - - - - - - - - │  ← dashed separator
│                             │
│    ┌───────────────────┐    │
│    │                   │    │
│    │    QR Code        │    │
│    │      [💲]         │    │  ← currency icon overlay
│    │                   │    │
│    └───────────────────┘    │
│                             │
└─────────────────────────────┘
         300 × 450 px

Sample image

USD QR sample KHR QR sample

Running Tests

pip install -e ".[dev]"
pytest

Publishing to PyPI

pip install build twine
python -m build
twine upload dist/*

License

MIT © 2026 Virackbot

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

bakong_khqr_image-0.1.3.tar.gz (203.5 kB view details)

Uploaded Source

Built Distribution

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

bakong_khqr_image-0.1.3-py3-none-any.whl (200.5 kB view details)

Uploaded Python 3

File details

Details for the file bakong_khqr_image-0.1.3.tar.gz.

File metadata

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

File hashes

Hashes for bakong_khqr_image-0.1.3.tar.gz
Algorithm Hash digest
SHA256 324d534942dce27f649572a0dbe8e0b614c991fb2005765fe2b90f8097d140e2
MD5 a2625ac32381c3a186f7632f9db63513
BLAKE2b-256 7e9067cb8b9002774c22d561dafc8f9170c3f73092ab27cd38e4e3d768c0978e

See more details on using hashes here.

Provenance

The following attestation bundles were made for bakong_khqr_image-0.1.3.tar.gz:

Publisher: publish-pypi.yml on Virackbot/bakong-khqr-image

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

File details

Details for the file bakong_khqr_image-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for bakong_khqr_image-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 11242e26d83e3e4d4a68efc993a7f858061bbf5aa3187831a049200ad4a5649b
MD5 016468a912caa34ec9ba1ffd264bf42d
BLAKE2b-256 c265405a9ab7c7248f1c802de997f8f4a8676a89f4d19ae4daa156b0c9bf68d0

See more details on using hashes here.

Provenance

The following attestation bundles were made for bakong_khqr_image-0.1.3-py3-none-any.whl:

Publisher: publish-pypi.yml on Virackbot/bakong-khqr-image

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