This commit is contained in:
Tizian.Breuch
2025-07-29 19:11:34 +02:00
parent 3907cba29d
commit 6fc6aaef3e
14 changed files with 375 additions and 31 deletions

View File

@@ -0,0 +1,17 @@
// src/Webshop.Application/DTOs/Payments/AdminPaymentMethodDto.cs
using System;
using Webshop.Domain.Enums;
namespace Webshop.Application.DTOs.Payments
{
public class AdminPaymentMethodDto
{
public Guid Id { get; set; } = Guid.NewGuid();
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public bool IsActive { get; set; }
public PaymentGatewayType PaymentGatewayType { get; set; }
public string? Configuration { get; set; } // Als JSON-String, den der Admin bearbeitet
public decimal? ProcessingFee { get; set; }
}
}

View File

@@ -1,7 +1,8 @@
// Auto-generiert von CreateWebshopFiles.ps1
// src/Webshop.Application/DTOs/Payments/PaymentMethodDto.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Domain.Enums;
@@ -12,8 +13,10 @@ namespace Webshop.Application.DTOs.Payments
public Guid Id { get; set; }
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public bool IsActive { get; set; }
public PaymentGatewayType GatewayType { get; set; }
public PaymentGatewayType PaymentGatewayType { get; set; }
public decimal? ProcessingFee { get; set; }
// Wichtig: Wir geben hier nur <20>ffentliche Konfigurationsdaten preis (z.B. IBAN)
public object? PublicConfiguration { get; set; }
}
}
}

View File

@@ -0,0 +1,100 @@
// src/Webshop.Application/Services/Admin/AdminPaymentMethodService.cs
using System;
using System.Collections.Generic;
using System.Linq; // Für Select
using System.Threading.Tasks;
using Webshop.Application.DTOs.Payments; // AdminPaymentMethodDto
using Webshop.Domain.Entities; // PaymentMethod
using Webshop.Domain.Interfaces; // IPaymentMethodRepository
using Webshop.Domain.Enums;
namespace Webshop.Application.Services.Admin
{
public class AdminPaymentMethodService : IAdminPaymentMethodService
{
private readonly IPaymentMethodRepository _paymentMethodRepository;
public AdminPaymentMethodService(IPaymentMethodRepository paymentMethodRepository)
{
_paymentMethodRepository = paymentMethodRepository;
}
public async Task<IEnumerable<AdminPaymentMethodDto>> GetAllAsync()
{
var paymentMethods = await _paymentMethodRepository.GetAllAsync();
return paymentMethods.Select(pm => new AdminPaymentMethodDto
{
Id = pm.Id,
Name = pm.Name,
Description = pm.Description,
IsActive = pm.IsActive,
PaymentGatewayType = pm.PaymentGatewayType,
Configuration = pm.Configuration, // Gibt den JSON-String für den Admin zurück
ProcessingFee = pm.ProcessingFee
}).ToList();
}
public async Task<AdminPaymentMethodDto?> GetByIdAsync(Guid id)
{
var paymentMethod = await _paymentMethodRepository.GetByIdAsync(id);
if (paymentMethod == null) return null;
return new AdminPaymentMethodDto
{
Id = paymentMethod.Id,
Name = paymentMethod.Name,
Description = paymentMethod.Description,
IsActive = paymentMethod.IsActive,
PaymentGatewayType = paymentMethod.PaymentGatewayType,
Configuration = paymentMethod.Configuration,
ProcessingFee = paymentMethod.ProcessingFee
};
}
public async Task<AdminPaymentMethodDto> CreateAsync(AdminPaymentMethodDto paymentMethodDto)
{
var newPaymentMethod = new PaymentMethod
{
Id = Guid.NewGuid(), // API generiert die ID
Name = paymentMethodDto.Name,
Description = paymentMethodDto.Description,
IsActive = paymentMethodDto.IsActive,
PaymentGatewayType = paymentMethodDto.PaymentGatewayType,
Configuration = paymentMethodDto.Configuration,
ProcessingFee = paymentMethodDto.ProcessingFee
};
await _paymentMethodRepository.AddAsync(newPaymentMethod);
paymentMethodDto.Id = newPaymentMethod.Id; // ID zurückschreiben
return paymentMethodDto;
}
public async Task<bool> UpdateAsync(AdminPaymentMethodDto paymentMethodDto)
{
var existingPaymentMethod = await _paymentMethodRepository.GetByIdAsync(paymentMethodDto.Id);
if (existingPaymentMethod == null) return false;
// Eigenschaften aktualisieren
existingPaymentMethod.Name = paymentMethodDto.Name;
existingPaymentMethod.Description = paymentMethodDto.Description;
existingPaymentMethod.IsActive = paymentMethodDto.IsActive;
existingPaymentMethod.PaymentGatewayType = paymentMethodDto.PaymentGatewayType;
existingPaymentMethod.Configuration = paymentMethodDto.Configuration;
existingPaymentMethod.ProcessingFee = paymentMethodDto.ProcessingFee;
await _paymentMethodRepository.UpdateAsync(existingPaymentMethod);
return true;
}
public async Task<bool> DeleteAsync(Guid id)
{
var paymentMethod = await _paymentMethodRepository.GetByIdAsync(id);
if (paymentMethod == null) return false;
await _paymentMethodRepository.DeleteAsync(id);
return true;
}
}
}

View File

@@ -0,0 +1,36 @@
// src/Webshop.Application/Services/Admin/IAdminPaymentMethodService.cs
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Payments; // Für AdminPaymentMethodDto
namespace Webshop.Application.Services.Admin
{
public interface IAdminPaymentMethodService
{
/// <summary>
/// Ruft alle Zahlungsmethoden ab (sowohl aktive als auch inaktive).
/// </summary>
Task<IEnumerable<AdminPaymentMethodDto>> GetAllAsync();
/// <summary>
/// Ruft eine einzelne Zahlungsmethode anhand ihrer ID ab.
/// </summary>
Task<AdminPaymentMethodDto?> GetByIdAsync(Guid id);
/// <summary>
/// Erstellt eine neue Zahlungsmethode.
/// </summary>
Task<AdminPaymentMethodDto> CreateAsync(AdminPaymentMethodDto paymentMethodDto);
/// <summary>
/// Aktualisiert eine bestehende Zahlungsmethode.
/// </summary>
Task<bool> UpdateAsync(AdminPaymentMethodDto paymentMethodDto);
/// <summary>
/// Löscht eine Zahlungsmethode anhand ihrer ID.
/// </summary>
Task<bool> DeleteAsync(Guid id);
}
}

View File

@@ -0,0 +1,16 @@
// src/Webshop.Application/Services/Public/IPaymentMethodService.cs
using System.Collections.Generic;
using System.Threading.Tasks;
using Webshop.Application.DTOs.Payments; // Für PaymentMethodDto
namespace Webshop.Application.Services.Public
{
public interface IPaymentMethodService
{
/// <summary>
/// Ruft alle aktiven Zahlungsmethoden ab, die im Checkout für den Kunden sichtbar sein sollen.
/// </summary>
/// <returns>Eine Liste von PaymentMethodDto mit öffentlichen Konfigurationsdaten.</returns>
Task<IEnumerable<PaymentMethodDto>> GetAllActiveAsync();
}
}

View File

@@ -0,0 +1,67 @@
// 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
}
}
}