upi_sms_parser 0.1.0
upi_sms_parser: ^0.1.0 copied to clipboard
A pure-Dart library for parsing Indian UPI transaction SMS messages. Extracts amount, merchant, UPI reference, account number, balance, transaction type, and timestamp from bank SMS. Includes a multi- [...]
// Example usage of the upi_sms_parser package.
//
// This file is shown on pub.dev's "Example" tab and is the first thing
// many developers will read to understand how to use the package — keep
// it runnable end-to-end with `dart run example/example.dart`.
import 'package:upi_sms_parser/upi_sms_parser.dart';
void main() {
final parser = UpiParser();
// ── Example 1: Parse a single SMS ─────────────────────────────────────
//
// `parser.parse(body, address)` runs the SMS through the filter first
// (rejecting OTPs/promos/etc.) and only then extracts fields — so a
// non-null result here means "this is a real transaction, and here's
// what we could read out of it".
const swiggySms = 'Payment Confirmed! Your Swiggy Order '
'#236190425176316 of Rs. 356.0 was successfully '
'paid on 26/04/2026 at 10:38 PM. '
'Txn ID: 260298464000257. Enjoy your order!';
final result = parser.parse(swiggySms, 'TX-SWIGGY-S');
if (result != null) {
print('Amount: ₹${result.amount}');
print('Type: ${result.type.name}');
print('Merchant: ${result.merchant}');
print('UPI Ref: ${result.upiRef}');
print('Date: ${result.timestamp}');
}
// ── Example 2: Quick filter check, without parsing ────────────────────
//
// Use `isUpiTransaction` when you just need a fast yes/no — e.g.
// deciding whether an incoming SMS is even worth storing — without
// paying for the full field-extraction pass.
const otpSms = 'Your OTP is 123456. Do not share.';
print(parser.isUpiTransaction(otpSms, 'TX-HDFC'));
// Output: false
// ── Example 3: Parse a whole inbox in bulk ────────────────────────────
//
// `parseAll` takes a list of {'body': ..., 'address': ...} maps —
// exactly the shape you'd get back from most SMS-reading plugins after
// a light `.map()` — and silently drops anything that isn't a genuine,
// fully-parseable UPI transaction.
final inbox = [
{'body': swiggySms, 'address': 'TX-SWIGGY-S'},
{'body': otpSms, 'address': 'TX-HDFC'},
{
'body': 'Dear UPI user A/C X6299 debited by 58.00 '
'on date 03Apr26 trf to Suresh Setiya '
'Refno 645955771455-SBI',
'address': 'JK-SBIUPI-S',
},
];
final transactions = parser.parseAll(inbox);
print('Found ${transactions.length} UPI transactions');
// Output: Found 2 UPI transactions
for (final t in transactions) {
print(' ₹${t.amount} - ${t.merchant ?? 'Unknown'} - ${t.type.name}');
}
}