From 4a1c359bf9cc87873562e2b8e54e7df02a49c423 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 20 Apr 2022 19:07:50 +0100 Subject: [PATCH] Separate out common functions --- handler/transactions.py | 111 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 100 insertions(+), 11 deletions(-) diff --git a/handler/transactions.py b/handler/transactions.py index 0fceebf..ffadf07 100644 --- a/handler/transactions.py +++ b/handler/transactions.py @@ -439,6 +439,94 @@ class Transactions(util.Base): return True return False + def find_trades_by_uid(self, uid): + """ + Find a list of trade IDs and references by a customer UID. + :return: tuple of (platform, trade_id, reference, currency) + """ + platform, username = self.get_uid(uid) + refs = self.get_refs() + matching_trades = [] + for reference in refs: + ref_data = self.get_ref(reference) + tx_platform = ref_data["subclass"] + tx_username = ref_data["buyer"] + trade_id = ref_data["id"] + currency = ref_data["currency"] + if tx_platform == platform and tx_username == username: + to_append = (platform, trade_id, reference, currency) + matching_trades.append(to_append) + return matching_trades + + def user_verification_successful(self, uid): + """ + A user has successfully completed verification. + """ + self.log.info(f"User has completed verification: {uid}") + trade_list = self.find_trades_by_uid(uid) + for platform, trade_id, reference, currency in trade_list: + self.log.info(f"Sending bank details/reference for {platform}/{reference}") + self.send_bank_details(platform, currency, trade_id) + self.send_reference(platform, trade_id, reference) + + def create_uid(self, platform, username): + return f"{platform}|{username}" + + def get_uid(self, external_user_id): + """ + Get the platform and username from the external user ID. + """ + spl = external_user_id.split("|") + if not len(spl) == 2: + self.log.error(f"Split invalid, cannot get customer: {spl}") + return False + platform, username = spl + return (platform, username) + + def get_send_settings(self, platform): + if platform == "agora": + send_setting = settings.Agora.Send + post_message = self.agora.agora.contact_message_post + elif platform == "lbtc": + send_setting = settings.LocalBitcoins.Send + post_message = self.lbtc.lbtc.contact_message_post + + return (send_setting, post_message) + + def send_reference(self, platform, trade_id, reference): + """ + Send the reference to a customer. + """ + send_setting, post_message = self.get_send_settings(platform) + if send_setting == "1": + post_message( + trade_id, + f"When sending the payment please use reference code: {reference}", + ) + + def send_verification_url(self, platform, uid, trade_id): + send_setting, post_message = self.get_send_settings(platform) + if send_setting == "1": + auth_url = self.ux.verify.create_applicant_and_get_link(uid) + post_message( + trade_id, + f"Hi! To continue the trade, please complete the verification form: {auth_url}", + ) + + def send_bank_details(self, platform, currency, trade_id): + """ + Send the bank details to a trade. + """ + send_setting, post_message = self.get_send_settings(platform) + if send_setting == "1": + account_info = self.markets.get_matching_account_details(platform, currency) + formatted_account_info = self.markets.format_payment_details(currency, account_info) + print("formatted", formatted_account_info) + post_message( + trade_id, + f"Payment details: \n{formatted_account_info}", + ) + def new_trade( self, subclass, @@ -475,17 +563,18 @@ class Transactions(util.Base): r.hmset(f"trade.{reference}", to_store) self.irc.sendmsg(f"Generated reference for {trade_id}: {reference}") self.ux.notify.notify_new_trade(amount, currency) - if subclass == "agora": - send_setting = settings.Agora.Send - post_message = self.agora.agora.contact_message_post - elif subclass == "lbtc": - send_setting = settings.LocalBitcoins.Send - post_message = self.lbtc.lbtc.contact_message_post - if send_setting == "1": - post_message( - trade_id, - f"Hi! When sending the payment please use reference code: {reference}", - ) + uid = self.create_uid(subclass, buyer) + print("UID of new trade", uid) + verified = self.ux.verify.get_external_user_id_status(uid) + if verified != "GREEN": + self.log.info(f"UID {uid} is not verified, sending link.") + self.send_verification_url(subclass, uid, trade_id) + return + else: # User is verified + self.log.info(f"UID {uid} is verified.") + self.log.info(f"Sending bank details/reference for {subclass}/{reference}") + self.send_bank_details(subclass, currency, trade_id) + self.send_reference(subclass, trade_id, reference) if existing_ref: return util.convert(existing_ref) else: