创建绑卡任务(从账号管理中选择账号)
(request: CreateBindCardTaskRequest)
| 2343 | |
| 2344 | @router.post("/bind-card/tasks") |
| 2345 | def create_bind_card_task(request: CreateBindCardTaskRequest): |
| 2346 | """创建绑卡任务(从账号管理中选择账号)""" |
| 2347 | bind_mode = str(request.bind_mode or "semi_auto").strip().lower() |
| 2348 | if bind_mode not in ("semi_auto", "third_party", "local_auto"): |
| 2349 | raise HTTPException(status_code=400, detail="bind_mode 必须为 semi_auto / third_party / local_auto") |
| 2350 | |
| 2351 | with get_db() as db: |
| 2352 | account = db.query(Account).filter(Account.id == request.account_id).first() |
| 2353 | if not account: |
| 2354 | raise HTTPException(status_code=404, detail="账号不存在") |
| 2355 | |
| 2356 | logger.info( |
| 2357 | "创建绑卡任务: account_id=%s email=%s plan=%s country=%s mode=%s auto_open=%s", |
| 2358 | account.id, account.email, request.plan_type, request.country, bind_mode, request.auto_open |
| 2359 | ) |
| 2360 | |
| 2361 | proxy = _resolve_runtime_proxy(request.proxy, account) |
| 2362 | try: |
| 2363 | link, source, fallback_reason, checkout_session_id, publishable_key, client_secret = _generate_checkout_link_for_account( |
| 2364 | account=account, |
| 2365 | request=request, |
| 2366 | proxy=proxy, |
| 2367 | ) |
| 2368 | except HTTPException: |
| 2369 | raise |
| 2370 | except ValueError as e: |
| 2371 | raise HTTPException(status_code=400, detail=str(e)) |
| 2372 | except Exception as e: |
| 2373 | logger.error(f"创建绑卡任务失败: {e}") |
| 2374 | raise HTTPException(status_code=500, detail=f"创建绑卡任务失败: {str(e)}") |
| 2375 | |
| 2376 | task = BindCardTask( |
| 2377 | account_id=account.id, |
| 2378 | plan_type=request.plan_type, |
| 2379 | workspace_name=request.workspace_name if request.plan_type == "team" else None, |
| 2380 | price_interval=request.price_interval if request.plan_type == "team" else None, |
| 2381 | seat_quantity=request.seat_quantity if request.plan_type == "team" else None, |
| 2382 | country=_normalize_checkout_country(request.country), |
| 2383 | currency=_normalize_checkout_currency(_normalize_checkout_country(request.country), request.currency), |
| 2384 | checkout_url=link, |
| 2385 | checkout_session_id=checkout_session_id, |
| 2386 | publishable_key=publishable_key, |
| 2387 | client_secret=client_secret, |
| 2388 | checkout_source=source, |
| 2389 | bind_mode=bind_mode, |
| 2390 | status="link_ready", |
| 2391 | ) |
| 2392 | db.add(task) |
| 2393 | db.commit() |
| 2394 | db.refresh(task) |
| 2395 | |
| 2396 | logger.info( |
| 2397 | "绑卡任务已创建: task_id=%s account_id=%s plan=%s source=%s status=%s", |
| 2398 | task.id, task.account_id, task.plan_type, source, task.status |
| 2399 | ) |
| 2400 | |
| 2401 | opened = False |
| 2402 | if request.auto_open and bind_mode == "semi_auto" and link: |
nothing calls this directly
no test coverage detected