BTCPay-Server-integration-and-HAProxy-setup

This commit is contained in:
sysadmin 2025-09-01 09:37:38 +01:00
parent ee4a5c3578
commit b4cee007c4
3 changed files with 178 additions and 4 deletions

View File

@ -0,0 +1,168 @@
using Microsoft.AspNetCore.Mvc;
using BTCPayServer.Client;
using BTCPayServer.Client.Models;
using Microsoft.AspNetCore.Authorization;
using Newtonsoft.Json.Linq;
namespace LittleShop.Controllers;
[ApiController]
[Route("api/btcpay-test")]
[Authorize(AuthenticationSchemes = "Cookies", Roles = "Admin")]
public class BTCPayTestController : ControllerBase
{
private readonly IConfiguration _configuration;
public BTCPayTestController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpGet("connection")]
public async Task<IActionResult> TestConnection()
{
try
{
var baseUrl = _configuration["BTCPayServer:BaseUrl"];
var apiKey = _configuration["BTCPayServer:ApiKey"];
if (string.IsNullOrEmpty(baseUrl) || string.IsNullOrEmpty(apiKey))
{
return BadRequest(new { error = "BTCPay Server configuration missing" });
}
// Create HttpClient with certificate bypass for internal networks
var httpClient = new HttpClient(new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
});
var client = new BTCPayServerClient(new Uri(baseUrl), apiKey, httpClient);
// Test basic connection by getting server info
var serverInfo = await client.GetServerInfo();
return Ok(new
{
status = "Connected",
baseUrl = baseUrl,
serverVersion = serverInfo?.Version,
supportedPaymentMethods = serverInfo?.SupportedPaymentMethods,
message = "BTCPay Server connection successful"
});
}
catch (Exception ex)
{
return StatusCode(500, new
{
error = ex.Message,
type = ex.GetType().Name,
baseUrl = _configuration["BTCPayServer:BaseUrl"]
});
}
}
[HttpGet("stores")]
public async Task<IActionResult> GetStores()
{
try
{
var baseUrl = _configuration["BTCPayServer:BaseUrl"];
var apiKey = _configuration["BTCPayServer:ApiKey"];
// Create HttpClient with certificate bypass for internal networks
var httpClient = new HttpClient(new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
});
var client = new BTCPayServerClient(new Uri(baseUrl), apiKey, httpClient);
// Get available stores
var stores = await client.GetStores();
return Ok(new
{
stores = stores.Select(s => new
{
id = s.Id,
name = s.Name,
website = s.Website,
defaultCurrency = s.DefaultCurrency
}).ToList(),
message = "Stores retrieved successfully"
});
}
catch (Exception ex)
{
return StatusCode(500, new
{
error = ex.Message,
type = ex.GetType().Name
});
}
}
[HttpPost("test-invoice")]
public async Task<IActionResult> CreateTestInvoice([FromBody] TestInvoiceRequest request)
{
try
{
var baseUrl = _configuration["BTCPayServer:BaseUrl"];
var apiKey = _configuration["BTCPayServer:ApiKey"];
var storeId = _configuration["BTCPayServer:StoreId"];
if (string.IsNullOrEmpty(storeId))
{
return BadRequest(new { error = "Store ID not configured" });
}
// Create HttpClient with certificate bypass for internal networks
var httpClient = new HttpClient(new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
});
var client = new BTCPayServerClient(new Uri(baseUrl), apiKey, httpClient);
// Create test invoice
var invoiceRequest = new CreateInvoiceRequest
{
Amount = request.Amount,
Currency = request.Currency ?? "GBP",
Metadata = JObject.FromObject(new
{
orderId = $"test-{Guid.NewGuid()}",
source = "LittleShop-Test"
})
};
var invoice = await client.CreateInvoice(storeId, invoiceRequest);
return Ok(new
{
status = "Invoice Created",
invoiceId = invoice.Id,
amount = invoice.Amount,
currency = invoice.Currency,
checkoutLink = invoice.CheckoutLink,
expiresAt = invoice.ExpirationTime,
message = "Test invoice created successfully"
});
}
catch (Exception ex)
{
return StatusCode(500, new
{
error = ex.Message,
type = ex.GetType().Name
});
}
}
}
public class TestInvoiceRequest
{
public decimal Amount { get; set; } = 0.01m;
public string? Currency { get; set; } = "GBP";
}

View File

@ -28,7 +28,13 @@ public class BTCPayServerService : IBTCPayServerService
_storeId = _configuration["BTCPayServer:StoreId"] ?? throw new ArgumentException("BTCPayServer:StoreId not configured"); _storeId = _configuration["BTCPayServer:StoreId"] ?? throw new ArgumentException("BTCPayServer:StoreId not configured");
_webhookSecret = _configuration["BTCPayServer:WebhookSecret"] ?? throw new ArgumentException("BTCPayServer:WebhookSecret not configured"); _webhookSecret = _configuration["BTCPayServer:WebhookSecret"] ?? throw new ArgumentException("BTCPayServer:WebhookSecret not configured");
_client = new BTCPayServerClient(new Uri(baseUrl), apiKey); // Create HttpClient with certificate bypass for internal networks
var httpClient = new HttpClient(new HttpClientHandler()
{
ServerCertificateCustomValidationCallback = (message, cert, chain, errors) => true
});
_client = new BTCPayServerClient(new Uri(baseUrl), apiKey, httpClient);
} }
public async Task<string> CreateInvoiceAsync(decimal amount, CryptoCurrency currency, string orderId, string? description = null) public async Task<string> CreateInvoiceAsync(decimal amount, CryptoCurrency currency, string orderId, string? description = null)

View File

@ -10,9 +10,9 @@
}, },
"BTCPayServer": { "BTCPayServer": {
"BaseUrl": "https://pay.silverlabs.uk", "BaseUrl": "https://pay.silverlabs.uk",
"ApiKey": "your-api-key", "ApiKey": "885a65ead85b87d5a10095b6cb6ad87866988cc2",
"StoreId": "your-store-id", "StoreId": "51kbAYszqX2gEK2E9EYwqbixcDmsafuBXukx7v1PrZUD",
"WebhookSecret": "your-webhook-secret" "WebhookSecret": ""
}, },
"RoyalMail": { "RoyalMail": {
"ApiKey": "your-royal-mail-api-key", "ApiKey": "your-royal-mail-api-key",