sunpengfei
2025-11-19 a62b3247a44963907a7f84c4d1de87fc8bd5d718
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using Baidu.Aip.BodyAnalysis;
using Furion.DatabaseAccessor;
using Furion.DataEncryption;
using Furion.DependencyInjection;
using Furion.HttpRemote;
using Furion.Logging;
using log4net;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
 
namespace ApiTools.Core
{
    /// <summary>
    /// 微信支付工具
    /// </summary>
    public class WeChatPayUtils(
            IOptions<WeChatPayOptions> options,
            IHttpRemoteService httpRemoteService,
            IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog
        ) : ITransient
    {
        private readonly IOptions<WeChatPayOptions> options = options;
        private readonly IHttpRemoteService httpRemoteService = httpRemoteService;
        private readonly IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog = repThreeResourceLog;
 
        public async Task<TResponse> Send<TPathParameters, TQueryParameters, TBodyParameters, TResponse>(
            EnumResourceMethod method,
            string path,
            WeChatPayRequest<TPathParameters, TQueryParameters, TBodyParameters> request,
            Func<HttpRequestBuilder, HttpRequestBuilder> build = null)
            where TResponse : WeChatPayResponse, new()
        {
            var log = new ThreeResourceLog
            {
                Method = method,
                Domain = "https://api.mch.weixin.qq.com",
                Path = path,
                Request = request.ToJson()
            };
            if (request.PathParameters != null)
            {
                var properties = typeof(TPathParameters).GetProperties();
                foreach (var property in properties)
                {
                    var jsonProperty = property.GetCustomAttribute<JsonPropertyAttribute>();
                    var name = jsonProperty != null ? jsonProperty.PropertyName : property.Name;
                    path = path.Replace($"{{{name}}}", property.GetValue(request.PathParameters).ToString());
                }
            }
            if (request.QueryParamters != null)
            {
                var properties = typeof(TQueryParameters).GetProperties();
                var queries = new Dictionary<string, string>();
                foreach (var property in properties)
                {
                    var jsonProperty = property.GetCustomAttribute<JsonPropertyAttribute>();
                    var name = jsonProperty != null ? jsonProperty.PropertyName : property.Name;
                    var value = property.GetValue(request.QueryParamters).ToString();
                    if (value.IsNotNull())
                    {
                        value = UrlEncoder.Default.Encode(value);
                    }
                    queries.Add(name, value);
                }
                var queriesString = queries.Select(it => $"{it.Key}={it.Value}").SplitJoin("&");
                path = $"{path}?{queriesString}";
            }
 
            var methodString = method.ToString().ToUpper();
            var builder = HttpRequestBuilder.Create(methodString, $"{log.Domain}{path}");
 
            var body = "";
            if (request.BodyParamters != null)
            {
                body = request.BodyParamters.ToJson(new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                });
                builder = builder.SetJsonContent(body);
                builder = builder.WithHeader("Content-Type", "application/json");
            }
            var authorization = GetAuthorization(methodString, path, body, options.Value.MchId, options.Value.SerialNo, options.Value.PrivateKey);
            builder = builder.WithHeader("Authorization", authorization);
            builder = builder.WithHeader("Accept", "application/json");
            if (build != null)
            {
                builder = build(builder);
            }
 
            await repThreeResourceLog.InsertNowAsync(log);
            var stopwatch = Stopwatch.StartNew();
            log.Response = await httpRemoteService.SendAsStringAsync(builder);
            log.UpdatedTime = DateTimeOffset.Now;
            stopwatch.Stop();
            log.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
            var result = log.Response.JsonTo<TResponse>();
            log.IsSuccess = false;
            await repThreeResourceLog.UpdateNowAsync(log);
            return result;
        }
 
        /// <summary>
        /// 发起批量转账
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<PartnerTransferBatchesResponse> PartnerTransferBatches(WeChatPayRequest<object, object, WeChatPayPartnerTransferBatchesBodyParameters> request)
        {
            foreach (var item in request.BodyParamters.TransferDetailList)
            {
                item.UserName = item.UserName.IsNotNull() ? Encrypt(options.Value.PublicKey, item.UserName) : null;
                item.UserIdCard = item.UserIdCard.IsNotNull() ? Encrypt(options.Value.PublicKey, item.UserIdCard) : null;
            }
            return await Send<object, object, WeChatPayPartnerTransferBatchesBodyParameters, PartnerTransferBatchesResponse>(
                EnumResourceMethod.Post,
                "/v3/partner-transfer/batches",
                request,
                it => it.WithHeader("Wechatpay-Serial", options.Value.SerialNo));
        }
 
