btcpayserver/BTCPayServer/Controllers/StoresController.CoinSwitch.cs

78 lines
2.5 KiB
C#
Raw Normal View History

2020-06-29 04:44:35 +02:00
using System.Threading.Tasks;
2018-12-11 12:47:38 +01:00
using BTCPayServer.Data;
using BTCPayServer.Models.StoreViewModels;
using BTCPayServer.Payments.CoinSwitch;
using Microsoft.AspNetCore.Mvc;
namespace BTCPayServer.Controllers
{
public partial class StoresController
{
[HttpGet]
[Route("{storeId}/coinswitch")]
public IActionResult UpdateCoinSwitchSettings(string storeId)
{
var store = HttpContext.GetStoreData();
if (store == null)
return NotFound();
UpdateCoinSwitchSettingsViewModel vm = new UpdateCoinSwitchSettingsViewModel();
SetExistingValues(store, vm);
return View(vm);
}
private void SetExistingValues(StoreData store, UpdateCoinSwitchSettingsViewModel vm)
{
var existing = store.GetStoreBlob().CoinSwitchSettings;
2020-06-28 10:55:27 +02:00
if (existing == null)
return;
2018-12-11 12:47:38 +01:00
vm.MerchantId = existing.MerchantId;
vm.Enabled = existing.Enabled;
2018-12-18 19:01:58 +01:00
vm.Mode = existing.Mode;
2019-03-31 18:55:14 +02:00
vm.AmountMarkupPercentage = existing.AmountMarkupPercentage;
2018-12-11 12:47:38 +01:00
}
[HttpPost]
[Route("{storeId}/coinswitch")]
public async Task<IActionResult> UpdateCoinSwitchSettings(string storeId, UpdateCoinSwitchSettingsViewModel vm,
string command)
{
var store = HttpContext.GetStoreData();
if (store == null)
return NotFound();
if (vm.Enabled)
{
if (!ModelState.IsValid)
{
return View(vm);
}
}
var coinSwitchSettings = new CoinSwitchSettings()
{
2020-06-28 10:55:27 +02:00
MerchantId = vm.MerchantId,
2018-12-18 19:01:58 +01:00
Enabled = vm.Enabled,
2019-03-31 18:55:14 +02:00
Mode = vm.Mode,
AmountMarkupPercentage = vm.AmountMarkupPercentage
2018-12-11 12:47:38 +01:00
};
2020-06-28 10:55:27 +02:00
2018-12-11 12:47:38 +01:00
switch (command)
{
case "save":
var storeBlob = store.GetStoreBlob();
storeBlob.CoinSwitchSettings = coinSwitchSettings;
store.SetStoreBlob(storeBlob);
await _Repo.UpdateStore(store);
TempData[WellKnownTempData.SuccessMessage] = "CoinSwitch settings modified";
2020-06-28 10:55:27 +02:00
return RedirectToAction(nameof(UpdateStore), new
{
storeId
});
2018-12-11 12:47:38 +01:00
default:
return View(vm);
}
}
}
}