khqr_generator 0.0.5
khqr_generator: ^0.0.5 copied to clipboard
KHQR Generator (This is not an official library affiliated with NBC.)
KHQR Generator for Dart #
A Dart library for generating and parsing KHQR codes, based on the Bakong KHQR specification and inspired by the bakong-khqr npm package.
Disclaimer: This library is an independent implementation and is not an official product of the National Bank of Cambodia (NBC) or the Bakong system.
Features #
- Generate KHQR codes for individual and merchant accounts.
- Support for static and dynamic QR codes.
- Verify the validity of KHQR codes.
- Decode KHQR codes to extract the embedded information.
- No external dependencies for core functionality.
Installation #
Add this to your package's pubspec.yaml file:
dependencies:
khqr_generator: latest_version
Then, run flutter pub get to install the package.
Usage #
Import the library in your Dart file:
import 'package:khqr_generator/khqr_generator.dart';
Generating an Individual QR Code #
To generate a QR code for an individual, use the KHQRGenerator.generateIndividual method. You need to provide an IndividualInfo object with the required information.
Example: Static QR Code
final individualInfo = IndividualInfo(
bakongAccountID: 'test@test',
merchantName: 'Test Merchant',
merchantCity: 'Phnom Penh',
);
final response = KHQRGenerator.generateIndividual(individualInfo);
if (response.status.code == 0) {
print('QR Code: ${response.data!.qr}');
print('MD5: ${response.data!.md5}');
} else {
print('Error: ${response.status.message}');
}
Example: Dynamic QR Code (with amount)
final individualInfo = IndividualInfo(
bakongAccountID: 'test@test',
merchantName: 'Test Merchant',
merchantCity: 'Phnom Penh',
amount: 100.0,
currency: KHQRCurrency.usd.code,
expirationTimestamp: DateTime.now().millisecondsSinceEpoch + 3600000, // 1 hour from now
);
final response = KHQRGenerator.generateIndividual(individualInfo);
if (response.status.code == 0) {
print('QR Code: ${response.data!.qr}');
} else {
print('Error: ${response.status.message}');
}
Generating a Merchant QR Code #
To generate a QR code for a merchant, use the KHQRGenerator.generateMerchant method with a MerchantInfo object.
final merchantInfo = MerchantInfo(
bakongAccountID: 'test@test',
merchantName: 'Test Merchant',
merchantCity: 'Phnom Penh',
merchantID: '1234567890',
acquiringBank: 'Test Bank',
);
final response = KHQRGenerator.generateMerchant(merchantInfo);
if (response.status.code == 0) {
print('QR Code: ${response.data!.qr}');
} else {
print('Error: ${response.status.message}');
}
Verifying a KHQR Code #
You can verify the integrity of a KHQR code using the KHQRGenerator.verify method. This checks the CRC and format of the QR string.
String qrCode = '...'; // The KHQR string
final result = KHQRGenerator.verify(qrCode);
if (result.isValid) {
print('QR Code is valid.');
} else {
print('QR Code is invalid.');
}
Decoding a KHQR Code #
To decode a KHQR code and extract the embedded information, use the KHQRGenerator.decode method.
String qrCode = '...'; // The KHQR string
final response = KHQRGenerator.decode(qrCode);
if (response.status.code == 0) {
final decodedData = response.data!.decodedData;
print('Decoded Data: $decodedData');
} else {
print('Error: ${response.status.message}');
}
API Reference #
KHQRGenerator #
This class contains the main methods for generating, verifying, and decoding KHQR codes.
static KHQRResponse generateIndividual(IndividualInfo info)static KHQRResponse generateMerchant(MerchantInfo info)static VerificationResult verify(String qr)static KHQRResponse decode(String qr)
IndividualInfo #
This class holds the information for an individual QR code.
| Parameter | Type | Required | Description |
|---|---|---|---|
bakongAccountID |
String |
Yes | The Bakong account ID. |
merchantName |
String |
Yes | The name of the merchant or individual. |
merchantCity |
String |
Yes | The city of the merchant or individual. |
amount |
double |
No | The transaction amount. If provided, the QR becomes dynamic. |
currency |
int |
No | The transaction currency code (from KHQRCurrency). Defaults to KHR. |
expirationTimestamp |
int |
No | The expiration timestamp for dynamic QR codes (in milliseconds since epoch). |
billNumber |
String |
No | The bill number for the transaction. |
mobileNumber |
String |
No | The mobile number associated with the transaction. |
storeLabel |
String |
No | The label for the store. |
terminalLabel |
String |
No | The label for the terminal. |
purposeOfTransaction |
String |
No | The purpose of the transaction. |
MerchantInfo #
This class extends IndividualInfo and adds merchant-specific fields.
| Parameter | Type | Required | Description |
|---|---|---|---|
merchantID |
String |
Yes | The ID of the merchant. |
acquiringBank |
String |
Yes | The acquiring bank for the merchant. |
KHQRCurrency #
An enum representing the supported currencies.
KHQRCurrency.khr(code: 116)KHQRCurrency.usd(code: 840)
Error Handling #
The library returns a KHQRResponse object which contains a status field. If status.code is 0, the operation was successful. Otherwise, it contains an error code and message.
You can find the list of error codes in the ErrorCode class.
final response = KHQRGenerator.generateIndividual(...);
if (response.status.code != 0) {
print('Error Code: ${response.status.errorCode}');
print('Error Message: ${response.status.message}');
}
License #
MIT License
Copyright (c) 2025 Sophoun NHEUM
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.