You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

FastAPI支付回调接口接收请求时返回422 Unprocessable Entity错误排查求助

FastAPI支付回调接口接收请求时返回422 Unprocessable Entity错误排查求助

各位好,我目前正在对接第三方支付API的回调功能,现在处于测试接收支付成功通知的开发阶段。

在测试账号验证过程中,我的FastAPI接口返回了422错误,日志如下:

INFO: 35.233.203.43:3072 - "POST /callback HTTP/1.1" 422

Unprocessable Entity.

我写的接口代码是这样的(接收x-www-form-urlencoded格式的表单数据):

from fastapi import FastAPI, HTTPException, Form 
import logging 
from app.services.database import Session 
from app.models.user_model import User 
 
# 配置日志
logging.basicConfig(level=logging.INFO) 
 
api_app = FastAPI() 
 
@api_app.post("/callback") 
async def handle_callback( 
    status: str = Form(...),          # 必填字段
    invoice_id: str = Form(...),      # 必填字段
    amount_crypto: float = Form(...), # 必填字段
    currency: str = Form(...),        # 必填字段
    order_id: str = Form(...),        # 必填字段
    token: str = Form(...)            # 必填字段
): 
    """ 
    处理Content-Type为x-www-form-urlencoded的POST请求
    """ 
    try: 
        # 记录入参日志
        logging.info(f"状态: {status}, 发票ID: {invoice_id}, 加密货币金额: {amount_crypto}, " 
                     f"币种: {currency}, 订单ID: {order_id}, 令牌: {token}") 
 
        # 校验必填字段
        if not all([status, invoice_id, amount_crypto, currency, order_id, token]): 
            logging.error("请求数据不完整") 
            raise HTTPException(status_code=400, detail="请求数据不完整") 
 
        # 业务处理逻辑
        if status == "success": 
            logging.info(f"支付处理成功: 订单ID={order_id}, 金额={amount_crypto} {currency}") 
            
        else: 
            logging.warning(f"状态为{status}的支付未处理") 
            return {"message": f"状态为{status}的支付已接收,但未处理"} 
 
        return {"message": "回调处理成功"} 
    except Exception as e: 
        logging.error(f"处理回调时出错: {str(e)}") 
        raise HTTPException(status_code=500, detail="服务器内部错误")

第三方支付方表示回调请求是x-www-form-urlencoded格式,但他们提供的请求示例数据是这样的:

{
    "status": "success",
    "invoice_id": "XXXXXXXX",
    "amount_crypto": 100,
    "currency": "USDT_TRC20",
    "order_id": "order_id",
    "token": "token",
    "invoice_info": {
        "uuid": "INV-XXXXXXXX",
        "created": "2024-08-22 11:49:59.756692",
        "address": "address",
        "currency": {
            "id": 4,
            "code": "USDT",
            "fullcode": "USDT_TRC20",
            "network": {
                "code": "TRC20",
                "id": 4,
                "icon": "[图标URL]",
                "fullname": "Tron"
            },
            "name": "Tether",
            "is_email_required": false,
            "stablecoin": true,
            "icon_base": "[图标URL]",
            "icon_network": "[图标URL]",
            "icon_qr": "[图标URL]",
            "order": 1
        },
        "date_finished": "2024-08-22 11:51:53.753528",
        "expiry_date": "2024-08-23 11:49:59.746385",
        "side_commission": "client",
        "type_payments": "crypto",
        "amount": 100,
        "amount_": 100,
        "status": "overpaid",
        "invoice_status": "success",
        "is_email_required": false,
        "project": {
            "id": 0,
            "name": "My Project",
            "fail": "",
            "success": "",
            "logo": ""
        },
        "tx_list": [
            ""
        ],
        "amount_in_crypto": null,
        "amount_in_fiat": 0.0,
        "amount_usd": 100.0,
        "amount_to_pay": 102.0,
        "amount_to_pay_usd": 102.0,
        "amount_paid": 102.0,
        "amount_paid_usd": 102.0,
        "fee": 1.4,
        "fee_usd": 1.4,
        "service_fee": 0.8048,
        "service_fee_usd": 0.8,
        "received": 99.7952,
        "received_usd": 99.8,
        "to_surcharge": 0.0,
        "to_surcharge_usd": 0.0,
        "total_rub": 0,
        "step": 3,
        "test_mode": false,
        "type": "up",
        "aml_enabled": false,
        "aml_side": "merchant",
        "aml_checks": [],
        "links_invoice": null
    }
}

我已经确认接口是按照表单格式来接收参数的,但还是一直收到422错误,有没有大佬能帮我分析一下问题可能出在哪里?比如是不是参数名不匹配?或者对方实际发送的是JSON格式而不是表单?

备注:内容来源于stack exchange,提问作者Мориарти

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 15:49:34