Alipay 全球开放 SDK .NET 项目使用教程
global-open-sdk-dotnet项目地址:https://gitcode.com/gh_mirrors/gl/global-open-sdk-dotnet
1. 项目的目录结构及介绍
global-open-sdk-dotnet/
├── ams-dotnet/
│ ├── src/
│ │ ├── example/
│ │ │ └── Program.cs
│ │ └── ...
│ ├── .gitignore
│ ├── LICENSE
│ ├── README.md
│ └── ...
├── .gitignore
├── LICENSE
└── README.md
ams-dotnet/
: 主项目目录,包含所有源代码和示例。src/
: 源代码目录。example/
: 示例代码目录,包含一个示例启动文件Program.cs
。
.gitignore
: Git 忽略文件。LICENSE
: 项目许可证文件。README.md
: 项目说明文件。
- 根目录下的
.gitignore
,LICENSE
, 和README.md
文件与ams-dotnet/
目录下的文件相同。
2. 项目的启动文件介绍
在 ams-dotnet/src/example/
目录下,有一个示例启动文件 Program.cs
。这个文件展示了如何使用 Alipay 全球开放 SDK 进行基本的支付操作。
// 示例代码片段
using System;
using Alipay.AopSdk.Core;
namespace Alipay.AopSdk.Example
{
class Program
{
static void Main(string[] args)
{
// 初始化配置
IAopClient client = new DefaultAopClient(
gatewayUrl: "the_alipay_gateway_endpoint",
appId: "your_client_id_here",
privateKey: "your_private_key_here",
format: "json",
version: "1.0",
signType: "RSA2",
alipayPublicKey: "your_public_key_here"
);
// 创建支付请求
AlipayTradePayRequest request = new AlipayTradePayRequest();
request.BizContent = "{" +
"\"out_trade_no\":\"20150320010101001\"," +
"\"scene\":\"bar_code\"," +
"\"auth_code\":\"28763443825664394\"," +
"\"subject\":\"Iphone6 16G\"," +
"\"total_amount\":\"0.01\"," +
"\"undiscountable_amount\":\"0.01\"" +
"}";
// 发送请求并获取响应
AlipayTradePayResponse response = client.Execute(request);
if (response.IsError)
{
Console.WriteLine("Error: " + response.Msg);
}
else
{
Console.WriteLine("Success: " + response.Body);
}
}
}
}
3. 项目的配置文件介绍
在项目中,配置信息通常在代码中硬编码,如上述示例中的 DefaultAopClient
初始化部分。这些配置信息包括:
gatewayUrl
: Alipay 网关地址。appId
: 客户端 ID。privateKey
: 私钥。alipayPublicKey
: Alipay 公钥。
为了更好地管理配置,建议将这些信息提取到一个单独的配置文件中,例如 appsettings.json
:
{
"AlipayConfig": {
"GatewayUrl": "the_alipay_gateway_endpoint",
"AppId": "your_client_id_here",
"PrivateKey": "your_private_key_here",
"AlipayPublicKey": "your_public_key_here"
}
}
然后在代码中读取这些配置:
// 读取配置文件
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.Build();
var alipayConfig = configuration.GetSection("AlipayConfig").Get<AlipayConfig>();
// 初始化客户端
IAopClient client = new DefaultAopClient(
gatewayUrl:
global-open-sdk-dotnet项目地址:https://gitcode.com/gh_mirrors/gl/global-open-sdk-dotnet