Bill class wraps the official Iranian bill verification algorithm. Given a billId + paymentId (or a combined 26-digit barcode), it can validate both check digits, classify the bill type (water, electricity, …), and compute the amount in toman or rial.
Construction
billId + paymentId or barcode to do real work.
Public methods
getResult() shape
Bill type encoding
The penultimate digit of the bill ID encodes the service:| Digit | BillTypes value |
|---|---|
| 1 | "آب" (water) |
| 2 | "برق" (electricity) |
| 3 | "گاز" (gas) |
| 4 | "تلفن ثابت" (landline) |
| 5 | "تلفن همراه" (mobile) |
| 6 | "عوارض شهرداری" (municipal) |
| 8 | "سازمان مالیات" (tax authority) |
| 9 | "جرایم راهنمایی و رانندگی" (traffic fines) |
| other | "unknown" (also causes verificationBillId() to return false) |
BillTypes union and the billTypes lookup map are exported.
Amount calculation
paymentId are control + scaling; the rest are the amount in 100s of toman or 1000s of rial. Picking the wrong currency is a 10× error — match it to your downstream domain.
Type exports
Pitfalls
- Result field is
type, notbillType. Older docs usebillType— those accesses returnundefined. - All constructor params are optional, but calling
getResult()on an emptyBillreturns garbage (NaN amount, “unknown” type, etc.). Guard upstream. unknownbill type forcesverificationBillId()to returnfalseeven if math checks out — intentional.- No Persian/Arabic digit normalization on the barcode — convert first if input may contain them.
Source
src/modules/bill/index.ts · Tests: test/bill.spec.ts