@@ -178,4 +178,52 @@ def mark_subscription(account_id: int, request: MarkSubscriptionRequest):
178178
179179 return {"success" : True , "subscription_type" : request .subscription_type }
180180
181+ # ============== 国家/货币配置 ==============
182+
183+ _countries_cache : dict = {} # {"data": [...], "expires_at": float}
184+
185+
186+ @router .get ("/countries" )
187+ def get_checkout_countries ():
188+ """从 ChatGPT checkout 接口获取支持的国家/货币列表(缓存 1 小时)"""
189+ import time
190+ import curl_cffi .requests as cffi_requests
191+
192+ now = time .time ()
193+ if _countries_cache .get ("expires_at" , 0 ) > now :
194+ return {"success" : True , "countries" : _countries_cache ["data" ]}
195+
196+ with get_db () as db :
197+ proxy = get_settings ().get_proxy_url (db = db )
198+
199+ try :
200+ resp = cffi_requests .get (
201+ "https://chatgpt.com/backend-api/checkout_pricing_config/countries" ,
202+ proxies = {"http" : proxy , "https" : proxy } if proxy else None ,
203+ timeout = 15 ,
204+ impersonate = "chrome110" ,
205+ )
206+ resp .raise_for_status ()
207+ data = resp .json ()
208+ countries = data if isinstance (data , list ) else data .get ("countries" , [])
209+ _countries_cache ["data" ] = countries
210+ _countries_cache ["expires_at" ] = now + 3600
211+ return {"success" : True , "countries" : countries }
212+ except Exception as e :
213+ logger .warning (f"获取国家列表失败: { e } " )
214+ fallback = [
215+ {"country_code" : "SG" , "currency" : "SGD" , "country_name" : "Singapore" },
216+ {"country_code" : "US" , "currency" : "USD" , "country_name" : "United States" },
217+ {"country_code" : "TR" , "currency" : "TRY" , "country_name" : "Turkey" },
218+ {"country_code" : "JP" , "currency" : "JPY" , "country_name" : "Japan" },
219+ {"country_code" : "HK" , "currency" : "HKD" , "country_name" : "Hong Kong" },
220+ {"country_code" : "GB" , "currency" : "GBP" , "country_name" : "United Kingdom" },
221+ {"country_code" : "AU" , "currency" : "AUD" , "country_name" : "Australia" },
222+ {"country_code" : "CA" , "currency" : "CAD" , "country_name" : "Canada" },
223+ {"country_code" : "IN" , "currency" : "INR" , "country_name" : "India" },
224+ {"country_code" : "BR" , "currency" : "BRL" , "country_name" : "Brazil" },
225+ {"country_code" : "MX" , "currency" : "MXN" , "country_name" : "Mexico" },
226+ ]
227+ return {"success" : False , "countries" : fallback , "error" : str (e )}
228+
181229
0 commit comments