Esempio n. 1
0
        public async Task <string> CreateAsync(
            decimal value,
            PaymentTypes type,
            User user,
            Dictionary <string, string> metadata,
            Currency currency = null)
        {
            currency ??= user.Currency;

            metadata[Payment.TypeMetadataName] = type.ToString();

            if (currency.Key != Currency.RUB.Key)
            {
                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 SaveAsync(payment, user, currency, type);

            return(payment.Confirmation.ConfirmationUrl);
        }