Skip to content

Instantly share code, notes, and snippets.

@darkyen

darkyen/javascript

Created Jan 31, 2015
Embed
What would you like to do?
var banks = {
SBI: 'SBI',
HDFC: 'HDFC'
};
var transactionTypes = {
MONEY_TRANSFER: 'MONEY_TRANSFER',
CARD: 'CARD',
NETBANKING: 'NETBANKING'
};
var utils = {
HDFC : {
toNumberFomINRFormat(str){
str = str.replace(/,/g, '');
return parseFloat(str, 10);
}
}
};
var subTypes = {
CARD: {
DEBIT: 'DEBIT',
CREDIT: 'CREDIT'
},
MONEY_TRANSFER: {
NEFT: 'NEFT',
ITGS: 'ITGS',
ATM : 'ATM'
},
NETBANKING: {
NETBANKING: 'NETBANKING'
}
};
class TransactionConverter{
_extractSBIDebitInfo(body){
var cardNumberRegExp = /Card ([0-9X]+ for)/;
var transAmountRegExp = /worth Rs([0-9,]+) on/;
var altTransAmountRegExp = /for Rs([0-9,]+) on/;
var handlerRegExp = /at ([A-Za-z0-9_.\/\*\? ]+) txn/;
var extractedAmount = body.match(transAmountRegExp);
if( extractedAmount === null ){
extractedAmount = body.match(altTransAmountRegExp);
}
return {
transactionTo : body.match(handlerRegExp)[1],
amount : utils.HDFC.toNumberFomINRFormat( extractedAmount[1] ),
cardNumberId : body.match(cardNumberRegExp)[1],
transactionType : transactionTypes.CARD,
transactionSubType : subTypes.CARD.DEBIT,
bank : banks.SBI
};
}
_extractSBINetBankingInfo(body){
var transAmountRegExp = /for Rs.([0-9,]+\.[0-9]+) on/;
var altTransAmountRegExp = /for Rs.([0-9,]+) on/;
var dateTimeRegExp = /on ([0-9\/: ]+)/;
var amount = body.match(transAmountRegExp);
if( amount === null ){
amount = body.match(altTransAmountRegExp);
}
return {
amount : utils.HDFC.toNumberFomINRFormat( amount[1] ),
timestamp : new Date(body.match(dateTimeRegExp)[1]),
accountNumberId : 'self',
transactionType : transactionTypes.NETBANKING,
transactionSubType : subTypes.NETBANKING.NETBANKING,
bank : banks.SBI
};
}
_extractSBIATMInfo(body){
var transAmountRegExp = /Rs([0-9]+) withdrawn/;
var transAccountNumber = /A\/c ([0-9x]+) on/;
return {
amount : utils.HDFC.toNumberFomINRFormat( body.match(transAmountRegExp)[1] ),
accountNumberId : body.match(transAccountNumber)[1],
transactionType : transactionTypes.MONEY_TRANSFER,
transactionSubType : subTypes.MONEY_TRANSFER.ATM,
bank : banks.SBI
};
}
_extractSBIInfo(message){
var lowerBody = message.body.toLowerCase();
console.log(message.body);
if( lowerBody.indexOf("pleasure to serve") !== -1 ){
return null;
}else if( lowerBody.indexOf("withdrawn at") !== -1 ){
return this._extractSBIATMInfo(message.body);
}else if( lowerBody.indexOf("state bank internet banking") !== -1 ){
return this._extractSBINetBankingInfo(message.body);
}else if( lowerBody.indexOf("sbi debit card") !== -1 ){
return this._extractSBIDebitInfo(message.body);
}
return null;
}
_extractHDFCDebitInfo(message){
var cardNumberRegExp = /ending (\d+)/;
var transAmountRegExp = /for Rs.([0-9,]+\.[0-9]+) in/;
var altTransAmountRegExp = /for Rs. ([0-9,]+\.[0-9]+) towards/;
var handlerRegExp = /at ([A-Za-z0-9_.\*\? ]+) on/;
var dateTimeRegExp = /on ([0-9\-:]+)/;
var extractedAmount = message.body.match(transAmountRegExp);
if( extractedAmount === null ){
extractedAmount = message.body.match(altTransAmountRegExp);
}
return {
timestamp : new Date(message.body.match(dateTimeRegExp)[1]),
transactionTo : message.body.match(handlerRegExp)[1],
amount : utils.HDFC.toNumberFomINRFormat( extractedAmount[1] ),
cardNumberId : message.body.match(cardNumberRegExp)[1],
transactionType : transactionTypes.CARD,
transactionSubType : subTypes.CARD.DEBIT,
bank : banks.HDFC
};
}
_extractHDFCNetBankingInfo(body){
var transAmountRegExp = /of Rs.([0-9,]+\.[0-9]+) has/;
var transAccountNumber = /number ([0-9X]+) for/;
console.log(body);
return {
amount : utils.HDFC.toNumberFomINRFormat( body.match(transAmountRegExp)[1] ),
bank : banks.HDFC,
accountNumberId : body.match(transAccountNumber)[1]
};
}
_extractHDFCNeftInfo(message){
return _.assign(this._extractHDFCNetBankingInfo(message.body),{
transactionType : transactionTypes.MONEY_TRANSFER,
transactionSubType : subTypes.MONEY_TRANSFER.NEFT
});
}
_extractHDFCOnlineTransactionInfo(message){
return _.assign(this._extractHDFCNetBankingInfo(message.body),{
transactionType : transactionTypes.NETBANKING,
transactionSubType : subTypes.NETBANKING.NETBANKING
});
}
_extractHDFCInfo(message){
var lowerBody = message.body.toLowerCase();
console.log(message);
if( lowerBody.indexOf("online payment txn") !== -1 ){
return this._extractHDFCOnlineTransactionInfo(message);
}if( lowerBody.indexOf("neft txn") !== -1 ){
return this._extractHDFCNeftInfo(message);
}else if( lowerBody.indexOf("debit/atm") !== -1 ){
return this._extractHDFCDebitInfo(message);
}
return null;
}
convertMessageToTransaction(message){
var bank = '', info = null;
console.log("probing new message");
if( message.address.toLowerCase().indexOf("sbi") != -1 ){
console.log("Found an SBI message");
bank = banks.SBI;
}else if( message.address.toLowerCase().indexOf("hdfc") != -1){
console.log("Found an HDFC Bank Message");
bank = banks.HDFC;
}
switch(bank){
case banks.SBI:
console.log("Extracting Info from SBI");
info = this._extractSBIInfo(message);
break;
case banks.HDFC:
console.log("Extracting info from HDFC");
info = this._extractHDFCInfo(message);
break;
default:
console.log("Unknown bank");
}
if( info ){
info = _.assign(info,{
messageTimestamp : new Date(+message.date),
hashed : sha256(JSON.stringify(message))
});
}
console.log("Returning Object", info);
return info;
}
convertMessagesToTransactions(smsArray){
console.log("Beginning to process", smsArray.length, "messages");
var transactions = smsArray.map(this.convertMessageToTransaction.bind(this))
.filter(x => !!x);
console.log("Processed ", smsArray.length, "messages and found", transactions.length, "transactions");
return transactions;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment