public async Task <string> CreatePayment( decimal value, Entities.Payment.Types type, Dictionary <string, string> metadata = null, User user = null, Currency currency = null) { user ??= _userService.User; currency ??= user.Currency; metadata[Entities.Payment.TypeMetadataName] = type.ToString(); if (currency.Key != "RUB") { using var client = new HttpClient(); const string url = "https://www.cbr-xml-daily.ru/daily_json.js"; var result = await client.GetAsync(url); var rate = JsonConvert.DeserializeObject <CurrenciesRate>(await result.Content.ReadAsStringAsync()); if (Equals(currency, Currency.EUR)) { value *= Convert.ToDecimal(rate.Valute.EUR.Value); } else if (Equals(currency, Currency.USD)) { value *= Convert.ToDecimal(rate.Valute.USD.Value); } } var newPayment = new NewPayment { Amount = new Amount { Value = value, Currency = "RUB" }, Metadata = metadata, Confirmation = new Confirmation { Type = ConfirmationType.Redirect, ReturnUrl = "localhost", Locale = user.Culture.Key == Culture.Russian.Key ? "ru_RU" : "en_US" }, }; var payment = await _client.CreatePaymentAsync(newPayment); await SavePaymentToDb(payment, user, currency, type); return(payment.Confirmation.ConfirmationUrl); }
private async Task <Entities.Payment> SavePaymentToDb(Yandex.Checkout.V3.Payment payment, User user, Currency currency, Entities.Payment.Types type) { var dbPayment = new Entities.Payment { Key = payment.Id, Value = payment.Amount.Value, Status = payment.Status, UserId = user.Id, CurrencyId = currency.Id, Type = type }; await _db.Payments.AddAsync(dbPayment); await _db.SaveChangesAsync(); return(dbPayment); }