INNER CODE UNIT · Python
stripe_webhook
liyupi/free-video-downloader · backend/api_payment.py:109
async def stripe_webhook(request: Request):
"""
Stripe Webhook 回调处理。
幂等性由 complete_order 保证:只有 pending 状态的订单才会被处理。
"""
payload = await request.body()
sig_header = request.headers.get("stripe-signature")
webhook_secret = _get_config("STRIPE_WEBHOOK_SECRET")
if not webhook_secret:
return JSONResponse(status_code=400, content={"error": "Webhook secret not configured"})
stripe.api_key = _get_config("STRIPE_SECRET_KEY")
try:
event = stripe.Webhook.construct_event(payload, sig_header, webhook_secret)
except ValueError:
return JSONResponse(status_code=400, content={"error": "Invalid payload"})