forked from hesabix/arc
Public pay, bank-return, and receipt pages now use the app Persian font, and show APP_NAME_FA from branding instead of a hardcoded Hesabix label. Updates also ensure nginx proxies /u/ to the backend HTML. Co-authored-by: Cursor <cursoragent@cursor.com>
455 lines
15 KiB
Python
Executable file
455 lines
15 KiB
Python
Executable file
"""
|
||
ماژول برای مدیریت پاسخهای صفحات پرداخت
|
||
شامل تشخیص هوشمند منبع (اپ/موبایل/دسکتاپ) و render کردن template های HTML
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
from pathlib import Path
|
||
from typing import Any, Dict, Optional
|
||
from fastapi import Request
|
||
from fastapi.responses import HTMLResponse
|
||
from jinja2 import Environment, FileSystemLoader, select_autoescape
|
||
import json
|
||
import os
|
||
from datetime import datetime
|
||
|
||
from sqlalchemy.orm import Session
|
||
|
||
from app.core.runtime_paths import REPOSITORY_ROOT
|
||
|
||
|
||
# تنظیم Jinja2 environment
|
||
TEMPLATE_DIR = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "templates")
|
||
jinja_env = Environment(
|
||
loader=FileSystemLoader(TEMPLATE_DIR),
|
||
autoescape=select_autoescape(['html', 'xml']),
|
||
enable_async=False
|
||
)
|
||
|
||
|
||
def format_number(value):
|
||
"""فرمت کردن عدد با جداکننده هزارگان"""
|
||
try:
|
||
return "{:,}".format(int(value))
|
||
except (ValueError, TypeError):
|
||
return str(value)
|
||
|
||
|
||
# اضافه کردن فیلتر به jinja
|
||
jinja_env.filters['format_number'] = format_number
|
||
|
||
|
||
def detect_source(request: Request, source_param: Optional[str] = None) -> str:
|
||
"""
|
||
تشخیص منبع درخواست (اپ/موبایل/دسکتاپ)
|
||
|
||
اولویت:
|
||
1. پارامتر source از URL (دقیقترین)
|
||
2. User-Agent header (پشتیبان)
|
||
|
||
Returns:
|
||
'app': از اپلیکیشن موبایل
|
||
'mobile_web': از مرورگر موبایل
|
||
'desktop': از مرورگر دسکتاپ
|
||
"""
|
||
# اولویت اول: پارامتر صریح
|
||
if source_param:
|
||
source_param = source_param.lower().strip()
|
||
if source_param in ['app', 'mobile_app', 'mobile-app']:
|
||
return 'app'
|
||
elif source_param in ['mobile_web', 'mobile-web', 'mobile']:
|
||
return 'mobile_web'
|
||
elif source_param in ['desktop', 'web']:
|
||
return 'desktop'
|
||
|
||
# اولویت دوم: User-Agent
|
||
user_agent = request.headers.get('user-agent', '').lower()
|
||
|
||
# تشخیص اپلیکیشن موبایل (معمولاً اپها User-Agent خاصی دارند)
|
||
if 'hesabix' in user_agent or 'flutter' in user_agent or 'dart' in user_agent:
|
||
return 'app'
|
||
|
||
# تشخیص موبایل
|
||
mobile_keywords = ['android', 'iphone', 'ipad', 'ipod', 'mobile', 'webos', 'blackberry', 'windows phone']
|
||
if any(keyword in user_agent for keyword in mobile_keywords):
|
||
return 'mobile_web'
|
||
|
||
# پیشفرض: دسکتاپ
|
||
return 'desktop'
|
||
|
||
|
||
def detect_is_mobile(user_agent: str) -> bool:
|
||
"""تشخیص اینکه درخواست از موبایل است یا نه"""
|
||
mobile_keywords = ['android', 'iphone', 'ipad', 'ipod', 'mobile', 'webos', 'blackberry', 'windows phone']
|
||
return any(keyword in user_agent.lower() for keyword in mobile_keywords)
|
||
|
||
|
||
def resolve_frontend_base_url(db: Session) -> str:
|
||
"""
|
||
دامنهٔ اپ وب برای لینکهای بازگشت از درگاه.
|
||
اولویت: تنظیمات لینکهای اشتراک (DB) → share_link_public_app_url → app_public_url
|
||
"""
|
||
from app.core.settings import get_settings
|
||
from app.services.system_settings_service import resolve_public_app_base_url_for_public_links
|
||
|
||
base = (resolve_public_app_base_url_for_public_links(db) or "").strip().rstrip("/")
|
||
if base:
|
||
return base
|
||
settings = get_settings()
|
||
for candidate in (settings.app_public_url, settings.share_link_public_app_url):
|
||
fallback = (candidate or "").strip().rstrip("/")
|
||
if fallback.lower().endswith("/public"):
|
||
fallback = fallback[: -len("/public")].rstrip("/")
|
||
if fallback:
|
||
return fallback
|
||
return ""
|
||
|
||
|
||
def join_frontend_url(base: str, path: str) -> str:
|
||
"""اتصال امن base + path (path همیشه با / شروع میشود)."""
|
||
base = (base or "").strip().rstrip("/")
|
||
path = path if (path or "").startswith("/") else f"/{path or ''}"
|
||
if not base:
|
||
return path
|
||
return f"{base}{path}"
|
||
|
||
|
||
_GENERIC_BRAND_NAMES = frozenset(
|
||
{
|
||
"",
|
||
"hesabix",
|
||
"hesabix api",
|
||
"hesabixapi",
|
||
"حسابیکس",
|
||
"حسابیکس",
|
||
}
|
||
)
|
||
_PAY_PAGE_FONT_FILES = frozenset(
|
||
{
|
||
"YekanBakhFaNum-Regular.woff2",
|
||
"YekanBakhFaNum-Regular.ttf",
|
||
"YekanBakhFaNum-SemiBold.woff2",
|
||
"YekanBakhFaNum-SemiBold.ttf",
|
||
"YekanBakhFaNum-Bold.woff2",
|
||
"YekanBakhFaNum-Bold.ttf",
|
||
"YekanBakhFaNum-ExtraBold.woff2",
|
||
"YekanBakhFaNum-ExtraBold.ttf",
|
||
}
|
||
)
|
||
|
||
|
||
def _normalize_brand_key(value: str) -> str:
|
||
return (value or "").replace("\u200c", "").strip().lower()
|
||
|
||
|
||
def _is_generic_brand_name(value: str) -> bool:
|
||
return _normalize_brand_key(value) in _GENERIC_BRAND_NAMES
|
||
|
||
|
||
def _parse_env_assignments(path: Path) -> Dict[str, str]:
|
||
out: Dict[str, str] = {}
|
||
try:
|
||
raw = path.read_text(encoding="utf-8", errors="replace")
|
||
except OSError:
|
||
return out
|
||
for line in raw.splitlines():
|
||
text = line.strip()
|
||
if not text or text.startswith("#") or "=" not in text:
|
||
continue
|
||
key, _, value = text.partition("=")
|
||
out[key.strip()] = value.strip().strip('"').strip("'")
|
||
return out
|
||
|
||
|
||
def _names_from_branding_json(directory: str) -> tuple[str, str]:
|
||
root = Path(directory)
|
||
for candidate in (root / "names.json", root / "meta" / "names.json"):
|
||
if not candidate.is_file():
|
||
continue
|
||
try:
|
||
data = json.loads(candidate.read_text(encoding="utf-8"))
|
||
except Exception:
|
||
continue
|
||
if not isinstance(data, dict):
|
||
continue
|
||
fa = str(data.get("fa") or data.get("fa_IR") or data.get("persian") or "").strip()
|
||
en = str(data.get("en") or data.get("en_US") or data.get("english") or "").strip()
|
||
if fa or en:
|
||
return fa, en
|
||
return "", ""
|
||
|
||
|
||
def branding_display_names() -> tuple[str, str]:
|
||
"""نام فارسی/انگلیسی ریبرند از env یا .deploy_env (همان منبع Flutter)."""
|
||
fa = (os.environ.get("APP_NAME_FA") or "").strip()
|
||
en = (os.environ.get("APP_NAME_EN") or "").strip()
|
||
mode = (os.environ.get("BRANDING_MODE") or "").strip().lower()
|
||
branding_dir = (os.environ.get("BRANDING_DIR") or "").strip()
|
||
env_path = Path(
|
||
os.environ.get("HESABIX_DEPLOY_ENV")
|
||
or os.environ.get("HESABIX_DEPLOY_ENV_FILE")
|
||
or "/opt/hesabix/.deploy_env"
|
||
)
|
||
if env_path.is_file():
|
||
parsed = _parse_env_assignments(env_path)
|
||
mode = (parsed.get("BRANDING_MODE") or mode).strip().lower() or mode
|
||
branding_dir = (parsed.get("BRANDING_DIR") or branding_dir).strip()
|
||
if not fa:
|
||
fa = (parsed.get("APP_NAME_FA") or "").strip()
|
||
if not en:
|
||
en = (parsed.get("APP_NAME_EN") or "").strip()
|
||
if mode == "custom" and branding_dir and (not fa or not en):
|
||
json_fa, json_en = _names_from_branding_json(branding_dir)
|
||
fa = fa or json_fa
|
||
en = en or json_en
|
||
if mode and mode not in ("custom", "1", "true", "yes"):
|
||
# حالت پیشفرض حسابیکس: نام سفارشی را اعمال نکن
|
||
if mode == "default":
|
||
return "", ""
|
||
return fa, en
|
||
|
||
|
||
def resolve_payment_display_name(db: Session) -> str:
|
||
"""نام نمایشی برند برای صفحات HTML پرداخت (ریبرند، سپس تنظیمات سیستم)."""
|
||
from app.services.system_settings_service import get_app_name
|
||
|
||
fa, en = branding_display_names()
|
||
if fa:
|
||
return fa
|
||
if en:
|
||
return en
|
||
name = (get_app_name(db) or "").strip()
|
||
if name.lower().endswith(" api"):
|
||
name = name[:-4].strip()
|
||
if _is_generic_brand_name(name):
|
||
return "حسابیکس"
|
||
return name
|
||
|
||
|
||
def rebrand_product_text(text: str, app_name: str | None) -> str:
|
||
"""جایگزینی حسابیکس/Hesabix در متنهای ثابت صفحات پرداخت."""
|
||
name = (app_name or "").strip()
|
||
if not text or not name or _is_generic_brand_name(name):
|
||
return text
|
||
return (
|
||
text.replace("حسابیکس", name)
|
||
.replace("حسابیکس", name)
|
||
.replace("Hesabix", name)
|
||
)
|
||
|
||
|
||
def pay_page_font_path(font_name: str) -> Optional[Path]:
|
||
"""مسیر فایل فونت فارسی باندلشده برای صفحات عمومی پرداخت."""
|
||
name = Path(str(font_name or "")).name
|
||
if name not in _PAY_PAGE_FONT_FILES:
|
||
return None
|
||
ui_root = REPOSITORY_ROOT / "hesabixUI" / "hesabix_ui"
|
||
for folder in (ui_root / "web" / "fonts", ui_root / "assets" / "fonts"):
|
||
path = folder / name
|
||
if path.is_file():
|
||
return path
|
||
return None
|
||
|
||
|
||
def pay_page_font_file_uri(font_name: str) -> str:
|
||
path = pay_page_font_path(font_name)
|
||
return path.resolve().as_uri() if path is not None else ""
|
||
|
||
|
||
def wallet_return_urls(db: Session, business_id: Optional[int] = None) -> Dict[str, str]:
|
||
"""URLهای وب برای دکمههای صفحه نتیجه شارژ کیف پول."""
|
||
base = resolve_frontend_base_url(db)
|
||
if business_id:
|
||
wallet_path = f"/business/{int(business_id)}/wallet"
|
||
dashboard = join_frontend_url(base, wallet_path)
|
||
retry = dashboard
|
||
else:
|
||
dashboard = join_frontend_url(base, "/user/profile/dashboard")
|
||
retry = join_frontend_url(base, "/user/profile/dashboard")
|
||
support = join_frontend_url(base, "/user/profile/support")
|
||
return {
|
||
"dashboard_url": dashboard,
|
||
"retry_url": retry,
|
||
"support_url": support,
|
||
}
|
||
|
||
|
||
def support_return_urls(db: Session) -> Dict[str, str]:
|
||
"""URLهای وب برای دکمههای صفحه نتیجه پرداخت پشتیبانی."""
|
||
base = resolve_frontend_base_url(db)
|
||
billing = join_frontend_url(base, "/user/profile/support/billing")
|
||
support = join_frontend_url(base, "/user/profile/support")
|
||
return {
|
||
"dashboard_url": billing,
|
||
"retry_url": billing,
|
||
"support_url": support,
|
||
}
|
||
|
||
|
||
def _payment_template_context(
|
||
*,
|
||
source: str,
|
||
app_name: Optional[str],
|
||
deep_link_scheme: str,
|
||
extra: Dict[str, Any],
|
||
) -> Dict[str, Any]:
|
||
ctx = {
|
||
"timestamp": datetime.now().strftime("%Y/%m/%d - %H:%M"),
|
||
"source": source,
|
||
"app_name": app_name or "حسابیکس",
|
||
"deep_link_scheme": deep_link_scheme or "hesabix",
|
||
}
|
||
ctx.update(extra)
|
||
return ctx
|
||
|
||
|
||
def render_payment_success(
|
||
request: Request,
|
||
transaction_id: int,
|
||
amount: float,
|
||
external_ref: str,
|
||
card_num: Optional[str] = None,
|
||
source: Optional[str] = None,
|
||
dashboard_url: str = "/",
|
||
app_name: Optional[str] = None,
|
||
deep_link_scheme: str = "hesabix",
|
||
) -> HTMLResponse:
|
||
"""
|
||
رندر صفحه موفقیت پرداخت
|
||
|
||
Args:
|
||
request: FastAPI Request object
|
||
transaction_id: شماره تراکنش
|
||
amount: مبلغ پرداخت شده
|
||
external_ref: شماره پیگیری
|
||
card_num: شماره کارت (اختیاری)
|
||
source: منبع درخواست (app/mobile_web/desktop)
|
||
dashboard_url: URL داشبورد
|
||
app_name: نام برند برای نمایش در صفحه
|
||
deep_link_scheme: اسکیمای deep link اپ (پیشفرض hesabix)
|
||
"""
|
||
if source is None:
|
||
source = detect_source(request)
|
||
|
||
template = jinja_env.get_template('payment/success.html')
|
||
|
||
# تنظیم URL داشبورد بر اساس منبع
|
||
if source == 'app':
|
||
scheme = deep_link_scheme or "hesabix"
|
||
dashboard_url = f"{scheme}://dashboard"
|
||
|
||
context = _payment_template_context(
|
||
source=source,
|
||
app_name=app_name,
|
||
deep_link_scheme=deep_link_scheme,
|
||
extra={
|
||
'transaction_id': transaction_id,
|
||
'amount': amount,
|
||
'external_ref': external_ref,
|
||
'card_num': card_num,
|
||
'dashboard_url': dashboard_url,
|
||
},
|
||
)
|
||
|
||
html_content = template.render(**context)
|
||
return HTMLResponse(content=html_content, status_code=200)
|
||
|
||
|
||
def render_payment_failed(
|
||
request: Request,
|
||
transaction_id: int,
|
||
external_ref: Optional[str] = None,
|
||
error_message: Optional[str] = None,
|
||
error_code: Optional[str] = None,
|
||
source: Optional[str] = None,
|
||
retry_url: str = "/",
|
||
dashboard_url: str = "/",
|
||
support_url: str = "/support",
|
||
app_name: Optional[str] = None,
|
||
deep_link_scheme: str = "hesabix",
|
||
) -> HTMLResponse:
|
||
"""
|
||
رندر صفحه شکست پرداخت
|
||
|
||
Args:
|
||
request: FastAPI Request object
|
||
transaction_id: شماره تراکنش
|
||
external_ref: شماره پیگیری (اختیاری)
|
||
error_message: پیام خطا
|
||
error_code: کد خطا
|
||
source: منبع درخواست
|
||
retry_url: URL برای تلاش مجدد
|
||
dashboard_url: URL داشبورد
|
||
support_url: URL پشتیبانی
|
||
app_name: نام برند برای نمایش در صفحه
|
||
deep_link_scheme: اسکیمای deep link اپ
|
||
"""
|
||
if source is None:
|
||
source = detect_source(request)
|
||
|
||
template = jinja_env.get_template('payment/failed.html')
|
||
|
||
# تنظیم URLها بر اساس منبع
|
||
if source == 'app':
|
||
scheme = deep_link_scheme or "hesabix"
|
||
dashboard_url = f"{scheme}://dashboard"
|
||
retry_url = f"{scheme}://wallet/topup"
|
||
support_url = f"{scheme}://support"
|
||
|
||
context = _payment_template_context(
|
||
source=source,
|
||
app_name=app_name,
|
||
deep_link_scheme=deep_link_scheme,
|
||
extra={
|
||
'transaction_id': transaction_id,
|
||
'external_ref': external_ref,
|
||
'error_message': error_message or 'خطایی در پردازش تراکنش رخ داده است.',
|
||
'error_code': error_code,
|
||
'retry_url': retry_url,
|
||
'dashboard_url': dashboard_url,
|
||
'support_url': support_url,
|
||
},
|
||
)
|
||
|
||
html_content = template.render(**context)
|
||
return HTMLResponse(content=html_content, status_code=200)
|
||
|
||
|
||
def render_public_business_pay_page(
|
||
context: Dict[str, Any],
|
||
*,
|
||
status_code: int = 200,
|
||
template_name: str = "public/pay_page.html",
|
||
) -> HTMLResponse:
|
||
"""صفحهٔ HTML عمومی لینک واریز کسبوکار (/u/{username}) بدون بارگذاری Flutter."""
|
||
template = jinja_env.get_template(template_name)
|
||
html_content = template.render(**context)
|
||
return HTMLResponse(
|
||
content=html_content,
|
||
status_code=status_code,
|
||
headers={"Cache-Control": "no-store, no-cache, must-revalidate"},
|
||
)
|
||
|
||
|
||
def should_return_json(request: Request) -> bool:
|
||
"""
|
||
تشخیص اینکه باید JSON برگردونیم یا HTML
|
||
|
||
بر اساس:
|
||
1. Accept header
|
||
2. پارامتر format در URL
|
||
"""
|
||
# بررسی پارامتر format
|
||
format_param = request.query_params.get('format', '').lower()
|
||
if format_param == 'json':
|
||
return True
|
||
if format_param == 'html':
|
||
return False
|
||
|
||
# بررسی Accept header
|
||
accept = request.headers.get('accept', '').lower()
|
||
if 'application/json' in accept and 'text/html' not in accept:
|
||
return True
|
||
|
||
# پیشفرض: HTML برای تجربه بهتر کاربری
|
||
return False
|