sunpengfei
2025-11-19 7b47c91bcf89d667a5c99cfafe0d899280f7fbe3
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
using Aop.Api.Domain;
using ApiTools.Core.Entities.Common;
using Furion;
using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.DistributedIDGenerator;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Ocsp;
using pingan.openbank.api.sdk.client;
using pingan.openbank.api.sdk.common.helper;
using pingan.openbank.api.sdk.common.http;
using pingan.openbank.api.sdk.common.util;
using pingan.openbank.api.sdk.entity;
using pingan.openbank.api.sdk.exception;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Metadata;
using System.Text;
using System.Threading.Tasks;
 
namespace ApiTools.Core
{
    /// <summary>
    /// 平安支付工具
    /// </summary>
    public class PingAnPayUtils(
            ILogger<PingAnPayUtils> logger,
            IOptions<PingAnPayOptions> options,
            IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog
        ) : ITransient
    {
        private readonly ILogger<PingAnPayUtils> logger = logger;
        private readonly IOptions<PingAnPayOptions> options = options;
        private readonly IRepository<ThreeResourceLog, LogDbContextLocator> repThreeResourceLog = repThreeResourceLog;
 
        /// <summary>
        /// 企业账户余额查询_银企直联此接口适应银行所有币种的活期账户的余额查询,其中的可用余额只包含自身的资金状况,而不包括集团内部的资金池。账面余额,也只是自身账号的账面金额。
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<CorAcctBalanceQueryResponse> CorAcctBalanceQuery(CorAcctBalanceQueryRequest request)
        {
            return await Send<CorAcctBalanceQueryRequest, CorAcctBalanceQueryResponse>(request, "/V1.0/bedl/CorAcctBalanceQuery");
        }
 
        /// <summary>
        /// 账户止付和解止付_银企直联 对公账户层的止付、解除止付
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<StopPaymentAndSettlementAccountsResponse> StopPaymentAndSettlementAccounts(StopPaymentAndSettlementAccountsRequest request)
        {
            return await Send<StopPaymentAndSettlementAccountsRequest, StopPaymentAndSettlementAccountsResponse>(request, "/V1.0/bedl/StopPaymentAndSettlementAccounts");
        }
 
        /// <summary>
        /// 账户止付详情查询_银企直联 对公账户层的冻结/止付详情查询,每页最大100条。
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<InquiryAccountStopPaymentDetailsResponse> InquiryAccountStopPaymentDetails(InquiryAccountStopPaymentDetailsRequest request)
        {
            return await Send<InquiryAccountStopPaymentDetailsRequest, InquiryAccountStopPaymentDetailsResponse>(request, "/V1.0/bedl/InquiryAccountStopPaymentDetails");
        }
 
        /// <summary>
        /// 单笔监管止付支付申请_银企直联
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<SingleApplicationSuspensionPaymentResponse> SingleApplicationSuspensionPayment(SingleApplicationSuspensionPaymentRequest request)
        {
            return await Send<SingleApplicationSuspensionPaymentRequest, SingleApplicationSuspensionPaymentResponse>(request, "/V1.0/bedl/SingleApplicationSuspensionPayment");
        }
 
        /// <summary>
        /// 单笔转账指令查询_银企直联
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<CorSingleTransferQueryResponse> CorSingleTransferQuery(CorSingleTransferQueryRequest request)
        {
            return await Send<CorSingleTransferQueryRequest, CorSingleTransferQueryResponse>(request, "/V1.0/bedl/CorSingleTransferQuery");
        }
 
        /// <summary>
        /// 当日历史回单数据查询接口_银企直联
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<SameDayHistoryReceiptDataQueryResponse> SameDayHistoryReceiptDataQuery(SameDayHistoryReceiptDataQueryRequest request)
        {
            return await Send<SameDayHistoryReceiptDataQueryRequest, SameDayHistoryReceiptDataQueryResponse>(request, "/V1.0/bedl/SameDayHistoryReceiptDataQuery");
        }
 
        /// <summary>
        /// 单笔或多笔回单PDF合并下载(新)_银企直联
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task<SingleOrBatchReceiptPDFMergeDownloadNewResponse> SingleOrBatchReceiptPDFMergeDownloadNew(SingleOrBatchReceiptPDFMergeDownloadNewRequest request)
        {
            return await Send<SingleOrBatchReceiptPDFMergeDownloadNewRequest, SingleOrBatchReceiptPDFMergeDownloadNewResponse>(request, "/V1.0/bedl/SingleOrBatchReceiptPDFMergeDownloadNew");
        }
 
        /// <summary>
        /// 发送请求
        /// </summary>
        /// <typeparam name="TRequest"></typeparam>
        /// <typeparam name="TResponse"></typeparam>
        /// <param name="request"></param>
        /// <param name="path"></param>
        /// <returns></returns>
        public async Task<TResponse> Send<TRequest, TResponse>(TRequest request, string path)
            where TRequest : PingAnPayBaseRequest, new()
            where TResponse : PingAnPayBaseResponse, new()
        {
            var logier = JwtUtils.GetCurrentLogier();
            var sdkRequest = new SdkRequest();
            sdkRequest.SetInterfaceName(path);
            if (request.CnsmrSeqNo.IsNull())
            {
                request.CnsmrSeqNo = $"{DateTime.Now:yyyyMMddHHmmssfff}{new Random(IDGen.NextID().GetHashCode()).Next(100, 999)}";
            }
            request.MrChCode = options.Value.MrChCode;
            var body = JObject.FromObject(request);
            sdkRequest.SetBody(body);
            sdkRequest.SetExtraHeaders(null);
            //string text = sdkRequest.GetAppId();
            //if (text.IsNull())
            //{
            //    string configFilePath = sdkRequest.GetConfigFilePath();
            //    text = ((!configFilePath.IsNull()) ? configFilePath : AbstractApiClient.CheckAppIdOnlyOne());
            //}
            var log = new ThreeResourceLog
            {
                CreatedTime = DateTimeOffset.Now,
                Id = IDGen.NextID(),
                TraceId = App.GetTraceId(),
                Method = EnumResourceMethod.Post,
                //Domain = AbstractApiClient.GetOpenBankConfig(text).GetBaseUrl(),
                Path = path,
                CreatedUserId = logier?.Id,
                CreatedChannelId = logier?.ChannelId,
                Request = request.ToJson(),
            };
            await repThreeResourceLog.InsertNowAsync(log);
            var stopwatch = Stopwatch.StartNew();
            HttpResult httpResult = GetClient().Invoke<HttpResult>(sdkRequest);
            var sdkReturn = httpResult.GetData();
            stopwatch.Stop();
 
            var result = sdkReturn.JsonTo<TResponse>();
 
            log.UpdatedTime = DateTimeOffset.Now;
            log.Response = sdkReturn;
            log.IsSuccess = result.Code.IsNotNull();
            log.ElapsedMilliseconds = stopwatch.ElapsedMilliseconds;
            await repThreeResourceLog.UpdateNowAsync(log);
 
            return result;
        }
 
        public string DownloadFile(string documentId, string fileName)
        {
            try
            {
                var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UserFiles", "PingAnPay");
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
                path += Path.DirectorySeparatorChar;
 
                var fileRequest = new FileRequest();
                fileRequest.SetFileNo(documentId);
                fileRequest.SetFilePathName(path);
                GetClient().FileDownLoad(fileRequest);
                return $"{path}{documentId}.zip";
            }
            catch (OpenBankSdkException ex)
            {
                Console.WriteLine(ex);
                throw;
            }
        }
 
        private ApiClient GetClient()
        {
            var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PingAnPayCert", "config.properties");
            var propertyOper = new PropertyOper(path);
            var client = ApiClient.GetInstance(propertyOper);
            return client;
        }
    }
}