developing
This commit is contained in:
parent
257135a127
commit
6a28a8fb97
|
@ -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)
|
||||
|
||||
|
|
|
@ -0,0 +1,8 @@
|
|||
order:
|
||||
lifetime: 10min
|
||||
order_status:
|
||||
- 未支付
|
||||
- 支付成功
|
||||
- 未转账
|
||||
- 金额不对
|
||||
- 订单超时
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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):
|
||||
"""
|
||||
判断两个时间戳的时间差是否大于指定的小时、分钟和秒数
|
||||
|
@ -33,3 +36,28 @@ def is_time_difference_greater_than(timestamp1, timestamp2, hours=0, minutes=0,
|
|||
|
||||
# 判断时间差是否大于指定的时间
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue