2017-09-13 08:47:34 +02:00
|
|
|
|
using DBreeze;
|
2018-01-18 10:33:26 +01:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using NBitpayClient;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using NBitcoin;
|
|
|
|
|
using NBitcoin.DataEncoders;
|
|
|
|
|
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Query;
|
|
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure.Internal;
|
|
|
|
|
using BTCPayServer.Models;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using BTCPayServer.Data;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using BTCPayServer.Models.InvoicingModels;
|
2018-01-18 10:33:26 +01:00
|
|
|
|
using BTCPayServer.Logging;
|
2018-02-19 03:06:08 +01:00
|
|
|
|
using BTCPayServer.Payments;
|
2018-07-24 05:19:43 +02:00
|
|
|
|
using System.Data.Common;
|
2017-09-13 08:47:34 +02:00
|
|
|
|
|
2017-10-20 21:06:37 +02:00
|
|
|
|
namespace BTCPayServer.Services.Invoices
|
2017-09-13 08:47:34 +02:00
|
|
|
|
{
|
2017-11-12 15:03:33 +01:00
|
|
|
|
public class InvoiceRepository : IDisposable
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private readonly DBreezeEngine _Engine;
|
|
|
|
|
public DBreezeEngine Engine
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return _Engine;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private ApplicationDbContextFactory _ContextFactory;
|
2017-11-12 15:03:33 +01:00
|
|
|
|
private CustomThreadPool _IndexerThread;
|
2018-01-11 14:52:28 +01:00
|
|
|
|
public InvoiceRepository(ApplicationDbContextFactory contextFactory, string dbreezePath)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-10-26 12:06:06 +02:00
|
|
|
|
int retryCount = 0;
|
2018-12-18 18:56:51 +01:00
|
|
|
|
retry:
|
2018-10-26 12:06:06 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_Engine = new DBreezeEngine(dbreezePath);
|
|
|
|
|
}
|
|
|
|
|
catch when (retryCount++ < 5) { goto retry; }
|
2017-11-12 15:03:33 +01:00
|
|
|
|
_IndexerThread = new CustomThreadPool(1, "Invoice Indexer");
|
2017-10-27 10:53:04 +02:00
|
|
|
|
_ContextFactory = contextFactory;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<bool> RemovePendingInvoice(string invoiceId)
|
|
|
|
|
{
|
2018-01-18 10:33:26 +01:00
|
|
|
|
Logs.PayServer.LogInformation($"Remove pending invoice {invoiceId}");
|
2017-10-27 10:53:04 +02:00
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
ctx.PendingInvoices.Remove(new PendingInvoiceData() { Id = invoiceId });
|
|
|
|
|
try
|
|
|
|
|
{
|
2018-03-29 05:09:57 +02:00
|
|
|
|
await ctx.SaveChangesAsync();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
catch (DbUpdateException) { return false; }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-16 17:34:40 +01:00
|
|
|
|
public async Task<InvoiceEntity> GetInvoiceFromScriptPubKey(Script scriptPubKey, string cryptoCode)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
using (var db = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
2018-02-16 17:34:40 +01:00
|
|
|
|
var key = scriptPubKey.Hash.ToString() + "#" + cryptoCode;
|
|
|
|
|
var result = await db.AddressInvoices
|
|
|
|
|
#pragma warning disable CS0618
|
|
|
|
|
.Where(a => a.Address == key)
|
|
|
|
|
#pragma warning restore CS0618
|
|
|
|
|
.Select(a => a.InvoiceData)
|
|
|
|
|
.Include(a => a.Payments)
|
|
|
|
|
.Include(a => a.RefundAddresses)
|
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
|
if (result == null)
|
|
|
|
|
return null;
|
|
|
|
|
return ToEntity(result);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<string[]> GetPendingInvoices()
|
|
|
|
|
{
|
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
return await ctx.PendingInvoices.Select(p => p.Id).ToArrayAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-19 04:48:48 +01:00
|
|
|
|
public async Task<AppData[]> GetAppsTaggingStore(string storeId)
|
|
|
|
|
{
|
|
|
|
|
if (storeId == null)
|
|
|
|
|
throw new ArgumentNullException(nameof(storeId));
|
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
return await ctx.Apps.Where(a => a.StoreDataId == storeId && a.TagAllInvoices).ToArrayAsync();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public async Task UpdateInvoice(string invoiceId, UpdateCustomerModel data)
|
|
|
|
|
{
|
|
|
|
|
using (var ctx = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
var invoiceData = await ctx.Invoices.FindAsync(invoiceId).ConfigureAwait(false);
|
|
|
|
|
if (invoiceData == null)
|
|
|
|
|
return;
|
|
|
|
|
if (invoiceData.CustomerEmail == null && data.Email != null)
|
|
|
|
|
{
|
|
|
|
|
invoiceData.CustomerEmail = data.Email;
|
|
|
|
|
}
|
|
|
|
|
await ctx.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 05:19:43 +02:00
|
|
|
|
public async Task<InvoiceEntity> CreateInvoiceAsync(string storeId, InvoiceEntity invoice, InvoiceLogs creationLogs, BTCPayNetworkProvider networkProvider)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2017-12-21 07:52:04 +01:00
|
|
|
|
List<string> textSearch = new List<string>();
|
2018-01-11 14:52:28 +01:00
|
|
|
|
invoice = Clone(invoice, null);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
invoice.Id = Encoders.Base58.EncodeData(RandomUtils.GetBytes(16));
|
2018-01-10 10:30:45 +01:00
|
|
|
|
#pragma warning disable CS0618
|
2017-10-27 10:53:04 +02:00
|
|
|
|
invoice.Payments = new List<PaymentEntity>();
|
2018-01-10 10:30:45 +01:00
|
|
|
|
#pragma warning restore CS0618
|
2017-10-27 10:53:04 +02:00
|
|
|
|
invoice.StoreId = storeId;
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
2018-05-16 03:26:45 +02:00
|
|
|
|
context.Invoices.Add(new Data.InvoiceData()
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
StoreDataId = storeId,
|
|
|
|
|
Id = invoice.Id,
|
|
|
|
|
Created = invoice.InvoiceTime,
|
2018-01-11 14:52:28 +01:00
|
|
|
|
Blob = ToBytes(invoice, null),
|
2017-10-27 10:53:04 +02:00
|
|
|
|
OrderId = invoice.OrderId,
|
2018-12-10 13:48:28 +01:00
|
|
|
|
#pragma warning disable CS0618 // Type or member is obsolete
|
|
|
|
|
Status = invoice.StatusString,
|
|
|
|
|
#pragma warning restore CS0618 // Type or member is obsolete
|
2017-10-27 10:53:04 +02:00
|
|
|
|
ItemCode = invoice.ProductInformation.ItemCode,
|
|
|
|
|
CustomerEmail = invoice.RefundMail
|
2017-11-12 15:37:21 +01:00
|
|
|
|
});
|
2017-12-21 07:52:04 +01:00
|
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
|
foreach (var paymentMethod in invoice.GetPaymentMethods(networkProvider))
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-02-19 07:09:05 +01:00
|
|
|
|
if (paymentMethod.Network == null)
|
2017-12-21 07:52:04 +01:00
|
|
|
|
throw new InvalidOperationException("CryptoCode unsupported");
|
2018-02-19 07:09:05 +01:00
|
|
|
|
var paymentDestination = paymentMethod.GetPaymentMethodDetails().GetPaymentDestination();
|
2018-02-18 18:38:03 +01:00
|
|
|
|
|
2018-03-13 07:28:39 +01:00
|
|
|
|
string address = GetDestination(paymentMethod, paymentMethod.Network.NBitcoinNetwork);
|
2017-12-21 07:52:04 +01:00
|
|
|
|
context.AddressInvoices.Add(new AddressInvoiceData()
|
|
|
|
|
{
|
|
|
|
|
InvoiceDataId = invoice.Id,
|
|
|
|
|
CreatedTime = DateTimeOffset.UtcNow,
|
2018-02-19 07:09:05 +01:00
|
|
|
|
}.Set(address, paymentMethod.GetId()));
|
2018-02-18 18:38:03 +01:00
|
|
|
|
|
2017-12-21 07:52:04 +01:00
|
|
|
|
context.HistoricalAddressInvoices.Add(new HistoricalAddressInvoiceData()
|
|
|
|
|
{
|
|
|
|
|
InvoiceDataId = invoice.Id,
|
|
|
|
|
Assigned = DateTimeOffset.UtcNow
|
2018-02-19 07:09:05 +01:00
|
|
|
|
}.SetAddress(paymentDestination, paymentMethod.GetId().ToString()));
|
2018-02-18 18:38:03 +01:00
|
|
|
|
textSearch.Add(paymentDestination);
|
2018-02-19 07:09:05 +01:00
|
|
|
|
textSearch.Add(paymentMethod.Calculate().TotalDue.ToString());
|
2017-12-21 07:52:04 +01:00
|
|
|
|
}
|
2017-11-12 15:51:14 +01:00
|
|
|
|
context.PendingInvoices.Add(new PendingInvoiceData() { Id = invoice.Id });
|
2018-03-28 15:37:01 +02:00
|
|
|
|
|
2018-09-08 07:32:26 +02:00
|
|
|
|
foreach (var log in creationLogs.ToList())
|
2018-03-28 15:37:01 +02:00
|
|
|
|
{
|
|
|
|
|
context.InvoiceEvents.Add(new InvoiceEventData()
|
|
|
|
|
{
|
|
|
|
|
InvoiceDataId = invoice.Id,
|
2018-07-24 05:19:43 +02:00
|
|
|
|
Message = log.Log,
|
|
|
|
|
Timestamp = log.Timestamp,
|
2018-03-28 15:37:01 +02:00
|
|
|
|
UniqueId = Encoders.Hex.EncodeData(RandomUtils.GetBytes(10))
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
await context.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-21 07:52:04 +01:00
|
|
|
|
textSearch.Add(invoice.Id);
|
|
|
|
|
textSearch.Add(invoice.InvoiceTime.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
textSearch.Add(invoice.ProductInformation.Price.ToString(CultureInfo.InvariantCulture));
|
|
|
|
|
textSearch.Add(invoice.OrderId);
|
2018-01-11 14:52:28 +01:00
|
|
|
|
textSearch.Add(ToString(invoice.BuyerInformation, null));
|
|
|
|
|
textSearch.Add(ToString(invoice.ProductInformation, null));
|
2017-12-21 07:52:04 +01:00
|
|
|
|
textSearch.Add(invoice.StoreId);
|
|
|
|
|
|
|
|
|
|
AddToTextSearch(invoice.Id, textSearch.ToArray());
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return invoice;
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-13 07:28:39 +01:00
|
|
|
|
private static string GetDestination(PaymentMethod paymentMethod, Network network)
|
2018-02-18 18:38:03 +01:00
|
|
|
|
{
|
2018-02-19 03:31:34 +01:00
|
|
|
|
// For legacy reason, BitcoinLikeOnChain is putting the hashes of addresses in database
|
2018-02-19 07:09:05 +01:00
|
|
|
|
if (paymentMethod.GetId().PaymentType == Payments.PaymentTypes.BTCLike)
|
2018-02-18 18:38:03 +01:00
|
|
|
|
{
|
2018-03-13 07:28:39 +01:00
|
|
|
|
return ((Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod)paymentMethod.GetPaymentMethodDetails()).GetDepositAddress(network).ScriptPubKey.Hash.ToString();
|
2018-02-18 18:38:03 +01:00
|
|
|
|
}
|
2018-02-19 03:31:34 +01:00
|
|
|
|
///////////////
|
2018-02-19 07:09:05 +01:00
|
|
|
|
return paymentMethod.GetPaymentMethodDetails().GetPaymentDestination();
|
2018-02-18 18:38:03 +01:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-04 16:37:09 +01:00
|
|
|
|
public async Task<bool> NewAddress(string invoiceId, Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod paymentMethod, BTCPayNetwork network)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
var invoice = await context.Invoices.FirstOrDefaultAsync(i => i.Id == invoiceId);
|
|
|
|
|
if (invoice == null)
|
|
|
|
|
return false;
|
2017-12-21 07:52:04 +01:00
|
|
|
|
|
2018-01-11 14:52:28 +01:00
|
|
|
|
var invoiceEntity = ToObject<InvoiceEntity>(invoice.Blob, network.NBitcoinNetwork);
|
2018-02-19 07:09:05 +01:00
|
|
|
|
var currencyData = invoiceEntity.GetPaymentMethod(network, paymentMethod.GetPaymentType(), null);
|
2017-12-21 07:52:04 +01:00
|
|
|
|
if (currencyData == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
2019-01-04 16:37:09 +01:00
|
|
|
|
var existingPaymentMethod = (Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod)currencyData.GetPaymentMethodDetails();
|
2018-02-18 18:38:03 +01:00
|
|
|
|
if (existingPaymentMethod.GetPaymentDestination() != null)
|
2017-12-21 07:52:04 +01:00
|
|
|
|
{
|
2018-02-18 18:38:03 +01:00
|
|
|
|
MarkUnassigned(invoiceId, invoiceEntity, context, currencyData.GetId());
|
2017-12-21 07:52:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-18 18:38:03 +01:00
|
|
|
|
existingPaymentMethod.SetPaymentDestination(paymentMethod.GetPaymentDestination());
|
2018-02-19 07:09:05 +01:00
|
|
|
|
currencyData.SetPaymentMethodDetails(existingPaymentMethod);
|
2017-12-21 07:52:04 +01:00
|
|
|
|
#pragma warning disable CS0618
|
2018-01-06 10:57:56 +01:00
|
|
|
|
if (network.IsBTC)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2017-12-21 07:52:04 +01:00
|
|
|
|
invoiceEntity.DepositAddress = currencyData.DepositAddress;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2018-01-08 17:56:37 +01:00
|
|
|
|
#pragma warning restore CS0618
|
2018-02-19 07:09:05 +01:00
|
|
|
|
invoiceEntity.SetPaymentMethod(currencyData);
|
2018-01-11 14:52:28 +01:00
|
|
|
|
invoice.Blob = ToBytes(invoiceEntity, network.NBitcoinNetwork);
|
2017-12-21 07:52:04 +01:00
|
|
|
|
|
2018-01-14 13:48:23 +01:00
|
|
|
|
context.AddressInvoices.Add(new AddressInvoiceData()
|
|
|
|
|
{
|
|
|
|
|
InvoiceDataId = invoiceId,
|
|
|
|
|
CreatedTime = DateTimeOffset.UtcNow
|
|
|
|
|
}
|
2018-03-13 07:28:39 +01:00
|
|
|
|
.Set(GetDestination(currencyData, network.NBitcoinNetwork), currencyData.GetId()));
|
2017-10-27 10:53:04 +02:00
|
|
|
|
context.HistoricalAddressInvoices.Add(new HistoricalAddressInvoiceData()
|
|
|
|
|
{
|
|
|
|
|
InvoiceDataId = invoiceId,
|
|
|
|
|
Assigned = DateTimeOffset.UtcNow
|
2018-02-18 18:38:03 +01:00
|
|
|
|
}.SetAddress(paymentMethod.GetPaymentDestination(), network.CryptoCode));
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
|
|
|
|
await context.SaveChangesAsync();
|
2018-02-18 18:38:03 +01:00
|
|
|
|
AddToTextSearch(invoice.Id, paymentMethod.GetPaymentDestination());
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-14 13:48:23 +01:00
|
|
|
|
public async Task AddInvoiceEvent(string invoiceId, object evt)
|
|
|
|
|
{
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
context.InvoiceEvents.Add(new InvoiceEventData()
|
|
|
|
|
{
|
|
|
|
|
InvoiceDataId = invoiceId,
|
|
|
|
|
Message = evt.ToString(),
|
|
|
|
|
Timestamp = DateTimeOffset.UtcNow,
|
|
|
|
|
UniqueId = Encoders.Hex.EncodeData(RandomUtils.GetBytes(10))
|
|
|
|
|
});
|
2018-07-24 05:19:43 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
2018-09-08 07:32:26 +02:00
|
|
|
|
catch (DbUpdateException) { } // Probably the invoice does not exists anymore
|
2018-01-14 13:48:23 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-19 07:09:05 +01:00
|
|
|
|
private static void MarkUnassigned(string invoiceId, InvoiceEntity entity, ApplicationDbContext context, PaymentMethodId paymentMethodId)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-02-19 07:09:05 +01:00
|
|
|
|
foreach (var address in entity.GetPaymentMethods(null))
|
2017-12-21 07:52:04 +01:00
|
|
|
|
{
|
2018-02-19 07:09:05 +01:00
|
|
|
|
if (paymentMethodId != null && paymentMethodId != address.GetId())
|
2017-12-21 07:52:04 +01:00
|
|
|
|
continue;
|
|
|
|
|
var historical = new HistoricalAddressInvoiceData();
|
|
|
|
|
historical.InvoiceDataId = invoiceId;
|
2018-02-19 07:09:05 +01:00
|
|
|
|
historical.SetAddress(address.GetPaymentMethodDetails().GetPaymentDestination(), address.GetId().ToString());
|
2017-12-21 07:52:04 +01:00
|
|
|
|
historical.UnAssigned = DateTimeOffset.UtcNow;
|
|
|
|
|
context.Attach(historical);
|
|
|
|
|
context.Entry(historical).Property(o => o.UnAssigned).IsModified = true;
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task UnaffectAddress(string invoiceId)
|
|
|
|
|
{
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
2018-05-16 03:26:45 +02:00
|
|
|
|
var invoiceData = await context.FindAsync<Data.InvoiceData>(invoiceId).ConfigureAwait(false);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
if (invoiceData == null)
|
|
|
|
|
return;
|
2018-01-11 14:52:28 +01:00
|
|
|
|
var invoiceEntity = ToObject<InvoiceEntity>(invoiceData.Blob, null);
|
2017-12-21 07:52:04 +01:00
|
|
|
|
MarkUnassigned(invoiceId, invoiceEntity, context, null);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await context.SaveChangesAsync();
|
|
|
|
|
}
|
|
|
|
|
catch (DbUpdateException) { } //Possibly, it was unassigned before
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string[] SearchInvoice(string searchTerms)
|
|
|
|
|
{
|
|
|
|
|
using (var tx = _Engine.GetTransaction())
|
|
|
|
|
{
|
2018-11-02 06:26:13 +01:00
|
|
|
|
var terms = searchTerms.Split(null);
|
|
|
|
|
searchTerms = string.Join(' ', terms.Select(t => t.Length > 50 ? t.Substring(0, 50) : t).ToArray());
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return tx.TextSearch("InvoiceSearch").Block(searchTerms)
|
|
|
|
|
.GetDocumentIDs()
|
|
|
|
|
.Select(id => Encoders.Base58.EncodeData(id))
|
|
|
|
|
.ToArray();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AddToTextSearch(string invoiceId, params string[] terms)
|
|
|
|
|
{
|
2017-12-21 07:52:04 +01:00
|
|
|
|
_IndexerThread.DoAsync(() =>
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2017-11-12 15:03:33 +01:00
|
|
|
|
using (var tx = _Engine.GetTransaction())
|
|
|
|
|
{
|
2018-01-04 14:21:47 +01:00
|
|
|
|
tx.TextAppend("InvoiceSearch", Encoders.Base58.DecodeData(invoiceId), string.Join(" ", terms.Where(t => !String.IsNullOrWhiteSpace(t))));
|
2017-11-12 15:03:33 +01:00
|
|
|
|
tx.Commit();
|
|
|
|
|
}
|
|
|
|
|
});
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-12-10 13:48:28 +01:00
|
|
|
|
public async Task UpdateInvoiceStatus(string invoiceId, InvoiceState invoiceState)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
2018-05-16 03:26:45 +02:00
|
|
|
|
var invoiceData = await context.FindAsync<Data.InvoiceData>(invoiceId).ConfigureAwait(false);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
if (invoiceData == null)
|
|
|
|
|
return;
|
2018-12-10 13:48:28 +01:00
|
|
|
|
invoiceData.Status = InvoiceState.ToString(invoiceState.Status);
|
|
|
|
|
invoiceData.ExceptionStatus = InvoiceState.ToString(invoiceState.ExceptionStatus);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
await context.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-11-06 04:15:52 +01:00
|
|
|
|
public async Task UpdatePaidInvoiceToInvalid(string invoiceId)
|
|
|
|
|
{
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
2018-05-16 03:26:45 +02:00
|
|
|
|
var invoiceData = await context.FindAsync<Data.InvoiceData>(invoiceId).ConfigureAwait(false);
|
2018-12-10 07:34:48 +01:00
|
|
|
|
if (invoiceData == null || !invoiceData.GetInvoiceState().CanMarkInvalid())
|
2017-11-06 04:15:52 +01:00
|
|
|
|
return;
|
|
|
|
|
invoiceData.Status = "invalid";
|
2018-12-10 07:34:48 +01:00
|
|
|
|
invoiceData.ExceptionStatus = "marked";
|
|
|
|
|
await context.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
public async Task UpdatePaidInvoiceToComplete(string invoiceId)
|
|
|
|
|
{
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
var invoiceData = await context.FindAsync<Data.InvoiceData>(invoiceId).ConfigureAwait(false);
|
|
|
|
|
if (invoiceData == null || !invoiceData.GetInvoiceState().CanMarkComplete())
|
|
|
|
|
return;
|
|
|
|
|
invoiceData.Status = "complete";
|
|
|
|
|
invoiceData.ExceptionStatus = "marked";
|
2017-11-06 04:15:52 +01:00
|
|
|
|
await context.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-12-06 09:05:27 +01:00
|
|
|
|
public async Task<InvoiceEntity> GetInvoice(string id, bool inludeAddressData = false)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
2018-05-16 03:26:45 +02:00
|
|
|
|
IQueryable<Data.InvoiceData> query =
|
2017-10-27 10:53:04 +02:00
|
|
|
|
context
|
|
|
|
|
.Invoices
|
|
|
|
|
.Include(o => o.Payments)
|
|
|
|
|
.Include(o => o.RefundAddresses);
|
2017-11-06 09:31:02 +01:00
|
|
|
|
if (inludeAddressData)
|
|
|
|
|
query = query.Include(o => o.HistoricalAddressInvoices).Include(o => o.AddressInvoices);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
query = query.Where(i => i.Id == id);
|
|
|
|
|
|
|
|
|
|
var invoice = await query.FirstOrDefaultAsync().ConfigureAwait(false);
|
|
|
|
|
if (invoice == null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return ToEntity(invoice);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 03:26:45 +02:00
|
|
|
|
private InvoiceEntity ToEntity(Data.InvoiceData invoice)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-01-11 14:52:28 +01:00
|
|
|
|
var entity = ToObject<InvoiceEntity>(invoice.Blob, null);
|
2019-01-05 05:31:05 +01:00
|
|
|
|
PaymentMethodDictionary paymentMethods = null;
|
2018-01-10 10:30:45 +01:00
|
|
|
|
#pragma warning disable CS0618
|
2017-12-21 07:52:04 +01:00
|
|
|
|
entity.Payments = invoice.Payments.Select(p =>
|
2017-11-06 09:31:02 +01:00
|
|
|
|
{
|
2018-01-11 14:52:28 +01:00
|
|
|
|
var paymentEntity = ToObject<PaymentEntity>(p.Blob, null);
|
2017-11-06 09:31:02 +01:00
|
|
|
|
paymentEntity.Accounted = p.Accounted;
|
2019-01-05 05:31:05 +01:00
|
|
|
|
|
|
|
|
|
// PaymentEntity on version 0 does not have their own fee, because it was assumed that the payment method have fixed fee.
|
|
|
|
|
// We want to hide this legacy detail in InvoiceRepository, so we fetch the fee from the PaymentMethod and assign it to the PaymentEntity.
|
|
|
|
|
if (paymentEntity.Version == 0)
|
|
|
|
|
{
|
|
|
|
|
if (paymentMethods == null)
|
|
|
|
|
paymentMethods = entity.GetPaymentMethods(null);
|
|
|
|
|
var paymentMethodDetails = paymentMethods.TryGet(paymentEntity.GetPaymentMethodId())?.GetPaymentMethodDetails();
|
|
|
|
|
if (paymentMethodDetails != null) // == null should never happen, but we never know.
|
2019-01-07 07:35:18 +01:00
|
|
|
|
paymentEntity.NetworkFee = paymentMethodDetails.GetNextNetworkFee();
|
2019-01-05 05:31:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
2017-11-06 09:31:02 +01:00
|
|
|
|
return paymentEntity;
|
2018-12-18 18:56:51 +01:00
|
|
|
|
})
|
|
|
|
|
.OrderBy(a => a.ReceivedTime).ToList();
|
2018-01-10 10:30:45 +01:00
|
|
|
|
#pragma warning restore CS0618
|
2018-12-10 13:48:28 +01:00
|
|
|
|
var state = invoice.GetInvoiceState();
|
|
|
|
|
entity.ExceptionStatus = state.ExceptionStatus;
|
|
|
|
|
entity.Status = state.Status;
|
2017-10-27 10:53:04 +02:00
|
|
|
|
entity.RefundMail = invoice.CustomerEmail;
|
|
|
|
|
entity.Refundable = invoice.RefundAddresses.Count != 0;
|
|
|
|
|
if (invoice.HistoricalAddressInvoices != null)
|
|
|
|
|
{
|
|
|
|
|
entity.HistoricalAddresses = invoice.HistoricalAddressInvoices.ToArray();
|
|
|
|
|
}
|
2017-11-06 09:31:02 +01:00
|
|
|
|
if (invoice.AddressInvoices != null)
|
|
|
|
|
{
|
2018-02-19 07:09:05 +01:00
|
|
|
|
entity.AvailableAddressHashes = invoice.AddressInvoices.Select(a => a.GetAddress() + a.GetpaymentMethodId().ToString()).ToHashSet();
|
2017-11-06 09:31:02 +01:00
|
|
|
|
}
|
2018-02-25 16:48:12 +01:00
|
|
|
|
if (invoice.Events != null)
|
2018-01-14 13:48:23 +01:00
|
|
|
|
{
|
|
|
|
|
entity.Events = invoice.Events.OrderBy(c => c.Timestamp).ToList();
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
private IQueryable<Data.InvoiceData> GetInvoiceQuery(ApplicationDbContext context, InvoiceQuery queryObject)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2019-01-16 21:33:04 +01:00
|
|
|
|
IQueryable<Data.InvoiceData> query = context.Invoices;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(queryObject.InvoiceId))
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2019-01-16 21:33:04 +01:00
|
|
|
|
query = query.Where(i => i.Id == queryObject.InvoiceId);
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
if (queryObject.StoreId != null && queryObject.StoreId.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
var stores = queryObject.StoreId.ToHashSet();
|
|
|
|
|
query = query.Where(i => stores.Contains(i.StoreDataId));
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
if (queryObject.UserId != null)
|
|
|
|
|
{
|
|
|
|
|
query = query.Where(i => i.StoreData.UserStores.Any(u => u.ApplicationUserId == queryObject.UserId));
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(queryObject.TextSearch))
|
|
|
|
|
{
|
|
|
|
|
var ids = new HashSet<string>(SearchInvoice(queryObject.TextSearch));
|
|
|
|
|
if (ids.Count == 0)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2019-01-16 21:33:04 +01:00
|
|
|
|
// Hacky way to return an empty query object. The nice way is much too elaborate:
|
|
|
|
|
// https://stackoverflow.com/questions/33305495/how-to-return-empty-iqueryable-in-an-async-repository-method
|
|
|
|
|
return query.Where(x => false);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2019-01-16 21:33:04 +01:00
|
|
|
|
query = query.Where(i => ids.Contains(i.Id));
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
if (queryObject.StartDate != null)
|
|
|
|
|
query = query.Where(i => queryObject.StartDate.Value <= i.Created);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
if (queryObject.EndDate != null)
|
|
|
|
|
query = query.Where(i => i.Created <= queryObject.EndDate.Value);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
if (queryObject.OrderId != null && queryObject.OrderId.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
var statusSet = queryObject.OrderId.ToHashSet();
|
|
|
|
|
query = query.Where(i => statusSet.Contains(i.OrderId));
|
|
|
|
|
}
|
|
|
|
|
if (queryObject.ItemCode != null && queryObject.ItemCode.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
var statusSet = queryObject.ItemCode.ToHashSet();
|
|
|
|
|
query = query.Where(i => statusSet.Contains(i.ItemCode));
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
if (queryObject.Status != null && queryObject.Status.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
var statusSet = queryObject.Status.ToHashSet();
|
|
|
|
|
query = query.Where(i => statusSet.Contains(i.Status));
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
if (queryObject.Unusual != null)
|
|
|
|
|
{
|
|
|
|
|
var unused = queryObject.Unusual.Value;
|
|
|
|
|
query = query.Where(i => unused == (i.Status == "invalid" || i.ExceptionStatus != null));
|
|
|
|
|
}
|
2018-05-06 06:16:39 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
if (queryObject.ExceptionStatus != null && queryObject.ExceptionStatus.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
var exceptionStatusSet = queryObject.ExceptionStatus.Select(s => NormalizeExceptionStatus(s)).ToHashSet();
|
|
|
|
|
query = query.Where(i => exceptionStatusSet.Contains(i.ExceptionStatus));
|
|
|
|
|
}
|
2018-05-05 16:25:09 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
query = query.OrderByDescending(q => q.Created);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
if (queryObject.Skip != null)
|
|
|
|
|
query = query.Skip(queryObject.Skip.Value);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
if (queryObject.Count != null)
|
|
|
|
|
query = query.Take(queryObject.Count.Value);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
return query;
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
public async Task<int> GetInvoicesTotal(InvoiceQuery queryObject)
|
|
|
|
|
{
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
var query = GetInvoiceQuery(context, queryObject);
|
|
|
|
|
return await query.CountAsync();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2019-01-16 21:33:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public async Task<InvoiceEntity[]> GetInvoices(InvoiceQuery queryObject)
|
|
|
|
|
{
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
var query = GetInvoiceQuery(context, queryObject);
|
|
|
|
|
query = query.Include(o => o.Payments)
|
|
|
|
|
.Include(o => o.RefundAddresses);
|
|
|
|
|
if (queryObject.IncludeAddresses)
|
|
|
|
|
query = query.Include(o => o.HistoricalAddressInvoices).Include(o => o.AddressInvoices);
|
|
|
|
|
if (queryObject.IncludeEvents)
|
|
|
|
|
query = query.Include(o => o.Events);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2019-01-16 21:33:04 +01:00
|
|
|
|
var data = await query.ToArrayAsync().ConfigureAwait(false);
|
|
|
|
|
return data.Select(ToEntity).ToArray();
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-05 16:25:09 +02:00
|
|
|
|
private string NormalizeExceptionStatus(string status)
|
|
|
|
|
{
|
|
|
|
|
status = status.ToLowerInvariant();
|
|
|
|
|
switch (status)
|
|
|
|
|
{
|
|
|
|
|
case "paidover":
|
|
|
|
|
case "over":
|
|
|
|
|
case "overpaid":
|
|
|
|
|
status = "paidOver";
|
|
|
|
|
break;
|
|
|
|
|
case "paidlate":
|
|
|
|
|
case "late":
|
|
|
|
|
status = "paidLate";
|
|
|
|
|
break;
|
|
|
|
|
case "paidpartial":
|
|
|
|
|
case "underpaid":
|
|
|
|
|
case "partial":
|
|
|
|
|
status = "paidPartial";
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return status;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 14:52:28 +01:00
|
|
|
|
public async Task AddRefundsAsync(string invoiceId, TxOut[] outputs, Network network)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
if (outputs.Length == 0)
|
|
|
|
|
return;
|
|
|
|
|
outputs = outputs.Take(10).ToArray();
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
foreach (var output in outputs)
|
|
|
|
|
{
|
2017-11-12 15:37:21 +01:00
|
|
|
|
context.RefundAddresses.Add(new RefundAddressesData()
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
Id = invoiceId + "-" + i,
|
|
|
|
|
InvoiceDataId = invoiceId,
|
2018-01-11 14:52:28 +01:00
|
|
|
|
Blob = ToBytes(output, network)
|
2017-11-12 15:37:21 +01:00
|
|
|
|
});
|
2017-10-27 10:53:04 +02:00
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
await context.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 14:52:28 +01:00
|
|
|
|
var addresses = outputs.Select(o => o.ScriptPubKey.GetDestinationAddress(network)).Where(a => a != null).ToArray();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
AddToTextSearch(invoiceId, addresses.Select(a => a.ToString()).ToArray());
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-17 18:26:33 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add a payment to an invoice
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="invoiceId"></param>
|
|
|
|
|
/// <param name="date"></param>
|
|
|
|
|
/// <param name="paymentData"></param>
|
|
|
|
|
/// <param name="cryptoCode"></param>
|
|
|
|
|
/// <param name="accounted"></param>
|
|
|
|
|
/// <returns>The PaymentEntity or null if already added</returns>
|
2019-01-04 16:37:09 +01:00
|
|
|
|
public async Task<PaymentEntity> AddPayment(string invoiceId, DateTimeOffset date, CryptoPaymentData paymentData, BTCPayNetwork network, bool accounted = false)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
2019-01-05 05:31:05 +01:00
|
|
|
|
var invoice = context.Invoices.Find(invoiceId);
|
|
|
|
|
if (invoice == null)
|
|
|
|
|
return null;
|
2019-01-04 16:37:09 +01:00
|
|
|
|
InvoiceEntity invoiceEntity = ToObject<InvoiceEntity>(invoice.Blob, network.NBitcoinNetwork);
|
|
|
|
|
PaymentMethod paymentMethod = invoiceEntity.GetPaymentMethod(new PaymentMethodId(network.CryptoCode, paymentData.GetPaymentType()), null);
|
|
|
|
|
IPaymentMethodDetails paymentMethodDetails = paymentMethod.GetPaymentMethodDetails();
|
2017-10-27 10:53:04 +02:00
|
|
|
|
PaymentEntity entity = new PaymentEntity
|
|
|
|
|
{
|
2019-01-05 05:31:05 +01:00
|
|
|
|
Version = 1,
|
2018-01-08 17:56:37 +01:00
|
|
|
|
#pragma warning disable CS0618
|
2019-01-04 16:37:09 +01:00
|
|
|
|
CryptoCode = network.CryptoCode,
|
2018-01-08 17:56:37 +01:00
|
|
|
|
#pragma warning restore CS0618
|
2018-02-16 17:34:40 +01:00
|
|
|
|
ReceivedTime = date.UtcDateTime,
|
2019-01-05 05:31:05 +01:00
|
|
|
|
Accounted = accounted,
|
2019-01-07 07:35:18 +01:00
|
|
|
|
NetworkFee = paymentMethodDetails.GetNextNetworkFee()
|
2017-10-27 10:53:04 +02:00
|
|
|
|
};
|
2018-02-17 18:19:35 +01:00
|
|
|
|
entity.SetCryptoPaymentData(paymentData);
|
2018-02-25 16:48:12 +01:00
|
|
|
|
|
2019-01-04 16:37:09 +01:00
|
|
|
|
if (paymentMethodDetails is Payments.Bitcoin.BitcoinLikeOnChainPaymentMethod bitcoinPaymentMethod &&
|
|
|
|
|
bitcoinPaymentMethod.NetworkFeeMode == NetworkFeeMode.MultiplePaymentsOnly &&
|
2019-01-07 07:35:18 +01:00
|
|
|
|
bitcoinPaymentMethod.NextNetworkFee == Money.Zero)
|
2019-01-04 16:37:09 +01:00
|
|
|
|
{
|
2019-01-07 07:35:18 +01:00
|
|
|
|
bitcoinPaymentMethod.NextNetworkFee = bitcoinPaymentMethod.FeeRate.GetFee(100); // assume price for 100 bytes
|
2019-01-04 16:37:09 +01:00
|
|
|
|
paymentMethod.SetPaymentMethodDetails(bitcoinPaymentMethod);
|
|
|
|
|
invoiceEntity.SetPaymentMethod(paymentMethod);
|
|
|
|
|
invoice.Blob = ToBytes(invoiceEntity, network.NBitcoinNetwork);
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
PaymentData data = new PaymentData
|
|
|
|
|
{
|
2018-02-17 18:19:35 +01:00
|
|
|
|
Id = paymentData.GetPaymentId(),
|
2018-01-11 14:52:28 +01:00
|
|
|
|
Blob = ToBytes(entity, null),
|
2018-02-16 17:34:40 +01:00
|
|
|
|
InvoiceDataId = invoiceId,
|
2018-02-25 16:48:12 +01:00
|
|
|
|
Accounted = accounted
|
2017-10-27 10:53:04 +02:00
|
|
|
|
};
|
|
|
|
|
|
2017-11-12 15:37:21 +01:00
|
|
|
|
context.Payments.Add(data);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
|
2018-03-17 18:26:33 +01:00
|
|
|
|
try
|
|
|
|
|
{
|
2018-03-29 05:10:03 +02:00
|
|
|
|
await context.SaveChangesAsync().ConfigureAwait(false);
|
2018-03-17 18:26:33 +01:00
|
|
|
|
}
|
2018-09-08 07:32:26 +02:00
|
|
|
|
catch (DbUpdateException) { return null; } // Already exists
|
2018-02-17 18:19:35 +01:00
|
|
|
|
AddToTextSearch(invoiceId, paymentData.GetSearchTerms());
|
2017-10-27 10:53:04 +02:00
|
|
|
|
return entity;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-10 10:30:45 +01:00
|
|
|
|
public async Task UpdatePayments(List<PaymentEntity> payments)
|
2017-11-06 09:31:02 +01:00
|
|
|
|
{
|
|
|
|
|
if (payments.Count == 0)
|
|
|
|
|
return;
|
|
|
|
|
using (var context = _ContextFactory.CreateContext())
|
|
|
|
|
{
|
|
|
|
|
foreach (var payment in payments)
|
|
|
|
|
{
|
2018-02-17 18:19:35 +01:00
|
|
|
|
var paymentData = payment.GetCryptoPaymentData();
|
2017-11-06 09:31:02 +01:00
|
|
|
|
var data = new PaymentData();
|
2018-02-17 18:19:35 +01:00
|
|
|
|
data.Id = paymentData.GetPaymentId();
|
2018-01-10 10:30:45 +01:00
|
|
|
|
data.Accounted = payment.Accounted;
|
2018-02-16 17:34:40 +01:00
|
|
|
|
data.Blob = ToBytes(payment, null);
|
2017-11-06 09:31:02 +01:00
|
|
|
|
context.Attach(data);
|
|
|
|
|
context.Entry(data).Property(o => o.Accounted).IsModified = true;
|
2018-02-16 17:34:40 +01:00
|
|
|
|
context.Entry(data).Property(o => o.Blob).IsModified = true;
|
2017-11-06 09:31:02 +01:00
|
|
|
|
}
|
|
|
|
|
await context.SaveChangesAsync().ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 14:52:28 +01:00
|
|
|
|
private T ToObject<T>(byte[] value, Network network)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-01-11 14:52:28 +01:00
|
|
|
|
return NBitcoin.JsonConverters.Serializer.ToObject<T>(ZipUtils.Unzip(value), network);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 14:52:28 +01:00
|
|
|
|
private byte[] ToBytes<T>(T obj, Network network)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-01-11 14:52:28 +01:00
|
|
|
|
return ZipUtils.Zip(NBitcoin.JsonConverters.Serializer.ToString(obj, network));
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 14:52:28 +01:00
|
|
|
|
private T Clone<T>(T invoice, Network network)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-01-11 14:52:28 +01:00
|
|
|
|
return NBitcoin.JsonConverters.Serializer.ToObject<T>(ToString(invoice, network), network);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 14:52:28 +01:00
|
|
|
|
private string ToString<T>(T data, Network network)
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
2018-01-11 14:52:28 +01:00
|
|
|
|
return NBitcoin.JsonConverters.Serializer.ToString(data, network);
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-11-12 15:03:33 +01:00
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (_Engine != null)
|
|
|
|
|
_Engine.Dispose();
|
|
|
|
|
if (_IndexerThread != null)
|
|
|
|
|
_IndexerThread.Dispose();
|
|
|
|
|
}
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class InvoiceQuery
|
|
|
|
|
{
|
2018-04-26 04:01:59 +02:00
|
|
|
|
public string[] StoreId
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
public string UserId
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
public string TextSearch
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
public DateTimeOffset? StartDate
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DateTimeOffset? EndDate
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int? Skip
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int? Count
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-06 10:00:55 +01:00
|
|
|
|
public string[] OrderId
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-06 10:00:55 +01:00
|
|
|
|
public string[] ItemCode
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-06 06:16:39 +02:00
|
|
|
|
public bool? Unusual { get; set; }
|
|
|
|
|
|
2018-04-26 04:01:59 +02:00
|
|
|
|
public string[] Status
|
2017-10-27 10:53:04 +02:00
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
2018-05-05 16:25:09 +02:00
|
|
|
|
|
|
|
|
|
public string[] ExceptionStatus
|
|
|
|
|
{
|
|
|
|
|
get; set;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-27 10:53:04 +02:00
|
|
|
|
public string InvoiceId
|
|
|
|
|
{
|
|
|
|
|
get;
|
|
|
|
|
set;
|
|
|
|
|
}
|
2018-01-08 12:06:16 +01:00
|
|
|
|
public bool IncludeAddresses { get; set; }
|
2018-01-14 13:48:23 +01:00
|
|
|
|
|
|
|
|
|
public bool IncludeEvents { get; set; }
|
2017-10-27 10:53:04 +02:00
|
|
|
|
}
|
2017-09-13 08:47:34 +02:00
|
|
|
|
}
|