Files
ShopSolution-backend/Webshop.Application/Services/Public/PaymentMethodService.cs
Tizian.Breuch 88f520a51b PaymentMethod
2025-09-25 16:31:19 +02:00

75 lines
2.8 KiB
C#

// src/Webshop.Application/Services/Public/PaymentMethodService.cs
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Webshop.Application;
using Webshop.Application.DTOs.Payments;
using Webshop.Domain.Enums;
using Webshop.Domain.Interfaces;
namespace Webshop.Application.Services.Public
{
public class PaymentMethodService : IPaymentMethodService
{
private readonly IPaymentMethodRepository _paymentMethodRepository;
public PaymentMethodService(IPaymentMethodRepository paymentMethodRepository)
{
_paymentMethodRepository = paymentMethodRepository;
}
public async Task<ServiceResult<IEnumerable<PaymentMethodDto>>> GetAllActiveAsync()
{
var paymentMethods = await _paymentMethodRepository.GetAllAsync();
var activeMethods = paymentMethods
.Where(pm => pm.IsActive)
.Select(pm => new PaymentMethodDto
{
Id = pm.Id,
Name = pm.Name,
Description = pm.Description,
PaymentGatewayType = pm.PaymentGatewayType,
ProcessingFee = pm.ProcessingFee,
PublicConfiguration = GetPublicConfiguration(pm.PaymentGatewayType, pm.Configuration)
}).ToList();
return ServiceResult.Ok<IEnumerable<PaymentMethodDto>>(activeMethods);
}
private object? GetPublicConfiguration(PaymentGatewayType type, string? configJson)
{
if (string.IsNullOrEmpty(configJson)) return null;
try
{
var config = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(configJson);
if (config == null) return null;
if (type == PaymentGatewayType.BankTransfer)
{
return new
{
IBAN = config.ContainsKey("IBAN") ? config["IBAN"].GetString() : null,
BIC = config.ContainsKey("BIC") ? config["BIC"].GetString() : null,
BankName = config.ContainsKey("BankName") ? config["BankName"].GetString() : null
};
}
if (type == PaymentGatewayType.Stripe)
{
return new { PublicKey = config.ContainsKey("PublicKey") ? config["PublicKey"].GetString() : null };
}
// Für PayPal oder andere geben wir keine öffentlichen Konfigurationsdetails preis
return null;
}
catch (JsonException)
{
// Log error if needed, but return null to the client
return null;
}
}
}
}