1
This commit is contained in:
parent
666896047e
commit
124613789c
@ -27,15 +27,9 @@ class PhoneNumberRequest(BaseModel):
|
|||||||
phone_code: str # 手机号验证码
|
phone_code: str # 手机号验证码
|
||||||
referral_code: str = None # 推荐码(可选)
|
referral_code: str = None # 推荐码(可选)
|
||||||
|
|
||||||
class OrderType(str, enum.Enum):
|
|
||||||
DELIVERY = "DELIVERY" # 配送订单
|
|
||||||
PRODUCT = "PRODUCT" # 商品订单
|
|
||||||
ONLINE_PAY = "ONLINE_PAY" # 在线支付
|
|
||||||
|
|
||||||
class WechatPayRequest(BaseModel):
|
class WechatPayRequest(BaseModel):
|
||||||
"""微信支付请求"""
|
"""微信支付请求"""
|
||||||
order_id: str
|
order_id: str
|
||||||
order_type: OrderType = Field(..., description="订单类型")
|
|
||||||
|
|
||||||
@router.post("/phone-login", response_model=ResponseModel)
|
@router.post("/phone-login", response_model=ResponseModel)
|
||||||
async def wechat_phone_login(
|
async def wechat_phone_login(
|
||||||
@ -120,43 +114,43 @@ async def create_payment(
|
|||||||
current_user: UserDB = Depends(get_current_user)
|
current_user: UserDB = Depends(get_current_user)
|
||||||
):
|
):
|
||||||
"""创建微信支付订单"""
|
"""创建微信支付订单"""
|
||||||
# 查询订单
|
|
||||||
if request.order_type == OrderType.PRODUCT:
|
|
||||||
order = db.query(MerchantOrderDB).filter(
|
|
||||||
MerchantOrderDB.order_id == request.order_id,
|
|
||||||
MerchantOrderDB.user_id == current_user.userid,
|
|
||||||
MerchantOrderDB.status == MerchantOrderStatus.CREATED
|
|
||||||
).first()
|
|
||||||
if not order:
|
|
||||||
return error_response(code=404, message="订单不存在或状态不正确")
|
|
||||||
amount = order.pay_amount
|
|
||||||
description = "商家团购订单"
|
|
||||||
|
|
||||||
elif request.order_type == OrderType.ONLINE_PAY:
|
|
||||||
order = db.query(MerchantPayOrderDB).filter(
|
|
||||||
MerchantPayOrderDB.order_id == request.order_id,
|
|
||||||
MerchantPayOrderDB.user_id == current_user.userid,
|
|
||||||
MerchantPayOrderDB.status == MerchantPayOrderStatus.UNPAID
|
|
||||||
).first()
|
|
||||||
if not order:
|
|
||||||
return error_response(code=404, message="订单不存在或状态不正确")
|
|
||||||
amount = order.amount
|
|
||||||
description = "商家在线买单"
|
|
||||||
elif request.order_type == OrderType.DELIVERY:
|
|
||||||
order = db.query(ShippingOrderDB).filter(
|
|
||||||
ShippingOrderDB.orderid == request.order_id,
|
|
||||||
ShippingOrderDB.userid == current_user.userid,
|
|
||||||
ShippingOrderDB.status == OrderStatus.UNPAID
|
|
||||||
).first()
|
|
||||||
if not order:
|
|
||||||
return error_response(code=404, message="订单不存在或状态不正确")
|
|
||||||
amount = order.final_amount
|
|
||||||
description = "小区配送订单"
|
|
||||||
|
|
||||||
else:
|
|
||||||
return error_response(code=400, message="不支持的订单类型")
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
# 根据订单号前缀判断订单类型
|
||||||
|
order_prefix = request.order_id[0]
|
||||||
|
|
||||||
|
description = ""
|
||||||
|
amount = 0
|
||||||
|
|
||||||
|
if order_prefix == "D": # 配送订单
|
||||||
|
order = db.query(ShippingOrderDB).filter(
|
||||||
|
ShippingOrderDB.orderid == request.order_id
|
||||||
|
).first()
|
||||||
|
if not order:
|
||||||
|
return error_response(code=404, message="订单不存在")
|
||||||
|
description = "配送费用"
|
||||||
|
amount = order.final_amount
|
||||||
|
|
||||||
|
elif order_prefix == "M": # 商家商品订单
|
||||||
|
order = db.query(MerchantOrderDB).filter(
|
||||||
|
MerchantOrderDB.order_id == request.order_id
|
||||||
|
).first()
|
||||||
|
if not order:
|
||||||
|
return error_response(code=404, message="订单不存在")
|
||||||
|
description = "商品购买"
|
||||||
|
amount = order.pay_amount
|
||||||
|
|
||||||
|
elif order_prefix == "P": # 商家在线买单
|
||||||
|
order = db.query(MerchantPayOrderDB).filter(
|
||||||
|
MerchantPayOrderDB.order_id == request.order_id
|
||||||
|
).first()
|
||||||
|
if not order:
|
||||||
|
return error_response(code=404, message="订单不存在")
|
||||||
|
description = "在线买单"
|
||||||
|
amount = order.amount
|
||||||
|
|
||||||
|
else:
|
||||||
|
return error_response(code=400, message="无效的订单号")
|
||||||
|
|
||||||
# 初始化微信支付客户端
|
# 初始化微信支付客户端
|
||||||
wechat = WeChatClient()
|
wechat = WeChatClient()
|
||||||
|
|
||||||
@ -199,10 +193,15 @@ async def payment_notify(
|
|||||||
# 获取订单信息
|
# 获取订单信息
|
||||||
out_trade_no = data.get("out_trade_no")
|
out_trade_no = data.get("out_trade_no")
|
||||||
transaction_id = data.get("transaction_id")
|
transaction_id = data.get("transaction_id")
|
||||||
trade_state = data.get("trade_state")
|
trade_state_desc = data.get("trade_state_desc")
|
||||||
success_time = data.get("success_time")
|
success_time = data.get("success_time")
|
||||||
|
|
||||||
if not all([out_trade_no, transaction_id, trade_state]) or trade_state != "SUCCESS":
|
print(f"out_trade_no: {out_trade_no}")
|
||||||
|
print(f"transaction_id: {transaction_id}")
|
||||||
|
print(f"trade_state_desc: {trade_state_desc}")
|
||||||
|
print(f"success_time: {success_time}")
|
||||||
|
|
||||||
|
if not all([out_trade_no, transaction_id, trade_state_desc]) or trade_state_desc != "支付成功":
|
||||||
return error_response(code=400, message="缺少必要的订单信息或支付未成功")
|
return error_response(code=400, message="缺少必要的订单信息或支付未成功")
|
||||||
|
|
||||||
# 判断订单类型并处理
|
# 判断订单类型并处理
|
||||||
@ -215,7 +214,7 @@ async def payment_notify(
|
|||||||
if not order:
|
if not order:
|
||||||
return error_response(code=404, message="订单不存在")
|
return error_response(code=404, message="订单不存在")
|
||||||
|
|
||||||
if trade_state == "SUCCESS":
|
if trade_state_desc == "支付成功":
|
||||||
# 更新订单状态
|
# 更新订单状态
|
||||||
order.pay_status = True
|
order.pay_status = True
|
||||||
order.status = OrderStatus.COMPLETED # 支付成功后状态为已完成
|
order.status = OrderStatus.COMPLETED # 支付成功后状态为已完成
|
||||||
@ -231,7 +230,7 @@ async def payment_notify(
|
|||||||
if not order:
|
if not order:
|
||||||
return error_response(code=404, message="订单不存在")
|
return error_response(code=404, message="订单不存在")
|
||||||
|
|
||||||
if trade_state == "SUCCESS":
|
if trade_state_desc == "支付成功":
|
||||||
# 添加积分
|
# 添加积分
|
||||||
point_manager = PointManager(db)
|
point_manager = PointManager(db)
|
||||||
point_manager.add_points(order.user_id, order.gift_points, PointRecordType.CONSUME_RETURN, f"团购订单奖励", order.order_id)
|
point_manager.add_points(order.user_id, order.gift_points, PointRecordType.CONSUME_RETURN, f"团购订单奖励", order.order_id)
|
||||||
@ -250,7 +249,7 @@ async def payment_notify(
|
|||||||
if not order:
|
if not order:
|
||||||
return error_response(code=404, message="订单不存在")
|
return error_response(code=404, message="订单不存在")
|
||||||
|
|
||||||
if trade_state == "SUCCESS":
|
if trade_state_desc == "支付成功":
|
||||||
# 添加积分
|
# 添加积分
|
||||||
point_manager = PointManager(db)
|
point_manager = PointManager(db)
|
||||||
point_manager.add_points(order.user_id, order.gift_points, PointRecordType.CONSUME_RETURN, f"买单订单奖励", order.order_id)
|
point_manager.add_points(order.user_id, order.gift_points, PointRecordType.CONSUME_RETURN, f"买单订单奖励", order.order_id)
|
||||||
|
|||||||
@ -65,7 +65,15 @@ class Settings(BaseSettings):
|
|||||||
WECHAT_API_V3_KEY: str = "OAhAqXqebeT4ZC9VTYFkSWU0CENEahx5" # API v3密钥
|
WECHAT_API_V3_KEY: str = "OAhAqXqebeT4ZC9VTYFkSWU0CENEahx5" # API v3密钥
|
||||||
WECHAT_PLATFORM_CERT_PATH: str = "app/cert/platform_key.pem" # 平台证书路径
|
WECHAT_PLATFORM_CERT_PATH: str = "app/cert/platform_key.pem" # 平台证书路径
|
||||||
|
|
||||||
|
# 微信模板消息ID
|
||||||
|
#配送订单创建成功
|
||||||
|
WECHAT_DELIVERY_ORDER_CREATED_TEMPLATE_ID: str = "-aFOuC2dv1E6Opn9auB39bSiU4p0DbKOrUtOFgS-AaA"
|
||||||
|
#配送订单接单
|
||||||
|
WECHAT_DELIVERY_ORDER_RECEIVED_TEMPLATE_ID: str = "zBdw3OQoZ4RQ9Yau-kpHJ-iGo6cjm7Q-dEh4Luzs2ww"
|
||||||
|
#配送订单完成
|
||||||
|
WECHAT_DELIVERY_ORDER_COMPLETED_TEMPLATE_ID: str = "Yr4BuHkwUw1LTWYfSz7MTEBWcqaqKGojqHlxt14moCU"
|
||||||
|
#配送订单取消
|
||||||
|
WECHAT_DELIVERY_ORDER_CANCELLED_TEMPLATE_ID: str = "5sS4wKt7jyoFGYMKQHGweIDB6UHrikF_ith0swd8z5Y"
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
case_sensitive = True
|
case_sensitive = True
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user