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 { /// /// 应用ID,您的APPID /// public string AppId { get; set; } /// /// 合作商户uid /// public string Uid { get; set; } /// /// 支付宝网关 /// public string Gatewayurl { get; set; } /// /// 商户私钥,您的原始格式RSA私钥 /// public string PrivateKey { get; set; } /// /// 支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。 /// public string AlipayPublicKey { get; set; } /// /// 签名方式 /// public string SignType { get; set; } = "RSA2"; /// /// 编码格式 /// public string CharSet { get; set; } = "UTF-8"; /// /// 是否从文件读取公私钥 如果为true ,那么公私钥应该配置为密钥文件路径 /// public bool IsKeyFromFile { get; set; } = false; public void SetOption(IConfigurationSection section) { if (section == null) { throw new ArgumentException(nameof(section)); } var options = section.Get(); 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; } } }