From 6a28a8fb971e59df9a5cd62b7652aa8dd80878ce Mon Sep 17 00:00:00 2001 From: wystan_rin Date: Wed, 13 Nov 2024 17:23:59 +0800 Subject: [PATCH] developing --- payment_backend/app.py | 2 +- payment_backend/config/param.ini | 2 +- payment_backend/config/param.yaml | 8 ++++++ payment_backend/config/utils.py | 41 +++++++++++++++++++++---------- payment_backend/services/order.py | 11 +++++---- payment_backend/utils/datetime.py | 30 +++++++++++++++++++++- 6 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 payment_backend/config/param.yaml diff --git a/payment_backend/app.py b/payment_backend/app.py index 94a0d1a..30fea6e 100644 --- a/payment_backend/app.py +++ b/payment_backend/app.py @@ -22,8 +22,8 @@ def create_order(): return jsonify({ "message": "Unsupported payment method. Currently, only USDT payments are supported." }), 400 - address = data.get('address', None) + if address is None: addresses = user_service.get_addresses(name, phone, email) diff --git a/payment_backend/config/param.ini b/payment_backend/config/param.ini index 93bc4de..20507e9 100644 --- a/payment_backend/config/param.ini +++ b/payment_backend/config/param.ini @@ -10,4 +10,4 @@ password: 'your_mysql_password' host: 'localhost' database: 'your_database_name' autocommit: false -allow_multi_statements: True \ No newline at end of file +allow_multi_statements: True diff --git a/payment_backend/config/param.yaml b/payment_backend/config/param.yaml new file mode 100644 index 0000000..2c9509f --- /dev/null +++ b/payment_backend/config/param.yaml @@ -0,0 +1,8 @@ +order: + lifetime: 10min + order_status: + - 未支付 + - 支付成功 + - 未转账 + - 金额不对 + - 订单超时 diff --git a/payment_backend/config/utils.py b/payment_backend/config/utils.py index 4a6d836..68c2f1b 100644 --- a/payment_backend/config/utils.py +++ b/payment_backend/config/utils.py @@ -36,25 +36,22 @@ setup_seed(random_seed) class Setting: def __init__(self, + settings:dict = None, config_parser: ConfigParser = None, argument_parser: Namespace = None, _visited=None, _parent=None, **kwargs): self._parent = _parent + + self.update(_visited=_visited, **settings) + if config_parser: - for section in config_parser.sections(): - section_config = Setting(_parent=self) - for option in config_parser.options(section): - section_config[option] = config_parser.get(section, option) - self.__dict__[section] = section_config + self.update_config_parser(config_parser) if argument_parser: - for k in vars(argument_parser): - self.__dict__[k] = getattr(argument_parser, k) + self.update_argument_parser(argument_parser) - if _visited is None: - _visited = set() self.update(_visited=_visited, **kwargs) def update(self, _visited=None, **kwargs): @@ -74,6 +71,17 @@ class Setting: _visited[obj_id] = v self.__dict__[k] = v + def update_config_parser(self, config_parser: ConfigParser): + for section in config_parser.sections(): + section_config = Setting(_parent=self) + for option in config_parser.options(section): + section_config[option] = config_parser.get(section, option) + self.__dict__[section] = section_config + + def update_argument_parser(self, argument_parser: Namespace): + for k in vars(argument_parser): + self.__dict__[k] = getattr(argument_parser, k) + def get(self, item, default=None): if item not in self.__dict__ and self._parent is not None: return self._parent.get(item, default) @@ -133,13 +141,20 @@ def log_config(config): retention=3) -def get_config(config_file=fr'{ROOT_DIR}/config/param.ini') -> Setting: +def get_config(yaml_file=fr'{ROOT_DIR}/config/param.yaml', ini_file=fr'{ROOT_DIR}/config/param.ini') -> Setting: requests.adapters.DEFAULT_RETRIES = 3 - configparser = ConfigParser() - configparser.read(config_file) + config = Setting() + if os.path.exists(yaml_file): + configparser = ConfigParser() + configparser.read(ini_file) + config.update_config_parser(configparser) parser = argparse.ArgumentParser(description='payment system') parser.add_argument("--seed", type=int, default=2024) args = parser.parse_args() - config = Setting(configparser, args) + config.update_argument_parser(args) + if os.path.exists(yaml_file): + with open('config.yaml', 'r') as file: + data = yaml.safe_load(file) + config.update(data) return config diff --git a/payment_backend/services/order.py b/payment_backend/services/order.py index 6d8cade..55b787c 100644 --- a/payment_backend/services/order.py +++ b/payment_backend/services/order.py @@ -1,10 +1,9 @@ import uuid from custom_decorators import singleton -from models import User from repositories.order import OrderRepository from services.payment import PaymentService -from utils.datetime import current, current_timestamp, is_time_difference_greater_than +from utils.datetime import current, current_timestamp, is_time_difference_greater_than, parse_time_string @singleton @@ -26,13 +25,15 @@ class OrderService: def finish_order(self, order_id): # 判断支付时间是否超过订单存活时间 quant, from_address, to_address, create_timestamp = self.order_repo.get_order_info(order_id) - current = current_timestamp() + now = current_timestamp() status = 0 - if is_time_difference_greater_than(create_timestamp, current, minutes=15): + if is_time_difference_greater_than(create_timestamp, now, **parse_time_string(self.config.order.lifetime)): # 订单超时 status = 4 else: - correct_quant, confirmed = self.payment_service.check_payment(quant, from_address, to_address, create_timestamp, current) + correct_quant, confirmed = self.payment_service.check_payment(quant, + from_address, to_address, + create_timestamp, now) if correct_quant and confirmed: # 支付成功 status = 1 diff --git a/payment_backend/utils/datetime.py b/payment_backend/utils/datetime.py index 40a2c50..8d52be4 100644 --- a/payment_backend/utils/datetime.py +++ b/payment_backend/utils/datetime.py @@ -1,12 +1,15 @@ import datetime +import re def current(): return datetime.datetime.now() + def current_timestamp(): datetime.datetime.now().timestamp() + def is_time_difference_greater_than(timestamp1, timestamp2, hours=0, minutes=0, seconds=0): """ 判断两个时间戳的时间差是否大于指定的小时、分钟和秒数 @@ -32,4 +35,29 @@ def is_time_difference_greater_than(timestamp1, timestamp2, hours=0, minutes=0, threshold = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds) # 判断时间差是否大于指定的时间 - return time_difference > threshold \ No newline at end of file + return time_difference > threshold + + +def parse_time_string(time_str): + # 定义支持的时间单位 + time_units = { + 'd': 'days', + 'h': 'hours', + 'min': 'minutes', + 's': 'seconds' + } + + # 使用正则表达式匹配数字和单位的模式 + matches = re.findall(fr"(\d+)({'|'.join(time_units.keys())})", time_str) + + if not matches: + raise ValueError(f"Invalid time string format: {time_str}") + + result = {} + for value, unit in matches: + if unit in time_units: + result[time_units[unit]] = int(value) + else: + raise ValueError(f"Unsupported unit: {unit}") + + return result