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

ASP.NET Core Web API集成Square支付遇CreateAsync方法报错求助

ASP.NET Core Web API集成Square支付报错问题

控制器代码

public class SquareController : Controller
{
    private readonly ISquareServices _services;
    public SquareController(ISquareServices squareServices)
    {
        _services = squareServices;
    }
    [HttpPost("customer/add")]
    public async Task<ActionResult> AddSquearCustomer([FromBody] AddDetails addDetails, CancellationToken cancellationToken = default(CancellationToken))
    {
        var result = await _services.CreatePaymentAsync(addDetails, cancellationToken);

        return StatusCode(StatusCodes.Status200OK, result);
    }
}

服务层代码

public class SquareServices : ISquareServices
{
    private readonly CreatePaymentRequest _createPaymentRequest;
    public SquareServices(CreatePaymentRequest createPaymentRequest)
    {
        _createPaymentRequest = createPaymentRequest;
    }
    public async Task<CreatePaymentResponse> CreatePaymentAsync(AddDetails addDetails, CancellationToken cancellationToken = default(CancellationToken))
    {
        var response = await _createPaymentRequest.CreateAsync(addDetails, cancellationToken);
        return (response);
    }
}

接口代码

public interface ISquareServices
{
    Task<CreatePaymentResponse> CreatePaymentAsync(AddDetails addDetails, CancellationToken cancellationToken = default(CancellationToken));
}

报错信息

CreatePaymentRequest 不包含 CreateAsync 的定义,也没有可访问的扩展方法 CreateAsync 接受类型为 CreatePaymentRequest 的第一个参数,报错行位于服务层的 var response = await _createPaymentRequest.CreateAsync(addDetails, cancellationToken); 处。


问题原因及修复方案

问题核心是对Square SDK的使用逻辑错误:CreatePaymentRequest只是请求数据模型,本身不具备发起请求的能力,真正执行支付请求需要通过Square SDK提供的PaymentsApi客户端类。

修复步骤

  1. 替换服务层依赖:将注入的CreatePaymentRequest改为PaymentsApi客户端
  2. 动态构建请求模型:在支付方法内根据业务参数生成CreatePaymentRequest实例
  3. 通过客户端发起请求:调用PaymentsApi的CreatePaymentAsync方法执行支付

修改后的服务层代码:

public class SquareServices : ISquareServices
{
    private readonly PaymentsApi _paymentsApi;

    // 注入Square官方支付客户端
    public SquareServices(PaymentsApi paymentsApi)
    {
        _paymentsApi = paymentsApi;
    }

    public async Task<CreatePaymentResponse> CreatePaymentAsync(AddDetails addDetails, CancellationToken cancellationToken = default(CancellationToken))
    {
        // 构建支付请求模型,补充必要参数
        var paymentRequest = new CreatePaymentRequest
        {
            IdempotencyKey = Guid.NewGuid().ToString(), // 唯一标识,防止重复提交
            AmountMoney = new Money
            {
                Amount = addDetails.Amount, // 注意单位为最小货币单位(如100代表1美元)
                Currency = "USD" // 根据业务设置对应币种
            },
            SourceId = addDetails.PaymentToken // 前端传入的支付令牌
            // 可根据需求添加其他参数:如订单ID、备注等
        };

        // 通过PaymentsApi发起支付请求
        return await _paymentsApi.CreatePaymentAsync(paymentRequest, cancellationToken: cancellationToken);
    }
}

补充客户端配置

在Program.cs中注册Square客户端的依赖注入:

builder.Services.AddSingleton<PaymentsApi>(sp =>
{
    var squareConfig = new SquareClientConfiguration.Builder()
        .AccessToken("你的Square访问令牌")
        .Environment(Square.Environment.Sandbox) // 测试环境用Sandbox,生产切换为Production
        .Build();
    return new PaymentsApi(squareConfig);
});
builder.Services.AddScoped<ISquareServices, SquareServices>();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 04:15:37