Skip to content

Commit

Permalink
feat: 查询订单
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Jan 16, 2025
1 parent ae9bd68 commit 89e78a7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
18 changes: 9 additions & 9 deletions src/order/afdian/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ async def process_order(out_trade_no: str) -> Tuple[bool, str]:
logger.error(
f"Query order failed, out_trade_no: {out_trade_no}, response: {response}"
)
return False, "Query order failed"
return None, "Query order failed"

order = response.get("data", {}).get("list", [])
if not order:
logger.error(
f"Order not found, out_trade_no: {out_trade_no}, response: {response}"
)
return False, "Order not found"
return None, "Order not found"

order = order[0]

Expand All @@ -45,16 +45,16 @@ async def process_order(out_trade_no: str) -> Tuple[bool, str]:

if not bill:
logger.error(f"Create bill failed, out_trade_no: {out_trade_no}")
return False, "Create bill failed"
return None, "Create bill failed"

bill = bill[0]
if not bill:
logger.error(f"Bill not found, out_trade_no: {out_trade_no}")
return False, "Bill not found"
return None, "Bill not found"

if bill.cdk:
logger.info(f"CDK already exists, out_trade_no: {out_trade_no}")
return False, "CDK already exists"
return None, "CDK already exists"

try:
plan = Plan.get(Plan.platform == "afdian", Plan.plan_id == order["plan_id"])
Expand All @@ -64,13 +64,13 @@ async def process_order(out_trade_no: str) -> Tuple[bool, str]:
)
except Exception as e:
logger.error(f"Plan not found, out_trade_no: {out_trade_no}, error: {e}")
return False, "Plan not found"
return None, "Plan not found"

expired = now + timedelta(days=plan.valid_days)
cdk = await acquire_cdk(expired)
if not cdk:
logger.error(f"Query CDK failed, out_trade_no: {out_trade_no}")
return False, "Query CDK failed"
return None, "Query CDK failed"

try:
bill = Bill.get(Bill.platform == "afdian", Bill.order_id == out_trade_no)
Expand All @@ -79,6 +79,6 @@ async def process_order(out_trade_no: str) -> Tuple[bool, str]:
bill.save()
except Exception as e:
logger.error(f"Update bill failed, out_trade_no: {out_trade_no}, error: {e}")
return False, "Update bill failed"
return None, "Update bill failed"

return True, "OK"
return bill, "OK"
13 changes: 11 additions & 2 deletions src/order/afdian/query_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from fastapi import APIRouter

from src.database import Bill, Plan
from .factory import process_order

router = APIRouter()

Expand All @@ -10,11 +11,19 @@
async def query_order(order_id: str):
logger.debug(f"order_id: {order_id}")

if not order_id:
logger.error(f"order_id is null")
return {"ec": 404, "msg": "Order not found"}

try:
bill = Bill.get(Bill.platform == "afdian", Bill.order_id == order_id)
except Exception as e:
logger.error(f"Query bill failed, order_id: {order_id}, error: {e}")
return {"ec": 404, "msg": "Bill not found"}
logger.warning(f"Query bill failed, order_id: {order_id}, error: {e}")

bill, message = await process_order(order_id)
if not bill:
logger.error(f"order not found, order_id: {order_id}")
return { "ec": 404, "msg": message }

try:
plan = Plan.get(Plan.platform == "afdian", Plan.plan_id == bill.plan_id)
Expand Down

0 comments on commit 89e78a7

Please sign in to comment.