Alipay 全球开放 SDK .NET 项目使用教程

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

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陶羚耘Ruby

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值