Compare commits

..

6 Commits

Author SHA1 Message Date
khwezi 73ef4b04a9 Merge pull request 'Used scope to inject services' (#59) from payments into master
Reviewed-on: #59
2026-06-02 00:03:50 +02:00
Khwezi Mngoma 5ab2d29aac Used scope to inject services
continuous-integration/drone/pr Build is passing
2026-06-02 00:03:01 +02:00
khwezi 780415b6d4 Merge pull request 'Fixed event service scope issue' (#58) from payments into master
Reviewed-on: #58
2026-06-01 23:33:11 +02:00
Khwezi Mngoma 139ca1f866 Fixed event service scope issue
continuous-integration/drone/pr Build is passing
2026-06-01 23:32:35 +02:00
khwezi 879094073a Merge pull request 'Added PayfastPaymentConfirmationReceivedEvent' (#57) from payments into master
Reviewed-on: #57
2026-06-01 22:52:40 +02:00
Khwezi Mngoma 45c2e8310a Added PayfastPaymentConfirmationReceivedEvent
continuous-integration/drone/pr Build is passing
2026-06-01 22:51:49 +02:00
4 changed files with 149 additions and 1 deletions
@@ -0,0 +1,86 @@
using LiteCharms.Features.Hasher;
using LiteCharms.Features.MidrandBooks.Orders;
namespace LiteCharms.Features.MidrandBooks.Payments.Events.Handlers;
public sealed class PayfastPaymentConfirmationReceivedEventHandler(IServiceProvider services, ILogger<PayfastPaymentConfirmationReceivedEvent> logger) :
INotificationHandler<PayfastPaymentConfirmationReceivedEvent>
{
public async ValueTask Handle(PayfastPaymentConfirmationReceivedEvent notification, CancellationToken cancellationToken)
{
await using var scope = services.CreateAsyncScope();
PaymentService paymentService = scope.ServiceProvider.GetRequiredService<PaymentService>();
OrderService orderService = scope.ServiceProvider.GetRequiredService<OrderService>();
HashService hashService = scope.ServiceProvider.GetRequiredService<HashService>();
var hashResult = hashService.DecodeLongIdHash(notification.Payload?.MPaymentId!);
if (hashResult.IsFailed)
{
logger.LogError("Failed to decode payment ID hash: {Hash}. Errors: {Errors}", notification.Payload?.MPaymentId, string.Join(", ", hashResult.Errors.Select(e => e.Message)));
throw new Exception($"Failed to decode payment ID hash: {notification.Payload?.MPaymentId}.");
}
var orderResult = await orderService.GetOrderAsync(hashResult.Value, cancellationToken);
if (orderResult.IsFailed)
{
logger.LogError("Failed to retrieve order for payment ID: {PaymentId}. Errors: {Errors}", notification.Payload?.MPaymentId, string.Join(", ", orderResult.Errors.Select(e => e.Message)));
throw new Exception($"Failed to retrieve order for payment ID: {notification.Payload?.MPaymentId}.");
}
var paymentResult = await paymentService.GetOrderPaymentAsync(orderResult.Value.CustomerId, cancellationToken);
if (paymentResult.IsFailed)
{
logger.LogError("Failed to retrieve payment for order ID: {OrderId}. Errors: {Errors}", orderResult.Value.Id, string.Join(", ", paymentResult.Errors.Select(e => e.Message)));
throw new Exception($"Failed to retrieve payment for order ID: {orderResult.Value.Id}.");
}
var isAlreadyProcessed = await paymentService.HasLedgerEntryAsync(orderResult.Value.Id, paymentResult.Value.Id, 1, cancellationToken);
if (isAlreadyProcessed.IsFailed)
{
logger.LogError("Failed to check existing ledger entry for order ID: {OrderId} and payment ID: {PaymentId}. Errors: {Errors}", orderResult.Value.Id, paymentResult.Value.Id, string.Join(", ", isAlreadyProcessed.Errors.Select(e => e.Message)));
throw new Exception($"Failed to check existing ledger entry for order ID: {orderResult.Value.Id} and payment ID: {paymentResult.Value.Id}.");
}
if (isAlreadyProcessed.Value)
{
logger.LogInformation("Payment confirmation for payment ID: {PaymentId} has already been processed. Skipping.", notification.Payload?.MPaymentId);
return;
}
var ledgerResult = await paymentService.WriteLedgerEntryAsync(new Models.CreateLedgerEntry
{
CustomerId = orderResult.Value.CustomerId,
OrderId = orderResult.Value.Id,
PaymentId = paymentResult.Value.Id,
Status = LedgerStatuses.Received,
PaymentGatewayId = 1,
PaymentGatewayReference = notification.CorrelationId,
}, cancellationToken);
if (ledgerResult.IsFailed)
{
logger.LogError("Failed to write ledger entry for payment ID: {PaymentId}. Errors: {Errors}", notification.Payload?.MPaymentId, string.Join(", ", ledgerResult.Errors.Select(e => e.Message)));
throw new Exception($"Failed to write ledger entry for payment ID: {notification.Payload?.MPaymentId}.");
}
var paymentCompletedResult = await paymentService.CompletePaymentAsync(paymentResult.Value.Id, PaymentStatuses.Paid, cancellationToken);
if (paymentCompletedResult.IsFailed)
{
logger.LogError("Failed to complete payment for order ID: {OrderId}. Errors: {Errors}", orderResult.Value.Id, string.Join(", ", paymentCompletedResult.Errors.Select(e => e.Message)));
throw new Exception($"Failed to complete payment for order ID: {orderResult.Value.Id}.");
}
var orderCompletedResult = await orderService.UpdateOrderStatusAsync(orderResult.Value.Id, OrderStatus.Completed, cancellationToken);
if (orderCompletedResult.IsFailed)
{
logger.LogError("Failed to update order status to Completed for order ID: {OrderId}. Errors: {Errors}", orderResult.Value.Id, string.Join(", ", orderCompletedResult.Errors.Select(e => e.Message)));
throw new Exception($"Failed to update order status to Completed for order ID: {orderResult.Value.Id}.");
}
logger.LogInformation("Received Payfast payment confirmation for payment ID: {PaymentId}", notification.Payload?.MPaymentId);
// TODO: Publish MediatR notifications or queue downstream Quartz jobs (Discord, Shipping, Customer Email, Royalties)
}
}
@@ -0,0 +1,22 @@
using LiteCharms.Features.Abstractions;
using LiteCharms.Features.Models;
namespace LiteCharms.Features.MidrandBooks.Payments.Events;
public sealed class PayfastPaymentConfirmationReceivedEvent : EventBase, IEvent
{
public string Name { get; set; } = nameof(PayfastPaymentConfirmationReceivedEvent);
public PayfastWebhookPayload? Payload { get; set; }
public PayfastPaymentConfirmationReceivedEvent() { }
private PayfastPaymentConfirmationReceivedEvent(PayfastWebhookPayload? payload, string paymentId)
{
Payload = payload;
CorrelationId = paymentId;
}
public static PayfastPaymentConfirmationReceivedEvent Create(PayfastWebhookPayload? payload, string paymentId) =>
new(payload, paymentId);
}
@@ -7,6 +7,27 @@ namespace LiteCharms.Features.MidrandBooks.Payments;
public sealed class PaymentService(IDbContextFactory<MidrandBooksDbContext> contextFactory) : IService public sealed class PaymentService(IDbContextFactory<MidrandBooksDbContext> contextFactory) : IService
{ {
public async ValueTask<Result<Payment>> GetOrderPaymentAsync(long orderId, CancellationToken cancellationToken = default)
{
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var payment = await context.Payments.AsNoTracking()
.Where(p => p.OrderId == orderId)
.OrderByDescending(p => p.Id)
.FirstOrDefaultAsync(cancellationToken);
return payment is not null
? Result.Ok(payment.ToModel())
: Result.Fail<Payment>("Could not find payment for the order");
}
catch (Exception ex)
{
return Result.Fail<Payment>(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result<Refund>> GetRefundAsync(long refundId, CancellationToken cancellationToken = default) public async ValueTask<Result<Refund>> GetRefundAsync(long refundId, CancellationToken cancellationToken = default)
{ {
try try
@@ -95,6 +116,25 @@ public sealed class PaymentService(IDbContextFactory<MidrandBooksDbContext> cont
} }
} }
public async ValueTask<Result<bool>> HasLedgerEntryAsync(long orderId, long paymentId, long gatewayId, CancellationToken cancellationToken = default)
{
try
{
await using var context = await contextFactory.CreateDbContextAsync(cancellationToken);
var exists = await context.Ledger.AnyAsync(l =>
l.OrderId == orderId &&
l.PaymentId == paymentId &&
l.PaymentGatewayId == gatewayId, cancellationToken);
return Result.Ok(exists);
}
catch (Exception ex)
{
return Result.Fail(new Error(ex.Message).CausedBy(ex));
}
}
public async ValueTask<Result> WriteLedgerEntryAsync(CreateLedgerEntry request, CancellationToken cancellationToken = default) public async ValueTask<Result> WriteLedgerEntryAsync(CreateLedgerEntry request, CancellationToken cancellationToken = default)
{ {
try try
@@ -23,7 +23,7 @@ public sealed class JobOrchestrator(ISchedulerFactory schedulerFactory) : IJobOr
var trigger = global::Quartz.TriggerBuilder.Create() var trigger = global::Quartz.TriggerBuilder.Create()
.WithIdentity(triggerKey) .WithIdentity(triggerKey)
.StartNow() .StartNow()
.Build(); .Build();
await scheduler.ScheduleJob(job, new List<ITrigger> { trigger }.AsReadOnly(), replace: true, cancellationToken); await scheduler.ScheduleJob(job, new List<ITrigger> { trigger }.AsReadOnly(), replace: true, cancellationToken);