mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 14:34:23 +01:00
Improve invoice logs, make sure logs are saved as fire and forget
This commit is contained in:
@@ -200,8 +200,21 @@ namespace BTCPayServer.Controllers
|
|||||||
entity.InternalTags.Add(AppService.GetAppInternalTag(app.Id));
|
entity.InternalTags.Add(AppService.GetAppInternalTag(app.Id));
|
||||||
}
|
}
|
||||||
|
|
||||||
entity = await _InvoiceRepository.CreateInvoiceAsync(store.Id, entity, logs, _NetworkProvider);
|
logs.Write($"Saving invoice...");
|
||||||
|
entity = await _InvoiceRepository.CreateInvoiceAsync(store.Id, entity, _NetworkProvider);
|
||||||
|
logs.Write($"Invoice saved!");
|
||||||
|
_ = Task.Run(async () =>
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
await fetchingAll;
|
await fetchingAll;
|
||||||
|
}
|
||||||
|
catch (AggregateException ex)
|
||||||
|
{
|
||||||
|
ex.Handle(e => { logs.Write($"Error while fetching rates {ex}"); return true; });
|
||||||
|
}
|
||||||
|
await _InvoiceRepository.AddInvoiceLogs(entity.Id, logs);
|
||||||
|
});
|
||||||
_EventAggregator.Publish(new Events.InvoiceEvent(entity, 1001, InvoiceEvent.Created));
|
_EventAggregator.Publish(new Events.InvoiceEvent(entity, 1001, InvoiceEvent.Created));
|
||||||
var resp = entity.EntityToDTO(_NetworkProvider);
|
var resp = entity.EntityToDTO(_NetworkProvider);
|
||||||
return new DataWrapper<InvoiceResponse>(resp) { Facade = "pos/invoice" };
|
return new DataWrapper<InvoiceResponse>(resp) { Facade = "pos/invoice" };
|
||||||
@@ -230,6 +243,7 @@ namespace BTCPayServer.Controllers
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
var logPrefix = $"{supportedPaymentMethod.PaymentId.ToString(true)}:";
|
||||||
var storeBlob = store.GetStoreBlob();
|
var storeBlob = store.GetStoreBlob();
|
||||||
var preparePayment = handler.PreparePayment(supportedPaymentMethod, store, network);
|
var preparePayment = handler.PreparePayment(supportedPaymentMethod, store, network);
|
||||||
var rate = await fetchingByCurrencyPair[new CurrencyPair(network.CryptoCode, entity.ProductInformation.Currency)];
|
var rate = await fetchingByCurrencyPair[new CurrencyPair(network.CryptoCode, entity.ProductInformation.Currency)];
|
||||||
@@ -243,7 +257,10 @@ namespace BTCPayServer.Controllers
|
|||||||
paymentMethod.SetId(supportedPaymentMethod.PaymentId);
|
paymentMethod.SetId(supportedPaymentMethod.PaymentId);
|
||||||
paymentMethod.Rate = rate.BidAsk.Bid;
|
paymentMethod.Rate = rate.BidAsk.Bid;
|
||||||
paymentMethod.PreferOnion = this.Request.IsOnion();
|
paymentMethod.PreferOnion = this.Request.IsOnion();
|
||||||
|
|
||||||
|
logs.Write($"{logPrefix} Creating payment method details...");
|
||||||
var paymentDetails = await handler.CreatePaymentMethodDetails(supportedPaymentMethod, paymentMethod, store, network, preparePayment);
|
var paymentDetails = await handler.CreatePaymentMethodDetails(supportedPaymentMethod, paymentMethod, store, network, preparePayment);
|
||||||
|
logs.Write($"{logPrefix} Payment method details created...");
|
||||||
paymentMethod.SetPaymentMethodDetails(paymentDetails);
|
paymentMethod.SetPaymentMethodDetails(paymentDetails);
|
||||||
|
|
||||||
Func<Money, Money, bool> compare = null;
|
Func<Money, Money, bool> compare = null;
|
||||||
@@ -272,7 +289,7 @@ namespace BTCPayServer.Controllers
|
|||||||
var limitValueCrypto = Money.Coins(limitValue.Value / limitValueRate.BidAsk.Bid);
|
var limitValueCrypto = Money.Coins(limitValue.Value / limitValueRate.BidAsk.Bid);
|
||||||
if (compare(paymentMethod.Calculate().Due, limitValueCrypto))
|
if (compare(paymentMethod.Calculate().Due, limitValueCrypto))
|
||||||
{
|
{
|
||||||
logs.Write($"{supportedPaymentMethod.PaymentId.CryptoCode}: {errorMessage}");
|
logs.Write($"{logPrefix} {errorMessage}");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,11 +61,36 @@ namespace BTCPayServer.Payments
|
|||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return ToString(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public string ToString(bool pretty)
|
||||||
|
{
|
||||||
|
if (pretty)
|
||||||
|
{
|
||||||
|
return $"{CryptoCode} ({PrettyMethod(PaymentType)})";
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
if (PaymentType == PaymentTypes.BTCLike)
|
if (PaymentType == PaymentTypes.BTCLike)
|
||||||
return CryptoCode;
|
return CryptoCode;
|
||||||
return CryptoCode + "_" + PaymentType.ToString();
|
return CryptoCode + "_" + PaymentType.ToString();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string PrettyMethod(PaymentTypes paymentType)
|
||||||
|
{
|
||||||
|
switch (paymentType)
|
||||||
|
{
|
||||||
|
case PaymentTypes.BTCLike:
|
||||||
|
return "On-Chain";
|
||||||
|
case PaymentTypes.LightningLike:
|
||||||
|
return "Off-Chain";
|
||||||
|
default:
|
||||||
|
return paymentType.ToString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static bool TryParse(string str, out PaymentMethodId paymentMethodId)
|
public static bool TryParse(string str, out PaymentMethodId paymentMethodId)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ retry:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<InvoiceEntity> CreateInvoiceAsync(string storeId, InvoiceEntity invoice, InvoiceLogs creationLogs, BTCPayNetworkProvider networkProvider)
|
public async Task<InvoiceEntity> CreateInvoiceAsync(string storeId, InvoiceEntity invoice, BTCPayNetworkProvider networkProvider)
|
||||||
{
|
{
|
||||||
List<string> textSearch = new List<string>();
|
List<string> textSearch = new List<string>();
|
||||||
invoice = Clone(invoice, null);
|
invoice = Clone(invoice, null);
|
||||||
@@ -165,17 +165,6 @@ retry:
|
|||||||
textSearch.Add(paymentMethod.Calculate().TotalDue.ToString());
|
textSearch.Add(paymentMethod.Calculate().TotalDue.ToString());
|
||||||
}
|
}
|
||||||
context.PendingInvoices.Add(new PendingInvoiceData() { Id = invoice.Id });
|
context.PendingInvoices.Add(new PendingInvoiceData() { Id = invoice.Id });
|
||||||
|
|
||||||
foreach (var log in creationLogs.ToList())
|
|
||||||
{
|
|
||||||
context.InvoiceEvents.Add(new InvoiceEventData()
|
|
||||||
{
|
|
||||||
InvoiceDataId = invoice.Id,
|
|
||||||
Message = log.Log,
|
|
||||||
Timestamp = log.Timestamp,
|
|
||||||
UniqueId = Encoders.Hex.EncodeData(RandomUtils.GetBytes(10))
|
|
||||||
});
|
|
||||||
}
|
|
||||||
await context.SaveChangesAsync().ConfigureAwait(false);
|
await context.SaveChangesAsync().ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -191,6 +180,24 @@ retry:
|
|||||||
return invoice;
|
return invoice;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task AddInvoiceLogs(string invoiceId, InvoiceLogs logs)
|
||||||
|
{
|
||||||
|
using (var context = _ContextFactory.CreateContext())
|
||||||
|
{
|
||||||
|
foreach (var log in logs.ToList())
|
||||||
|
{
|
||||||
|
context.InvoiceEvents.Add(new InvoiceEventData()
|
||||||
|
{
|
||||||
|
InvoiceDataId = invoiceId,
|
||||||
|
Message = log.Log,
|
||||||
|
Timestamp = log.Timestamp,
|
||||||
|
UniqueId = Encoders.Hex.EncodeData(RandomUtils.GetBytes(10))
|
||||||
|
});
|
||||||
|
}
|
||||||
|
await context.SaveChangesAsync().ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private static string GetDestination(PaymentMethod paymentMethod, Network network)
|
private static string GetDestination(PaymentMethod paymentMethod, Network network)
|
||||||
{
|
{
|
||||||
// For legacy reason, BitcoinLikeOnChain is putting the hashes of addresses in database
|
// For legacy reason, BitcoinLikeOnChain is putting the hashes of addresses in database
|
||||||
|
|||||||
Reference in New Issue
Block a user