Skip to main content

Bill Validation

Parse and validate Iranian utility bills (قبض) including electricity, water, gas, and telecommunication bills.

Basic Usage

import { Bill } from "@persian-tools/persian-tools";

const bill = new Bill({
	billId: "1234567890123",
	paymentId: "1234567890123",
});

const result = bill.getResult();
console.log(result);
// {
//   isValid: true,
//   billType: 'برق',
//   amount: 50000,
//   ...
// }

API Reference

Constructor

options
object
required

Methods

getResult()

Get parsed bill information.
result
object

getBarcode()

Get the barcode representation of the bill.
result
string
Barcode string

Examples

Bill Payment Processing

import { Bill } from "@persian-tools/persian-tools";

const processBillPayment = (billId: string, paymentId: string) => {
	const bill = new Bill({ billId, paymentId });
	const result = bill.getResult();

	if (!result.isValid) {
		throw new Error("اطلاعات قبض نامعتبر است");
	}

	return {
		type: result.billType,
		amount: result.amount,
		barcode: bill.getBarcode(),
	};
};

Bill Validator

import { Bill } from "@persian-tools/persian-tools";

const validateBill = (billData: { billId: string; paymentId: string }) => {
	try {
		const bill = new Bill(billData);
		const result = bill.getResult();

		return {
			isValid: result.isValid,
			info:
				result.isValid ?
					{
						type: result.billType,
						amount: result.amount,
					}
				:	null,
		};
	} catch (error) {
		return { isValid: false, error: "خطا در پردازش قبض" };
	}
};

Use Cases

Process utility bill payments:
const payBill = (billId: string, paymentId: string) => {
  const bill = new Bill({ billId, paymentId });
  const result = bill.getResult();
  if (result.isValid) {
    // Process payment
  }
};
Scan and validate bills:
const scanBill = (barcode: string) => {
  // Parse barcode to get billId and paymentId
  const bill = new Bill({ billId, paymentId });
  return bill.getResult();
};

Type Definition

interface BillConstructor {
	billId: string;
	paymentId: string;
}

interface BillResult {
	isValid: boolean;
	billType?: string;
	amount?: number;
}

class Bill {
	constructor(options: BillConstructor);
	getResult(): BillResult;
	getBarcode(): string;
}