btcpayserver/BTCPayServer/Controllers/StoresController.Integrations.cs

143 lines
5.4 KiB
C#
Raw Normal View History

using System.Linq;
2020-09-18 17:20:31 +02:00
using System.Threading.Tasks;
using BTCPayServer.Data;
2020-11-06 12:42:26 +01:00
using BTCPayServer.Models;
2020-09-18 17:20:31 +02:00
using BTCPayServer.Models.StoreViewModels;
using Microsoft.AspNetCore.Mvc;
2020-11-06 12:42:26 +01:00
using NBitcoin;
using NBitcoin.DataEncoders;
2020-09-18 17:20:31 +02:00
namespace BTCPayServer.Controllers
{
public partial class StoresController
{
[HttpGet("{storeId}/integrations")]
2020-09-18 17:20:31 +02:00
public IActionResult Integrations()
{
return View("Integrations",new IntegrationsViewModel());
2020-09-18 17:20:31 +02:00
}
2021-02-23 16:51:58 +01:00
[HttpGet("{storeId}/webhooks")]
2020-11-06 12:42:26 +01:00
public async Task<IActionResult> Webhooks()
{
2021-02-23 16:51:58 +01:00
var webhooks = await _Repo.GetWebhooks(CurrentStore.Id);
2020-11-06 12:42:26 +01:00
return View(nameof(Webhooks), new WebhooksViewModel()
{
Webhooks = webhooks.Select(w => new WebhooksViewModel.WebhookViewModel()
{
Id = w.Id,
Url = w.GetBlob().Url
}).ToArray()
});
}
2021-02-23 16:51:58 +01:00
[HttpGet("{storeId}/webhooks/new")]
2020-11-06 12:42:26 +01:00
public IActionResult NewWebhook()
{
2021-02-23 16:51:58 +01:00
return View(nameof(ModifyWebhook), new EditWebhookViewModel
2020-11-06 12:42:26 +01:00
{
Active = true,
Everything = true,
IsNew = true,
Secret = Encoders.Base58.EncodeData(RandomUtils.GetBytes(20))
});
}
2021-02-23 16:51:58 +01:00
[HttpGet("{storeId}/webhooks/{webhookId}/remove")]
2020-11-06 12:42:26 +01:00
public async Task<IActionResult> DeleteWebhook(string webhookId)
{
var webhook = await _Repo.GetWebhook(CurrentStore.Id, webhookId);
if (webhook is null)
return NotFound();
2021-02-23 16:51:58 +01:00
return View("Confirm", new ConfirmModel
2020-11-06 12:42:26 +01:00
{
Title = $"Delete a webhook",
Description = "This webhook will be removed from this store, do you wish to continue?",
Action = "Delete"
});
}
2021-02-23 16:51:58 +01:00
[HttpPost("{storeId}/webhooks/{webhookId}/remove")]
2020-11-06 12:42:26 +01:00
public async Task<IActionResult> DeleteWebhookPost(string webhookId)
{
var webhook = await _Repo.GetWebhook(CurrentStore.Id, webhookId);
if (webhook is null)
return NotFound();
2021-02-23 16:51:58 +01:00
2020-11-06 12:42:26 +01:00
await _Repo.DeleteWebhook(CurrentStore.Id, webhookId);
TempData[WellKnownTempData.SuccessMessage] = "Webhook successfully deleted";
return RedirectToAction(nameof(Webhooks), new { storeId = CurrentStore.Id });
}
2021-02-23 16:51:58 +01:00
[HttpPost("{storeId}/webhooks/new")]
2020-11-06 12:42:26 +01:00
public async Task<IActionResult> NewWebhook(string storeId, EditWebhookViewModel viewModel)
{
if (!ModelState.IsValid)
2021-02-23 16:51:11 +01:00
return View(nameof(ModifyWebhook), viewModel);
2020-11-06 12:42:26 +01:00
2021-02-23 16:51:58 +01:00
await _Repo.CreateWebhook(CurrentStore.Id, viewModel.CreateBlob());
2020-11-06 12:42:26 +01:00
TempData[WellKnownTempData.SuccessMessage] = "The webhook has been created";
return RedirectToAction(nameof(Webhooks), new { storeId });
}
2021-02-23 16:51:58 +01:00
[HttpGet("{storeId}/webhooks/{webhookId}")]
2020-11-06 12:42:26 +01:00
public async Task<IActionResult> ModifyWebhook(string webhookId)
{
var webhook = await _Repo.GetWebhook(CurrentStore.Id, webhookId);
if (webhook is null)
return NotFound();
2021-02-23 16:51:58 +01:00
2020-11-06 12:42:26 +01:00
var blob = webhook.GetBlob();
var deliveries = await _Repo.GetWebhookDeliveries(CurrentStore.Id, webhookId, 20);
return View(nameof(ModifyWebhook), new EditWebhookViewModel(blob)
{
Deliveries = deliveries
.Select(s => new DeliveryViewModel(s)).ToList()
});
}
2021-02-23 16:51:58 +01:00
[HttpPost("{storeId}/webhooks/{webhookId}")]
2020-11-06 12:42:26 +01:00
public async Task<IActionResult> ModifyWebhook(string webhookId, EditWebhookViewModel viewModel)
{
var webhook = await _Repo.GetWebhook(CurrentStore.Id, webhookId);
if (webhook is null)
return NotFound();
await _Repo.UpdateWebhook(CurrentStore.Id, webhookId, viewModel.CreateBlob());
TempData[WellKnownTempData.SuccessMessage] = "The webhook has been updated";
return RedirectToAction(nameof(Webhooks), new { storeId = CurrentStore.Id });
}
2021-02-23 16:51:58 +01:00
[HttpPost("{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}/redeliver")]
2020-11-06 12:42:26 +01:00
public async Task<IActionResult> RedeliverWebhook(string webhookId, string deliveryId)
{
var delivery = await _Repo.GetWebhookDelivery(CurrentStore.Id, webhookId, deliveryId);
if (delivery is null)
return NotFound();
2021-02-23 16:51:58 +01:00
2020-11-06 12:42:26 +01:00
var newDeliveryId = await WebhookNotificationManager.Redeliver(deliveryId);
if (newDeliveryId is null)
return NotFound();
2021-02-23 16:51:58 +01:00
2020-11-06 12:42:26 +01:00
TempData[WellKnownTempData.SuccessMessage] = "Successfully planned a redelivery";
return RedirectToAction(nameof(ModifyWebhook),
new
{
storeId = CurrentStore.Id,
webhookId
});
}
2021-02-23 16:51:58 +01:00
[HttpGet("{storeId}/webhooks/{webhookId}/deliveries/{deliveryId}/request")]
2020-11-06 12:42:26 +01:00
public async Task<IActionResult> WebhookDelivery(string webhookId, string deliveryId)
{
var delivery = await _Repo.GetWebhookDelivery(CurrentStore.Id, webhookId, deliveryId);
if (delivery is null)
return NotFound();
2021-02-23 16:51:58 +01:00
return File(delivery.GetBlob().Request, "application/json");
2020-11-06 12:42:26 +01:00
}
2020-09-18 17:20:31 +02:00
}
}