        /// <summary>
        /// 查询特约商户账户实时余额
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<WeChatPayEcommerceFundBalanceResponse> EcommerceFundBalance(WeChatPayRequest<WeChatPayEcommerceFundBalancePathParamters, WeChatPayEcommerceFundBalanceQueryParamters, object> request)
        {
            return await Send<WeChatPayEcommerceFundBalancePathParamters, WeChatPayEcommerceFundBalanceQueryParamters, object, WeChatPayEcommerceFundBalanceResponse>(
                EnumResourceMethod.Get,
                "/v3/ecommerce/fund/balance/{sub_mchid}",
                request,
                it => it.WithHeader("Wechatpay-Serial", options.Value.SerialNo));
        }
 
        /// <summary>
        /// 生成认证信息
        /// </summary>
        /// <param name="method"></param>
        /// <param name="path"></param>
        /// <param name="body"></param>
        /// <param name="mchId"></param>
        /// <param name="serialNo"></param>
        /// <param name="privateKey"></param>
        /// <returns></returns>
        private string GetAuthorization(string method, string path, string body, string mchId, string serialNo, string privateKey)
        {
            var timestamp = DateTime.Now.ToTimeStamp(false);
            var nonce_str = GetHexDumpRandom();
            var signature = GetSignature(method, path, body, privateKey, timestamp, nonce_str);
            return $"WECHATPAY2-SHA256-RSA2048 mchid=\"{mchId}\",nonce_str=\"{nonce_str}\",signature=\"{signature}\",timestamp=\"{timestamp}\",serial_no=\"{serialNo}\"";
        }
 
        /// <summary>
        /// 加密敏感字段
        /// </summary>
        /// <param name="publicKey"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public string Encrypt(string publicKey, string data)
        {
            byte[] dataToEncrypt = Encoding.UTF8.GetBytes(data);
            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportFromPem(publicKey.ToCharArray());
                byte[] encryptedBytes = rsa.Encrypt(dataToEncrypt, true);
                return Convert.ToBase64String(encryptedBytes);
            }
        }
 
        /// <summary>
        /// 解密敏感字段
        /// </summary>
        /// <param name="privareKey"></param>
        /// <param name="encryptedBase64"></param>
        /// <returns></returns>
        public string Decrypt(string privareKey, string encryptedBase64)
        {
            byte[] encryptedBytes = Convert.FromBase64String(encryptedBase64);
            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportFromPem(privareKey.ToCharArray());
                byte[] decryptedBytes = rsa.Decrypt(encryptedBytes, true);
                return Encoding.UTF8.GetString(decryptedBytes);
            }
        }
 
        /// <summary>
        /// 生成签名
        /// </summary>
        /// <param name="method"></param>
        /// <param name="path"></param>
        /// <param name="body"></param>
        /// <param name="privateKey"></param>
        /// <param name="timestamp"></param>
        /// <param name="nonce_str"></param>
        /// <returns></returns>
        private string GetSignature(string method, string path, string body, string privateKey, long timestamp, string nonce_str)
        {
            string signString = $"{method}\n{path}\n{timestamp}\n{nonce_str}\n{body}\n";
            using (var rsa = new RSACryptoServiceProvider())
            {
                rsa.ImportFromPem(privateKey.ToCharArray());
 
                // 将字符串转换为字节数组(使用UTF8编码)
                byte[] dataBytes = Encoding.UTF8.GetBytes(signString);
 
                // 执行签名:SHA256哈希算法,PKCS#1 v1.5填充模式
                byte[] signatureBytes = rsa.SignData(
                    dataBytes,
                    HashAlgorithmName.SHA256,
                    RSASignaturePadding.Pkcs1
                );
 
                // 将签名结果转换为Base64字符串
                var base64 = Convert.ToBase64String(signatureBytes);
                return base64;
            }
        }
 
        /// <summary>
        /// 生成一个请求随机串
        /// </summary>
        /// <returns></returns>
        private string GetHexDumpRandom()
        {
            // 16字节缓冲区,与hexdump -n 16对应
            byte[] randomBytes = new byte[16];
 
            // 使用加密级随机数生成器,类似于/dev/random
            using (var rng = RandomNumberGenerator.Create())
            {
                rng.GetBytes(randomBytes);
            }
 
            // 构建输出字符串
            StringBuilder sb = new StringBuilder();
 
            // 4/4 "%08X" 对应:将16字节分成4个4字节整数,每个以8位十六进制显示
            for (int i = 0; i < 4; i++)
            {
                // 从字节数组中解析32位整数(大端字节序)
                uint value = BitConverter.ToUInt32(randomBytes, i * 4);
                // 如果系统是小端字节序,需要转换为大端
                if (BitConverter.IsLittleEndian)
                {
                    value = ReverseBytes(value);
                }
                // 格式化为8位十六进制大写形式
                sb.AppendFormat("{0:X8}", value);
            }
 
            return sb.ToString();
        }
 
        // 反转32位整数的字节序(小端转大端)
        private static uint ReverseBytes(uint value)
        {
            return (value << 24) | ((value & 0x0000FF00) << 8) |
                   ((value & 0x00FF0000) >> 8) | (value >> 24);
        }
    }
}