diff --git a/handler/app.py b/handler/app.py index 4318f1c..a07b1fa 100755 --- a/handler/app.py +++ b/handler/app.py @@ -15,6 +15,16 @@ from revolut import Revolut from transactions import Transactions +def convert(data): + 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 WebApp(object): """ Our Klein webapp. @@ -35,10 +45,24 @@ class WebApp(object): except JSONDecodeError: self.log.error("Failed to parse JSON callback: {content}", content=content) return dumps({"success": False}) - self.log.info("Callback received: {parsed}", parsed=parsed) + self.log.info("Callback received: {parsed}", parsed=parsed["data"]["id"]) self.tx.transaction(parsed) return dumps({"success": True}) + @app.route("/find//") + def find(self, request, reference, amount): + try: + int(amount) + except ValueError: + return dumps({"success": False, "msg": "Amount is not an integer"}) + rtrn = self.tx.find_tx(reference, amount) + if rtrn == "AMOUNT_INVALID": + return dumps({"success": False, "msg": "Reference found but amount invalid"}) + elif not rtrn: + return dumps({"success": False, "msg": "Reference not found"}) + else: + return dumps(convert(rtrn)) + def start(handler): """ diff --git a/handler/transactions.py b/handler/transactions.py index 938b8f3..09fb8df 100644 --- a/handler/transactions.py +++ b/handler/transactions.py @@ -55,5 +55,16 @@ class Transactions(object): self.log.info("Transaction processed: {formatted}", formatted=dumps(to_store, indent=2)) r.hmset(f"tx.{txid}", to_store) - def find_tx(self, amount, reference): - pass + def find_tx(self, reference, amount): + all_transactions = r.scan(0, match="tx.*") + print(f"ALL {all_transactions}") + for tx_iter in all_transactions[1]: + tx_obj = r.hgetall(tx_iter) + print(f"ITER {tx_iter}") + print(f"OBJ {tx_obj}") + if tx_obj[b"reference"] == str.encode(reference): + if tx_obj[b"amount"] == str.encode(amount): + return tx_obj + else: + return "AMOUNT_INVALID" + return False