# Twisted/Klein imports from twisted.logger import Logger # Other library imports from json import dumps # Project imports from db import r def convert(data): """ Recursively convert a dictionary. """ if isinstance(data, bytes): return data.decode("ascii") if isinstance(data, dict): return dict(map(convert, data.items())) if isinstance(data, tuple): return map(convert, data) return data class Transactions(object): """ Handler class for incoming Revolut transactions. """ def __init__(self): """ Initialise the Transaction object. Set the logger. """ self.log = Logger("transactions") def set_agora(self, agora): self.agora = agora def set_irc(self, irc): self.irc = irc def transaction(self, data): """ Store details of transaction. :param data: details of transaction :type data: dict """ event = data["event"] ts = data["timestamp"] inside = data["data"] txid = inside["id"] txtype = inside["type"] state = inside["state"] reference = inside["reference"] leg = inside["legs"][0] account_type = leg["counterparty"]["account_type"] amount = leg["amount"] currency = leg["currency"] description = leg["description"] to_store = { "event": event, "ts": ts, "txid": txid, "txtype": txtype, "state": state, "reference": reference, "account_type": account_type, "amount": amount, "currency": currency, "description": description, } self.log.info("Transaction processed: {formatted}", formatted=dumps(to_store, indent=2)) r.hmset(f"tx.{txid}", to_store) self.irc.client.msg( self.irc.client.channel, f"AUTO Incoming transaction: {amount}{currency} ({reference}) - {state} - {description}" ) def find_tx(self, reference, amount): """ Find transactions that match the given reference and amount. :param reference: transaction reference in Revolut :param amount: transaction amount :type reference: string :type amount: int :return: transaction details or AMOUNT_INVALID, or False :rtype: dict or string or bool """ all_transactions = r.scan(0, match="tx.*") for tx_iter in all_transactions[1]: tx_obj = r.hgetall(tx_iter) if tx_obj[b"reference"] == str.encode(reference): if tx_obj[b"amount"] == str.encode(amount): return convert(tx_obj) else: return "AMOUNT_INVALID" return False