2021-04-19 02:53:57 +02:00
#nullable enable
2020-09-19 16:53:45 +02:00
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 ;
2021-07-12 14:58:11 +02:00
using BTCPayServer.Client.Models ;
2020-09-18 17:20:31 +02:00
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
{
2021-04-08 06:37:05 +02:00
[HttpGet("{storeId}/integrations")]
2020-09-18 17:20:31 +02:00
public IActionResult Integrations ( )
2021-04-08 06:37:05 +02:00
{
2021-04-19 02:53:57 +02:00
return View ( "Integrations" , new IntegrationsViewModel ( ) ) ;
2020-09-18 17:20:31 +02:00
}
2021-07-12 14:58:11 +02:00
private async Task < Data . WebhookDeliveryData ? > LastDeliveryForWebhook ( string webhookId )
{
return ( await _Repo . GetWebhookDeliveries ( CurrentStore . Id , webhookId , 1 ) ) . ToList ( ) . FirstOrDefault ( ) ;
}
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 ( )
{
2021-07-12 14:58:11 +02:00
Webhooks = webhooks . Select ( async w = > {
var lastDelivery = await LastDeliveryForWebhook ( w . Id ) ;
var lastDeliveryBlob = lastDelivery ? . GetBlob ( ) ;
return new WebhooksViewModel . WebhookViewModel ( )
{
Id = w . Id ,
Url = w . GetBlob ( ) . Url ,
LastDeliveryErrorMessage = lastDeliveryBlob ? . ErrorMessage ,
LastDeliveryTimeStamp = lastDelivery ? . Timestamp ,
LastDeliverySuccessful = lastDeliveryBlob = = null ? true : lastDeliveryBlob . Status = = WebhookDeliveryStatus . HttpSuccess ,
} ;
}
) . Select ( t = > t . Result ) . ToArray ( )
2020-11-06 12:42:26 +01:00
} ) ;
}
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-04-19 02:53:57 +02:00
[HttpGet("{storeId}/webhooks/{webhookId}/test")]
public async Task < IActionResult > TestWebhook ( string webhookId )
{
var webhook = await _Repo . GetWebhook ( CurrentStore . Id , webhookId ) ;
if ( webhook is null )
return NotFound ( ) ;
return View ( nameof ( TestWebhook ) ) ;
}
[HttpPost("{storeId}/webhooks/{webhookId}/test")]
2021-04-19 07:52:08 +02:00
public async Task < IActionResult > TestWebhook ( string webhookId , TestWebhookViewModel viewModel )
2021-04-19 02:53:57 +02:00
{
2021-04-26 05:09:26 +02:00
var result = await WebhookNotificationManager . TestWebhook ( CurrentStore . Id , webhookId , viewModel . Type ) ;
if ( result . Success ) {
2021-04-26 06:19:17 +02:00
TempData [ WellKnownTempData . SuccessMessage ] = $"{viewModel.Type.ToString()} event delivered successfully! Delivery ID is {result.DeliveryId}" ;
2021-04-26 05:09:26 +02:00
} else {
2021-04-26 05:30:10 +02:00
TempData [ WellKnownTempData . ErrorMessage ] = $"{viewModel.Type.ToString()} event could not be delivered. Error message received: {(result.ErrorMessage ?? " unknown ")}" ;
2021-04-26 05:09:26 +02:00
}
2021-04-19 07:52:08 +02:00
return View ( nameof ( TestWebhook ) ) ;
2021-04-19 02:53:57 +02:00
}
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
}
}