2022-06-13 13:35:26 +00:00
|
|
|
|
using Dapper;
|
2022-09-07 14:52:40 +00:00
|
|
|
|
using VoidCat.Model.Payments;
|
2022-06-13 13:35:26 +00:00
|
|
|
|
using VoidCat.Services.Abstractions;
|
|
|
|
|
|
2022-09-07 14:52:40 +00:00
|
|
|
|
namespace VoidCat.Services.Payment;
|
2022-06-13 13:35:26 +00:00
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2022-09-07 14:52:40 +00:00
|
|
|
|
public class PostgresPaymentOrderStore : IPaymentOrderStore
|
2022-06-13 13:35:26 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly PostgresConnectionFactory _connection;
|
|
|
|
|
|
2022-09-07 14:52:40 +00:00
|
|
|
|
public PostgresPaymentOrderStore(PostgresConnectionFactory connection)
|
2022-06-13 13:35:26 +00:00
|
|
|
|
{
|
|
|
|
|
_connection = connection;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2022-09-07 14:52:40 +00:00
|
|
|
|
public async ValueTask<PaymentOrder?> Get(Guid id)
|
2022-06-13 13:35:26 +00:00
|
|
|
|
{
|
|
|
|
|
await using var conn = await _connection.Get();
|
2022-09-07 14:52:40 +00:00
|
|
|
|
var order = await conn.QuerySingleOrDefaultAsync<DtoPaymentOrder>(
|
|
|
|
|
@"select * from ""PaymentOrder"" where ""Id"" = :id", new {id});
|
2022-06-13 13:35:26 +00:00
|
|
|
|
if (order.Service is PaymentServices.Strike)
|
|
|
|
|
{
|
2022-09-07 14:52:40 +00:00
|
|
|
|
var lnDetails = await conn.QuerySingleAsync<LightningPaymentOrder>(
|
|
|
|
|
@"select * from ""PaymentOrderLightning"" where ""Order"" = :id", new
|
2022-06-13 13:35:26 +00:00
|
|
|
|
{
|
|
|
|
|
id = order.Id
|
|
|
|
|
});
|
2022-09-07 14:52:40 +00:00
|
|
|
|
return new LightningPaymentOrder
|
2022-06-13 13:35:26 +00:00
|
|
|
|
{
|
|
|
|
|
Id = order.Id,
|
|
|
|
|
File = order.File,
|
|
|
|
|
Price = new(order.Amount, order.Currency),
|
|
|
|
|
Service = order.Service,
|
|
|
|
|
Status = order.Status,
|
|
|
|
|
Invoice = lnDetails.Invoice,
|
|
|
|
|
Expire = lnDetails.Expire
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return order;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2022-09-07 14:52:40 +00:00
|
|
|
|
public ValueTask<IReadOnlyList<PaymentOrder>> Get(Guid[] ids)
|
2022-06-13 13:35:26 +00:00
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2022-09-07 14:52:40 +00:00
|
|
|
|
public async ValueTask Add(Guid id, PaymentOrder obj)
|
2022-06-13 13:35:26 +00:00
|
|
|
|
{
|
|
|
|
|
await using var conn = await _connection.Get();
|
|
|
|
|
await using var txn = await conn.BeginTransactionAsync();
|
|
|
|
|
await conn.ExecuteAsync(
|
2022-09-07 14:52:40 +00:00
|
|
|
|
@"insert into ""PaymentOrder""(""Id"", ""File"", ""Service"", ""Currency"", ""Amount"", ""Status"")
|
2022-06-13 13:35:26 +00:00
|
|
|
|
values(:id, :file, :service, :currency, :amt, :status)",
|
|
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
id,
|
|
|
|
|
file = obj.File,
|
|
|
|
|
service = (int) obj.Service,
|
|
|
|
|
currency = (int) obj.Price.Currency,
|
|
|
|
|
amt = obj.Price.Amount, // :amount wasn't working?
|
|
|
|
|
status = (int) obj.Status
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-07 14:52:40 +00:00
|
|
|
|
if (obj is LightningPaymentOrder ln)
|
2022-06-13 13:35:26 +00:00
|
|
|
|
{
|
|
|
|
|
await conn.ExecuteAsync(
|
2022-09-07 14:52:40 +00:00
|
|
|
|
@"insert into ""PaymentOrderLightning""(""Order"", ""Invoice"", ""Expire"") values(:order, :invoice, :expire)",
|
2022-06-13 13:35:26 +00:00
|
|
|
|
new
|
|
|
|
|
{
|
|
|
|
|
order = id,
|
|
|
|
|
invoice = ln.Invoice,
|
|
|
|
|
expire = ln.Expire.ToUniversalTime()
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await txn.CommitAsync();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
public async ValueTask Delete(Guid id)
|
|
|
|
|
{
|
|
|
|
|
await using var conn = await _connection.Get();
|
2022-09-07 14:52:40 +00:00
|
|
|
|
await conn.ExecuteAsync(@"delete from ""PaymentOrder"" where ""Id"" = :id", new {id});
|
2022-06-13 13:35:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2022-09-07 14:52:40 +00:00
|
|
|
|
public async ValueTask UpdateStatus(Guid order, PaymentOrderStatus status)
|
2022-06-13 13:35:26 +00:00
|
|
|
|
{
|
|
|
|
|
await using var conn = await _connection.Get();
|
2022-09-07 14:52:40 +00:00
|
|
|
|
await conn.ExecuteAsync(@"update ""PaymentOrder"" set ""Status"" = :status where ""Id"" = :id",
|
2022-06-13 13:35:26 +00:00
|
|
|
|
new {id = order, status = (int) status});
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 14:52:40 +00:00
|
|
|
|
private sealed class DtoPaymentOrder : PaymentOrder
|
2022-06-13 13:35:26 +00:00
|
|
|
|
{
|
2022-09-07 14:52:40 +00:00
|
|
|
|
public PaymentCurrencies Currency { get; init; }
|
2022-06-13 13:35:26 +00:00
|
|
|
|
public decimal Amount { get; init; }
|
|
|
|
|
}
|
|
|
|
|
}
|