Make app invoices show link to app in receipt (#3995)

This commit is contained in:
Andrew Camilleri
2022-07-22 13:29:34 +02:00
committed by GitHub
parent 1322fd97f6
commit 88f8f3938c
2 changed files with 40 additions and 33 deletions

View File

@@ -19,7 +19,6 @@ using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using NBitpayClient; using NBitpayClient;
using NicolasDorier.RateLimits; using NicolasDorier.RateLimits;
using static BTCPayServer.Controllers.UIAppsController;
namespace BTCPayServer.Controllers namespace BTCPayServer.Controllers
{ {
@@ -226,9 +225,9 @@ namespace BTCPayServer.Controllers
OrderId = orderId ?? AppService.GetAppOrderId(app), OrderId = orderId ?? AppService.GetAppOrderId(app),
NotificationURL = NotificationURL =
string.IsNullOrEmpty(notificationUrl) ? settings.NotificationUrl : notificationUrl, string.IsNullOrEmpty(notificationUrl) ? settings.NotificationUrl : notificationUrl,
RedirectURL = !string.IsNullOrEmpty(redirectUrl) ? redirectUrl RedirectURL = !string.IsNullOrEmpty(redirectUrl) ? redirectUrl
: !string.IsNullOrEmpty(settings.RedirectUrl) ? settings.RedirectUrl : !string.IsNullOrEmpty(settings.RedirectUrl) ? settings.RedirectUrl
: Request.GetDisplayUrl(), : Request.GetDisplayUrl(),
FullNotifications = true, FullNotifications = true,
ExtendedNotifications = true, ExtendedNotifications = true,
PosData = string.IsNullOrEmpty(posData) ? null : posData, PosData = string.IsNullOrEmpty(posData) ? null : posData,
@@ -239,7 +238,10 @@ namespace BTCPayServer.Controllers
: requiresRefundEmail == RequiresRefundEmail.On, : requiresRefundEmail == RequiresRefundEmail.On,
}, store, HttpContext.Request.GetAbsoluteRoot(), }, store, HttpContext.Request.GetAbsoluteRoot(),
new List<string>() { AppService.GetAppInternalTag(appId) }, new List<string>() { AppService.GetAppInternalTag(appId) },
cancellationToken); cancellationToken, (entity) =>
{
entity.Metadata.OrderUrl = Request.GetDisplayUrl();
} );
return RedirectToAction(nameof(UIInvoiceController.Checkout), "UIInvoice", new { invoiceId = invoice.Data.Id }); return RedirectToAction(nameof(UIInvoiceController.Checkout), "UIInvoice", new { invoiceId = invoice.Data.Id });
} }
catch (BitpayHttpException e) catch (BitpayHttpException e)
@@ -377,31 +379,32 @@ namespace BTCPayServer.Controllers
try try
{ {
var invoice = await _InvoiceController.CreateInvoiceCore(new BitpayCreateInvoiceRequest() var invoice = await _InvoiceController.CreateInvoiceCore(new BitpayCreateInvoiceRequest()
{ {
OrderId = AppService.GetAppOrderId(app), OrderId = AppService.GetAppOrderId(app),
Currency = settings.TargetCurrency, Currency = settings.TargetCurrency,
ItemCode = request.ChoiceKey ?? string.Empty, ItemCode = request.ChoiceKey ?? string.Empty,
ItemDesc = title, ItemDesc = title,
BuyerEmail = request.Email, BuyerEmail = request.Email,
Price = price, Price = price,
NotificationURL = settings.NotificationUrl, NotificationURL = settings.NotificationUrl,
FullNotifications = true, FullNotifications = true,
ExtendedNotifications = true, ExtendedNotifications = true,
SupportedTransactionCurrencies = paymentMethods, SupportedTransactionCurrencies = paymentMethods,
RedirectURL = request.RedirectUrl ?? RedirectURL = request.RedirectUrl ?? Request.GetDisplayUrl(),
HttpContext.Request.GetAbsoluteUri($"/apps/{appId}/crowdfund") }, store, HttpContext.Request.GetAbsoluteRoot(),
}, store, HttpContext.Request.GetAbsoluteRoot(), new List<string>() {AppService.GetAppInternalTag(appId)},
new List<string> { AppService.GetAppInternalTag(appId) }, cancellationToken, (entity) =>
cancellationToken: cancellationToken); {
entity.Metadata.OrderUrl = Request.GetDisplayUrl();
});
if (request.RedirectToCheckout) if (request.RedirectToCheckout)
{ {
return RedirectToAction(nameof(UIInvoiceController.Checkout), "UIInvoice", return RedirectToAction(nameof(UIInvoiceController.Checkout), "UIInvoice",
new { invoiceId = invoice.Data.Id }); new {invoiceId = invoice.Data.Id});
}
else
{
return Ok(invoice.Data.Id);
} }
return Ok(invoice.Data.Id);
} }
catch (BitpayHttpException e) catch (BitpayHttpException e)
{ {

View File

@@ -87,14 +87,14 @@ namespace BTCPayServer.Controllers
internal async Task<DataWrapper<InvoiceResponse>> CreateInvoiceCore(BitpayCreateInvoiceRequest invoice, internal async Task<DataWrapper<InvoiceResponse>> CreateInvoiceCore(BitpayCreateInvoiceRequest invoice,
StoreData store, string serverUrl, List<string>? additionalTags = null, StoreData store, string serverUrl, List<string>? additionalTags = null,
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default, Action<InvoiceEntity>? entityManipulator = null)
{ {
var entity = await CreateInvoiceCoreRaw(invoice, store, serverUrl, additionalTags, cancellationToken); var entity = await CreateInvoiceCoreRaw(invoice, store, serverUrl, additionalTags, cancellationToken, entityManipulator);
var resp = entity.EntityToDTO(); var resp = entity.EntityToDTO();
return new DataWrapper<InvoiceResponse>(resp) { Facade = "pos/invoice" }; return new DataWrapper<InvoiceResponse>(resp) { Facade = "pos/invoice" };
} }
internal async Task<InvoiceEntity> CreateInvoiceCoreRaw(BitpayCreateInvoiceRequest invoice, StoreData store, string serverUrl, List<string>? additionalTags = null, CancellationToken cancellationToken = default) internal async Task<InvoiceEntity> CreateInvoiceCoreRaw(BitpayCreateInvoiceRequest invoice, StoreData store, string serverUrl, List<string>? additionalTags = null, CancellationToken cancellationToken = default, Action<InvoiceEntity>? entityManipulator = null)
{ {
var storeBlob = store.GetStoreBlob(); var storeBlob = store.GetStoreBlob();
var entity = _InvoiceRepository.CreateNewInvoice(); var entity = _InvoiceRepository.CreateNewInvoice();
@@ -164,10 +164,10 @@ namespace BTCPayServer.Controllers
entity.DefaultPaymentMethod = invoice.DefaultPaymentMethod; entity.DefaultPaymentMethod = invoice.DefaultPaymentMethod;
entity.RequiresRefundEmail = invoice.RequiresRefundEmail; entity.RequiresRefundEmail = invoice.RequiresRefundEmail;
return await CreateInvoiceCoreRaw(entity, store, excludeFilter, null, cancellationToken); return await CreateInvoiceCoreRaw(entity, store, excludeFilter, null, cancellationToken, entityManipulator);
} }
internal async Task<InvoiceEntity> CreateInvoiceCoreRaw(CreateInvoiceRequest invoice, StoreData store, string serverUrl, List<string>? additionalTags = null, CancellationToken cancellationToken = default) internal async Task<InvoiceEntity> CreateInvoiceCoreRaw(CreateInvoiceRequest invoice, StoreData store, string serverUrl, List<string>? additionalTags = null, CancellationToken cancellationToken = default, Action<InvoiceEntity>? entityManipulator = null)
{ {
var storeBlob = store.GetStoreBlob(); var storeBlob = store.GetStoreBlob();
var entity = _InvoiceRepository.CreateNewInvoice(); var entity = _InvoiceRepository.CreateNewInvoice();
@@ -207,10 +207,10 @@ namespace BTCPayServer.Controllers
entity.RequiresRefundEmail = invoice.Checkout.RequiresRefundEmail; entity.RequiresRefundEmail = invoice.Checkout.RequiresRefundEmail;
if (additionalTags != null) if (additionalTags != null)
entity.InternalTags.AddRange(additionalTags); entity.InternalTags.AddRange(additionalTags);
return await CreateInvoiceCoreRaw(entity, store, excludeFilter, invoice.AdditionalSearchTerms, cancellationToken); return await CreateInvoiceCoreRaw(entity, store, excludeFilter, invoice.AdditionalSearchTerms, cancellationToken, entityManipulator);
} }
internal async Task<InvoiceEntity> CreateInvoiceCoreRaw(InvoiceEntity entity, StoreData store, IPaymentFilter? invoicePaymentMethodFilter, string[]? additionalSearchTerms = null, CancellationToken cancellationToken = default) internal async Task<InvoiceEntity> CreateInvoiceCoreRaw(InvoiceEntity entity, StoreData store, IPaymentFilter? invoicePaymentMethodFilter, string[]? additionalSearchTerms = null, CancellationToken cancellationToken = default, Action<InvoiceEntity>? entityManipulator = null)
{ {
InvoiceLogs logs = new InvoiceLogs(); InvoiceLogs logs = new InvoiceLogs();
logs.Write("Creation of invoice starting", InvoiceEventData.EventSeverity.Info); logs.Write("Creation of invoice starting", InvoiceEventData.EventSeverity.Info);
@@ -327,6 +327,10 @@ namespace BTCPayServer.Controllers
entity.InternalTags.Add(AppService.GetAppInternalTag(app.Id)); entity.InternalTags.Add(AppService.GetAppInternalTag(app.Id));
} }
if (entityManipulator != null)
{
entityManipulator.Invoke(entity);
}
using (logs.Measure("Saving invoice")) using (logs.Measure("Saving invoice"))
{ {
entity = await _InvoiceRepository.CreateInvoiceAsync(store.Id, entity, additionalSearchTerms); entity = await _InvoiceRepository.CreateInvoiceAsync(store.Id, entity, additionalSearchTerms);