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
{
///
/// 微信支付工具
///
public class WeChatPayUtils(
IOptions options,
IHttpRemoteService httpRemoteService,
IRepository repThreeResourceLog
) : ITransient
{
private readonly IOptions options = options;
private readonly IHttpRemoteService httpRemoteService = httpRemoteService;
private readonly IRepository repThreeResourceLog = repThreeResourceLog;
public async Task Send(
EnumResourceMethod method,
string path,
WeChatPayRequest request,
Func 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();
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();
foreach (var property in properties)
{
var jsonProperty = property.GetCustomAttribute();
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();
log.IsSuccess = false;
await repThreeResourceLog.UpdateNowAsync(log);
return result;
}
///
/// 发起批量转账
///
///
///
public async Task PartnerTransferBatches(WeChatPayRequest