PaymentMethod
This commit is contained in:
@@ -1,13 +1,13 @@
|
||||
// src/Webshop.Application/Services/Public/PaymentMethodService.cs
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.Json; // Für JSON-Verarbeitung
|
||||
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
|
||||
@@ -19,11 +19,11 @@ namespace Webshop.Application.Services.Public
|
||||
_paymentMethodRepository = paymentMethodRepository;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<PaymentMethodDto>> GetAllActiveAsync()
|
||||
public async Task<ServiceResult<IEnumerable<PaymentMethodDto>>> GetAllActiveAsync()
|
||||
{
|
||||
var paymentMethods = await _paymentMethodRepository.GetAllAsync();
|
||||
|
||||
return paymentMethods
|
||||
var activeMethods = paymentMethods
|
||||
.Where(pm => pm.IsActive)
|
||||
.Select(pm => new PaymentMethodDto
|
||||
{
|
||||
@@ -34,34 +34,42 @@ namespace Webshop.Application.Services.Public
|
||||
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;
|
||||
|
||||
// Beispiel: Nur für BankTransfer geben wir IBAN etc. preis
|
||||
if (type == PaymentGatewayType.BankTransfer)
|
||||
try
|
||||
{
|
||||
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") };
|
||||
}
|
||||
}
|
||||
var config = JsonSerializer.Deserialize<Dictionary<string, JsonElement>>(configJson);
|
||||
if (config == null) return null;
|
||||
|
||||
// Für Stripe/PayPal geben wir nur den Public Key preis
|
||||
if (type == PaymentGatewayType.Stripe)
|
||||
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)
|
||||
{
|
||||
var config = JsonSerializer.Deserialize<Dictionary<string, string>>(configJson);
|
||||
if (config != null)
|
||||
{
|
||||
return new { PublicKey = config.GetValueOrDefault("PublicKey") };
|
||||
}
|
||||
// Log error if needed, but return null to the client
|
||||
return null;
|
||||
}
|
||||
|
||||
return null; // Für andere Typen geben wir keine Konfigurationsdetails preis
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user