thai_promptpay 0.3.0
thai_promptpay: ^0.3.0 copied to clipboard
Pure-Dart PromptPay (Thai EMVCo QR) toolkit: build and parse the QR payload for mobile, National ID / Tax ID, e-Wallet and Bill Payment, with amount and CRC validation.
// A tour of thai_promptpay. Run with: dart run example/example.dart
import 'package:thai_promptpay/thai_promptpay.dart';
void main() {
// ── Encode ────────────────────────────────────────────────────────────────
// Mobile, static (no amount):
print(promptPayMobile('0812345678'));
// Mobile (formatted input is fine), 100.00 baht = 10000 satang → dynamic QR:
print(promptPayMobile('081-234-5678', amountSatang: 10000));
// 13-digit National ID / personal Tax ID (MOD-11 validated), 250.75 baht:
print(promptPayNationalId('1101700230708', amountSatang: 25075));
// 15-digit e-Wallet ID:
print(promptPayEWallet('004999000000001'));
// Render the returned string as a QR with any package (qr_flutter, qr, ...).
// ── Decode (verifies the CRC) ────────────────────────────────────────────
final p = decodePromptPay(promptPayMobile('0812345678', amountSatang: 5000));
print('${p.target.type.name} ${p.target.value} '
'${p.amountSatang} satang dynamic=${p.isDynamic}');
// mobile 0812345678 5000 satang dynamic=true
// Non-throwing: returns null on a bad payload / CRC mismatch.
print(tryDecodePromptPay('not a promptpay qr')); // null
// ── Bill Payment (EMVCo tag 30) ──────────────────────────────────────────
// The QR on invoices/utilities/tax forms: a Biller ID + Ref1/Ref2 (+amount).
final bill = encodeBillPayment(
billerId: '010553609264101',
ref1: '000002201649894',
ref2: 'INV0001',
amountSatang: 25075, // 250.75 baht
);
print(bill);
final b = decodeBillPayment(bill);
print('bill ${b.billerId} ref1=${b.ref1} ref2=${b.ref2} '
'${b.amountSatang} satang dynamic=${b.isDynamic}');
// decodeAny → a sealed union; the switch is exhaustive over both kinds.
switch (decodeAny(bill)) {
case PromptPayPayload p2:
print('personal: ${p2.target}');
case BillPaymentPayload b2:
print('bill payment to biller ${b2.billerId}');
}
// ── Slip Verify (Mini-QR) ────────────────────────────────────────────────
// The small QR on a transfer *slip* (a receipt-verification code, decoded
// offline). It is its own sealed SlipData type, not a payable QR.
final slip = decodeSlip(
'004100060000010103014022000111222233344ABCD125102TH910417DF',
);
switch (slip) {
case BankSlip s:
print('slip from ${s.bank?.nameEn} (${s.sendingBankCode}) '
'ref=${s.transRef} country=${s.countryCode}');
// slip from Siam Commercial Bank (014) ref=00111222233344ABCD12 country=TH
case TrueMoneySlip s:
print('truemoney ${s.eventType} ${s.transactionId} ${s.date}');
}
// Non-throwing variant + direct bank lookup:
print(tryDecodeSlip('not a slip')); // null
print(thaiBankByCode('002')?.nameTh); // ธนาคารกรุงเทพ
// CRC-16/CCITT-FALSE is exported too.
print(crc16ccitt('123456789').toRadixString(16)); // 29b1
}