public static async Task <bool> ActivateInvoicePaymentMethod(this InvoiceRepository invoiceRepository, EventAggregator eventAggregator, BTCPayNetworkProvider btcPayNetworkProvider, PaymentMethodHandlerDictionary paymentMethodHandlerDictionary, StoreData store, InvoiceEntity invoice, PaymentMethodId paymentMethodId) { if (invoice.GetInvoiceState().Status != InvoiceStatusLegacy.New) { return(false); } bool success = false; var eligibleMethodToActivate = invoice.GetPaymentMethod(paymentMethodId); if (!eligibleMethodToActivate.GetPaymentMethodDetails().Activated) { var payHandler = paymentMethodHandlerDictionary[paymentMethodId]; var supportPayMethod = invoice.GetSupportedPaymentMethod() .Single(method => method.PaymentId == paymentMethodId); var paymentMethod = invoice.GetPaymentMethod(paymentMethodId); var network = btcPayNetworkProvider.GetNetwork(paymentMethodId.CryptoCode); var prepare = payHandler.PreparePayment(supportPayMethod, store, network); InvoiceLogs logs = new InvoiceLogs(); try { var pmis = invoice.GetPaymentMethods().Select(method => method.GetId()).ToHashSet(); logs.Write($"{paymentMethodId}: Activating", InvoiceEventData.EventSeverity.Info); var newDetails = await payHandler.CreatePaymentMethodDetails(logs, supportPayMethod, paymentMethod, store, network, prepare, pmis); eligibleMethodToActivate.SetPaymentMethodDetails(newDetails); await invoiceRepository.UpdateInvoicePaymentMethod(invoice.Id, eligibleMethodToActivate); eventAggregator.Publish(new InvoicePaymentMethodActivated(paymentMethodId, invoice)); eventAggregator.Publish(new InvoiceNeedUpdateEvent(invoice.Id)); success = true; } catch (PaymentMethodUnavailableException ex) { logs.Write($"{paymentMethodId}: Payment method unavailable ({ex.Message})", InvoiceEventData.EventSeverity.Error); } catch (Exception ex) { logs.Write($"{paymentMethodId}: Unexpected exception ({ex})", InvoiceEventData.EventSeverity.Error); } await invoiceRepository.AddInvoiceLogs(invoice.Id, logs); } return(success); }
private static void MarkUnassigned(string invoiceId, InvoiceEntity entity, ApplicationDbContext context, PaymentMethodId paymentMethodId) { foreach (var address in entity.GetPaymentMethods(null)) { if (paymentMethodId != null && paymentMethodId != address.GetId()) { continue; } var historical = new HistoricalAddressInvoiceData(); historical.InvoiceDataId = invoiceId; historical.SetAddress(address.GetPaymentMethodDetails().GetPaymentDestination(), address.GetId().ToString()); historical.UnAssigned = DateTimeOffset.UtcNow; context.Attach(historical); context.Entry(historical).Property(o => o.UnAssigned).IsModified = true; } }
public async Task <InvoiceEntity> CreateInvoiceAsync(string storeId, InvoiceEntity invoice, InvoiceLogs creationLogs, BTCPayNetworkProvider networkProvider) { List <string> textSearch = new List <string>(); invoice = Clone(invoice, null); invoice.Id = Encoders.Base58.EncodeData(RandomUtils.GetBytes(16)); #pragma warning disable CS0618 invoice.Payments = new List <PaymentEntity>(); #pragma warning restore CS0618 invoice.StoreId = storeId; using (var context = _ContextFactory.CreateContext()) { context.Invoices.Add(new Data.InvoiceData() { StoreDataId = storeId, Id = invoice.Id, Created = invoice.InvoiceTime, Blob = ToBytes(invoice, null), OrderId = invoice.OrderId, #pragma warning disable CS0618 // Type or member is obsolete Status = invoice.StatusString, #pragma warning restore CS0618 // Type or member is obsolete ItemCode = invoice.ProductInformation.ItemCode, CustomerEmail = invoice.RefundMail }); foreach (var paymentMethod in invoice.GetPaymentMethods(networkProvider)) { if (paymentMethod.Network == null) { throw new InvalidOperationException("CryptoCode unsupported"); } var paymentDestination = paymentMethod.GetPaymentMethodDetails().GetPaymentDestination(); string address = GetDestination(paymentMethod, paymentMethod.Network.NBitcoinNetwork); context.AddressInvoices.Add(new AddressInvoiceData() { InvoiceDataId = invoice.Id, CreatedTime = DateTimeOffset.UtcNow, }.Set(address, paymentMethod.GetId())); context.HistoricalAddressInvoices.Add(new HistoricalAddressInvoiceData() { InvoiceDataId = invoice.Id, Assigned = DateTimeOffset.UtcNow }.SetAddress(paymentDestination, paymentMethod.GetId().ToString())); textSearch.Add(paymentDestination); textSearch.Add(paymentMethod.Calculate().TotalDue.ToString()); } 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); } textSearch.Add(invoice.Id); textSearch.Add(invoice.InvoiceTime.ToString(CultureInfo.InvariantCulture)); textSearch.Add(invoice.ProductInformation.Price.ToString(CultureInfo.InvariantCulture)); textSearch.Add(invoice.OrderId); textSearch.Add(ToString(invoice.BuyerInformation, null)); textSearch.Add(ToString(invoice.ProductInformation, null)); textSearch.Add(invoice.StoreId); AddToTextSearch(invoice.Id, textSearch.ToArray()); return(invoice); }
public async Task <InvoiceEntity> CreateInvoiceAsync(string storeId, InvoiceEntity invoice, string[] additionalSearchTerms = null) { var textSearch = new HashSet <string>(); invoice = Clone(invoice); invoice.Networks = _btcPayNetworkProvider; invoice.Id = Encoders.Base58.EncodeData(RandomUtils.GetBytes(16)); #pragma warning disable CS0618 invoice.Payments = new List <PaymentEntity>(); #pragma warning restore CS0618 invoice.StoreId = storeId; using (var context = _applicationDbContextFactory.CreateContext()) { var invoiceData = new Data.InvoiceData() { StoreDataId = storeId, Id = invoice.Id, Created = invoice.InvoiceTime, Blob = ToBytes(invoice, null), OrderId = invoice.Metadata.OrderId, #pragma warning disable CS0618 // Type or member is obsolete Status = invoice.StatusString, #pragma warning restore CS0618 // Type or member is obsolete ItemCode = invoice.Metadata.ItemCode, CustomerEmail = invoice.RefundMail, Archived = false }; await context.Invoices.AddAsync(invoiceData); foreach (var paymentMethod in invoice.GetPaymentMethods()) { if (paymentMethod.Network == null) { throw new InvalidOperationException("CryptoCode unsupported"); } var details = paymentMethod.GetPaymentMethodDetails(); if (!details.Activated) { continue; } var paymentDestination = details.GetPaymentDestination(); string address = GetDestination(paymentMethod); await context.AddressInvoices.AddAsync(new AddressInvoiceData() { InvoiceDataId = invoice.Id, CreatedTime = DateTimeOffset.UtcNow, }.Set(address, paymentMethod.GetId())); textSearch.Add(paymentDestination); textSearch.Add(paymentMethod.Calculate().TotalDue.ToString()); } await context.PendingInvoices.AddAsync(new PendingInvoiceData() { Id = invoice.Id }); textSearch.Add(invoice.Id); textSearch.Add(invoice.InvoiceTime.ToString(CultureInfo.InvariantCulture)); if (!invoice.IsUnsetTopUp()) { textSearch.Add(invoice.Price.ToString(CultureInfo.InvariantCulture)); } textSearch.Add(invoice.Metadata.OrderId); textSearch.Add(invoice.StoreId); textSearch.Add(invoice.Metadata.BuyerEmail); if (additionalSearchTerms != null) { textSearch.AddRange(additionalSearchTerms); } AddToTextSearch(context, invoiceData, textSearch.ToArray()); await context.SaveChangesAsync().ConfigureAwait(false); } return(invoice); }