Skip to content

ttwrpz/promptparse-rs

Repository files navigation

PromptParse (Rust)

Crates.io Documentation License: MIT

"All-in-one Rust library for PromptPay & EMVCo QR Codes"

No external dependencies & Cross-platform. You can use it anywhere!

This is a Rust port of maythiwat/promptparse

Features

  • Parse — PromptPay & EMVCo QR Code data strings into structs
  • Generate — QR Code data from pre-made templates (PromptPay AnyID, PromptPay Bill Payment, TrueMoney, etc.)
  • Manipulate — any values from parsed QR Code data and encode back into QR Code data
  • Validate — checksum and data structure for known QR Code formats (Slip Verify API Mini QR)

Installation

Add this to your Cargo.toml:

[dependencies]
promptparse = "1.0.1"

Usage

Parsing data and get value from tag

use promptparse::parse;

fn main() {
    // Example data
    let ppqr = parse("000201010211...", false, true).unwrap();

    // Get Value of Tag ID '00'
    let value = ppqr.get_tag_value("00", None).unwrap(); // Returns "01"
}

Build QR data and append CRC tag

use promptparse::{encode, tag, with_crc_tag};

fn main() {
    // Example data
    let data = vec![
        tag("00", "01"),
        tag("01", "11"),
        // ...
    ];

    // Set CRC Tag ID '63'
    let result = with_crc_tag(&encode(&data), "63", true); // Returns "000201010211..."
}

Generate PromptPay Bill Payment QR

use promptparse::generate::{bill_payment, BillPaymentConfig};

fn main() {
    let config = BillPaymentConfig {
        biller_id: "1xxxxxxxxxxxx".to_string(),
        amount: Some(300.0),
        ref1: "INV12345".to_string(),
        ref2: None,
        ref3: None,
    };

    let payload = bill_payment(config).unwrap();
    // TODO: Create QR Code from payload
}

Generate PromptPay AnyID QR

use promptparse::generate::{any_id, AnyIdConfig, ProxyType};

fn main() {
    let config = AnyIdConfig {
        proxy_type: ProxyType::Msisdn,
        target: "0812223333".to_string(),
        amount: Some(100.0),
    };

    let payload = any_id(config).unwrap();
    // TODO: Create QR Code from payload
}

Generate TrueMoney QR

use promptparse::generate::{true_money, TrueMoneyConfig};

fn main() {
    let config = TrueMoneyConfig {
        mobile_no: "08xxxxxxxx".to_string(),
        amount: Some(10.0),
        message: Some("Hello World!".to_string()),
    };

    let payload = true_money(config);
    // TODO: Create QR Code from payload
}

Validate & extract data from Slip Verify QR

use promptparse::validate;

fn main() {
    let data = validate::slip_verify("00550006000001...").unwrap();
    println!("Sending Bank: {}", data.sending_bank);
    println!("Transaction Ref: {}", data.trans_ref);

    // TODO: Inquiry transaction from Bank Open API
}

Convert BOT Barcode to PromptPay QR Tag 30 (Bill Payment)

use promptparse::parse_barcode;

fn main() {
    let bot_barcode = parse_barcode("|310109999999901\r...").unwrap();
    let payload = bot_barcode.to_qr_tag30().unwrap();

    // TODO: Create QR Code from payload
}

Error Handling

The library uses Result<T, PromptParseError> for operations that can fail:

use promptparse::{parse, PromptParseError};

fn main() {
    match parse("invalid_data", true, true) {
        Some(qr) => {
            println!("Parsed successfully: {:?}", qr);
        }
        None => {
            println!("Failed to parse QR code");
        }
    }
}

About This Port

This Rust implementation is a faithful port of the excellent promptparse TypeScript/JavaScript library by Maythiwat Chomchuen. The original library has been thoroughly tested and is widely used in the Thai payment ecosystem.

All credit for the original design, implementation, and testing goes to the original author. This port aims to bring the same reliability and functionality to Rust developers.

References

See Also

License

This project is MIT licensed (see LICENSE), maintaining the same license as the original work.