2024-04-04 09:31:04 +02:00
#nullable enable
using System ;
using System.Reflection.Metadata ;
2020-11-16 04:05:15 +01:00
using System.Runtime.InteropServices ;
2023-02-21 07:06:34 +01:00
using BTCPayServer.Payments ;
2020-06-28 10:15:42 +02:00
using BTCPayServer.Services.Invoices ;
2024-04-04 09:31:04 +02:00
using Newtonsoft.Json ;
2020-06-28 10:15:42 +02:00
using Newtonsoft.Json.Linq ;
namespace BTCPayServer.Data
{
public static class PaymentDataExtensions
{
2024-04-04 09:31:04 +02:00
public static PaymentData Set ( this PaymentData paymentData , InvoiceEntity invoiceEntity , IPaymentMethodHandler handler , object details )
2023-02-21 07:06:34 +01:00
{
2024-04-04 09:31:04 +02:00
var prompt = invoiceEntity . GetPaymentPrompt ( handler . PaymentMethodId ) ? ? throw new InvalidOperationException ( $"Payment prompt for {handler.PaymentMethodId} is not found" ) ;
var paymentBlob = new PaymentBlob ( )
{
Destination = prompt . Destination ,
PaymentMethodFee = prompt . PaymentMethodFee ,
Divisibility = prompt . Divisibility
} . SetDetails ( handler , details ) ;
paymentData . InvoiceDataId = invoiceEntity . Id ;
paymentData . SetBlob ( handler . PaymentMethodId , paymentBlob ) ;
return paymentData ;
2023-02-21 07:06:34 +01:00
}
2024-04-04 09:31:04 +02:00
public static PaymentEntity SetBlob ( this PaymentData paymentData , PaymentEntity entity )
2020-06-28 10:15:42 +02:00
{
2024-04-04 09:31:04 +02:00
paymentData . Amount = entity . Value ;
paymentData . Currency = entity . Currency ;
paymentData . Status = entity . Status ;
paymentData . SetBlob ( entity . PaymentMethodId , ( PaymentBlob ) entity ) ;
return entity ;
}
public static PaymentData SetBlob ( this PaymentData paymentData , PaymentMethodId paymentMethodId , PaymentBlob blob )
{
paymentData . Type = paymentMethodId . ToString ( ) ;
paymentData . Blob2 = JToken . FromObject ( blob , InvoiceDataExtensions . DefaultSerializer ) . ToString ( Newtonsoft . Json . Formatting . None ) ;
return paymentData ;
}
2024-05-01 03:22:07 +02:00
public static PaymentMethodId GetPaymentMethodId ( this PaymentData paymentData )
{
return PaymentMethodId . Parse ( paymentData . Type ) ;
}
2024-04-04 09:31:04 +02:00
public static PaymentEntity GetBlob ( this PaymentData paymentData )
{
var entity = JToken . Parse ( paymentData . Blob2 ) . ToObject < PaymentEntity > ( InvoiceDataExtensions . DefaultSerializer ) ? ? throw new FormatException ( $"Invalid {nameof(PaymentEntity)}" ) ;
entity . Status = paymentData . Status ! . Value ;
entity . Currency = paymentData . Currency ;
2024-05-01 03:22:07 +02:00
entity . PaymentMethodId = GetPaymentMethodId ( paymentData ) ;
2024-04-04 09:31:04 +02:00
entity . Value = paymentData . Amount ! . Value ;
entity . Id = paymentData . Id ;
entity . ReceivedTime = paymentData . Created ! . Value ;
return entity ;
2020-06-28 10:15:42 +02:00
}
}
}