mirror of
https://github.com/cnlimiter/codex-register.git
synced 2026-05-18 18:27:35 +08:00
feat(accounts): 添加账号cookies存储及支付链接国家选择功能
- 在账号详情页添加cookies编辑与保存功能,用于支付请求 - 支付页面新增国家选择下拉框,支持多国货币计费 - 优化无痕打开浏览器功能,支持注入账号cookies - 更新数据库模型、API路由及前端界面
This commit is contained in:
@@ -23,62 +23,101 @@ def _build_proxies(proxy: Optional[str]) -> Optional[dict]:
|
||||
return None
|
||||
|
||||
|
||||
def generate_plus_link(account: Account, proxy: Optional[str] = None) -> str:
|
||||
"""生成 Plus 支付链接"""
|
||||
if not account.access_token:
|
||||
raise ValueError("账号缺少 access_token")
|
||||
|
||||
headers = {
|
||||
"Authorization": f"Bearer {account.access_token}",
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
payload = {
|
||||
"plan_type": "plus",
|
||||
"checkout_ui_mode": "hosted",
|
||||
"cancel_url": "https://chatgpt.com/",
|
||||
"success_url": "https://chatgpt.com/",
|
||||
}
|
||||
|
||||
resp = cffi_requests.post(
|
||||
PAYMENT_CHECKOUT_URL,
|
||||
headers=headers,
|
||||
json=payload,
|
||||
proxies=_build_proxies(proxy),
|
||||
timeout=30,
|
||||
impersonate="chrome110",
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
if "url" in data:
|
||||
return data["url"]
|
||||
raise ValueError(data.get("detail", "API 未返回支付链接"))
|
||||
_COUNTRY_CURRENCY_MAP = {
|
||||
"SG": "SGD",
|
||||
"US": "USD",
|
||||
"TR": "TRY",
|
||||
"JP": "JPY",
|
||||
"HK": "HKD",
|
||||
"GB": "GBP",
|
||||
"EU": "EUR",
|
||||
"AU": "AUD",
|
||||
"CA": "CAD",
|
||||
"IN": "INR",
|
||||
"BR": "BRL",
|
||||
"MX": "MXN",
|
||||
}
|
||||
|
||||
|
||||
def generate_team_link(
|
||||
def _extract_oai_did(cookies_str: str) -> Optional[str]:
|
||||
"""从 cookie 字符串中提取 oai-device-id"""
|
||||
for part in cookies_str.split(";"):
|
||||
part = part.strip()
|
||||
if part.startswith("oai-did="):
|
||||
return part[len("oai-did="):].strip()
|
||||
return None
|
||||
|
||||
|
||||
def _parse_cookie_str(cookies_str: str, domain: str) -> list:
|
||||
"""将 'key=val; key2=val2' 格式解析为 Playwright cookie 列表"""
|
||||
cookies = []
|
||||
for part in cookies_str.split(";"):
|
||||
part = part.strip()
|
||||
if "=" not in part:
|
||||
continue
|
||||
name, _, value = part.partition("=")
|
||||
cookies.append({
|
||||
"name": name.strip(),
|
||||
"value": value.strip(),
|
||||
"domain": domain,
|
||||
"path": "/",
|
||||
})
|
||||
return cookies
|
||||
|
||||
|
||||
def _open_url_system_browser(url: str) -> bool:
|
||||
"""回退方案:调用系统浏览器以无痕模式打开"""
|
||||
platform = sys.platform
|
||||
try:
|
||||
if platform == "win32":
|
||||
for browser, flag in [("chrome", "--incognito"), ("msedge", "--inprivate")]:
|
||||
try:
|
||||
subprocess.Popen(f'start {browser} {flag} "{url}"', shell=True)
|
||||
return True
|
||||
except Exception:
|
||||
continue
|
||||
elif platform == "darwin":
|
||||
subprocess.Popen(["open", "-a", "Google Chrome", "--args", "--incognito", url])
|
||||
return True
|
||||
else:
|
||||
for binary in ["google-chrome", "chromium-browser", "chromium"]:
|
||||
try:
|
||||
subprocess.Popen([binary, "--incognito", url])
|
||||
return True
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
except Exception as e:
|
||||
logger.warning(f"系统浏览器无痕打开失败: {e}")
|
||||
return False
|
||||
|
||||
|
||||
def generate_plus_link(
|
||||
account: Account,
|
||||
workspace_name: str = "MyTeam",
|
||||
price_interval: str = "month",
|
||||
seat_quantity: int = 5,
|
||||
proxy: Optional[str] = None,
|
||||
country: str = "SG",
|
||||
) -> str:
|
||||
"""生成 Team 支付链接"""
|
||||
"""生成 Plus 支付链接(后端携带账号 cookie 发请求)"""
|
||||
if not account.access_token:
|
||||
raise ValueError("账号缺少 access_token")
|
||||
|
||||
currency = _COUNTRY_CURRENCY_MAP.get(country, "USD")
|
||||
headers = {
|
||||
"Authorization": f"Bearer {account.access_token}",
|
||||
"Content-Type": "application/json",
|
||||
"oai-language": "zh-CN",
|
||||
}
|
||||
if account.cookies:
|
||||
headers["cookie"] = account.cookies
|
||||
oai_did = _extract_oai_did(account.cookies)
|
||||
if oai_did:
|
||||
headers["oai-device-id"] = oai_did
|
||||
|
||||
payload = {
|
||||
"plan_name": "chatgptteamplan",
|
||||
"team_plan_data": {
|
||||
"workspace_name": workspace_name,
|
||||
"price_interval": price_interval,
|
||||
"seat_quantity": seat_quantity,
|
||||
},
|
||||
"plan_name": "chatgptplusplan",
|
||||
"billing_details": {"country": country, "currency": currency},
|
||||
"promo_campaign": {
|
||||
"promo_campaign_id": "team-1-month-free",
|
||||
"is_coupon_from_query_param": True,
|
||||
"promo_campaign_id": "plus-1-month-free",
|
||||
"is_coupon_from_query_param": False,
|
||||
},
|
||||
"checkout_ui_mode": "custom",
|
||||
}
|
||||
@@ -98,36 +137,86 @@ def generate_team_link(
|
||||
raise ValueError(data.get("detail", "API 未返回 checkout_session_id"))
|
||||
|
||||
|
||||
def open_url_incognito(url: str) -> bool:
|
||||
"""调用本机浏览器以无痕模式打开 URL"""
|
||||
platform = sys.platform
|
||||
def generate_team_link(
|
||||
account: Account,
|
||||
workspace_name: str = "MyTeam",
|
||||
price_interval: str = "month",
|
||||
seat_quantity: int = 5,
|
||||
proxy: Optional[str] = None,
|
||||
country: str = "SG",
|
||||
) -> str:
|
||||
"""生成 Team 支付链接(后端携带账号 cookie 发请求)"""
|
||||
if not account.access_token:
|
||||
raise ValueError("账号缺少 access_token")
|
||||
|
||||
currency = _COUNTRY_CURRENCY_MAP.get(country, "USD")
|
||||
headers = {
|
||||
"Authorization": f"Bearer {account.access_token}",
|
||||
"Content-Type": "application/json",
|
||||
"oai-language": "zh-CN",
|
||||
}
|
||||
if account.cookies:
|
||||
headers["cookie"] = account.cookies
|
||||
oai_did = _extract_oai_did(account.cookies)
|
||||
if oai_did:
|
||||
headers["oai-device-id"] = oai_did
|
||||
|
||||
payload = {
|
||||
"plan_name": "chatgptteamplan",
|
||||
"team_plan_data": {
|
||||
"workspace_name": workspace_name,
|
||||
"price_interval": price_interval,
|
||||
"seat_quantity": seat_quantity,
|
||||
},
|
||||
"billing_details": {"country": country, "currency": currency},
|
||||
"promo_campaign": {
|
||||
"promo_campaign_id": "team-1-month-free",
|
||||
"is_coupon_from_query_param": True,
|
||||
},
|
||||
"cancel_url": "https://chatgpt.com/#pricing",
|
||||
"checkout_ui_mode": "custom",
|
||||
}
|
||||
|
||||
resp = cffi_requests.post(
|
||||
PAYMENT_CHECKOUT_URL,
|
||||
headers=headers,
|
||||
json=payload,
|
||||
proxies=_build_proxies(proxy),
|
||||
timeout=30,
|
||||
impersonate="chrome110",
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
if "checkout_session_id" in data:
|
||||
return TEAM_CHECKOUT_BASE_URL + data["checkout_session_id"]
|
||||
raise ValueError(data.get("detail", "API 未返回 checkout_session_id"))
|
||||
|
||||
|
||||
def open_url_incognito(url: str, cookies_str: Optional[str] = None) -> bool:
|
||||
"""用 Playwright 以无痕模式打开 URL,可注入 cookie"""
|
||||
import threading
|
||||
try:
|
||||
if platform == "win32":
|
||||
# 依次尝试 Chrome、Edge
|
||||
for browser, flag in [("chrome", "--incognito"), ("msedge", "--inprivate")]:
|
||||
try:
|
||||
subprocess.Popen(
|
||||
f'start {browser} {flag} "{url}"',
|
||||
shell=True,
|
||||
)
|
||||
return True
|
||||
except Exception:
|
||||
continue
|
||||
elif platform == "darwin":
|
||||
subprocess.Popen(
|
||||
["open", "-a", "Google Chrome", "--args", "--incognito", url]
|
||||
)
|
||||
return True
|
||||
else:
|
||||
for binary in ["google-chrome", "chromium-browser", "chromium"]:
|
||||
try:
|
||||
subprocess.Popen([binary, "--incognito", url])
|
||||
return True
|
||||
except FileNotFoundError:
|
||||
continue
|
||||
except Exception as e:
|
||||
logger.warning(f"无痕打开浏览器失败: {e}")
|
||||
return False
|
||||
from playwright.sync_api import sync_playwright
|
||||
except ImportError:
|
||||
logger.warning("playwright 未安装,回退到系统浏览器")
|
||||
return _open_url_system_browser(url)
|
||||
|
||||
def _launch():
|
||||
try:
|
||||
with sync_playwright() as p:
|
||||
browser = p.chromium.launch(headless=False, args=["--incognito"])
|
||||
ctx = browser.new_context()
|
||||
if cookies_str:
|
||||
ctx.add_cookies(_parse_cookie_str(cookies_str, "chatgpt.com"))
|
||||
page = ctx.new_page()
|
||||
page.goto(url)
|
||||
# 保持窗口打开直到用户关闭
|
||||
page.wait_for_timeout(300_000) # 最多等待 5 分钟
|
||||
except Exception as e:
|
||||
logger.warning(f"Playwright 无痕打开失败: {e}")
|
||||
|
||||
threading.Thread(target=_launch, daemon=True).start()
|
||||
return True
|
||||
|
||||
|
||||
def check_subscription_status(account: Account, proxy: Optional[str] = None) -> str:
|
||||
|
||||
Reference in New Issue
Block a user