using LiteCharms.Features.Hasher; using LiteCharms.Features.MidrandBooks.Orders; namespace LiteCharms.Features.MidrandBooks.Payments.Events.Handlers; public sealed class PayfastPaymentConfirmationReceivedEventHandler(IServiceProvider services, ILogger logger) : INotificationHandler { public async ValueTask Handle(PayfastPaymentConfirmationReceivedEvent notification, CancellationToken cancellationToken) { await using var scope = services.CreateAsyncScope(); PaymentService paymentService = scope.ServiceProvider.GetRequiredService(); OrderService orderService = scope.ServiceProvider.GetRequiredService(); HashService hashService = scope.ServiceProvider.GetRequiredService(); 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) } }