| Crates.io | prompt_pay |
| lib.rs | prompt_pay |
| version | 0.2.0 |
| created_at | 2024-12-12 08:43:50.926146+00 |
| updated_at | 2024-12-15 17:35:36.170521+00 |
| description | A utility crate for generating PromptPay payloads. |
| homepage | |
| repository | https://github.com/Siriphol-2000/prompt_pay_rust |
| max_upload_size | |
| id | 1481000 |
| size | 28,400 |
PromptPayUtils is a utility module for generating PromptPay Payloads compliant with Thailand's PromptPay system. It can be used to transfer money or make payments via registered phone numbers or national IDs in the PromptPay system.
PromptPayUtils operates as follows:
Phone Number and National ID Sanitization:
sanitize_phone_number function cleans the phone number by removing unnecessary characters (e.g., -, +) and ensures the phone number is exactly 9 digits long.sanitize_national_id function processes national IDs, removes hyphens, and ensures the ID is in the correct 13-digit format.Amount Conversion to Satangs:
Payload Creation:
generate_payload function combines the sanitized phone number or national ID with the formatted amount to create a PromptPay Payload. The payload also includes a CRC checksum to ensure data integrity.CRC-16 (XMODEM) Calculation:
calculate_precise_crc function computes the CRC-16 checksum using the XMODEM algorithm to protect the payload from tampering.Add PromptPayUtils to your Rust project by copying this code into the desired file or by including it as a dependency if it's developed into a library.
use promptpay_utils::{InputType, PromptPayUtils};
fn main() {
let phone_number = "+66-812345678".to_string();
let national_id = "1234567890123".to_string();
let amount = 123.45;
// Using phone number as input
match PromptPayUtils::generate_payload(InputType::PhoneNumber(phone_number), amount) {
Ok(payload) => println!("Payload (Phone): {}", payload),
Err(err) => eprintln!("Error: {}", err),
}
// Using national ID as input
match PromptPayUtils::generate_payload(InputType::NationalID(national_id), amount) {
Ok(payload) => println!("Payload (National ID): {}", payload),
Err(err) => eprintln!("Error: {}", err),
}
}