# qris-gen
[](https://crates.io/crates/qris-gen)
[](https://docs.rs/qris-gen)
[](https://opensource.org/licenses/MIT)
A Rust library for generating dynamic QRIS (Quick Response Code Indonesian Standard) with custom amounts and QR code image generation.
## Features
- ✅ Parse static QRIS code
- ✅ Convert static QRIS to dynamic QRIS
- ✅ Set custom transaction amount
- ✅ Generate QR code image (PNG format)
- ✅ Base64 encoding for QR code images
- ✅ Custom QR code size support
- ✅ CRC-16 checksum validation
- ✅ TLV (Tag-Length-Value) parsing
## Installation
Add this to your `Cargo.toml`:
```toml
[dependencies]
qris-gen = "0.1.0"
```
Or use cargo add:
```bash
cargo add qris-gen
```
## Usage
### Basic Example
```rust
use qris_gen::{Qris, generate_qr_base64};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Your static QRIS code
let static_qr = "00020101021126610014COM.GO-JEK.WWW01189360091430525747800210G0525747800303UMI51440014ID.CO.QRIS.WWW0215ID10264726320860303UMI5204549953033605802ID5920Titip Biyung, Grosir6008SURABAYA61056024562070703A016304091D";
// Generate dynamic QRIS with custom amount
let dynamic_qr = Qris::from_static(static_qr)?
.set_dynamic()
.amount(50000)? // Set amount to Rp 50.000
.build();
println!("Dynamic QRIS: {}", dynamic_qr);
Ok(())
}
```
### Generate QR Code Image
```rust
use qris_gen::{Qris, generate_qr_base64, generate_qr_base64_with_size};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let static_qr = "00020101021126610014COM.GO-JEK.WWW...";
let dynamic_qr = Qris::from_static(static_qr)?
.set_dynamic()
.amount(100000)?
.build();
// Generate QR code with default size
let qr_base64 = generate_qr_base64(&dynamic_qr)?;
println!("QR Code Base64: {}", qr_base64);
// Generate QR code with custom size (512x512)
let qr_base64_large = generate_qr_base64_with_size(&dynamic_qr, 512)?;
// Use in HTML
// <img src="data:image/png;base64,{qr_base64_large}" />
Ok(())
}
```
### Complete Example
```rust
use qris_gen::{Qris, generate_qr_base64_with_size, QrisError};
fn create_payment_qr(
static_qris: &str,
amount: u64
) -> Result<String, QrisError> {
// Convert to dynamic QRIS with amount
let dynamic_qris = Qris::from_static(static_qris)?
.set_dynamic()
.amount(amount)?
.build();
// Generate QR code image as base64
let qr_image = generate_qr_base64_with_size(&dynamic_qris, 512)?;
Ok(qr_image)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let static_qr = "00020101021126610014COM.GO-JEK.WWW...";
// Create payment QR for Rp 75.000
let payment_qr = create_payment_qr(static_qr, 75000)?;
println!("Payment QR Code generated successfully!");
println!("Base64 length: {} characters", payment_qr.len());
Ok(())
}
```
## API Documentation
### `Qris`
Main struct for handling QRIS operations.
#### Methods
- `from_static(qr: &str) -> Result<Self, QrisError>` - Parse a static QRIS string
- `set_dynamic(self) -> Self` - Convert to dynamic QRIS
- `amount(self, amount: u64) -> Result<Self, QrisError>` - Set transaction amount
- `build(self) -> String` - Build the final QRIS string with CRC checksum
### QR Code Generation
- `generate_qr_base64(data: &str) -> Result<String, QrisError>` - Generate QR code with default size
- `generate_qr_base64_with_size(data: &str, size: u32) -> Result<String, QrisError>` - Generate QR code with custom size
### Error Handling
```rust
use qris_gen::QrisError;
match Qris::from_static(invalid_qr) {
Ok(qris) => println!("Valid QRIS"),
Err(QrisError::InvalidFormat) => println!("Invalid QRIS format"),
Err(QrisError::MissingTag(tag)) => println!("Missing tag: {}", tag),
Err(QrisError::InvalidAmount) => println!("Invalid amount (must be > 0)"),
Err(e) => println!("Error: {}", e),
}
```
## QRIS Standard
This library implements the QRIS (Quick Response Code Indonesian Standard) specification:
- **Tag 01**: Point of Initiation Method (11 = static, 12 = dynamic)
- **Tag 54**: Transaction Amount
- **Tag 63**: CRC-16 Checksum
## Requirements
- Rust 1.56 or later
- Dependencies:
- `crc` - For CRC-16 checksum calculation
- `qrcode` - For QR code generation
- `image` - For image processing
- `base64` - For base64 encoding
- `thiserror` - For error handling
## Examples
Check the `examples/` directory for more usage examples:
```bash
cargo run --example basic
cargo run --example with_image
```
## Testing
Run tests with:
```bash
cargo test
```
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
1. Fork the repository
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
## Acknowledgments
- QRIS Standard by Bank Indonesia
- Indonesian payment ecosystem
## Disclaimer
This library is for educational and development purposes. Make sure to comply with all relevant regulations and standards when implementing payment systems.
---
Made with ❤️ for Indonesian developers