payment
This commit is contained in:
100
Webshop.Application/Services/Admin/AdminPaymentMethodService.cs
Normal file
100
Webshop.Application/Services/Admin/AdminPaymentMethodService.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
67
Webshop.Application/Services/Public/PaymentMethodService.cs
Normal file
67
Webshop.Application/Services/Public/PaymentMethodService.cs
Normal 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
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user