You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.5 KiB
Python

# 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):
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.
"""
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"]
account_id = leg["counterparty"]["account_id"]
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,
"account_id": account_id,
"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)
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
:return: transaction details or AMOUNT_INVALID, or False
"""
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