developing

This commit is contained in:
wystan_rin 2024-11-13 17:23:59 +08:00
parent 257135a127
commit 6a28a8fb97
6 changed files with 73 additions and 21 deletions

View File

@ -22,8 +22,8 @@ def create_order():
return jsonify({ return jsonify({
"message": "Unsupported payment method. Currently, only USDT payments are supported." "message": "Unsupported payment method. Currently, only USDT payments are supported."
}), 400 }), 400
address = data.get('address', None) address = data.get('address', None)
if address is None: if address is None:
addresses = user_service.get_addresses(name, phone, email) addresses = user_service.get_addresses(name, phone, email)

View File

@ -10,4 +10,4 @@ password: 'your_mysql_password'
host: 'localhost' host: 'localhost'
database: 'your_database_name' database: 'your_database_name'
autocommit: false autocommit: false
allow_multi_statements: True allow_multi_statements: True

View File

@ -0,0 +1,8 @@
order:
lifetime: 10min
order_status:
- 未支付
- 支付成功
- 未转账
- 金额不对
- 订单超时

View File

@ -36,25 +36,22 @@ setup_seed(random_seed)
class Setting: class Setting:
def __init__(self, def __init__(self,
settings:dict = None,
config_parser: ConfigParser = None, config_parser: ConfigParser = None,
argument_parser: Namespace = None, argument_parser: Namespace = None,
_visited=None, _visited=None,
_parent=None, _parent=None,
**kwargs): **kwargs):
self._parent = _parent self._parent = _parent
self.update(_visited=_visited, **settings)
if config_parser: if config_parser:
for section in config_parser.sections(): self.update_config_parser(config_parser)
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
if argument_parser: if argument_parser:
for k in vars(argument_parser): self.update_argument_parser(argument_parser)
self.__dict__[k] = getattr(argument_parser, k)
if _visited is None:
_visited = set()
self.update(_visited=_visited, **kwargs) self.update(_visited=_visited, **kwargs)
def update(self, _visited=None, **kwargs): def update(self, _visited=None, **kwargs):
@ -74,6 +71,17 @@ class Setting:
_visited[obj_id] = v _visited[obj_id] = v
self.__dict__[k] = 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): def get(self, item, default=None):
if item not in self.__dict__ and self._parent is not None: if item not in self.__dict__ and self._parent is not None:
return self._parent.get(item, default) return self._parent.get(item, default)
@ -133,13 +141,20 @@ def log_config(config):
retention=3) 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 requests.adapters.DEFAULT_RETRIES = 3
configparser = ConfigParser() config = Setting()
configparser.read(config_file) if os.path.exists(yaml_file):
configparser = ConfigParser()
configparser.read(ini_file)
config.update_config_parser(configparser)
parser = argparse.ArgumentParser(description='payment system') parser = argparse.ArgumentParser(description='payment system')
parser.add_argument("--seed", type=int, default=2024) parser.add_argument("--seed", type=int, default=2024)
args = parser.parse_args() 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 return config

View File

@ -1,10 +1,9 @@
import uuid import uuid
from custom_decorators import singleton from custom_decorators import singleton
from models import User
from repositories.order import OrderRepository from repositories.order import OrderRepository
from services.payment import PaymentService 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 @singleton
@ -26,13 +25,15 @@ class OrderService:
def finish_order(self, order_id): def finish_order(self, order_id):
# 判断支付时间是否超过订单存活时间 # 判断支付时间是否超过订单存活时间
quant, from_address, to_address, create_timestamp = self.order_repo.get_order_info(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 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 status = 4
else: 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: if correct_quant and confirmed:
# 支付成功 # 支付成功
status = 1 status = 1

View File

@ -1,12 +1,15 @@
import datetime import datetime
import re
def current(): def current():
return datetime.datetime.now() return datetime.datetime.now()
def current_timestamp(): def current_timestamp():
datetime.datetime.now().timestamp() datetime.datetime.now().timestamp()
def is_time_difference_greater_than(timestamp1, timestamp2, hours=0, minutes=0, seconds=0): 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) threshold = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)
# 判断时间差是否大于指定的时间 # 判断时间差是否大于指定的时间
return time_difference > threshold 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