/// <summary> /// Invoice version < 1 were saving metadata directly under the InvoiceEntity /// object. But in version > 2, the metadata is saved under the InvoiceEntity.Metadata object /// This method is extracting metadata from the InvoiceEntity of version < 1 invoices and put them in InvoiceEntity.Metadata. /// </summary> internal void MigrateLegacyInvoice() { T TryParseMetadata <T>(string field) where T : class { if (AdditionalData.TryGetValue(field, out var token) && token is JObject obj) { return(obj.ToObject <T>()); } return(null); } if (TryParseMetadata <BuyerInformation>("buyerInformation") is BuyerInformation buyerInformation && TryParseMetadata <ProductInformation>("productInformation") is ProductInformation productInformation) { var wellknown = new InvoiceMetadata() { BuyerAddress1 = buyerInformation.BuyerAddress1, BuyerAddress2 = buyerInformation.BuyerAddress2, BuyerCity = buyerInformation.BuyerCity, BuyerCountry = buyerInformation.BuyerCountry, BuyerEmail = buyerInformation.BuyerEmail, BuyerName = buyerInformation.BuyerName, BuyerPhone = buyerInformation.BuyerPhone, BuyerState = buyerInformation.BuyerState, BuyerZip = buyerInformation.BuyerZip, ItemCode = productInformation.ItemCode, ItemDesc = productInformation.ItemDesc, Physical = productInformation.Physical, TaxIncluded = productInformation.TaxIncluded }; if (AdditionalData.TryGetValue("posData", out var token) && token is JValue val && val.Type == JTokenType.String) { wellknown.PosData = val.Value <string>(); } if (AdditionalData.TryGetValue("orderId", out var token2) && token2 is JValue val2 && val2.Type == JTokenType.String) { wellknown.OrderId = val2.Value <string>(); } Metadata = wellknown; Currency = productInformation.Currency; Price = productInformation.Price; }
public async Task <InvoiceEntity> UpdateInvoiceMetadata(string invoiceId, string storeId, JObject metadata) { using var context = _applicationDbContextFactory.CreateContext(); var invoiceData = await GetInvoiceRaw(invoiceId, context); if (invoiceData == null || (storeId != null && !invoiceData.StoreDataId.Equals(storeId, StringComparison.InvariantCultureIgnoreCase))) { return(null); } var blob = invoiceData.GetBlob(_btcPayNetworkProvider); blob.Metadata = InvoiceMetadata.FromJObject(metadata); invoiceData.Blob = ToBytes(blob); await context.SaveChangesAsync().ConfigureAwait(false); return(ToEntity(invoiceData)); }
public async Task <InvoiceEntity> UpdateInvoiceMetadata(string invoiceId, string storeId, JObject metadata) { using var context = _applicationDbContextFactory.CreateContext(); var invoiceData = await GetInvoiceRaw(invoiceId, context); if (invoiceData == null || (storeId != null && !invoiceData.StoreDataId.Equals(storeId, StringComparison.InvariantCultureIgnoreCase))) { return(null); } var blob = invoiceData.GetBlob(_btcPayNetworkProvider); var newMetadata = InvoiceMetadata.FromJObject(metadata); var oldOrderId = blob.Metadata.OrderId; var newOrderId = newMetadata.OrderId; if (newOrderId != oldOrderId) { // OrderId is saved in 2 places: (1) the invoice table and (2) in the metadata field. We are updating both for consistency. invoiceData.OrderId = newOrderId; if (oldOrderId != null && (newOrderId is null || !newOrderId.Equals(oldOrderId, StringComparison.InvariantCulture))) { RemoveFromTextSearch(context, invoiceData, oldOrderId); } if (newOrderId != null) { AddToTextSearch(context, invoiceData, new[] { newOrderId }); } } blob.Metadata = newMetadata; invoiceData.Blob = ToBytes(blob); await context.SaveChangesAsync().ConfigureAwait(false); return(ToEntity(invoiceData)); }