AlipayOptions.cs
2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Security.Cryptography;
using Alipay.AopSdk.Core.Domain;
using Alipay.AopSdk.Core.Util;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;
namespace Alipay.AopSdk.AspnetCore
{
public class AlipayOptions
{
/// <summary>
/// 应用ID,您的APPID
/// </summary>
public string AppId { get; set; }
/// <summary>
/// 合作商户uid
/// </summary>
public string Uid { get; set; }
/// <summary>
/// 支付宝网关
/// </summary>
public string Gatewayurl { get; set; }
/// <summary>
/// 商户私钥,您的原始格式RSA私钥
/// </summary>
public string PrivateKey { get; set; }
/// <summary>
/// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
/// </summary>
public string AlipayPublicKey { get; set; }
/// <summary>
/// 签名方式
/// </summary>
public string SignType { get; set; } = "RSA2";
/// <summary>
/// 编码格式
/// </summary>
public string CharSet { get; set; } = "UTF-8";
/// <summary>
/// 是否从文件读取公私钥 如果为true ,那么公私钥应该配置为密钥文件路径
/// </summary>
public bool IsKeyFromFile { get; set; } = false;
public void SetOption(IConfigurationSection section)
{
if (section == null)
{
throw new ArgumentException(nameof(section));
}
var options = section.Get<AlipayOptions>();
SetOption(options);
}
public void SetOption(AlipayOptions options)
{
if (options == null)
{
throw new ArgumentException(nameof(options));
}
//SignType私钥检查
if (string.IsNullOrEmpty(options.SignType))
{
throw new Exception("您的支付宝配置未能通过检查,详细信息:签名类型未指定!");
}
//RSA私钥检查
if (string.IsNullOrEmpty(options.PrivateKey))
{
throw new Exception("您的支付宝配置未能通过检查,详细信息:未能获取到商户私钥!");
}
//RSA私钥格式检查
RSA rsaCsp = AlipaySignature.LoadCertificateString(options.PrivateKey, options.SignType);
if (rsaCsp == null)
{
throw new Exception("您的支付宝配置未能通过检查,详细信息:商户私钥格式错误,未能导入!");
}
this.Uid = options.Uid;
this.AlipayPublicKey = options.AlipayPublicKey;
this.AppId = options.AppId;
this.CharSet = options.CharSet;
this.Gatewayurl = options.Gatewayurl;
this.PrivateKey = options.PrivateKey;
this.SignType = options.SignType;
}
}
}