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 @singleton class OrderService: def __init__(self, config): self.config = config self.payment_service = PaymentService() self.order_repo = OrderRepository(config) def create_order(self, address=None): date_str = current().strftime('%Y%m%d%H%M%S') unique_id = str(uuid.uuid4()).split('-')[0] order_id = f"{date_str}-{unique_id}" self.order_repo.create(order_id, address, self.config['PaymentAddresses']) return order_id def finish_order(self, order_id): # 判断支付时间是否超过订单存活时间 quant, from_address, to_address, create_timestamp = self.order_repo.get_order_info(order_id) current = current_timestamp() status = 0 if is_time_difference_greater_than(create_timestamp, current, minutes=15): # 订单超时 status = 4 else: correct_quant, confirmed = self.payment_service.check_payment(quant, from_address, to_address, create_timestamp, current) if correct_quant and confirmed: # 支付成功 status = 1 elif correct_quant < 0: # 没有转账 status = 2 elif confirmed: # 金额不对 status = 3 if status: self.order_repo.update_status(order_id, status) return status