-
Notifications
You must be signed in to change notification settings - Fork 10
/
sheba.dart
60 lines (53 loc) · 1.81 KB
/
sheba.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import 'package:persian_tools/src/constants/sheba/constants.dart';
import 'package:persian_tools/src/core/sheba/model.dart';
/// Takes Sheba code and gives information from it
class Sheba {
final String _shebaCode;
/// [RegExp] to checking validation of the sheba
final pattern = RegExp(shebaRegExp);
/// [RegExp] to finding bank code
final patternCode = RegExp(bankCodeRegExp);
Sheba(this._shebaCode);
// TODO:adding documentation
int _iso7064Mod97_10(String iban) {
String remainder = iban, block;
while (remainder.length > 2) {
// like slice in TS
try {
block = remainder.substring(0, 9);
} on RangeError catch (_) {
block = remainder;
}
remainder =
'${int.parse(block) % 97}' + remainder.substring(block.length);
}
return int.parse(remainder) % 97;
}
/// Checks validation of the sheba
bool get isValid {
final shebaCode = _shebaCode;
if (shebaCode.length != 26) return false;
if (!pattern.hasMatch(shebaCode)) return false;
final d1 = shebaCode.codeUnitAt(0) - 65 + 10;
final d2 = shebaCode.codeUnitAt(1) - 65 + 10;
var iban = shebaCode.substring(4);
iban += '$d1$d2${shebaCode.substring(2, 4)}';
final remainder = _iso7064Mod97_10(iban);
return remainder != 1 ? false : true;
}
/// Returns all information from sheba code
BankInformation? call() {
final shebaCode = _shebaCode;
if (!isValid) return null;
final bankCode = patternCode.firstMatch(shebaCode)?[1] ?? '';
var bank = banksInfo[bankCode];
if (bank == null) return null;
if (bank.isAccountNumberAvailable) {
final data = bank.process!(shebaCode);
bank.accountNumber = data.accountNumber;
bank.formattedAccountNumber = data.formattedAccountNumber;
}
bank.process = null;
return bank;
}
}