Collapse portal under src/, unify pnpm workspace/scripts, thin VIP topic re-exports, drop orphan vip scaffolds and one-shot scripts, ignore xhs/release, document root commands. Co-authored-by: Cursor <cursoragent@cursor.com>
110 lines
3.8 KiB
Python
110 lines
3.8 KiB
Python
"""
|
||
NOWPayments 加密货币 / 海外支付适配器。
|
||
密钥:NOWPAYMENTS_API_KEY / NOWPAYMENTS_IPN_SECRET
|
||
文档:https://documenter.getpostman.com/view/7907941/S1a32n38
|
||
"""
|
||
from __future__ import annotations
|
||
|
||
import logging
|
||
from typing import Any, Dict, Optional
|
||
|
||
import requests
|
||
|
||
from .base import PaymentProvider
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
NOWPAYMENTS_API_BASE = "https://api.nowpayments.io/v1"
|
||
|
||
|
||
class NowPaymentsProvider(PaymentProvider):
|
||
def __init__(self, api_key: str, ipn_secret: str = ""):
|
||
self.api_key = (api_key or "").strip()
|
||
self.ipn_secret = (ipn_secret or "").strip()
|
||
|
||
@property
|
||
def name(self) -> str:
|
||
return "nowpayments"
|
||
|
||
def create_order(
|
||
self,
|
||
*,
|
||
order_id: str,
|
||
name: str,
|
||
total_fee_yuan: str,
|
||
pay_type: str,
|
||
notify_url: str,
|
||
return_url: Optional[str] = None,
|
||
client_ip: Optional[str] = None,
|
||
extra: Optional[Dict[str, Any]] = None,
|
||
) -> Dict[str, Any]:
|
||
if not self.api_key:
|
||
return {
|
||
"ok": False,
|
||
"error": "NOWPAYMENTS_API_KEY not configured",
|
||
"provider": self.name,
|
||
}
|
||
|
||
extra = extra or {}
|
||
price_amount = float(extra.get("price_amount") or total_fee_yuan)
|
||
price_currency = str(extra.get("price_currency") or "usd").lower()
|
||
pay_currency = str(extra.get("pay_currency") or "usdttrc20").lower()
|
||
|
||
payload = {
|
||
"price_amount": price_amount,
|
||
"price_currency": price_currency,
|
||
"pay_currency": pay_currency,
|
||
"order_id": order_id,
|
||
"order_description": name or pay_type,
|
||
"ipn_callback_url": notify_url,
|
||
"success_url": return_url or notify_url,
|
||
"cancel_url": return_url or notify_url,
|
||
}
|
||
try:
|
||
res = requests.post(
|
||
f"{NOWPAYMENTS_API_BASE}/invoice",
|
||
json=payload,
|
||
headers={
|
||
"x-api-key": self.api_key,
|
||
"Content-Type": "application/json",
|
||
},
|
||
timeout=20,
|
||
)
|
||
data = res.json() if res.content else {}
|
||
if res.status_code >= 400:
|
||
logger.warning("nowpayments invoice failed: %s %s", res.status_code, data)
|
||
return {
|
||
"ok": False,
|
||
"error": data.get("message") or data.get("error") or res.text,
|
||
"provider": self.name,
|
||
"status": res.status_code,
|
||
}
|
||
invoice_url = data.get("invoice_url") or data.get("payment_url")
|
||
return {
|
||
"ok": True,
|
||
"provider": self.name,
|
||
"order_id": order_id,
|
||
"redirect_url": invoice_url,
|
||
"raw": data,
|
||
}
|
||
except Exception as exc:
|
||
logger.exception("nowpayments create_order")
|
||
return {"ok": False, "error": str(exc), "provider": self.name}
|
||
|
||
def verify_notify(self, data: Dict[str, Any]) -> bool:
|
||
if not self.ipn_secret:
|
||
return bool(data.get("order_id") or data.get("payment_id"))
|
||
return bool(data.get("order_id") or data.get("payment_id"))
|
||
|
||
def parse_notify(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
||
return {
|
||
"order_id": str(data.get("order_id") or ""),
|
||
"trade_no": str(data.get("payment_id") or data.get("invoice_id") or ""),
|
||
"pay_price": str(data.get("price_amount") or data.get("actually_paid") or ""),
|
||
"pay_time": str(data.get("updated_at") or data.get("created_at") or ""),
|
||
"trade_status": str(data.get("payment_status") or data.get("status") or ""),
|
||
}
|
||
|
||
def success_response(self) -> str:
|
||
return "OK"
|