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

.NET Core中使用Authorize.Net创建预授权交易报错求助

解决Authorize.Net预授权交易在.NET Core中的请求失败问题

错误原因分析

1. XML请求错误(E00004:请求的API方法名称无效)

该错误主要由以下问题导致:

  • 请求端点错误:混淆了XML与JSON API的端点地址
  • HTTP方法错误:使用GET而非POST请求
  • Content-Type头设置错误:未指定text/xml格式
  • 命名空间拼写错误:XML根节点的命名空间不符合官方要求

2. JSON请求错误(E00001:XmlNodeConverter can only convert JSON that begins with an object)

核心问题出在请求或响应的解析逻辑上:

  • 请求端点错误:用XML端点发送JSON请求
  • Content-Type头设置错误:未指定application/json格式,导致服务器无法解析
  • 客户端解析错误:误用XML解析器处理JSON响应
  • JSON序列化错误:请求结构被意外包装成数组,或类型转换错误(如将amount序列化为字符串)

解决方案

针对XML请求的修复步骤

  1. 使用正确端点:
    沙箱环境:https://apitest.authorize.net/xml/v1/request.api
    生产环境:https://api.authorize.net/xml/v1/request.api
  2. 设置正确HTTP头:
    必须添加Content-Type: text/xml; charset=utf-8
  3. 确保请求方法为POST:
    Authorize.Net API仅接受POST请求
  4. 验证XML结构:
    你的XML结构本身合规,确认命名空间xmlns="AnetApi/xml/v1/schema/AnetApiSchema.xsd"无拼写错误

针对JSON请求的修复步骤

  1. 使用正确端点:
    沙箱环境:https://apitest.authorize.net/json/v1/request.api
    生产环境:https://api.authorize.net/json/v1/request.api
  2. 设置正确HTTP头:
    必须添加Content-Type: application/json; charset=utf-8
  3. 修正序列化/解析逻辑:
    使用System.Text.Json或Newtonsoft.Json等JSON专用工具处理请求和响应,避免用XML解析器处理JSON数据
  4. 验证JSON结构:
    确保amount为数字类型,空值字段(如company)设为null而非空字符串

.NET Core示例代码

XML请求示例

using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

public class AnetXmlAuthService
{
    private readonly HttpClient _httpClient;
    private const string SandboxEndpoint = "https://apitest.authorize.net/xml/v1/request.api";
    private const string MerchantName = "87Xn5GphzvA7";
    private const string TransactionKey = "6QSmxLHw5a647937";

    public AnetXmlAuthService()
    {
        _httpClient = new HttpClient();
    }

    public async Task<string> SendPreAuthRequest()
    {
        var xmlPayload = @"<createTransactionRequest xmlns=""AnetApi/xml/v1/schema/AnetApiSchema.xsd"">
    <merchantAuthentication>
        <name>87Xn5GphzvA7</name>
        <transactionKey>6QSmxLHw5a647937</transactionKey>
    </merchantAuthentication>
    <refId>123456</refId>
    <transactionRequest>    
        <transactionType>authOnlyTransaction</transactionType>
        <amount>2.99</amount>
        <currencyCode>USD</currencyCode>
        <payment>
            <creditCard><cardNumber>5424000000000015</cardNumber>
            <expirationDate>2025-12</expirationDate>
            <cardCode>999</cardCode>
            </creditCard>
        </payment>
        <billTo>
            <firstName>Anonymous</firstName>
            <lastName>User</lastName>
            <company />
            <address>123 Main Street</address>
            <city>Birmingham</city>
            <state>Alabama</state>
            <zip>12345</zip>
            <country>US</country>
        </billTo>
        <authorizationIndicatorType>
        <authorizationIndicator>pre</authorizationIndicator></authorizationIndicatorType>
    </transactionRequest>
</createTransactionRequest>";

        var content = new StringContent(xmlPayload, Encoding.UTF8, "text/xml");
        var response = await _httpClient.PostAsync(SandboxEndpoint, content);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
}

JSON请求示例

using System.Net.Http;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

public class AnetJsonAuthService
{
    private readonly HttpClient _httpClient;
    private const string SandboxEndpoint = "https://apitest.authorize.net/json/v1/request.api";
    private const string MerchantName = "87Xn5GphzvA7";
    private const string TransactionKey = "6QSmxLHw5a647937";

    public AnetJsonAuthService()
    {
        _httpClient = new HttpClient();
    }

    public async Task<string> SendPreAuthRequest()
    {
        var requestPayload = new
        {
            createTransactionRequest = new
            {
                merchantAuthentication = new
                {
                    name = MerchantName,
                    transactionKey = TransactionKey
                },
                refId = "123456",
                transactionRequest = new
                {
                    transactionType = "authOnlyTransaction",
                    amount = 2.99,
                    currencyCode = "USD",
                    payment = new
                    {
                        creditCard = new
                        {
                            cardNumber = "5424000000000015",
                            expirationDate = "2025-12",
                            cardCode = "999"
                        }
                    },
                    billTo = new
                    {
                        firstName = "Anonymous",
                        lastName = "User",
                        company = (string)null,
                        address = "123 Main Street",
                        city = "Birmingham",
                        state = "Alabama",
                        zip = "12345",
                        country = "US"
                    },
                    authorizationIndicatorType = new
                    {
                        authorizationIndicator = "pre"
                    }
                }
            }
        };

        var jsonContent = JsonSerializer.Serialize(requestPayload);
        var content = new StringContent(jsonContent, Encoding.UTF8, "application/json");
        var response = await _httpClient.PostAsync(SandboxEndpoint, content);
        response.EnsureSuccessStatusCode();
        return await response.Content.ReadAsStringAsync();
    }
}

关键注意事项

  • 严格区分XML与JSON的API端点,二者路径不同
  • 始终使用POST方法发送请求
  • Content-Type必须与请求格式严格匹配
  • 沙箱环境的测试密钥、卡号仅能在沙箱端点使用,不可用于生产环境

内容的提问来源于stack exchange,提问作者Andrew Wigglesworth

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 19:40:54