khqr 0.1.0

Unofficial Rust SDK for Bakong KHQR
Documentation

Bakong KHQR SDK

Crates.io Docs License: MIT

Unofficial Rust SDK for Bakong KHQR

Features

  • QR Code Generation (Individual & Merchant accounts)
  • QR Code Decoding
  • CRC16 Verification
  • Bakong API Integration
  • Support for KHR and USD currencies

Installation

Add to your Cargo.toml:

[dependencies]
bakong-khqr = "0.1.0"

Quick Start

use bakong_khqr::{BakongKHQR, IndividualInfo};

let khqr = BakongKHQR::new("your_token");

let info = IndividualInfo::builder()
    .bakong_account_id("user@bank")
    .merchant_name("Coffee Shop")
    .merchant_city("Phnom Penh")
    .amount(10000.0)
    .build()
    .unwrap();

let result = khqr.generate_qr(info).unwrap();
println!("QR: {}", result.qr);
println!("MD5: {}", result.md5);

Examples

Run the examples:

# QR Generation
cargo run --example generate_qr --features rustls-tls

# QR Verification
cargo run --example verify_qr --features rustls-tls

# QR Decoding
cargo run --example decode_qr --features rustls-tls

# API - Check Account (requires token)
BAKONG_TOKEN=your_token cargo run --example api_check_account --features rustls-tls

# API - Check Transaction (requires token)
BAKONG_TOKEN=your_token cargo run --example api_check_transaction --features rustls-tls

API Reference

QR Generation

// Individual QR
let info = IndividualInfo::builder()
    .bakong_account_id("user@bank")
    .merchant_name("Shop Name")
    .merchant_city("Phnom Penh")
    .currency("KHR")  // or "USD"
    .amount(10000.0)  // optional, omit for static QR
    .build()
    .unwrap();

let result = khqr.generate_qr(info).unwrap();

// Merchant QR
let info = MerchantInfo::builder()
    .bakong_account_id("merchant@bank")
    .merchant_id("MERCHANT001")
    .acquiring_bank("ABA Bank")
    .merchant_name("Store Name")
    .amount(50.00)
    .build()
    .unwrap();

let result = khqr.generate_merchant_qr(info).unwrap();

QR Verification

use bakong_khqr::{KHQRDecoder, verify_crc};

// Quick verification
let (is_valid, expected, actual) = verify_crc(qr_string);

// Detailed verification
let result = KHQRDecoder::verify(qr_string).unwrap();
if result.is_valid {
    println!("Valid QR code");
}

QR Decoding

use bakong_khqr::KHQRDecoder;

let decoded = KHQRDecoder::decode(qr_string).unwrap();
println!("Merchant: {}", decoded.merchant_name);
println!("Amount: {:?}", decoded.amount);
println!("Currency: {}", decoded.currency);

API Methods

// Check if Bakong account exists
let response = khqr.check_bakong_account("user@bank").await?;

// Check transaction by MD5
let response = khqr.check_transaction_by_md5(md5_hash).await?;

// Generate payment deeplink
let response = khqr.generate_deeplink(
    qr_string,
    SourceInfo {
        app_name: "My App".to_string(),
        app_icon_url: None,
        app_deep_link_callback: None,
    },
).await?;

Configuration

Sandbox (Default)

let khqr = BakongKHQR::new("sandbox_token");

Production

use bakong_khqr::BakongConfig;

let khqr = BakongKHQR::with_config(
    BakongConfig::production("production_token")
);

Custom Base URL

let khqr = BakongKHQR::with_config(
    BakongConfig::sandbox("token")
        .with_base_url("http://localhost:8080")
);

Environment

Environment URL
Sandbox https://sit-api-bakong.nbc.gov.kh
Production https://api-bakong.nbc.gov.kh

Requirements

Documentation

License

MIT License - see LICENSE for details.

Contributing

Contributions welcome! Please open an issue or PR on GitHub