payment/services/order.py

44 lines
1.6 KiB
Python

import uuid
from custom_decorators import singleton
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.order_repo = OrderRepository(config)
self.payment_service = PaymentService()
def create_order(self, from_address, to_address):
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, from_address, to_address)
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