2023-02-21 15:06:34 +09:00
|
|
|
using System.Reflection.Metadata;
|
2019-08-30 00:24:42 +09:00
|
|
|
using BTCPayServer.Services.Invoices;
|
2023-02-21 15:06:34 +09:00
|
|
|
using Newtonsoft.Json;
|
2019-08-30 00:24:42 +09:00
|
|
|
|
|
|
|
namespace BTCPayServer.Data
|
|
|
|
{
|
|
|
|
public static class InvoiceDataExtensions
|
|
|
|
{
|
2023-02-21 15:06:34 +09:00
|
|
|
public static void SetBlob(this InvoiceData invoiceData, InvoiceEntity blob)
|
|
|
|
{
|
|
|
|
if (blob.Metadata is null)
|
|
|
|
blob.Metadata = new InvoiceMetadata();
|
|
|
|
invoiceData.HasTypedBlob<InvoiceEntity>().SetBlob(blob);
|
|
|
|
}
|
2022-04-24 05:19:34 +02:00
|
|
|
public static InvoiceEntity GetBlob(this InvoiceData invoiceData, BTCPayNetworkProvider networks)
|
2020-06-28 17:15:42 +09:00
|
|
|
{
|
2023-02-21 15:06:34 +09:00
|
|
|
#pragma warning disable CS0618 // Type or member is obsolete
|
|
|
|
if (invoiceData.Blob is not null && invoiceData.Blob.Length != 0)
|
2020-08-25 14:33:00 +09:00
|
|
|
{
|
2023-02-21 15:06:34 +09:00
|
|
|
var entity = JsonConvert.DeserializeObject<InvoiceEntity>(ZipUtils.Unzip(invoiceData.Blob), InvoiceRepository.DefaultSerializerSettings);
|
|
|
|
entity.Networks = networks;
|
|
|
|
if (entity.Metadata is null)
|
2020-08-25 14:33:00 +09:00
|
|
|
{
|
2023-02-21 15:06:34 +09:00
|
|
|
if (entity.Version < InvoiceEntity.GreenfieldInvoices_Version)
|
|
|
|
{
|
|
|
|
entity.MigrateLegacyInvoice();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
entity.Metadata = new InvoiceMetadata();
|
|
|
|
}
|
2020-08-25 14:33:00 +09:00
|
|
|
}
|
2023-02-21 15:06:34 +09:00
|
|
|
return entity;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-04-10 11:07:03 +09:00
|
|
|
var entity = invoiceData.HasTypedBlob<InvoiceEntity>().GetBlob();
|
2023-02-21 15:06:34 +09:00
|
|
|
entity.Networks = networks;
|
|
|
|
return entity;
|
2020-08-25 14:33:00 +09:00
|
|
|
}
|
2023-02-21 15:06:34 +09:00
|
|
|
#pragma warning restore CS0618 // Type or member is obsolete
|
2020-06-28 17:15:42 +09:00
|
|
|
}
|
2019-08-30 00:24:42 +09:00
|
|
|
public static InvoiceState GetInvoiceState(this InvoiceData invoiceData)
|
|
|
|
{
|
|
|
|
return new InvoiceState(invoiceData.Status, invoiceData.ExceptionStatus);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|