指南
FastAPI
在 FastAPI 应用中集成 StableOps。
安装
pip install stableops fastapi uvicorn python-dotenv配置
STABLEOPS_API_KEY=sk_sandbox_...
STABLEOPS_WEBHOOK_SECRET=whsec_...
STABLEOPS_API_URL=https://api.stableops.dev创建 lib/stableops.py:
import os
from stableops import StableOps
stableops = StableOps(
api_key=os.environ["STABLEOPS_API_KEY"],
environment="sandbox",
base_url=os.getenv("STABLEOPS_API_URL", "https://api.stableops.dev"),
)创建支付单
from datetime import datetime, timedelta, timezone
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
from lib.stableops import stableops
router = APIRouter()
class CreateOrderRequest(BaseModel):
merchant_order_id: str
amount: str
@router.post("/orders")
def create_order(input: CreateOrderRequest):
try:
# 30 分钟后未支付自动过期,订单进入 expired 并释放地址。
expires_at = (datetime.now(timezone.utc) + timedelta(minutes=30)).isoformat()
order = stableops.payment_orders.create(
merchant_order_id=input.merchant_order_id,
amount=input.amount,
settlement_asset="USDC",
accepted_assets=[{"chain": "base", "asset": "USDC"}],
expires_at=expires_at,
)
return {
"id": order.id,
"status": order.status,
"amount": order.amount,
"payment_instructions": [
instruction.model_dump() for instruction in order.payment_instructions
],
}
except Exception as exc:
raise HTTPException(status_code=500, detail=str(exc)) from excPython SDK 会用 merchant_order_id 同时作为 Idempotency-Key 请求头,
保证创建请求可安全重试。
验证 Webhook
import json
import os
from fastapi import APIRouter, Header, Request, Response
from stableops.webhooks import SIGNATURE_HEADER, verify_webhook_signature
router = APIRouter()
@router.post("/webhooks/stableops")
async def stableops_webhook(
request: Request,
x_product_signature: str | None = Header(default=None, alias=SIGNATURE_HEADER),
):
raw_body = await request.body()
result = verify_webhook_signature(
body=raw_body,
header=x_product_signature,
secret=os.environ["STABLEOPS_WEBHOOK_SECRET"],
)
if not result.valid:
return Response(f"invalid signature: {result.reason}", status_code=400)
event = json.loads(raw_body)
if event["type"] == "payment.finalized":
payment_order_id = event["data"]["payment_order_id"]
# 按 X-Event-Id 去重后再履约。
return Response("ok")