2017-09-13 16:50:36 +02:00
|
|
|
|
using BTCPayServer.Authentication;
|
2018-02-26 10:58:02 +01:00
|
|
|
|
using BTCPayServer.Configuration;
|
2017-10-17 06:52:30 +02:00
|
|
|
|
using BTCPayServer.Data;
|
2018-03-02 20:16:16 +01:00
|
|
|
|
using BTCPayServer.HostedServices;
|
2017-09-13 16:50:36 +02:00
|
|
|
|
using BTCPayServer.Models;
|
|
|
|
|
using BTCPayServer.Models.StoreViewModels;
|
2018-02-12 19:27:36 +01:00
|
|
|
|
using BTCPayServer.Services;
|
2017-09-15 09:06:57 +02:00
|
|
|
|
using BTCPayServer.Services.Stores;
|
|
|
|
|
using BTCPayServer.Services.Wallets;
|
2017-09-13 16:50:36 +02:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
|
|
|
using Microsoft.AspNetCore.Identity;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2017-10-17 06:52:30 +02:00
|
|
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
2018-02-12 19:27:36 +01:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2017-09-13 16:50:36 +02:00
|
|
|
|
using NBitcoin;
|
2017-12-06 10:08:21 +01:00
|
|
|
|
using NBitcoin.DataEncoders;
|
2017-10-04 17:05:38 +02:00
|
|
|
|
using NBXplorer.DerivationStrategy;
|
2017-09-13 16:50:36 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2018-01-19 08:00:20 +01:00
|
|
|
|
using System.Net.Http;
|
2018-02-12 19:27:36 +01:00
|
|
|
|
using System.Threading;
|
2017-09-13 16:50:36 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace BTCPayServer.Controllers
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[Route("stores")]
|
|
|
|
|
[Authorize(AuthenticationSchemes = "Identity.Application")]
|
2018-03-23 08:24:57 +01:00
|
|
|
|
[Authorize(Policy = StorePolicies.OwnStore)]
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[AutoValidateAntiforgeryToken]
|
2018-02-25 16:48:12 +01:00
|
|
|
|
public partial class StoresController : Controller
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-03-23 08:24:57 +01:00
|
|
|
|
public string CreatedStoreId { get; set; }
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public StoresController(
|
2018-03-02 20:16:16 +01:00
|
|
|
|
NBXplorerDashboard dashboard,
|
2018-02-25 16:48:12 +01:00
|
|
|
|
IServiceProvider serviceProvider,
|
2018-02-26 10:58:02 +01:00
|
|
|
|
BTCPayServerOptions btcpayServerOptions,
|
|
|
|
|
BTCPayServerEnvironment btcpayEnv,
|
2018-02-12 19:27:36 +01:00
|
|
|
|
IOptions<MvcJsonOptions> mvcJsonOptions,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
StoreRepository repo,
|
|
|
|
|
TokenRepository tokenRepo,
|
|
|
|
|
UserManager<ApplicationUser> userManager,
|
|
|
|
|
AccessTokenController tokenController,
|
2018-01-11 06:36:12 +01:00
|
|
|
|
BTCPayWalletProvider walletProvider,
|
2017-12-21 07:52:04 +01:00
|
|
|
|
BTCPayNetworkProvider networkProvider,
|
2018-01-08 14:45:09 +01:00
|
|
|
|
ExplorerClientProvider explorerProvider,
|
2018-02-12 19:27:36 +01:00
|
|
|
|
IFeeProviderFactory feeRateProvider,
|
2018-03-23 09:27:48 +01:00
|
|
|
|
LanguageService langService,
|
2017-10-27 10:53:04 +02:00
|
|
|
|
IHostingEnvironment env)
|
|
|
|
|
{
|
2018-03-02 20:16:16 +01:00
|
|
|
|
_Dashboard = dashboard;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
_Repo = repo;
|
|
|
|
|
_TokenRepository = tokenRepo;
|
|
|
|
|
_UserManager = userManager;
|
2018-03-23 09:27:48 +01:00
|
|
|
|
_LangService = langService;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
_TokenController = tokenController;
|
2018-01-11 06:36:12 +01:00
|
|
|
|
_WalletProvider = walletProvider;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
_Env = env;
|
2018-01-06 10:57:56 +01:00
|
|
|
|
_NetworkProvider = networkProvider;
|
2018-01-08 14:45:09 +01:00
|
|
|
|
_ExplorerProvider = explorerProvider;
|
2018-02-12 19:27:36 +01:00
|
|
|
|
_MvcJsonOptions = mvcJsonOptions.Value;
|
|
|
|
|
_FeeRateProvider = feeRateProvider;
|
2018-02-25 16:48:12 +01:00
|
|
|
|
_ServiceProvider = serviceProvider;
|
2018-02-26 10:58:02 +01:00
|
|
|
|
_BtcpayServerOptions = btcpayServerOptions;
|
|
|
|
|
_BTCPayEnv = btcpayEnv;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2018-03-02 20:16:16 +01:00
|
|
|
|
NBXplorerDashboard _Dashboard;
|
2018-02-26 10:58:02 +01:00
|
|
|
|
BTCPayServerOptions _BtcpayServerOptions;
|
|
|
|
|
BTCPayServerEnvironment _BTCPayEnv;
|
2018-02-25 16:48:12 +01:00
|
|
|
|
IServiceProvider _ServiceProvider;
|
2018-01-06 10:57:56 +01:00
|
|
|
|
BTCPayNetworkProvider _NetworkProvider;
|
2018-01-08 14:45:09 +01:00
|
|
|
|
private ExplorerClientProvider _ExplorerProvider;
|
2018-02-12 19:27:36 +01:00
|
|
|
|
private MvcJsonOptions _MvcJsonOptions;
|
|
|
|
|
private IFeeProviderFactory _FeeRateProvider;
|
2018-01-11 06:36:12 +01:00
|
|
|
|
BTCPayWalletProvider _WalletProvider;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
AccessTokenController _TokenController;
|
|
|
|
|
StoreRepository _Repo;
|
|
|
|
|
TokenRepository _TokenRepository;
|
|
|
|
|
UserManager<ApplicationUser> _UserManager;
|
2018-03-23 09:27:48 +01:00
|
|
|
|
private LanguageService _LangService;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
IHostingEnvironment _Env;
|
|
|
|
|
|
|
|
|
|
[TempData]
|
|
|
|
|
public string StatusMessage
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 19:27:36 +01:00
|
|
|
|
[HttpGet]
|
2018-03-21 17:07:11 +01:00
|
|
|
|
[Route("{storeId}/wallet/{cryptoCode}")]
|
|
|
|
|
public async Task<IActionResult> Wallet(string storeId, string cryptoCode)
|
2018-02-12 19:27:36 +01:00
|
|
|
|
{
|
|
|
|
|
var store = await _Repo.FindStore(storeId, GetUserId());
|
|
|
|
|
if (store == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
WalletModel model = new WalletModel();
|
|
|
|
|
model.ServerUrl = GetStoreUrl(storeId);
|
2018-03-21 17:07:11 +01:00
|
|
|
|
model.CryptoCurrency = cryptoCode;
|
2018-02-12 19:27:36 +01:00
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetStoreUrl(string storeId)
|
|
|
|
|
{
|
|
|
|
|
return HttpContext.Request.GetAbsoluteRoot() + "/stores/" + storeId + "/";
|
2018-03-24 12:40:26 +01:00
|
|
|
|
}
|
2018-02-12 19:27:36 +01:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpGet]
|
2018-03-23 08:24:57 +01:00
|
|
|
|
[Route("{storeId}/users")]
|
|
|
|
|
public async Task<IActionResult> StoreUsers(string storeId)
|
|
|
|
|
{
|
|
|
|
|
StoreUsersViewModel vm = new StoreUsersViewModel();
|
|
|
|
|
await FillUsers(storeId, vm);
|
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task FillUsers(string storeId, StoreUsersViewModel vm)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-03-23 08:24:57 +01:00
|
|
|
|
var users = await _Repo.GetStoreUsers(storeId);
|
|
|
|
|
vm.StoreId = storeId;
|
|
|
|
|
vm.Users = users.Select(u => new StoreUsersViewModel.StoreUserViewModel()
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-03-23 08:24:57 +01:00
|
|
|
|
Email = u.Email,
|
|
|
|
|
Id = u.Id,
|
|
|
|
|
Role = u.Role
|
|
|
|
|
}).ToList();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-23 08:24:57 +01:00
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("{storeId}/users")]
|
|
|
|
|
public async Task<IActionResult> StoreUsers(string storeId, StoreUsersViewModel vm)
|
2018-02-15 05:33:29 +01:00
|
|
|
|
{
|
2018-03-23 08:24:57 +01:00
|
|
|
|
await FillUsers(storeId, vm);
|
2018-03-24 12:40:26 +01:00
|
|
|
|
if (!ModelState.IsValid)
|
2018-02-15 05:33:29 +01:00
|
|
|
|
{
|
2018-03-23 08:24:57 +01:00
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
|
|
|
|
var user = await _UserManager.FindByEmailAsync(vm.Email);
|
2018-03-24 12:40:26 +01:00
|
|
|
|
if (user == null)
|
2018-03-23 08:24:57 +01:00
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError(nameof(vm.Email), "User not found");
|
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
2018-03-24 12:40:26 +01:00
|
|
|
|
if (!StoreRoles.AllRoles.Contains(vm.Role))
|
2018-03-23 08:24:57 +01:00
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError(nameof(vm.Role), "Invalid role");
|
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
2018-03-24 12:40:26 +01:00
|
|
|
|
if (!await _Repo.AddStoreUser(storeId, user.Id, vm.Role))
|
2018-03-23 08:24:57 +01:00
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError(nameof(vm.Email), "The user already has access to this store");
|
|
|
|
|
return View(vm);
|
2018-02-15 05:33:29 +01:00
|
|
|
|
}
|
2018-03-23 08:24:57 +01:00
|
|
|
|
StatusMessage = "User added successfully";
|
|
|
|
|
return RedirectToAction(nameof(StoreUsers));
|
2018-02-15 05:33:29 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpGet]
|
2018-03-23 08:24:57 +01:00
|
|
|
|
[Route("{storeId}/users/{userId}/delete")]
|
|
|
|
|
public async Task<IActionResult> DeleteStoreUser(string storeId, string userId)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-03-23 08:24:57 +01:00
|
|
|
|
StoreUsersViewModel vm = new StoreUsersViewModel();
|
|
|
|
|
var store = await _Repo.FindStore(storeId, userId);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
if (store == null)
|
|
|
|
|
return NotFound();
|
2018-03-23 08:24:57 +01:00
|
|
|
|
var user = await _UserManager.FindByIdAsync(userId);
|
|
|
|
|
if (user == null)
|
|
|
|
|
return NotFound();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return View("Confirm", new ConfirmModel()
|
|
|
|
|
{
|
2018-03-23 08:24:57 +01:00
|
|
|
|
Title = $"Remove store user",
|
|
|
|
|
Description = $"Are you sure to remove access to remove {store.Role} access to {user.Email}?",
|
2017-10-27 10:53:04 +02:00
|
|
|
|
Action = "Delete"
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2018-03-23 08:24:57 +01:00
|
|
|
|
[Route("{storeId}/users/{userId}/delete")]
|
|
|
|
|
public async Task<IActionResult> DeleteStoreUserPost(string storeId, string userId)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-03-23 08:24:57 +01:00
|
|
|
|
await _Repo.RemoveStoreUser(storeId, userId);
|
|
|
|
|
StatusMessage = "User removed successfully";
|
|
|
|
|
return RedirectToAction(nameof(StoreUsers), new { storeId = storeId, userId = userId });
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("{storeId}")]
|
|
|
|
|
public async Task<IActionResult> UpdateStore(string storeId)
|
|
|
|
|
{
|
|
|
|
|
var store = await _Repo.FindStore(storeId, GetUserId());
|
|
|
|
|
if (store == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
2017-12-21 07:52:04 +01:00
|
|
|
|
var storeBlob = store.GetStoreBlob();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var vm = new StoreViewModel();
|
2017-12-03 15:35:52 +01:00
|
|
|
|
vm.Id = store.Id;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
vm.StoreName = store.StoreName;
|
2018-01-09 03:41:07 +01:00
|
|
|
|
vm.SetCryptoCurrencies(_ExplorerProvider, store.GetDefaultCrypto());
|
2018-03-23 09:27:48 +01:00
|
|
|
|
vm.SetLanguages(_LangService, storeBlob.DefaultLang);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
vm.StoreWebsite = store.StoreWebsite;
|
2017-12-03 06:43:52 +01:00
|
|
|
|
vm.NetworkFee = !storeBlob.NetworkFeeDisabled;
|
2018-03-25 18:57:44 +02:00
|
|
|
|
vm.LightningMaxValue = storeBlob.LightningMaxValue?.ToString() ?? "";
|
2017-10-27 10:53:04 +02:00
|
|
|
|
vm.SpeedPolicy = store.SpeedPolicy;
|
2018-02-25 16:48:12 +01:00
|
|
|
|
AddPaymentMethods(store, vm);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
vm.StatusMessage = StatusMessage;
|
2017-12-03 06:43:52 +01:00
|
|
|
|
vm.MonitoringExpiration = storeBlob.MonitoringExpiration;
|
2018-01-17 07:11:05 +01:00
|
|
|
|
vm.InvoiceExpiration = storeBlob.InvoiceExpiration;
|
2018-01-17 07:59:31 +01:00
|
|
|
|
vm.RateMultiplier = (double)storeBlob.GetRateMultiplier();
|
2018-01-19 09:13:29 +01:00
|
|
|
|
vm.PreferredExchange = storeBlob.PreferredExchange.IsCoinAverage() ? "coinaverage" : storeBlob.PreferredExchange;
|
2018-03-18 05:48:06 +01:00
|
|
|
|
vm.AllowCoinConversion = storeBlob.AllowCoinConversion;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return View(vm);
|
|
|
|
|
}
|
2018-03-24 12:40:26 +01:00
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2018-02-25 16:48:12 +01:00
|
|
|
|
private void AddPaymentMethods(StoreData store, StoreViewModel vm)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-03-24 12:40:26 +01:00
|
|
|
|
var derivationByCryptoCode =
|
2018-03-20 18:48:11 +01:00
|
|
|
|
store
|
|
|
|
|
.GetSupportedPaymentMethods(_NetworkProvider)
|
|
|
|
|
.OfType<DerivationStrategy>()
|
|
|
|
|
.ToDictionary(c => c.Network.CryptoCode);
|
|
|
|
|
foreach (var network in _NetworkProvider.GetAll())
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-03-20 18:48:11 +01:00
|
|
|
|
var strategy = derivationByCryptoCode.TryGet(network.CryptoCode);
|
2018-02-25 16:48:12 +01:00
|
|
|
|
vm.DerivationSchemes.Add(new StoreViewModel.DerivationScheme()
|
2018-01-08 14:45:09 +01:00
|
|
|
|
{
|
2018-03-20 18:48:11 +01:00
|
|
|
|
Crypto = network.CryptoCode,
|
|
|
|
|
Value = strategy?.DerivationStrategyBase?.ToString() ?? string.Empty
|
2018-02-25 16:48:12 +01:00
|
|
|
|
});
|
2018-02-07 13:59:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-20 18:48:11 +01:00
|
|
|
|
var lightningByCryptoCode = store
|
2018-02-25 16:48:12 +01:00
|
|
|
|
.GetSupportedPaymentMethods(_NetworkProvider)
|
2018-03-20 18:48:11 +01:00
|
|
|
|
.OfType<Payments.Lightning.LightningSupportedPaymentMethod>()
|
|
|
|
|
.ToDictionary(c => c.CryptoCode);
|
|
|
|
|
|
|
|
|
|
foreach (var network in _NetworkProvider.GetAll())
|
2018-01-08 14:45:09 +01:00
|
|
|
|
{
|
2018-03-20 18:48:11 +01:00
|
|
|
|
var lightning = lightningByCryptoCode.TryGet(network.CryptoCode);
|
2018-02-25 16:48:12 +01:00
|
|
|
|
vm.LightningNodes.Add(new StoreViewModel.LightningNode()
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-03-20 18:48:11 +01:00
|
|
|
|
CryptoCode = network.CryptoCode,
|
|
|
|
|
Address = lightning?.GetLightningUrl()?.BaseUri.AbsoluteUri ?? string.Empty
|
2018-02-25 16:48:12 +01:00
|
|
|
|
});
|
2018-01-08 14:45:09 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("{storeId}")]
|
|
|
|
|
public async Task<IActionResult> UpdateStore(string storeId, StoreViewModel model)
|
|
|
|
|
{
|
2018-03-25 18:57:44 +02:00
|
|
|
|
CurrencyValue currencyValue = null;
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(model.LightningMaxValue))
|
|
|
|
|
{
|
|
|
|
|
if(!CurrencyValue.TryParse(model.LightningMaxValue, out currencyValue))
|
|
|
|
|
{
|
|
|
|
|
ModelState.AddModelError(nameof(model.LightningMaxValue), "Invalid currency value");
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-01-08 14:45:09 +01:00
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return View(model);
|
|
|
|
|
}
|
2018-01-19 10:11:43 +01:00
|
|
|
|
if (model.PreferredExchange != null)
|
|
|
|
|
model.PreferredExchange = model.PreferredExchange.Trim().ToLowerInvariant();
|
2018-01-08 14:45:09 +01:00
|
|
|
|
var store = await _Repo.FindStore(storeId, GetUserId());
|
|
|
|
|
if (store == null)
|
|
|
|
|
return NotFound();
|
2018-02-25 16:48:12 +01:00
|
|
|
|
AddPaymentMethods(store, model);
|
2018-01-08 14:45:09 +01:00
|
|
|
|
|
|
|
|
|
bool needUpdate = false;
|
|
|
|
|
if (store.SpeedPolicy != model.SpeedPolicy)
|
|
|
|
|
{
|
|
|
|
|
needUpdate = true;
|
|
|
|
|
store.SpeedPolicy = model.SpeedPolicy;
|
|
|
|
|
}
|
|
|
|
|
if (store.StoreName != model.StoreName)
|
|
|
|
|
{
|
|
|
|
|
needUpdate = true;
|
|
|
|
|
store.StoreName = model.StoreName;
|
|
|
|
|
}
|
|
|
|
|
if (store.StoreWebsite != model.StoreWebsite)
|
|
|
|
|
{
|
|
|
|
|
needUpdate = true;
|
|
|
|
|
store.StoreWebsite = model.StoreWebsite;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 03:41:07 +01:00
|
|
|
|
if (store.GetDefaultCrypto() != model.DefaultCryptoCurrency)
|
|
|
|
|
{
|
|
|
|
|
needUpdate = true;
|
|
|
|
|
store.SetDefaultCrypto(model.DefaultCryptoCurrency);
|
|
|
|
|
}
|
|
|
|
|
model.SetCryptoCurrencies(_ExplorerProvider, model.DefaultCryptoCurrency);
|
2018-03-23 09:27:48 +01:00
|
|
|
|
model.SetLanguages(_LangService, model.DefaultLang);
|
2018-01-09 03:41:07 +01:00
|
|
|
|
|
2018-01-08 14:45:09 +01:00
|
|
|
|
var blob = store.GetStoreBlob();
|
|
|
|
|
blob.NetworkFeeDisabled = !model.NetworkFee;
|
|
|
|
|
blob.MonitoringExpiration = model.MonitoringExpiration;
|
2018-01-17 07:59:31 +01:00
|
|
|
|
blob.InvoiceExpiration = model.InvoiceExpiration;
|
2018-03-23 09:27:48 +01:00
|
|
|
|
blob.DefaultLang = model.DefaultLang;
|
2018-03-25 18:57:44 +02:00
|
|
|
|
blob.LightningMaxValue = currencyValue;
|
2018-01-19 08:19:13 +01:00
|
|
|
|
|
|
|
|
|
bool newExchange = blob.PreferredExchange != model.PreferredExchange;
|
2018-01-19 08:00:20 +01:00
|
|
|
|
blob.PreferredExchange = model.PreferredExchange;
|
|
|
|
|
|
2018-01-17 07:59:31 +01:00
|
|
|
|
blob.SetRateMultiplier(model.RateMultiplier);
|
2018-03-18 05:48:06 +01:00
|
|
|
|
blob.AllowCoinConversion = model.AllowCoinConversion;
|
2018-01-08 14:45:09 +01:00
|
|
|
|
|
|
|
|
|
if (store.SetStoreBlob(blob))
|
|
|
|
|
{
|
|
|
|
|
needUpdate = true;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 09:13:29 +01:00
|
|
|
|
if (!blob.PreferredExchange.IsCoinAverage() && newExchange)
|
2018-01-19 08:00:20 +01:00
|
|
|
|
{
|
|
|
|
|
using (HttpClient client = new HttpClient())
|
|
|
|
|
{
|
|
|
|
|
var rate = await client.GetAsync(model.RateSource);
|
|
|
|
|
if (rate.StatusCode == System.Net.HttpStatusCode.NotFound)
|
|
|
|
|
{
|
2018-01-19 09:13:29 +01:00
|
|
|
|
ModelState.AddModelError(nameof(model.PreferredExchange), $"Unsupported exchange ({model.RateSource})");
|
2018-01-19 08:00:20 +01:00
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-08 14:45:09 +01:00
|
|
|
|
if (needUpdate)
|
|
|
|
|
{
|
|
|
|
|
await _Repo.UpdateStore(store);
|
|
|
|
|
StatusMessage = "Store successfully updated";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return RedirectToAction(nameof(UpdateStore), new
|
|
|
|
|
{
|
|
|
|
|
storeId = storeId
|
|
|
|
|
});
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-24 12:40:26 +01:00
|
|
|
|
private DerivationStrategy ParseDerivationStrategy(string derivationScheme, Script hint, BTCPayNetwork network)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-03-24 12:40:26 +01:00
|
|
|
|
var parser = new DerivationSchemeParser(network.NBitcoinNetwork, network.DefaultSettings.ChainType);
|
|
|
|
|
parser.HintScriptPubKey = hint;
|
|
|
|
|
return new DerivationStrategy(parser.Parse(derivationScheme), network);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("{storeId}/Tokens")]
|
|
|
|
|
public async Task<IActionResult> ListTokens(string storeId)
|
|
|
|
|
{
|
|
|
|
|
var model = new TokensViewModel();
|
|
|
|
|
var tokens = await _TokenRepository.GetTokensByStoreIdAsync(storeId);
|
|
|
|
|
model.StatusMessage = StatusMessage;
|
|
|
|
|
model.Tokens = tokens.Select(t => new TokenViewModel()
|
|
|
|
|
{
|
|
|
|
|
Facade = t.Facade,
|
|
|
|
|
Label = t.Label,
|
|
|
|
|
SIN = t.SIN,
|
|
|
|
|
Id = t.Value
|
|
|
|
|
}).ToArray();
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("/api-tokens")]
|
|
|
|
|
[Route("{storeId}/Tokens/Create")]
|
|
|
|
|
public async Task<IActionResult> CreateToken(string storeId, CreateTokenViewModel model)
|
|
|
|
|
{
|
|
|
|
|
if (!ModelState.IsValid)
|
|
|
|
|
{
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
model.Label = model.Label ?? String.Empty;
|
2018-03-23 08:24:57 +01:00
|
|
|
|
storeId = model.StoreId ?? storeId;
|
|
|
|
|
var userId = GetUserId();
|
|
|
|
|
if (userId == null)
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
var store = await _Repo.FindStore(storeId, userId);
|
|
|
|
|
if (store == null)
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
if (store.Role != StoreRoles.Owner)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-03-23 08:24:57 +01:00
|
|
|
|
StatusMessage = "Error: You need to be owner of this store to request pairing codes";
|
|
|
|
|
return RedirectToAction(nameof(UserStoresController.ListStores), "UserStores");
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tokenRequest = new TokenRequest()
|
|
|
|
|
{
|
|
|
|
|
Facade = model.Facade,
|
|
|
|
|
Label = model.Label,
|
|
|
|
|
Id = model.PublicKey == null ? null : NBitpayClient.Extensions.BitIdExtensions.GetBitIDSIN(new PubKey(model.PublicKey))
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
string pairingCode = null;
|
|
|
|
|
if (model.PublicKey == null)
|
|
|
|
|
{
|
|
|
|
|
tokenRequest.PairingCode = await _TokenRepository.CreatePairingCodeAsync();
|
|
|
|
|
await _TokenRepository.UpdatePairingCode(new PairingCodeEntity()
|
|
|
|
|
{
|
|
|
|
|
Id = tokenRequest.PairingCode,
|
|
|
|
|
Facade = model.Facade,
|
|
|
|
|
Label = model.Label,
|
|
|
|
|
});
|
|
|
|
|
await _TokenRepository.PairWithStoreAsync(tokenRequest.PairingCode, storeId);
|
|
|
|
|
pairingCode = tokenRequest.PairingCode;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
pairingCode = ((DataWrapper<List<PairingCodeResponse>>)await _TokenController.Tokens(tokenRequest)).Data[0].PairingCode;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 18:07:42 +01:00
|
|
|
|
GeneratedPairingCode = pairingCode;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return RedirectToAction(nameof(RequestPairing), new
|
|
|
|
|
{
|
|
|
|
|
pairingCode = pairingCode,
|
|
|
|
|
selectedStore = storeId
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-09 18:07:42 +01:00
|
|
|
|
public string GeneratedPairingCode { get; set; }
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("/api-tokens")]
|
|
|
|
|
[Route("{storeId}/Tokens/Create")]
|
|
|
|
|
public async Task<IActionResult> CreateToken(string storeId)
|
|
|
|
|
{
|
|
|
|
|
var userId = GetUserId();
|
|
|
|
|
if (string.IsNullOrWhiteSpace(userId))
|
|
|
|
|
return Unauthorized();
|
|
|
|
|
var model = new CreateTokenViewModel();
|
|
|
|
|
model.Facade = "merchant";
|
|
|
|
|
ViewBag.HidePublicKey = storeId == null;
|
|
|
|
|
ViewBag.ShowStores = storeId == null;
|
|
|
|
|
ViewBag.ShowMenu = storeId != null;
|
|
|
|
|
model.StoreId = storeId;
|
|
|
|
|
if (storeId == null)
|
|
|
|
|
{
|
|
|
|
|
model.Stores = new SelectList(await _Repo.GetStoresByUserId(userId), nameof(StoreData.Id), nameof(StoreData.StoreName), storeId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return View(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
|
|
|
|
[Route("{storeId}/Tokens/Delete")]
|
|
|
|
|
public async Task<IActionResult> DeleteToken(string storeId, string tokenId)
|
|
|
|
|
{
|
|
|
|
|
var token = await _TokenRepository.GetToken(tokenId);
|
|
|
|
|
if (token == null ||
|
|
|
|
|
token.StoreId != storeId ||
|
|
|
|
|
!await _TokenRepository.DeleteToken(tokenId))
|
|
|
|
|
StatusMessage = "Failure to revoke this token";
|
|
|
|
|
else
|
|
|
|
|
StatusMessage = "Token revoked";
|
|
|
|
|
return RedirectToAction(nameof(ListTokens));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet]
|
|
|
|
|
[Route("/api-access-request")]
|
|
|
|
|
public async Task<IActionResult> RequestPairing(string pairingCode, string selectedStore = null)
|
|
|
|
|
{
|
2018-03-23 08:24:57 +01:00
|
|
|
|
if (pairingCode == null)
|
|
|
|
|
return NotFound();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var pairing = await _TokenRepository.GetPairingAsync(pairingCode);
|
|
|
|
|
if (pairing == null)
|
|
|
|
|
{
|
|
|
|
|
StatusMessage = "Unknown pairing code";
|
2018-03-23 08:24:57 +01:00
|
|
|
|
return RedirectToAction(nameof(UserStoresController.ListStores), "UserStores");
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
var stores = await _Repo.GetStoresByUserId(GetUserId());
|
|
|
|
|
return View(new PairingModel()
|
|
|
|
|
{
|
|
|
|
|
Id = pairing.Id,
|
|
|
|
|
Facade = pairing.Facade,
|
|
|
|
|
Label = pairing.Label,
|
|
|
|
|
SIN = pairing.SIN ?? "Server-Initiated Pairing",
|
|
|
|
|
SelectedStore = selectedStore ?? stores.FirstOrDefault()?.Id,
|
|
|
|
|
Stores = stores.Select(s => new PairingModel.StoreViewModel()
|
|
|
|
|
{
|
|
|
|
|
Id = s.Id,
|
|
|
|
|
Name = string.IsNullOrEmpty(s.StoreName) ? s.Id : s.StoreName
|
|
|
|
|
}).ToArray()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost]
|
2018-03-23 08:24:57 +01:00
|
|
|
|
[Route("/api-access-request")]
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public async Task<IActionResult> Pair(string pairingCode, string selectedStore)
|
|
|
|
|
{
|
|
|
|
|
if (pairingCode == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
var store = await _Repo.FindStore(selectedStore, GetUserId());
|
|
|
|
|
var pairing = await _TokenRepository.GetPairingAsync(pairingCode);
|
|
|
|
|
if (store == null || pairing == null)
|
|
|
|
|
return NotFound();
|
|
|
|
|
|
2018-03-24 12:40:26 +01:00
|
|
|
|
if (store.Role != StoreRoles.Owner)
|
2018-03-23 08:24:57 +01:00
|
|
|
|
{
|
|
|
|
|
StatusMessage = "Error: You can't approve a pairing without being owner of the store";
|
|
|
|
|
return RedirectToAction(nameof(UserStoresController.ListStores), "UserStores");
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
var pairingResult = await _TokenRepository.PairWithStoreAsync(pairingCode, store.Id);
|
|
|
|
|
if (pairingResult == PairingResult.Complete || pairingResult == PairingResult.Partial)
|
|
|
|
|
{
|
2018-03-01 15:09:41 +01:00
|
|
|
|
StatusMessage = "Pairing is successful";
|
2017-10-27 10:53:04 +02:00
|
|
|
|
if (pairingResult == PairingResult.Partial)
|
|
|
|
|
StatusMessage = "Server initiated pairing code: " + pairingCode;
|
|
|
|
|
return RedirectToAction(nameof(ListTokens), new
|
|
|
|
|
{
|
|
|
|
|
storeId = store.Id
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
StatusMessage = $"Pairing failed ({pairingResult})";
|
|
|
|
|
return RedirectToAction(nameof(ListTokens), new
|
|
|
|
|
{
|
|
|
|
|
storeId = store.Id
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string GetUserId()
|
|
|
|
|
{
|
|
|
|
|
return _UserManager.GetUserId(User);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-13 16:50:36 +02:00
|
|
|
|
}
|