| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Net.Http; |
| | | 5 | | using System.Text; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Microsoft.Extensions.Logging; |
| | | 8 | | using Microsoft.Extensions.Options; |
| | | 9 | | using Newtonsoft.Json; |
| | | 10 | | using Newtonsoft.Json.Converters; |
| | | 11 | | using Newtonsoft.Json.Serialization; |
| | | 12 | | using SVETA.Api.Data.DTO.Wallet; |
| | | 13 | | using SVETA.Api.Helpers; |
| | | 14 | | using SVETA.Api.Services.Interfaces; |
| | | 15 | | using WinSolutions.Sveta.Common; |
| | | 16 | | using WinSolutions.Sveta.Server.Data.DataModel.Entities; |
| | | 17 | | using WinSolutions.Sveta.Server.Data.DataModel.Kinds; |
| | | 18 | | using WinSolutions.Sveta.Server.Services.Interfaces; |
| | | 19 | | using SVETA.Api.Data.Domain; |
| | | 20 | | |
| | | 21 | | namespace SVETA.Api.Services.Implements |
| | | 22 | | { |
| | | 23 | | public class WalletHttpClient: IWalletHttpClient |
| | | 24 | | { |
| | | 25 | | private readonly ILogger<WalletHttpClient> _logger; |
| | | 26 | | private readonly HttpClient httpClient; |
| | | 27 | | private readonly WalletSettings _walletSettings; |
| | | 28 | | private readonly ConfigurationsSettings _confsSettings; |
| | | 29 | | private readonly IEventService _eventService; |
| | | 30 | | private readonly IUserService _userService; |
| | | 31 | | private readonly IAuthenticationService _authUserService; |
| | | 32 | | |
| | 0 | 33 | | public WalletHttpClient(ILogger<WalletHttpClient> logger, |
| | 0 | 34 | | IOptions<WalletSettings> options, |
| | 0 | 35 | | IEventService eventService, |
| | 0 | 36 | | IAuthenticationService authUserService, |
| | 0 | 37 | | IUserService userService, |
| | 0 | 38 | | IOptions<ConfigurationsSettings> confsSettings |
| | 0 | 39 | | ) |
| | 0 | 40 | | { |
| | 0 | 41 | | _confsSettings = confsSettings.Value; |
| | 0 | 42 | | _walletSettings = options.Value; |
| | 0 | 43 | | _authUserService = authUserService; |
| | 0 | 44 | | _eventService = eventService; |
| | 0 | 45 | | _userService = userService; |
| | 0 | 46 | | httpClient = new HttpClient(); |
| | 0 | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Возвращает баланс кошелька в формате ВТБ |
| | | 51 | | /// </summary> |
| | | 52 | | /// <param name="walletId">идентификатор кошелька</param> |
| | | 53 | | /// <returns></returns> |
| | | 54 | | public async Task<WalletResponseDTO<WalletBalanceResponse>> GetWalletBalance(string walletId) |
| | 0 | 55 | | { |
| | 0 | 56 | | StringContent data = CreateContent(new { id = walletId }); |
| | 0 | 57 | | var response = await httpClient.PostAsync(GetUrl(_confsSettings.GetConfValue("WalletSettings", "GetAcc")), d |
| | 0 | 58 | | var raw = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); |
| | 0 | 59 | | return JsonConvert.DeserializeObject<WalletResponseDTO<WalletBalanceResponse>>(await response.Content.ReadAs |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Возвращает свободный баланс на сумму |
| | | 64 | | /// </summary> |
| | | 65 | | /// <param name="walletId">Идентификатор кошелька</param> |
| | | 66 | | /// <param name="sum">Сумма, которая должна быть доступна на балансе</param> |
| | | 67 | | /// <returns></returns> |
| | | 68 | | public async Task<WalletResponseDTO<WalletBalanceFreeVtbResponse>> GetFreeBalance(string walletId, decimal sum) |
| | 0 | 69 | | { |
| | 0 | 70 | | var response = await httpClient.PostAsync(GetUrl(_confsSettings.GetConfValue("WalletSettings", "GetAccBalanc |
| | 0 | 71 | | CreateContent(new {id = walletId, sum})); |
| | 0 | 72 | | var raw = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); |
| | 0 | 73 | | return JsonConvert.DeserializeObject<WalletResponseDTO<WalletBalanceFreeVtbResponse>>( |
| | 0 | 74 | | await response.Content.ReadAsStringAsync()); |
| | 0 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Отправляет запрос на создание транзакции |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="movementGuid">Guid документа для которого создается транзакция</param> |
| | | 81 | | /// <param name="request">Объект с данными</param> |
| | | 82 | | /// <returns></returns> |
| | | 83 | | public async Task<WalletResponseDTO<WalletTransactionResponse>> CreateTransaction(Guid movementGuid, WalletReque |
| | 0 | 84 | | { |
| | 0 | 85 | | var response = await httpClient.PostAsync(GetUrl(_confsSettings.GetConfValue("WalletSettings", "CreateTransa |
| | 0 | 86 | | CreateContent(request)); |
| | 0 | 87 | | var raw = JsonConvert.DeserializeObject(await response.Content.ReadAsStringAsync()); |
| | 0 | 88 | | var content = JsonConvert.DeserializeObject<WalletResponseDTO<WalletTransactionResponse>>(await response.Con |
| | 0 | 89 | | await CreateEvent(movementGuid, new { Request = request, Response = content }); |
| | 0 | 90 | | return content; |
| | 0 | 91 | | } |
| | | 92 | | /// <summary> |
| | | 93 | | /// Отправляет запрос на проверку смс |
| | | 94 | | /// </summary> |
| | | 95 | | /// <param name="trGuid">GUID транзакции в SVETA</param> |
| | | 96 | | /// <param name="checkRequest"></param> |
| | | 97 | | /// <returns></returns> |
| | | 98 | | public async Task<WalletResponseDTO<WalletTransactActionResponseFull>> CheckSms(Guid trGuid, |
| | | 99 | | WalletCheckSmsRequest checkRequest) |
| | 0 | 100 | | { |
| | 0 | 101 | | var response = await httpClient.PostAsync(GetUrl(_confsSettings.GetConfValue("WalletSettings", "ActionTransa |
| | 0 | 102 | | CreateContent(checkRequest)); |
| | 0 | 103 | | var content = JsonConvert.DeserializeObject<WalletResponseDTO<WalletTransactActionResponseFull>>(await respo |
| | 0 | 104 | | await CreateEvent(trGuid, new { Request = checkRequest, Response = content }); |
| | 0 | 105 | | return content; |
| | 0 | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Запрашивает повторное смс с кодом |
| | | 110 | | /// </summary> |
| | | 111 | | /// <param name="trGuid">GUID транзакции в SVETA</param> |
| | | 112 | | /// <param name="request"></param> |
| | | 113 | | /// <returns></returns> |
| | | 114 | | public async Task<WalletResponseDTO<WalletTransactActionResponseFull>> RetrySms(Guid trGuid, |
| | | 115 | | WalletConfirmReqDTO request) |
| | 0 | 116 | | { |
| | 0 | 117 | | var response = await httpClient.PostAsync(GetUrl(_confsSettings.GetConfValue("WalletSettings", "ActionTransa |
| | 0 | 118 | | CreateContent(request)); |
| | 0 | 119 | | var content = JsonConvert.DeserializeObject<WalletResponseDTO<WalletTransactActionResponseFull>>(await respo |
| | | 120 | | |
| | 0 | 121 | | await CreateEvent(trGuid, new { Request = request, Response = content }); |
| | 0 | 122 | | return content; |
| | 0 | 123 | | } |
| | | 124 | | /// <summary> |
| | | 125 | | /// Подтверждение транзакции |
| | | 126 | | /// </summary> |
| | | 127 | | /// <param name="trGuid">GUID транзакции в SVETA</param> |
| | | 128 | | /// <param name="request">Объект с запросом</param> |
| | | 129 | | /// <returns></returns> |
| | | 130 | | public async Task<WalletResponseDTO<WalletTransactActionResponseFull>> ConfirmTransaction(Guid trGuid, WalletCon |
| | 0 | 131 | | { |
| | 0 | 132 | | var response = await httpClient.PostAsync(GetUrl(_confsSettings.GetConfValue("WalletSettings", "ActionTransa |
| | 0 | 133 | | CreateContent(request)); |
| | | 134 | | |
| | 0 | 135 | | var content = JsonConvert.DeserializeObject<WalletResponseDTO<WalletTransactActionResponseFull>>(await respo |
| | | 136 | | |
| | 0 | 137 | | await CreateEvent(trGuid, new { Request = request, Response = content }); |
| | 0 | 138 | | return content; |
| | 0 | 139 | | } |
| | | 140 | | |
| | | 141 | | /// <summary> |
| | | 142 | | /// Отмена транзакции |
| | | 143 | | /// </summary> |
| | | 144 | | /// <param name="trGuid"></param> |
| | | 145 | | /// <param name="request"></param> |
| | | 146 | | /// <returns></returns> |
| | | 147 | | public async Task<WalletResponseDTO<WalletTransactActionResponseFull>> CancelTransaction(Guid trGuid, |
| | | 148 | | WalletConfirmReqDTO request) |
| | 0 | 149 | | { |
| | 0 | 150 | | var response = await httpClient.PostAsync(GetUrl(_confsSettings.GetConfValue("WalletSettings", "ActionTransa |
| | 0 | 151 | | CreateContent(request)); |
| | | 152 | | |
| | 0 | 153 | | var content = JsonConvert.DeserializeObject<WalletResponseDTO<WalletTransactActionResponseFull>>(await respo |
| | 0 | 154 | | await CreateEvent(trGuid, new { Request = request, Response = content }); |
| | 0 | 155 | | return content; |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Изменяет сумму сделки |
| | | 160 | | /// </summary> |
| | | 161 | | /// <param name="trGuid"></param> |
| | | 162 | | /// <param name="request"></param> |
| | | 163 | | /// <returns></returns> |
| | | 164 | | public async Task<WalletResponseDTO<WalletTransactionChangeDeal>> ChangeDeal(Guid trGuid, |
| | | 165 | | WalletChangeDealRequestDto request) |
| | 0 | 166 | | { |
| | 0 | 167 | | var response = await httpClient.PostAsync(GetUrl(_confsSettings.GetConfValue("WalletSettings", "ChangeTransa |
| | 0 | 168 | | CreateContent(request)); |
| | | 169 | | |
| | 0 | 170 | | var content = JsonConvert.DeserializeObject<WalletResponseDTO<WalletTransactionChangeDeal>>(await response.C |
| | 0 | 171 | | await CreateEvent(trGuid, new { Request = request, Response = content }); |
| | 0 | 172 | | return content; |
| | 0 | 173 | | } |
| | | 174 | | |
| | | 175 | | //WalletResponseDTO<WalletTransactionChangeDeal> |
| | | 176 | | public async Task<WalletResponseDTO<WalletTransactionCheckChangeDeal>> CheckChangeDeal(Guid trGuid, |
| | | 177 | | WalletChangeDealRequestDto request) |
| | 0 | 178 | | { |
| | 0 | 179 | | var response = |
| | 0 | 180 | | await httpClient.PostAsync(GetUrl(_confsSettings.GetConfValue("WalletSettings", "CheckChangeTransaction" |
| | 0 | 181 | | var content = JsonConvert.DeserializeObject<WalletResponseDTO<WalletTransactionCheckChangeDeal>>(await respo |
| | 0 | 182 | | await CreateEvent(trGuid, new {Request = request, Response = content}); |
| | 0 | 183 | | return content; |
| | 0 | 184 | | } |
| | | 185 | | |
| | | 186 | | /// <summary> |
| | | 187 | | /// Возвращает информацию о транзакции |
| | | 188 | | /// </summary> |
| | | 189 | | /// <param name="transactionId">Идентификатор транзакции</param> |
| | | 190 | | /// <returns></returns> |
| | | 191 | | public async Task<WalletResponseDTO<WalletTransactionHistory>> GetTransactionInfo(string transactionId) |
| | 0 | 192 | | { |
| | 0 | 193 | | var response = await httpClient.PostAsync(GetUrl(_confsSettings.GetConfValue("WalletSettings", "GetTransacti |
| | 0 | 194 | | CreateContent(new { id = transactionId })); |
| | 0 | 195 | | return JsonConvert.DeserializeObject<WalletResponseDTO<WalletTransactionHistory>>( |
| | 0 | 196 | | await response.Content.ReadAsStringAsync()); |
| | 0 | 197 | | } |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Возвращает историческую информацию по кошельку |
| | | 201 | | /// </summary> |
| | | 202 | | /// <param name="request"></param> |
| | | 203 | | /// <returns></returns> |
| | | 204 | | public async Task<WalletResponseDTO<List<WalletHistoryResponse>>> GetTransactionHistory(WalletHistoryRequest req |
| | 0 | 205 | | { |
| | 0 | 206 | | var response = await httpClient.PostAsync(GetUrl(_confsSettings.GetConfValue("WalletSettings", "AccountHisto |
| | 0 | 207 | | CreateContent(request)); |
| | 0 | 208 | | var content = await response.Content.ReadAsStringAsync(); |
| | 0 | 209 | | return JsonConvert.DeserializeObject<WalletResponseDTO<List<WalletHistoryResponse>>>(content); |
| | 0 | 210 | | } |
| | | 211 | | |
| | | 212 | | private async Task CreateEvent(Guid extId, object request) |
| | 0 | 213 | | { |
| | 0 | 214 | | Event requestEvent = new Event |
| | 0 | 215 | | { |
| | 0 | 216 | | DtCreated = DateTime.UtcNow, |
| | 0 | 217 | | User = await _userService.GetUser(_authUserService.UserId), |
| | 0 | 218 | | EventsKind = await _eventService.GetEventKind((long)EventKind.Create), |
| | 0 | 219 | | Entity = "WalletRequest", |
| | 0 | 220 | | RecordGuid = extId, |
| | 0 | 221 | | ReasonJson = JsonConvert.SerializeObject(request) |
| | 0 | 222 | | }; |
| | 0 | 223 | | await _eventService.CreateEvent(requestEvent); |
| | 0 | 224 | | } |
| | 0 | 225 | | private string GetUrl(string chankUrl) => $"{_walletSettings.Url}/" + |
| | 0 | 226 | | $"{_walletSettings.AppName}/" + |
| | 0 | 227 | | $"{chankUrl}?tnx={(_walletSettings.Tnx.Encrypted ? SymmetricCrypto.DecryptData(Convert.FromBase64String(_wal |
| | | 228 | | private static StringContent CreateContent(object body) |
| | 0 | 229 | | { |
| | 0 | 230 | | var serializeSettings = new JsonSerializerSettings |
| | 0 | 231 | | { |
| | 0 | 232 | | ContractResolver = new CamelCasePropertyNamesContractResolver() |
| | 0 | 233 | | }; |
| | 0 | 234 | | serializeSettings.Converters.Add(new IsoDateTimeConverter() { DateTimeFormat = "yyyy-MM-ddTHH:mm:ss" }); |
| | 0 | 235 | | var res = JsonConvert.SerializeObject(body, serializeSettings); |
| | 0 | 236 | | var cont = new StringContent( |
| | 0 | 237 | | res, |
| | 0 | 238 | | Encoding.UTF8, |
| | 0 | 239 | | "application/json"); |
| | 0 | 240 | | return cont; |
| | 0 | 241 | | } |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | public interface IWalletHttpClient |
| | | 245 | | { |
| | | 246 | | Task<WalletResponseDTO<WalletBalanceResponse>> GetWalletBalance(string walletId); |
| | | 247 | | Task<WalletResponseDTO<WalletBalanceFreeVtbResponse>> GetFreeBalance(string walletId, decimal sum); |
| | | 248 | | Task<WalletResponseDTO<WalletTransactionResponse>> CreateTransaction(Guid movementGuid, WalletRequest request); |
| | | 249 | | Task<WalletResponseDTO<WalletTransactActionResponseFull>> CheckSms(Guid trGuid, WalletCheckSmsRequest checkReque |
| | | 250 | | Task<WalletResponseDTO<WalletTransactActionResponseFull>> RetrySms(Guid trGuid, WalletConfirmReqDTO request); |
| | | 251 | | Task<WalletResponseDTO<WalletTransactActionResponseFull>> ConfirmTransaction(Guid trGuid, WalletConfirmReqDTO re |
| | | 252 | | Task<WalletResponseDTO<WalletTransactActionResponseFull>> CancelTransaction(Guid trGuid, WalletConfirmReqDTO req |
| | | 253 | | |
| | | 254 | | Task<WalletResponseDTO<WalletTransactionChangeDeal>> ChangeDeal(Guid trGuid, WalletChangeDealRequestDto request) |
| | | 255 | | Task<WalletResponseDTO<WalletTransactionHistory>> GetTransactionInfo(string transactionId); |
| | | 256 | | Task<WalletResponseDTO<List<WalletHistoryResponse>>> GetTransactionHistory(WalletHistoryRequest request); |
| | | 257 | | |
| | | 258 | | Task<WalletResponseDTO<WalletTransactionCheckChangeDeal>> CheckChangeDeal(Guid trGuid, |
| | | 259 | | WalletChangeDealRequestDto request); |
| | | 260 | | } |
| | | 261 | | } |