155 lines
4.6 KiB
Python
155 lines
4.6 KiB
Python
from fastapi import APIRouter
|
||
from pydantic import BaseModel
|
||
from cryptoai.utils.upay import Upay
|
||
from datetime import datetime
|
||
from cryptoai.models.subscription_order import SubscriptionOrderManager
|
||
import random
|
||
from cryptoai.routes.user import get_current_user
|
||
from cryptoai.models.user import User
|
||
from fastapi import Depends
|
||
from sqlalchemy.orm import Session
|
||
from cryptoai.utils.db_manager import get_db
|
||
from cryptoai.models.user_subscription import UserSubscriptionManager
|
||
from datetime import timedelta
|
||
import logging
|
||
from fastapi import Request
|
||
|
||
router = APIRouter()
|
||
|
||
logger = logging.getLogger(__name__)
|
||
logger.setLevel(logging.DEBUG)
|
||
|
||
class CreateOrderRequest(BaseModel):
|
||
subscribe_type: int
|
||
|
||
@router.get('/pricing')
|
||
async def pricing():
|
||
return {
|
||
"code": 200,
|
||
"data": {
|
||
"price_month": 29,
|
||
"price_year": 219
|
||
}
|
||
}
|
||
|
||
@router.post("/create_order")
|
||
async def create_order(request: CreateOrderRequest,
|
||
current_user: User = Depends(get_current_user),
|
||
session: Session = Depends(get_db)):
|
||
|
||
if current_user["id"] > 2:
|
||
return {
|
||
"code": 500,
|
||
"message": "暂时还没有开放订阅功能"
|
||
}
|
||
|
||
upay = Upay()
|
||
|
||
# 生成唯一的商户订单号
|
||
merchant_order_no = f"D{datetime.now().strftime('%Y%m%d%H%M%S')}{random.randint(100, 999)}"
|
||
|
||
## 1=包月,2=包年
|
||
if request.subscribe_type == 1:
|
||
fiat_amount = "29"
|
||
product_name = "会员订阅:1个月"
|
||
elif request.subscribe_type == 2:
|
||
fiat_amount = "219"
|
||
product_name = "会员订阅:1年"
|
||
else:
|
||
return {
|
||
"code": 500,
|
||
"message": "Invalid subscribe type"
|
||
}
|
||
|
||
|
||
result = upay.create_order(
|
||
merchant_order_no=merchant_order_no,
|
||
chain_type="1", ## TRC20
|
||
fiat_amount=fiat_amount,
|
||
fiat_currency="USD",
|
||
notify_url="https://api.ibtc.work/payment/notify",
|
||
product_name=product_name,
|
||
redirect_url="https://tradus.vip/subscription-success"
|
||
)
|
||
print(result)
|
||
|
||
if result['code'] == "1":
|
||
payment_order_id = result['data']['orderNo']
|
||
order_id = result['data']['merchantOrderNo']
|
||
|
||
## 创建订阅记录
|
||
subscription_order_manager = SubscriptionOrderManager(session)
|
||
subscription_order_manager.create_order(
|
||
order_id=order_id,
|
||
user_id=current_user["id"],
|
||
payment_order_id=payment_order_id,
|
||
amount=float(fiat_amount),
|
||
member_type=1,
|
||
currency="USD",
|
||
time_type=request.subscribe_type,
|
||
status=1
|
||
)
|
||
|
||
return {
|
||
"code": 200,
|
||
"data": {
|
||
"order_no": result['data']['orderNo'],
|
||
"pay_url": result['data']['payUrl'],
|
||
"status": result['data']['status']
|
||
}
|
||
}
|
||
else:
|
||
return {
|
||
"code": 500,
|
||
"message": result['message']
|
||
}
|
||
|
||
|
||
class NotifyRequest(BaseModel):
|
||
code: int
|
||
message: str
|
||
data: dict
|
||
# order_no: str
|
||
# merchant_order_no: str
|
||
# status: str
|
||
# amount: str
|
||
# currency: str
|
||
# chain_type: str
|
||
|
||
@router.post("/notify")
|
||
async def notify(notify: NotifyRequest, session: Session = Depends(get_db)):
|
||
try:
|
||
data = notify.data
|
||
# 更新订单状态
|
||
subscription_order_manager = SubscriptionOrderManager(session)
|
||
subscription_order_manager.update_order_status(data['merchantOrderNo'], 2)
|
||
|
||
order = subscription_order_manager.get_order_by_id(data['merchantOrderNo'])
|
||
|
||
if order is None:
|
||
return {
|
||
"code": 500,
|
||
"message": "Order not found"
|
||
}
|
||
|
||
user_id = order['user_id']
|
||
member_type = order['member_type']
|
||
time_type = order['time_type']
|
||
|
||
if time_type == 1:
|
||
expire_time = datetime.now() + timedelta(days=30)
|
||
elif time_type == 2:
|
||
expire_time = datetime.now() + timedelta(days=365)
|
||
|
||
#增加用户订阅记录
|
||
user_subscription_manager = UserSubscriptionManager(session)
|
||
user_subscription_manager.create_subscription(user_id,
|
||
member_type,
|
||
time_type,
|
||
data['merchantOrderNo'],
|
||
expire_time)
|
||
|
||
return "ok"
|
||
except Exception as e:
|
||
logger.error(f"创建用户订阅失败: {e}")
|
||
raise e |