private async Task MigratePaymentMethodCriteria() { using (var ctx = _DBContextFactory.CreateContext()) { foreach (var store in await ctx.Stores.AsQueryable().ToArrayAsync()) { var blob = store.GetStoreBlob(); CurrencyValue onChainMinValue = null; CurrencyValue lightningMaxValue = null; if (blob.AdditionalData.TryGetValue("onChainMinValue", out var onChainMinValueJToken)) { CurrencyValue.TryParse(onChainMinValueJToken.Value <string>(), out onChainMinValue); blob.AdditionalData.Remove("onChainMinValue"); } if (blob.AdditionalData.TryGetValue("lightningMaxValue", out var lightningMaxValueJToken)) { CurrencyValue.TryParse(lightningMaxValueJToken.Value <string>(), out lightningMaxValue); blob.AdditionalData.Remove("lightningMaxValue"); } blob.PaymentMethodCriteria = store.GetEnabledPaymentIds(_NetworkProvider).Select(paymentMethodId => { var matchedFromBlob = blob.PaymentMethodCriteria?.SingleOrDefault(criteria => criteria.PaymentMethod == paymentMethodId && criteria.Value != null); return(matchedFromBlob switch { null when paymentMethodId.PaymentType == LightningPaymentType.Instance && lightningMaxValue != null => new PaymentMethodCriteria() { Above = false, PaymentMethod = paymentMethodId, Value = lightningMaxValue }, null when paymentMethodId.PaymentType == BitcoinPaymentType.Instance && onChainMinValue != null => new PaymentMethodCriteria() { Above = true, PaymentMethod = paymentMethodId, Value = onChainMinValue }, _ => new PaymentMethodCriteria() { PaymentMethod = paymentMethodId, Above = matchedFromBlob?.Above ?? true, Value = matchedFromBlob?.Value } }); }).ToList();
public async Task <IActionResult> CheckoutExperience(CheckoutExperienceViewModel model) { CurrencyValue lightningMaxValue = null; if (!string.IsNullOrWhiteSpace(model.LightningMaxValue)) { if (!CurrencyValue.TryParse(model.LightningMaxValue, out lightningMaxValue)) { ModelState.AddModelError(nameof(model.LightningMaxValue), "Invalid lightning max value"); } } CurrencyValue onchainMinValue = null; if (!string.IsNullOrWhiteSpace(model.OnChainMinValue)) { if (!CurrencyValue.TryParse(model.OnChainMinValue, out onchainMinValue)) { ModelState.AddModelError(nameof(model.OnChainMinValue), "Invalid on chain min value"); } } bool needUpdate = false; var blob = StoreData.GetStoreBlob(); var defaultPaymentMethodId = model.DefaultPaymentMethod == null ? null : PaymentMethodId.Parse(model.DefaultPaymentMethod); if (StoreData.GetDefaultPaymentId(_NetworkProvider) != defaultPaymentMethodId) { needUpdate = true; StoreData.SetDefaultPaymentId(defaultPaymentMethodId); } SetCryptoCurrencies(model, StoreData); model.SetLanguages(_LangService, model.DefaultLang); if (!ModelState.IsValid) { return(View(model)); } blob.CustomLogo = string.IsNullOrWhiteSpace(model.CustomLogo) ? null : new Uri(model.CustomLogo, UriKind.Absolute); blob.CustomCSS = string.IsNullOrWhiteSpace(model.CustomCSS) ? null : new Uri(model.CustomCSS, UriKind.Absolute); blob.HtmlTitle = string.IsNullOrWhiteSpace(model.HtmlTitle) ? null : model.HtmlTitle; blob.DefaultLang = model.DefaultLang; blob.RequiresRefundEmail = model.RequiresRefundEmail; blob.OnChainMinValue = onchainMinValue; blob.LightningMaxValue = lightningMaxValue; blob.LightningAmountInSatoshi = model.LightningAmountInSatoshi; blob.RedirectAutomatically = model.RedirectAutomatically; if (StoreData.SetStoreBlob(blob)) { needUpdate = true; } if (needUpdate) { await _Repo.UpdateStore(StoreData); StatusMessage = "Store successfully updated"; } return(RedirectToAction(nameof(CheckoutExperience), new { storeId = StoreData.Id })); }
public async Task <IActionResult> CheckoutExperience(CheckoutExperienceViewModel model) { bool needUpdate = false; var blob = CurrentStore.GetStoreBlob(); var defaultPaymentMethodId = model.DefaultPaymentMethod == null ? null : PaymentMethodId.Parse(model.DefaultPaymentMethod); if (CurrentStore.GetDefaultPaymentId(_NetworkProvider) != defaultPaymentMethodId) { needUpdate = true; CurrentStore.SetDefaultPaymentId(defaultPaymentMethodId); } SetCryptoCurrencies(model, CurrentStore); model.SetLanguages(_LangService, model.DefaultLang); model.PaymentMethodCriteria ??= new List <PaymentMethodCriteriaViewModel>(); for (var index = 0; index < model.PaymentMethodCriteria.Count; index++) { var methodCriterion = model.PaymentMethodCriteria[index]; if (!string.IsNullOrWhiteSpace(methodCriterion.Value)) { if (!CurrencyValue.TryParse(methodCriterion.Value, out var value)) { model.AddModelError(viewModel => viewModel.PaymentMethodCriteria[index].Value, $"{methodCriterion.PaymentMethod}: invalid format (1.0 USD)", this); } } } if (!ModelState.IsValid) { return(View(model)); } blob.PaymentMethodCriteria = model.PaymentMethodCriteria .Where(viewModel => !string.IsNullOrEmpty(viewModel.Value)).Select(viewModel => { CurrencyValue.TryParse(viewModel.Value, out var cv); return(new PaymentMethodCriteria() { Above = viewModel.Type == PaymentMethodCriteriaViewModel.CriteriaType.GreaterThan, Value = cv, PaymentMethod = PaymentMethodId.Parse(viewModel.PaymentMethod) }); }).ToList(); blob.RequiresRefundEmail = model.RequiresRefundEmail; blob.LazyPaymentMethods = model.LazyPaymentMethods; blob.LightningAmountInSatoshi = model.LightningAmountInSatoshi; blob.LightningPrivateRouteHints = model.LightningPrivateRouteHints; blob.OnChainWithLnInvoiceFallback = model.OnChainWithLnInvoiceFallback; blob.RedirectAutomatically = model.RedirectAutomatically; blob.ShowRecommendedFee = model.ShowRecommendedFee; blob.RecommendedFeeBlockTarget = model.RecommendedFeeBlockTarget; blob.CustomLogo = model.CustomLogo; blob.CustomCSS = model.CustomCSS; blob.HtmlTitle = string.IsNullOrWhiteSpace(model.HtmlTitle) ? null : model.HtmlTitle; blob.DefaultLang = model.DefaultLang; if (CurrentStore.SetStoreBlob(blob)) { needUpdate = true; } if (needUpdate) { await _Repo.UpdateStore(CurrentStore); TempData[WellKnownTempData.SuccessMessage] = "Store successfully updated"; } return(RedirectToAction(nameof(CheckoutExperience), new { storeId = CurrentStore.Id })); }
public async Task <IActionResult> CheckoutAppearance(CheckoutAppearanceViewModel model) { bool needUpdate = false; var blob = CurrentStore.GetStoreBlob(); var defaultPaymentMethodId = model.DefaultPaymentMethod == null ? null : PaymentMethodId.Parse(model.DefaultPaymentMethod); if (CurrentStore.GetDefaultPaymentId() != defaultPaymentMethodId) { needUpdate = true; CurrentStore.SetDefaultPaymentId(defaultPaymentMethodId); } SetCryptoCurrencies(model, CurrentStore); model.SetLanguages(_LangService, model.DefaultLang); model.PaymentMethodCriteria ??= new List <PaymentMethodCriteriaViewModel>(); for (var index = 0; index < model.PaymentMethodCriteria.Count; index++) { var methodCriterion = model.PaymentMethodCriteria[index]; if (!string.IsNullOrWhiteSpace(methodCriterion.Value)) { if (!CurrencyValue.TryParse(methodCriterion.Value, out var value)) { model.AddModelError(viewModel => viewModel.PaymentMethodCriteria[index].Value, $"{methodCriterion.PaymentMethod}: Invalid format. Make sure to enter a valid amount and currency code. Examples: '5 USD', '0.001 BTC'", this); } } } if (!ModelState.IsValid) { return(View(model)); } // Payment criteria for Off-Chain should also affect LNUrl foreach (var newCriteria in model.PaymentMethodCriteria.ToList()) { var paymentMethodId = PaymentMethodId.Parse(newCriteria.PaymentMethod); if (paymentMethodId.PaymentType == PaymentTypes.LightningLike) { model.PaymentMethodCriteria.Add(new PaymentMethodCriteriaViewModel() { PaymentMethod = new PaymentMethodId(paymentMethodId.CryptoCode, PaymentTypes.LNURLPay).ToString(), Type = newCriteria.Type, Value = newCriteria.Value }); } // Should not be able to set LNUrlPay criteria directly in UI if (paymentMethodId.PaymentType == PaymentTypes.LNURLPay) { model.PaymentMethodCriteria.Remove(newCriteria); } } blob.PaymentMethodCriteria ??= new List <PaymentMethodCriteria>(); foreach (var newCriteria in model.PaymentMethodCriteria) { var paymentMethodId = PaymentMethodId.Parse(newCriteria.PaymentMethod); var existingCriteria = blob.PaymentMethodCriteria.FirstOrDefault(c => c.PaymentMethod == paymentMethodId); if (existingCriteria != null) { blob.PaymentMethodCriteria.Remove(existingCriteria); } CurrencyValue.TryParse(newCriteria.Value, out var cv); blob.PaymentMethodCriteria.Add(new PaymentMethodCriteria() { Above = newCriteria.Type == PaymentMethodCriteriaViewModel.CriteriaType.GreaterThan, Value = cv, PaymentMethod = paymentMethodId }); } blob.RequiresRefundEmail = model.RequiresRefundEmail; blob.LazyPaymentMethods = model.LazyPaymentMethods; blob.RedirectAutomatically = model.RedirectAutomatically; blob.CustomLogo = model.CustomLogo; blob.CustomCSS = model.CustomCSS; blob.HtmlTitle = string.IsNullOrWhiteSpace(model.HtmlTitle) ? null : model.HtmlTitle; blob.AutoDetectLanguage = model.AutoDetectLanguage; blob.DefaultLang = model.DefaultLang; if (CurrentStore.SetStoreBlob(blob)) { needUpdate = true; } if (needUpdate) { await _Repo.UpdateStore(CurrentStore); TempData[WellKnownTempData.SuccessMessage] = "Store successfully updated"; } return(RedirectToAction(nameof(CheckoutAppearance), new { storeId = CurrentStore.Id })); }
public async Task <IActionResult> CheckoutExperience(string storeId, CheckoutExperienceViewModel model) { CurrencyValue lightningMaxValue = null; if (!string.IsNullOrWhiteSpace(model.LightningMaxValue)) { if (!CurrencyValue.TryParse(model.LightningMaxValue, out lightningMaxValue)) { ModelState.AddModelError(nameof(model.LightningMaxValue), "Invalid lightning max value"); } } CurrencyValue onchainMinValue = null; if (!string.IsNullOrWhiteSpace(model.OnChainMinValue)) { if (!CurrencyValue.TryParse(model.OnChainMinValue, out onchainMinValue)) { ModelState.AddModelError(nameof(model.OnChainMinValue), "Invalid on chain min value"); } } var store = await _Repo.FindStore(storeId, GetUserId()); if (store == null) { return(NotFound()); } bool needUpdate = false; var blob = store.GetStoreBlob(); if (store.GetDefaultCrypto() != model.DefaultCryptoCurrency) { needUpdate = true; store.SetDefaultCrypto(model.DefaultCryptoCurrency); } model.SetCryptoCurrencies(_ExplorerProvider, model.DefaultCryptoCurrency); model.SetLanguages(_LangService, model.DefaultLang); if (!ModelState.IsValid) { return(View(model)); } blob.DefaultLang = model.DefaultLang; blob.AllowCoinConversion = model.AllowCoinConversion; blob.LightningMaxValue = lightningMaxValue; blob.OnChainMinValue = onchainMinValue; blob.CustomLogo = string.IsNullOrWhiteSpace(model.CustomLogo) ? null : new Uri(model.CustomLogo, UriKind.Absolute); blob.CustomCSS = string.IsNullOrWhiteSpace(model.CustomCSS) ? null : new Uri(model.CustomCSS, UriKind.Absolute); if (store.SetStoreBlob(blob)) { needUpdate = true; } if (needUpdate) { await _Repo.UpdateStore(store); StatusMessage = "Store successfully updated"; } return(RedirectToAction(nameof(CheckoutExperience), new { storeId = storeId })); }
public async Task <IActionResult> CheckoutExperience(CheckoutExperienceViewModel model) { CurrencyValue lightningMaxValue = null; if (!string.IsNullOrWhiteSpace(model.LightningMaxValue)) { if (!CurrencyValue.TryParse(model.LightningMaxValue, out lightningMaxValue)) { ModelState.AddModelError(nameof(model.LightningMaxValue), "Invalid lightning max value"); } } CurrencyValue onchainMinValue = null; if (!string.IsNullOrWhiteSpace(model.OnChainMinValue)) { if (!CurrencyValue.TryParse(model.OnChainMinValue, out onchainMinValue)) { ModelState.AddModelError(nameof(model.OnChainMinValue), "Invalid on chain min value"); } } bool needUpdate = false; var blob = CurrentStore.GetStoreBlob(); var defaultPaymentMethodId = model.DefaultPaymentMethod == null ? null : PaymentMethodId.Parse(model.DefaultPaymentMethod); if (CurrentStore.GetDefaultPaymentId(_NetworkProvider) != defaultPaymentMethodId) { needUpdate = true; CurrentStore.SetDefaultPaymentId(defaultPaymentMethodId); } SetbitcoinCurrencies(model, CurrentStore); model.SetLanguages(_LangService, model.DefaultLang); if (!ModelState.IsValid) { return(View(model)); } blob.CustomLogo = model.CustomLogo; blob.CustomCSS = model.CustomCSS; blob.HtmlTitle = string.IsNullOrWhiteSpace(model.HtmlTitle) ? null : model.HtmlTitle; blob.DefaultLang = model.DefaultLang; blob.RequiresRefundEmail = model.RequiresRefundEmail; blob.ShowRecommendedFee = model.ShowRecommendedFee; blob.RecommendedFeeBlockTarget = model.RecommendedFeeBlockTarget; blob.OnChainMinValue = onchainMinValue; blob.LightningMaxValue = lightningMaxValue; blob.LightningAmountInSatoshi = model.LightningAmountInSatoshi; blob.LightningPrivateRouteHints = model.LightningPrivateRouteHints; blob.RedirectAutomatically = model.RedirectAutomatically; if (CurrentStore.SetStoreBlob(blob)) { needUpdate = true; } if (needUpdate) { await _Repo.UpdateStore(CurrentStore); TempData[WellKnownTempData.SuccessMessage] = "Store successfully updated"; } return(RedirectToAction(nameof(CheckoutExperience), new { storeId = CurrentStore.Id })); }