67 lines
2.6 KiB
C#
67 lines
2.6 KiB
C#
// src/Webshop.Application/Services/Public/PaymentMethodService.cs
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text.Json; // Für JSON-Verarbeitung
|
|
using System.Threading.Tasks;
|
|
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<IEnumerable<PaymentMethodDto>> GetAllActiveAsync()
|
|
{
|
|
var paymentMethods = await _paymentMethodRepository.GetAllAsync();
|
|
|
|
return 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();
|
|
}
|
|
|
|
private object? GetPublicConfiguration(PaymentGatewayType type, string? configJson)
|
|
{
|
|
if (string.IsNullOrEmpty(configJson)) return null;
|
|
|
|
// Beispiel: Nur für BankTransfer geben wir IBAN etc. preis
|
|
if (type == PaymentGatewayType.BankTransfer)
|
|
{
|
|
var config = JsonSerializer.Deserialize<Dictionary<string, string>>(configJson);
|
|
if (config != null)
|
|
{
|
|
// Filter, um geheime Schlüssel NICHT an das Frontend zu senden
|
|
return new { IBAN = config.GetValueOrDefault("IBAN"), BIC = config.GetValueOrDefault("BIC"), BankName = config.GetValueOrDefault("BankName") };
|
|
}
|
|
}
|
|
|
|
// Für Stripe/PayPal geben wir nur den Public Key preis
|
|
if (type == PaymentGatewayType.Stripe)
|
|
{
|
|
var config = JsonSerializer.Deserialize<Dictionary<string, string>>(configJson);
|
|
if (config != null)
|
|
{
|
|
return new { PublicKey = config.GetValueOrDefault("PublicKey") };
|
|
}
|
|
}
|
|
|
|
return null; // Für andere Typen geben wir keine Konfigurationsdetails preis
|
|
}
|
|
}
|
|
} |