AlipayEasyV11 1.0.0

dotnet add package AlipayEasyV11 --version 1.0.0
                    
NuGet\Install-Package AlipayEasyV11 -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AlipayEasyV11" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AlipayEasyV11" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="AlipayEasyV11" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AlipayEasyV11 --version 1.0.0
                    
#r "nuget: AlipayEasyV11, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package AlipayEasyV11@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AlipayEasyV11&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=AlipayEasyV11&version=1.0.0
                    
Install as a Cake Tool

AlipaySdk

NuGet NuGet License

支付宝支付 SDK,支持扫码支付、APP 支付、小程序支付、密钥验证等功能。

功能特性

  • ✅ 扫码支付(预下单)
  • ✅ APP 支付
  • ✅ 小程序支付
  • ✅ 订单查询
  • ✅ 订单关闭
  • ✅ 退款处理
  • ✅ 异步回调处理
  • ✅ 自动签名验签
  • ✅ 密钥格式验证(PKCS1/PKCS8/纯 Base64)
  • ✅ 支持 .NET Standard 2.0 / .NET 6.0 / .NET 8.0 / .NET 9.0

安装

dotnet add package AlipaySdk

快速开始

1. 配置

using AlipaySdk;

var config = new AlipayConfig
{
    AppId = "你的AppId",
    AlipayPublicKey = "支付宝公钥",
    PrivateKey = "应用私钥",
    GatewayUrl = "https://openapi.alipay.com/gateway.do",
    SignType = "RSA2",
    Charset = "UTF-8",
    NotifyUrl = "https://你的域名/api/alipay/notify"
};

2. 创建客户端

using var client = new AlipayClient(config);

3. 创建订单

var result = await client.CreatePreOrderAsync(
    subject: "商品标题",
    outTradeNo: "商户订单号",
    totalAmount: 99.00m,
    body: "商品描述"
);

if (result.Success && result.Data != null)
{
    Console.WriteLine($"订单号: {result.Data.OutTradeNo}");
    Console.WriteLine($"二维码: {result.Data.QrCode}");
}

4. 处理异步回调

[HttpPost("notify")]
public async Task<IActionResult> AlipayNotify()
{
    var parameters = new Dictionary<string, string>();
    foreach (var key in Request.Form.Keys)
    {
        parameters[key] = Request.Form[key].ToString();
    }

    // 验证签名
    if (!client.VerifyNotify(parameters))
    {
        return Ok("fail");
    }

    // 解析通知
    var notify = client.ParseNotify(parameters);

    // 处理业务逻辑
    if (notify.TradeStatus == TradeStatus.TRADE_SUCCESS)
    {
        // 支付成功
    }

    return Ok("success");
}

API 文档

AlipayClient

方法 说明
CreatePreOrderAsync 创建扫码支付订单
CreateAppOrderAsync 创建 APP 支付订单
CreateMiniAppOrderAsync 创建小程序支付订单
QueryOrderAsync 查询订单
QueryOrderByTradeNoAsync 通过交易号查询
CloseOrderAsync 关闭订单
RefundAsync 申请退款
VerifyNotify 验证回调签名
ParseNotify 解析回调数据
GenerateQrCode 生成二维码图片

AlipayClient (静态方法)

方法 说明
VerifyPrivateKey 验证私钥格式
VerifyPublicKey 验证公钥格式
LoadPrivateKeyFromFile 从文件加载私钥
LoadPublicKeyFromFile 从文件加载公钥

密钥格式

支持以下密钥格式:

  • 私钥:PKCS8 格式(-----BEGIN PRIVATE KEY-----
  • 私钥:PKCS1 格式(-----BEGIN RSA PRIVATE KEY-----
  • 私钥:纯 Base64(无 PEM 头)
  • 公钥:PKCS1 格式(-----BEGIN RSA PUBLIC KEY-----
  • 公钥:PKCS8 格式(-----BEGIN PUBLIC KEY-----
  • 公钥:纯 Base64(无 PEM 头)

示例项目

查看 samples/ 目录获取完整示例:

  • SampleConsole/ - 控制台应用示例
  • SampleAspNet/ - ASP.NET Core 示例

常见问题

Q: 签名验证失败怎么办?

  1. 确认使用的是支付宝公钥,不是应用公钥
  2. 检查私钥和公钥是否配对
  3. 确保签名原文包含 sign_type 参数

Q: 支持沙箱环境吗?

支持,只需修改 GatewayUrl 为沙箱地址:

config.GatewayUrl = "https://openapi.alipaydev.com/gateway.do";

许可证

MIT License

贡献

欢迎提交 Issue 和 Pull Request!

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 125 2/6/2026