feat(avatar): route token recharge through Huihui org orders
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
"""Huihui organization token-recharge order API."""
|
||||
import os
|
||||
|
||||
import httpx
|
||||
|
||||
from services.huihui_payment import HuihuiPaymentClient, HuihuiPaymentError
|
||||
|
||||
|
||||
class HuihuiOrgPaymentClient(HuihuiPaymentClient):
|
||||
def __init__(self):
|
||||
super().__init__({
|
||||
"HUIHUI_PAYMENT_BASE_URL": os.getenv("HUIHUI_ORG_BASE_URL", ""),
|
||||
"HUIHUI_APP_ID": os.getenv("HUIHUI_APP_ID", ""),
|
||||
"HUIHUI_ACCESS_ID": os.getenv("HUIHUI_ACCESS_ID", ""),
|
||||
"HUIHUI_ACCESS_SECRET": os.getenv("HUIHUI_ACCESS_SECRET", ""),
|
||||
})
|
||||
|
||||
def _request(self, method: str, path: str, *, token: str, user_id: str, body=None) -> dict:
|
||||
if not self.configured or not token or not user_id:
|
||||
raise HuihuiPaymentError("会会订单服务或登录凭证未配置")
|
||||
try:
|
||||
response = httpx.request(
|
||||
method, f"{self.base_url}{path}",
|
||||
headers={"Authorization": f"Bearer {token}", "appId": self.app_id},
|
||||
params=self._signed_params(user_id), json=body,
|
||||
timeout=self.timeout, follow_redirects=True,
|
||||
)
|
||||
except httpx.HTTPError as exc:
|
||||
raise HuihuiPaymentError("会会订单服务连接失败") from exc
|
||||
payload = self._json(response)
|
||||
if response.status_code >= 400 or payload.get("code") not in (0, 200, "0", "200"):
|
||||
raise HuihuiPaymentError(str(payload.get("message") or "会会订单请求失败"))
|
||||
data = payload.get("data")
|
||||
if not isinstance(data, dict):
|
||||
raise HuihuiPaymentError("会会订单信息缺失")
|
||||
return data
|
||||
|
||||
def create_order(self, *, token: str, user_id: str, name: str, phone: str,
|
||||
avatar: str, amount: str, pay_type: int) -> dict:
|
||||
body = {"chargeAmount": float(amount), "payType": pay_type,
|
||||
"commodityName": "数字分身积分充值", "userId": user_id,
|
||||
"name": name or "", "phoneNumber": phone or "", "avatar": avatar or ""}
|
||||
return self._request("POST", "/order/tokenRecharge", token=token, user_id=user_id, body=body)
|
||||
|
||||
def get_order(self, *, token: str, user_id: str, order_no: str) -> dict:
|
||||
if not order_no.isdigit():
|
||||
raise HuihuiPaymentError("会会订单号格式不正确")
|
||||
return self._request("GET", f"/order/get/{order_no}", token=token, user_id=user_id)
|
||||
Reference in New Issue
Block a user