初始化提交
This commit is contained in:
150
NBATransfer-backend/services/order_service.py
Normal file
150
NBATransfer-backend/services/order_service.py
Normal file
@@ -0,0 +1,150 @@
|
||||
from flask import current_app
|
||||
from models import db, User, Order, Transaction
|
||||
from datetime import datetime
|
||||
import uuid
|
||||
from sqlalchemy import desc
|
||||
|
||||
class OrderService:
|
||||
@staticmethod
|
||||
def _generate_order_no():
|
||||
"""生成订单号"""
|
||||
return f"ORD{datetime.utcnow().strftime('%Y%m%d%H%M%S')}{uuid.uuid4().hex[:8].upper()}"
|
||||
|
||||
@staticmethod
|
||||
def create_order(user_id, data):
|
||||
"""创建充值订单"""
|
||||
user = User.query.get(user_id)
|
||||
|
||||
if not user:
|
||||
return {'error': '用户不存在'}, 404
|
||||
|
||||
amount = data.get('amount')
|
||||
payment_method = data.get('payment_method', 'alipay') # alipay, wechat
|
||||
|
||||
# 验证金额
|
||||
if not amount or amount <= 0:
|
||||
return {'error': '充值金额必须大于0'}, 400
|
||||
|
||||
# 验证支付方式
|
||||
if payment_method not in ['alipay', 'wechat']:
|
||||
return {'error': '不支持的支付方式'}, 400
|
||||
|
||||
# 创建订单
|
||||
order = Order(
|
||||
order_no=OrderService._generate_order_no(),
|
||||
user_id=user_id,
|
||||
amount=amount,
|
||||
payment_method=payment_method,
|
||||
status='pending'
|
||||
)
|
||||
|
||||
try:
|
||||
db.session.add(order)
|
||||
db.session.commit()
|
||||
|
||||
# TODO: 调用支付接口
|
||||
# 这里返回支付参数,前端跳转到支付页面
|
||||
payment_params = {
|
||||
'order_no': order.order_no,
|
||||
'amount': order.amount,
|
||||
'payment_method': payment_method,
|
||||
# 实际项目中应返回支付宝或微信的支付参数
|
||||
'qr_code': f'https://payment.example.com/qr/{order.order_no}',
|
||||
'payment_url': f'https://payment.example.com/pay/{order.order_no}'
|
||||
}
|
||||
|
||||
return {
|
||||
'message': '订单创建成功',
|
||||
'order': order.to_dict(),
|
||||
'payment': payment_params
|
||||
}, 201
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return {'error': '订单创建失败'}, 500
|
||||
|
||||
@staticmethod
|
||||
def get_orders(user_id, page=1, per_page=20, status=None):
|
||||
"""获取订单列表"""
|
||||
query = Order.query.filter_by(user_id=user_id)
|
||||
|
||||
if status:
|
||||
query = query.filter_by(status=status)
|
||||
|
||||
# 分页查询
|
||||
pagination = query.order_by(desc(Order.created_at))\
|
||||
.paginate(page=page, per_page=per_page, error_out=False)
|
||||
|
||||
return {
|
||||
'orders': [order.to_dict() for order in pagination.items],
|
||||
'total': pagination.total,
|
||||
'page': page,
|
||||
'per_page': per_page,
|
||||
'pages': pagination.pages
|
||||
}, 200
|
||||
|
||||
@staticmethod
|
||||
def get_order(user_id, order_id):
|
||||
"""获取订单详情"""
|
||||
order = Order.query.filter_by(id=order_id, user_id=user_id).first()
|
||||
|
||||
if not order:
|
||||
return {'error': '订单不存在'}, 404
|
||||
|
||||
return order.to_dict(), 200
|
||||
|
||||
@staticmethod
|
||||
def alipay_callback(data):
|
||||
"""支付宝支付回调(预留接口)"""
|
||||
# TODO: 实现支付宝回调逻辑
|
||||
return {'message': '处理成功'}, 200
|
||||
|
||||
@staticmethod
|
||||
def wechat_callback(data):
|
||||
"""微信支付回调(预留接口)"""
|
||||
# TODO: 实现微信支付回调逻辑
|
||||
return {'message': '处理成功'}, 200
|
||||
|
||||
@staticmethod
|
||||
def payment_notify(order_no):
|
||||
"""模拟支付通知(仅用于测试)"""
|
||||
order = Order.query.filter_by(order_no=order_no).first()
|
||||
|
||||
if not order:
|
||||
return {'error': '订单不存在'}, 404
|
||||
|
||||
if order.status != 'pending':
|
||||
return {'error': '订单状态不正确'}, 400
|
||||
|
||||
try:
|
||||
# 更新订单状态
|
||||
order.status = 'paid'
|
||||
order.paid_at = datetime.utcnow()
|
||||
order.transaction_id = f"TXN{uuid.uuid4().hex[:16].upper()}"
|
||||
|
||||
# 增加用户余额
|
||||
user = User.query.get(order.user_id)
|
||||
balance_before = user.balance
|
||||
user.balance += order.amount
|
||||
balance_after = user.balance
|
||||
|
||||
# 创建交易记录
|
||||
transaction = Transaction(
|
||||
user_id=user.id,
|
||||
type='recharge',
|
||||
amount=order.amount,
|
||||
balance_before=balance_before,
|
||||
balance_after=balance_after,
|
||||
description=f'充值 - 订单号: {order.order_no}',
|
||||
order_id=order.id
|
||||
)
|
||||
|
||||
db.session.add(transaction)
|
||||
db.session.commit()
|
||||
|
||||
return {
|
||||
'message': '支付成功',
|
||||
'order': order.to_dict()
|
||||
}, 200
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
return {'error': '支付处理失败'}, 500
|
||||
Reference in New Issue
Block a user