From bd7134e051a9ec679c42d153327fb8ae45208671 Mon Sep 17 00:00:00 2001 From: F-Node-Karlsruhe Date: Sun, 27 Jun 2021 21:50:00 +0200 Subject: [PATCH 1/3] implement manual payment check --- app.py | 8 +++++++ iota.py | 55 ++++++++++++++++++++++++++++++++++++++++------ templates/pay.html | 15 ++++++++++++- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/app.py b/app.py index e519628..153bbdc 100644 --- a/app.py +++ b/app.py @@ -128,6 +128,14 @@ def await_payment(data): iota_listener.socket_session_ids[user_token_hash] = request.sid +# socket endpoint for manual check on payment +@socketio.on('check_payment') +def await_payment(data): + + socketio.start_background_task(iota_listener.manual_payment_check, data['iota_address'], data['user_token_hash']) + + + if __name__ == '__main__': diff --git a/iota.py b/iota.py index fb78b19..d656a60 100644 --- a/iota.py +++ b/iota.py @@ -41,6 +41,9 @@ def __init__(self, socketio): # link user_token_hashes to session ids self.socket_session_ids = {} + # manual payment checks + self.manual_pamyent_checks = set() + # create the iota client self.client = iota_client.Client(nodes_name_password=[[node_url]], mqtt_broker_options=broker_options) @@ -79,16 +82,13 @@ def mqtt_worker(self): message = self.client.get_message_data(json.loads(event['payload'])['messageId']) - if self.check_payment(message): + if self.payment_valid(message): # this must be easier to access within value transfers user_token_hash = bytes(message['payload']['transaction'][0]['essence']['payload']['indexation'][0]['data']).decode() - add_to_paid_db(user_token_hash, session_lifetime) - - if user_token_hash in self.socket_session_ids.keys(): + self.unlock_content(user_token_hash) - # emit pamyent received event to the user - self.socketio.emit('payment_received', room=self.socket_session_ids.pop(user_token_hash)) + except Exception as e: LOG.warning(e) @@ -96,7 +96,18 @@ def mqtt_worker(self): self.q.task_done() - def check_payment(self, message): + def unlock_content(self, user_token_hash): + + add_to_paid_db(user_token_hash, session_lifetime) + + # prevent key errors + if user_token_hash in self.socket_session_ids.keys(): + + # emit pamyent received event to the user + self.socketio.emit('payment_received', room=self.socket_session_ids.pop(user_token_hash)) + + + def payment_valid(self, message): for output in message['payload']['transaction'][0]['essence']['outputs']: @@ -108,6 +119,36 @@ def check_payment(self, message): return False + + def manual_payment_check(self, address, user_token_hash): + + # easy checks first to prevent overload + if user_token_hash not in self.manual_pamyent_checks and is_own_address(address): + + self.manual_pamyent_checks.add(user_token_hash) + + outputs = self.client.find_outputs(addresses=[address]) + + for output in outputs: + + message = self.client.get_message_data(output['message_id']) + + if user_token_hash == bytes(message['payload']['transaction'][0]['essence']['payload']['indexation'][0]['data']).decode(): + + if self.payment_valid(message): + + self.unlock_content(user_token_hash) + + # prevent key errors + if user_token_hash in self.socket_session_ids.keys(): + + # emit pamyent not found + self.socketio.emit('payment_not_found', room=self.socket_session_ids[user_token_hash]) + + self.manual_pamyent_checks.remove(user_token_hash) + + + def stop(self): self.q.put(self.STOP) LOG.info('MQTT worker stopped') diff --git a/templates/pay.html b/templates/pay.html index 154ee74..4fcdb84 100644 --- a/templates/pay.html +++ b/templates/pay.html @@ -13,8 +13,18 @@ socket.on('payment_received', function() { location.reload(); }); - + socket.on('payment_not_found', function() { + console.log('Payment not found'); + }); + $('#check_payment_button').on('click', function(){ + socket.emit('check_payment', {user_token_hash: '{{ user_token_hash }}', iota_address: '{{ iota_address }}'}); + }); + // show manual payment check after 30 seconds time + setTimeout("showCheckPayment()", 30000); }); + function showCheckPayment() { + document.getElementById("check_payment").style.display = "inline"; + }