├── .gitignore ├── LICENSE ├── README.md ├── billify.js ├── billify.min.js ├── package.json └── tests ├── index.html └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Ali Master 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Iranian Bill Detector 2 | bill.js exposes a simple API for detect bill information. 3 | 4 | [![npm version](https://img.shields.io/npm/v/npm.svg?style=flat-square)](https://www.npmjs.com/package/billify) 5 | [![npm](https://img.shields.io/npm/l/express.svg?style=flat-square)](https://www.npmjs.com/package/billify) 6 | [![npm downloads](https://img.shields.io/npm/dt/express.svg?style=flat-square)](https://www.npmjs.com/package/billify) 7 | [![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg?style=flat-square)](https://gitter.im/billify/Lobby?utm_source=share-link&utm_medium=link&utm_campaign=share-link) 8 | 9 | ### Features 10 | - Detect Bill information by `bill Id` and `bill Payment` 11 | - Detect Bill information by `bill Barcode` 12 | 13 | ### Usage 14 | - *Import This package into you'r project* 15 | ```javascript 16 | // ES6 17 | import billify from "billify"; 18 | 19 | // RequireJS 20 | var billify = require('billify'); 21 | 22 | // Define 23 | define(['billify'], function(){ 24 | // some you'r code... 25 | }); 26 | ``` 27 | 28 | ### Callback functions 29 | ```javascript 30 | // set bill id in prototype function of billify 31 | billify.setId(8887858300146) 32 | 33 | // set bill payment in prototype function of billify 34 | billify.setPeymentId(51350244) 35 | 36 | // set billId and billPaymentId in constructor of billify 37 | billify(8887858300146, 51350244); 38 | 39 | // set config 40 | billify.setConfig({ 41 | // Rial OR Toman 42 | currency: "Rial", 43 | // Farsi OR English 44 | lang: "en", // value just: fa or en 45 | // convert numbers from english to farsi and reverse 46 | EnToFa: true, // value just: true or false 47 | }); 48 | 49 | // If You want to find Bill Information from Barcode, you can use this fuature 50 | // bill set Barcode with 26 length 51 | billify.setBarcode(88878583001460000051650244); 52 | // and use this prototype for find and get bill information 53 | billify.findByBarcode(); 54 | 55 | // bill get amount 56 | billify.getBillAmount(); 57 | 58 | // bill get type 59 | billify.getBillType(); 60 | 61 | // bill get Barcode 62 | billify.getBarcode(); 63 | 64 | // verification Bill Id 65 | // return true or false 66 | billify.verificationBillId(); 67 | 68 | // verification Bill Payment 69 | // return true or false 70 | billify.verificationBillPayment(); 71 | 72 | // verification bill[id and payment] 73 | billify.verificationBill(); 74 | 75 | // get all data of bill 76 | // return object 77 | billify.getData(); 78 | ``` 79 | 80 | ### Installation 81 | ##### Method One: 82 | Download and extract the [latest pre-built release](https://github.com/ali-master/billify/releases). 83 | 84 | Just grab billify.min.js and include them with a script tag. 85 | ```javascript 86 | 87 | 98 | ``` 99 | 100 | ##### Method Two: 101 | installation with NPM 102 | ```sh 103 | $ npm install billify 104 | ``` 105 | 106 | ### Contributors & Forks 107 | > Contributors: [https://github.com/ali-master/billify/graphs/contributors](https://github.com/ali-master/billify/graphs/contributors) 108 | 109 | > Forks: [https://github.com/ali-master/billify/network/members](https://github.com/ali-master/billify/network/members) 110 | 111 | You can try out the Node package online at [tonicdev](https://runkit.com/alimaster/billify) 112 | -------------------------------------------------------------------------------- /billify.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @return object 3 | * @ref http://www.chargereseller.com/files/chargereseller-bill-validation-document.pdf 4 | */ 5 | "use strict" 6 | // billify.js 7 | var billify = function () { 8 | this._barcode = null; 9 | this._currency = "toman"; 10 | this._lang = "en"; 11 | this._EnToFa = false; 12 | this._billTypes = { 13 | "en": { 14 | 1: "water", 15 | 2: "power", 16 | 3: "gas", 17 | 4: "telphone", 18 | 5: "mobile", 19 | 6: "municipality", 20 | }, 21 | "fa": { 22 | 1: "آب", 23 | 2: "برق", 24 | 3: "گاز", 25 | 4: "تلفن", 26 | 5: "تلفن همراه", 27 | 6: "شهرداری", 28 | } 29 | } 30 | 31 | if(arguments[0] && arguments[1]){ 32 | this.setId(arguments[0]); 33 | this.setPeymentId(arguments[1]); 34 | } 35 | }; 36 | 37 | billify.prototype.version = "1.2.6"; 38 | billify.prototype.setConfig = function(options) { 39 | var options = options || {}; 40 | 41 | this._currency = options.currency.toLocaleLowerCase() == "rial" ? "rial" : "toman"; 42 | this._lang = options.lang.toLocaleLowerCase() == "fa" ? "fa" : "en"; 43 | this._EnToFa = options.EnToFa == true ? true : false; 44 | }; 45 | billify.prototype.setId = function(billId) { 46 | var billId = billId || null; 47 | this._billId = billId; 48 | }; 49 | billify.prototype.setPeymentId = function(billPayment) { 50 | var billPayment = billPayment || null; 51 | this._billPayment = billPayment; 52 | }; 53 | billify.prototype.setPeymentId = function(billPayment) { 54 | var billPayment = billPayment || null; 55 | this._billPayment = billPayment; 56 | }; 57 | billify.prototype.setBarcode = function(barcode) { 58 | var barcode = barcode || null; 59 | this._barcode = barcode; 60 | }; 61 | 62 | billify.prototype.getBillAmount = function() { 63 | var currency = (this._currency == "rial") ? 1000 : 100; 64 | var amount = parseInt(this._billPayment.toString().slice(0, -5)) * currency; 65 | 66 | return (this._EnToFa == true ? this._digitsEn2Fa(amount) : amount) || "undefined"; 67 | } 68 | billify.prototype.getBillType = function() { 69 | var billLangType = (this._lang == "fa") ? this._billTypes.fa : this._billTypes.en; 70 | return billLangType[this._billId.toString().slice(-2, -1)] || "undefined"; 71 | }; 72 | 73 | billify.prototype.getBarcode = function() { 74 | var width = 13; 75 | var padding = "0"; 76 | 77 | var billPayment = this._billPayment.toString(); 78 | var lpadBillPayment = billPayment; 79 | 80 | if (billPayment.length < width) lpadBillPayment = padding.repeat(width - billPayment.length) + billPayment; 81 | 82 | return (this._billId + "" + lpadBillPayment) || "undefined"; 83 | }; 84 | billify.prototype.findByBarcode = function() { 85 | if(this._barcode != null) { 86 | this._billId = this._barcode.substr(0, 13); 87 | this._billPayment = parseInt(this._barcode.split(this._billId)[1]); 88 | } 89 | }; 90 | 91 | billify.prototype.verificationBillPayment = function() { 92 | return this._verification(this._billPayment.toString().slice(0, -2), this._billPayment.toString().substr(-2, 1)); 93 | }; 94 | billify.prototype.verificationBillId = function() { 95 | return this._verification(this._billId.toString().slice(0, -1), this._billId.toString().slice(-1)); 96 | }; 97 | billify.prototype.verificationBill = function() { 98 | if(this.verificationBillPayment() == true && this.verificationBillId() == true) 99 | return true; 100 | else 101 | return false; 102 | }; 103 | billify.prototype._verification = function(sum, checkId) { 104 | var totalSum = 0; 105 | var status = 0; 106 | var sum = sum || null; 107 | var checkId = checkId || null; 108 | 109 | var stringToArray = sum.split('').reverse().join('').split(''); 110 | 111 | stringToArray.forEach(function(value, index){ 112 | totalSum += (2 + index % 6) * parseInt(value); 113 | }); 114 | 115 | var modify = totalSum % 11; 116 | 117 | status = (modify < 2) ? 0 : 11 - modify; 118 | return (status == parseInt(checkId)) ? true : false; 119 | }; 120 | billify.prototype._digitsEn2Fa = function(number) { 121 | return number.toString().replace(/\d/g, function(dist){ 122 | return String.fromCharCode(dist.charCodeAt(0) + 1728); 123 | }); 124 | }; 125 | billify.prototype.getData = function() { 126 | return { 127 | // مبلغ قبض 128 | "amount": (this._EnToFa == true) ? this._digitsEn2Fa(this.getBillAmount()) : this.getBillAmount(), 129 | 130 | // نوع قبض 131 | "type": this.getBillType(), 132 | 133 | // بارکد قبض 134 | "barcode": (this._EnToFa == true) ? this._digitsEn2Fa(this.getBarcode()) : this.getBarcode(), 135 | 136 | // صحت ارتباط شناسه قبض و پرداخت 137 | "validationBill": this.verificationBill(), 138 | 139 | // صحت شناسه قبض 140 | "verificationBillId": this.verificationBillId(), 141 | 142 | // صحت شناسه پرداخت 143 | "verificationBillPayment": this.verificationBillPayment(), 144 | 145 | "configs": { 146 | // واحد پولی 147 | "currency": this._currency, 148 | "lang": this._lang, 149 | "EnToFa": this._EnToFa 150 | } 151 | }; 152 | } 153 | 154 | if (typeof module != 'undefined' && module.exports && this.module !== module) { 155 | module.exports = billify; 156 | }else if (typeof define === 'function' && define.amd) { 157 | define(billify); 158 | } 159 | -------------------------------------------------------------------------------- /billify.min.js: -------------------------------------------------------------------------------- 1 | "use strict";var billify=function(){this._barcode=null,this._currency="toman",this._lang="en",this._EnToFa=!1,this._billTypes={en:{1:"water",2:"power",3:"gas",4:"telphone",5:"mobile",6:"municipality"},fa:{1:"آب",2:"برق",3:"گاز",4:"تلفن",5:"تلفن همراه",6:"شهرداری"}},arguments[0]&&arguments[1]&&(this.setId(arguments[0]),this.setPeymentId(arguments[1]))};billify.prototype.version="1.2.6",billify.prototype.setConfig=function(i){var i=i||{};this._currency="rial"==i.currency.toLocaleLowerCase()?"rial":"toman",this._lang="fa"==i.lang.toLocaleLowerCase()?"fa":"en",this._EnToFa=1==i.EnToFa?!0:!1},billify.prototype.setId=function(i){var i=i||null;this._billId=i},billify.prototype.setPeymentId=function(i){var i=i||null;this._billPayment=i},billify.prototype.setPeymentId=function(i){var i=i||null;this._billPayment=i},billify.prototype.setBarcode=function(i){var i=i||null;this._barcode=i},billify.prototype.getBillAmount=function(){var i="rial"==this._currency?1e3:100,t=parseInt(this._billPayment.toString().slice(0,-5))*i;return(1==this._EnToFa?this._digitsEn2Fa(t):t)||"undefined"},billify.prototype.getBillType=function(){var i="fa"==this._lang?this._billTypes.fa:this._billTypes.en;return i[this._billId.toString().slice(-2,-1)]||"undefined"},billify.prototype.getBarcode=function(){var i=13,t="0",e=this._billPayment.toString(),n=e;return e.lengtho?0:11-o,n==parseInt(t)?!0:!1},billify.prototype._digitsEn2Fa=function(i){return i.toString().replace(/\d/g,function(i){return String.fromCharCode(i.charCodeAt(0)+1728)})},billify.prototype.getData=function(){return{amount:1==this._EnToFa?this._digitsEn2Fa(this.getBillAmount()):this.getBillAmount(),type:this.getBillType(),barcode:1==this._EnToFa?this._digitsEn2Fa(this.getBarcode()):this.getBarcode(),validationBill:this.verificationBill(),verificationBillId:this.verificationBillId(),verificationBillPayment:this.verificationBillPayment(),configs:{currency:this._currency,lang:this._lang,EnToFa:this._EnToFa}}},"undefined"!=typeof module&&module.exports&&this.module!==module?module.exports=billify:"function"==typeof define&&define.amd&&define(billify); 2 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "billify", 3 | "version": "1.2.6", 4 | "description": "Iranian Bill Detector", 5 | "main": "billify.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+https://github.com/ali-master/billify.git" 12 | }, 13 | "bugs": { 14 | "url": "https://github.com/ali-master/billify/issues" 15 | }, 16 | "homepage": "https://github.com/ali-master/billify#readme", 17 | "keywords": [ 18 | "billify", 19 | "bill" 20 | ], 21 | "author": "Ali Torki", 22 | "license": "MIT", 23 | "devDependencies": {} 24 | } 25 | -------------------------------------------------------------------------------- /tests/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | billify - unit test 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 |
16 | 17 | 18 | -------------------------------------------------------------------------------- /tests/test.js: -------------------------------------------------------------------------------- 1 | QUnit.test("billify", function(assert) { 2 | function billTest(billId, billPayment, expected) { 3 | var bill = new billify(); 4 | bill.setId(billId) 5 | bill.setPeymentId(billPayment) 6 | assert.equal(bill.getData().amount, expected); 7 | } 8 | billTest(2813823309056, 25143031, 25100); 9 | billTest(2813823309056, 18143115, 18100); 10 | billTest(2813823309056, 38743208, 38700); 11 | billTest(2813823309056, 74952182, 74900); 12 | billTest(2813823309056, 11652243, undefined); 13 | billTest(7106329993556, 78850759, 78800); 14 | }); 15 | --------------------------------------------------------------------------------