vnqr 🇻🇳
Thư viện Python tạo, đọc và vẽ mã QR thanh toán VietQR (chuẩn EMVCo / NAPAS247).
✨ Tính năng
- ✅ Tạo chuỗi VietQR đầy đủ tuỳ chọn: QR tĩnh/động, chuyển đến tài khoản hoặc thẻ, số hoá đơn, mã tham chiếu, tên/thành phố đơn vị thụ hưởng
- ✅ Nhận ngân hàng theo BIN, mã hoặc tên (
"970422","MB","MBBank"). Danh mục 76 ngân hàng đóng gói sẵn - ✅ Tự bỏ dấu tiếng Việt ở các trường văn bản
- ✅ Giải mã chuỗi VietQR, kiểm tra CRC
- ✅ Vẽ ảnh QR đẹp: thẻ VietQR có logo VietQR, Napas247 và ngân hàng; mẫu in có thông tin chuyển khoản; module bo góc, chấm tròn, gradient, logo giữa, nền trong suốt
- ✅ Xuất PNG / WebP / JPEG / SVG, dạng bytes, base64 hoặc data URL
- ✅ Type hints đầy đủ (mypy strict)
📦 Cài đặt
pip install vnqr
# hoặc
uv pip install vnqr
🚀 Sử dụng nhanh
from vnqr import build_vietqr, render_qr
qr = build_vietqr(
bank="MB", # BIN "970422", mã "MB" hoặc tên "MBBank"
account="0123456789",
amount=2_000_000, # bỏ trống -> QR tĩnh, người chuyển tự nhập số tiền
content="Thanh toán đơn DH123", # tự bỏ dấu: "Thanh toan don DH123"
)
render_qr(qr, "compact").save("vietqr.png") # thẻ VietQR 540x540
Tạo chuỗi VietQR
from vnqr import build_vietqr
qr = build_vietqr(
"BIDV",
"9704181234567890",
150_000,
"Tiền điện tháng 10",
service="card", # "account" (QRIBFTTA, mặc định) | "card" (QRIBFTTC)
bill_number="HD001", # tag 62-01
reference_label="REF-9", # tag 62-05
merchant_name="Công ty Điện lực", # tag 59 (tối đa 25 ký tự, viết hoa, bỏ dấu)
merchant_city="Hà Nội", # tag 60 (tối đa 15 ký tự)
dynamic=None, # None: động nếu có số tiền / số HĐ / mã tham chiếu
)
generate_vietqr_string(bank_bin, bank_account, total_amount, content) vẫn giữ nguyên như các phiên bản trước.
Giải mã chuỗi VietQR
from vnqr import parse_vietqr, verify_crc
info = parse_vietqr(qr) # ValueError nếu sai cấu trúc / sai CRC
info.bank_bin, info.account, info.amount, info.content
info.bank.short_name # "BIDV" (tra từ danh mục)
info.service # "card"
verify_crc(qr) # True
Tra cứu ngân hàng
from vnqr import find_bank, list_banks, resolve_bin
find_bank("Vietcombank") # Bank(bin='970436', code='VCB', short_name='Vietcombank', ...)
resolve_bin("tcb") # '970407'
len(list_banks()) # 76
🎨 Vẽ ảnh QR
render_qr(qr_string, template, ...) trả về RenderedQR với .image (PIL), .save(path), .to_bytes(fmt), .to_base64(fmt), .to_data_url(fmt) và .to_svg().
| Mẫu | Kích thước | Nội dung |
|---|---|---|
qr_only (mặc định) |
size x size (500) |
Mã QR + icon VietQR giữa. Xuất được SVG |
compact |
540 x 540 × scale |
Logo VietQR, khung, mã QR, Napas247 | logo ngân hàng |
print |
600 x ~750 × scale |
Như compact, thêm số tiền, nội dung, tên chủ TK, số TK, tên ngân hàng |
from vnqr import QRStyle, render_qr
# Thẻ in, đọc sẵn ngân hàng/số TK/số tiền/nội dung từ chuỗi QR
render_qr(qr, "print", account_name="Nguyễn Văn A", scale=2).save("in.png")
# Ghi đè nội dung hiển thị (bản có dấu), ngân hàng, logo ngân hàng
render_qr(qr, "print", description="Tiền điện tháng 10", bank="BIDV", bank_logo="bidv.png")
# Kiểu module
style = QRStyle(
module_shape="rounded", # square | rounded | circle | gapped
eye_shape="rounded", # square | rounded | circle (3 ô định vị)
color="#003B70",
gradient="diagonal", # None | vertical | horizontal | diagonal | radial
gradient_color="#7A1FA2",
eye_color=None, # màu riêng cho ô định vị
background="#FFFFFF", # None = trong suốt
error_correction="Q", # L | M | Q | H
)
render_qr(qr, style=style, center_logo="logo.png", center_logo_ratio=0.2).save("qr.webp")
# Không logo giữa; xuất SVG vector
render_qr(qr, center_logo=False).save("qr.svg")
# Dùng trong HTML / API
data_url = render_qr(qr, size=300).to_data_url() # data:image/png;base64,...
b64 = render_qr(qr, "compact").to_base64("webp")
Đổi màu, đổi hình module hoặc chèn logo đều làm giảm độ tương phản và phần dữ liệu dư. Hãy giữ module màu đậm trên nền sáng, và quét thử bằng app ngân hàng thật trước khi phát hành.
generate_qr_image() và generate_qr_image_with_icon() (ảnh đen trắng, trả về base64) vẫn giữ nguyên.
💡 Ví dụ
| File | Nội dung |
|---|---|
examples/simple_example.py |
Tạo chuỗi và ảnh QR đơn giản nhất |
examples/basic_usage.py |
Xuất HTML xem trực quan |
examples/save_qr_image.py |
Lưu QR ra file PNG |
examples/qr_with_icon.py |
QR đen trắng có icon giữa |
examples/styled_qr.py |
Mẫu thẻ, mẫu in, bo góc, gradient, WebP/SVG |
examples/crawl_banks.py |
Lấy danh sách ngân hàng mới nhất từ API VietQR |
python examples/styled_qr.py
python -m vnqr.tools.craw_bank # crawl danh sách ngân hàng
🏦 Mã BIN của các ngân hàng phổ biến
Không cần nhớ BIN, có thể truyền mã hoặc tên ngân hàng. Tra đầy đủ bằng list_banks().
| Ngân hàng | Mã | BIN |
|---|---|---|
| Vietcombank | VCB | 970436 |
| VietinBank | ICB | 970415 |
| BIDV | BIDV | 970418 |
| Agribank | VBA | 970405 |
| Techcombank | TCB | 970407 |
| MBBank | MB | 970422 |
| ACB | ACB | 970416 |
| VPBank | VPB | 970432 |
| TPBank | TPB | 970423 |
| ABBANK | ABB | 970425 |
🔧 Yêu cầu hệ thống
- Python >= 3.10
qrcode >= 8.2,pillow >= 10,crcmod,pypng,requests
📝 Cấu trúc dự án
vnqr/
├── src/vnqr/
│ ├── vietqr.py # build_vietqr, generate_vietqr_string
│ ├── parse.py # parse_vietqr, verify_crc
│ ├── banks.py # danh mục ngân hàng
│ ├── render.py # render_qr, QRStyle (mẫu compact / print / qr_only, SVG)
│ ├── qr.py # generate_qr_image(_with_icon)
│ ├── text.py # remove_diacritics
│ ├── crc.py, element.py, constants.py
│ ├── assets/ # logo VietQR, Napas247, logo ngân hàng, font Be Vietnam Pro
│ ├── data/banks.json
│ └── tools/ # crawl danh sách ngân hàng
├── examples/
├── tests/
├── DISCLAIMER.md
└── pyproject.toml
🧪 Testing
uv run pytest # test ảnh giải mã lại bằng zxing-cpp
uv run mypy src/vnqr
uv run black --check src tests && uv run isort --check src tests
🤝 Đóng góp
- Fork dự án
- Tạo branch (
git checkout -b feature/AmazingFeature) - Commit (
git commit -m 'Add some AmazingFeature') - Push (
git push origin feature/AmazingFeature) - Mở Pull Request
📄 License
Mã nguồn phát hành theo giấy phép MIT, xem LICENSE. Font Be Vietnam Pro theo SIL OFL 1.1.
⚠️ Tuyên bố miễn trừ trách nhiệm
VietQR, NAPAS và NAPAS247 là nhãn hiệu của NAPAS. Logo ngân hàng thuộc về từng ngân hàng. Thư viện không có liên kết với các tổ chức này và không được họ bảo trợ. Phần mềm được cung cấp nguyên bản (AS-IS); người triển khai chịu trách nhiệm về tính chính xác của thông tin thanh toán và về an toàn dữ liệu. Chi tiết: DISCLAIMER.md.
👤 Author
Do Quoc Vuong: vuongtlt13@gmail.com, @vuongtlt13
🙏 Acknowledgments
- NAPAS: tiêu chuẩn VietQR
- vietqr-core: bố cục ảnh thẻ VietQR
- VietQR.io: danh mục ngân hàng
- qrcode, Pillow
Made with ❤️ in Vietnam
Metadata
Release files for vnqr 0.2.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 |
|---|---|---|---|---|
| vnqr-0.2.0.tar.gz | 456.0 kB | Without provenance | Details for vnqr-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| vnqr-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for vnqr-0.2.0-py3-none-any.whl |
Total release size: 936.5 kB
Release files / vnqr-0.2.0.tar.gz
| Download URL | vnqr-0.2.0.tar.gz |
|---|---|
| Size | 456.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9750dea0e57708f6856c01b264d71bbf233b9224f3495bb52812dc6455aace6f
|
|
BLAKE2b-256 checksum How to use checksums |
0f4e832538fb61ce7090fe7baccd5ab8ec4b1a33290ed365e28c3271424ff99e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / vnqr-0.2.0-py3-none-any.whl
| Download URL | vnqr-0.2.0-py3-none-any.whl |
|---|---|
| Size | 480.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bf3709efc174dafdb923d27ee4243968a400709b99a67e332b4ccfecfe00afb7
|
|
BLAKE2b-256 checksum How to use checksums |
c5641c53625ec8612a096a7da32f0bab795a742ea2f3796b3122c6b96ca6aee6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.7 {"installer":{"name":"uv","version":"0.13.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for vnqr 0.2.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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A 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.